1 Star 0 Fork 3

linyingzhou/renderhelp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sample_05_model.cpp 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
skywind3000 提交于 2020-08-12 22:46 . 提交剩余 demo
#include "RenderHelp.h"
#include "Model.h"
int main(void)
{
RenderHelp rh(600, 800);
// 加载模型
Model model("res/diablo3_pose.obj");
Vec3f eye_pos = {0, -0.5, 1.7};
Vec3f eye_at = {0, 0, 0};
Vec3f eye_up = {0, 1, 0};
Vec3f light_dir = {1, 1, 0.85}; // 光的方向
float perspective = 3.1415926f * 0.5f;
Mat4x4f mat_model = matrix_set_scale(1, 1, 1);
Mat4x4f mat_view = matrix_set_lookat(eye_pos, eye_at, eye_up);
Mat4x4f mat_proj = matrix_set_perspective(perspective, 6 / 8.0, 1.0, 500.0f);
Mat4x4f mat_mvp = mat_model * mat_view * mat_proj;
// 用于将法向量从模型坐标系变换到世界坐标系
Mat4x4f mat_model_it = matrix_invert(mat_model).Transpose();
// 顶点属性
struct { Vec3f pos; Vec3f normal; Vec2f uv; } vs_input[3];
const int VARYING_UV = 0;
const int VARYING_NORMAL = 1;
rh.SetVertexShader([&] (int index, ShaderContext& output) -> Vec4f {
Vec4f pos = vs_input[index].pos.xyz1() * mat_mvp;
Vec4f normal = (vs_input[index].normal.xyz1() * mat_model_it);
output.varying_vec2f[VARYING_UV] = vs_input[index].uv;
output.varying_vec3f[VARYING_NORMAL] = normal.xyz(); // 转化为三维
return pos;
});
rh.SetPixelShader([&] (ShaderContext& input) -> Vec4f {
Vec2f uv = input.varying_vec2f[VARYING_UV];
Vec3f n = input.varying_vec3f[VARYING_NORMAL];
Vec3f l = vector_normalize(light_dir);
Vec4f color = model.diffuse(uv); // 从模型取得纹理颜色
// 点乘 n,l 获得光照强度,Saturate 可以对范围 [0,1] 裁剪
float intense = Saturate(vector_dot(n, l)) + 0.1;
return color * intense;
});
// 迭代模型每一个面
for (int i = 0; i < model.nfaces(); i++) {
// 设置三个顶点的输入,供 VS 读取
for (int j = 0; j < 3; j++) {
vs_input[j].pos = model.vert(i, j);
vs_input[j].uv = model.uv(i, j);
vs_input[j].normal = model.normal(i, j);
}
// 绘制三角形
rh.DrawPrimitive();
}
rh.SaveFile("output.bmp");
#if defined(WIN32) || defined(_WIN32)
system("mspaint output.bmp");
#endif
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/linyingzhou/renderhelp.git
git@gitee.com:linyingzhou/renderhelp.git
linyingzhou
renderhelp
renderhelp
master

搜索帮助