@@ -20,7 +20,7 @@ Eigen::Matrix4f get_view_matrix(const Eigen::Vector3f &eye_pos)
20
20
0 ,0 ,1 ,-eye_pos[2 ],
21
21
0 ,0 ,0 ,1 ;
22
22
23
- view = translate* view;
23
+ view = translate * view;
24
24
25
25
return view;
26
26
}
@@ -97,10 +97,8 @@ Eigen::Vector3f vertex_shader(const vertex_shader_payload& payload)
97
97
98
98
Eigen::Vector3f normal_fragment_shader (const fragment_shader_payload& payload)
99
99
{
100
- Eigen::Vector3f return_color = (payload.normal .head <3 >().normalized () + Eigen::Vector3f (1 .0f , 1 .0f , 1 .0f )) / 2 .f ;
101
- Eigen::Vector3f result;
102
- result << return_color.x () * 255 , return_color.y () * 255 , return_color.z () * 255 ;
103
- return result;
100
+ Eigen::Vector3f color = (payload.normal .head <3 >().normalized () + Eigen::Vector3f{ 1 .0f , 1 .0f , 1 .0f }) / 2 .0f ;
101
+ return color * 255 .0f ;
104
102
}
105
103
106
104
struct light
@@ -111,24 +109,24 @@ struct light
111
109
112
110
Eigen::Vector3f phong_fragment_shader (const fragment_shader_payload& payload)
113
111
{
114
- static Eigen::Vector3f ka = Eigen::Vector3f ( 0 .005f , 0 .005f , 0 .005f ) ;
112
+ Eigen::Vector3f ka{ 0 .005f , 0 .005f , 0 .005f } ;
115
113
Eigen::Vector3f kd = payload.color ;
116
- static Eigen::Vector3f ks = Eigen::Vector3f ( 0 .7937f , 0 .7937f , 0 .7937f ) ;
114
+ Eigen::Vector3f ks{ 0 .7937f , 0 .7937f , 0 .7937f } ;
117
115
118
- static std::vector<light> lights = {
119
- light{{20 .0f , 20 .0f , 20 .0f }, {500 .0f , 500 .0f , 500 .0f }},
120
- light{{-20 .0f , 20 .0f , 0 .0f }, {500 .0f , 500 .0f , 500 .0f }} };
116
+ std::vector<light> lights = {
117
+ light{{ 20 .0f , 20 .0f , 20 .0f }, { 500 .0f , 500 .0f , 500 .0f }},
118
+ light{{ -20 .0f , 20 .0f , 0 .0f }, { 500 .0f , 500 .0f , 500 .0f }} };
121
119
122
- static Eigen::Vector3f amb_light_intensity{5 .0f , 5 .0f , 5 .0f };
123
- static Eigen::Vector3f eye_pos{0 .0f , 0 .0f , 10 .0f };
120
+ Eigen::Vector3f amb_light_intensity{ 5 .0f , 5 .0f , 5 .0f };
121
+ Eigen::Vector3f eye_pos{ 0 .0f , 0 .0f , 10 .0f };
124
122
125
- constexpr static float p = 150 .0f ;
123
+ constexpr float p = 150 .0f ;
126
124
127
125
Eigen::Vector3f normal = payload.normal .normalized ();
128
126
Eigen::Vector3f point = payload.view_pos ;
129
127
Eigen::Vector3f viewDir = (eye_pos - point).normalized ();
130
128
131
- Eigen::Vector3f color = { 0 .0f , 0 .0f , 0 .0f };
129
+ Eigen::Vector3f color{ 0 .0f , 0 .0f , 0 .0f };
132
130
133
131
for (auto & light : lights)
134
132
{
@@ -152,30 +150,27 @@ Eigen::Vector3f phong_fragment_shader(const fragment_shader_payload& payload)
152
150
153
151
Eigen::Vector3f texture_fragment_shader (const fragment_shader_payload &payload)
154
152
{
155
- // 好像 OpenCV 读取 png 时的返回值范围是 [0, 255],而非 [0, 1]
156
- static constexpr float reciprocal = 1 .0f / 255 .0f ;
157
-
158
- static Eigen::Vector3f ka = Eigen::Vector3f (0 .005f , 0 .005f , 0 .005f );
153
+ Eigen::Vector3f ka{ 0 .005f , 0 .005f , 0 .005f };
159
154
Eigen::Vector3f kd =
160
155
payload.texture ?
161
- payload.texture ->getColorBilinear (payload.tex_coords .x (), payload.tex_coords .y ()) * reciprocal :
156
+ payload.texture ->getColorBilinear (payload.tex_coords .x (), payload.tex_coords .y ()) / 255 . 0f :
162
157
payload.color ;
163
- static Eigen::Vector3f ks = Eigen::Vector3f ( 0 .7937f , 0 .7937f , 0 .7937f ) ;
158
+ Eigen::Vector3f ks{ 0 .7937f , 0 .7937f , 0 .7937f } ;
164
159
165
- static std::vector<light> lights = {
166
- light{{20 .0f , 20 .0f , 20 .0f }, {500 .0f , 500 .0f , 500 .0f }},
167
- light{{-20 .0f , 20 .0f , 0 .0f }, {500 .0f , 500 .0f , 500 .0f }} };
160
+ std::vector<light> lights = {
161
+ light{{ 20 .0f , 20 .0f , 20 .0f }, {500 .0f , 500 .0f , 500 .0f }},
162
+ light{{ -20 .0f , 20 .0f , 0 .0f }, {500 .0f , 500 .0f , 500 .0f }} };
168
163
169
- static Eigen::Vector3f amb_light_intensity{ 5 .0f , 5 .0f , 5 .0f };
170
- static Eigen::Vector3f eye_pos{ 0 .0f , 0 .0f , 10 .0f };
164
+ Eigen::Vector3f amb_light_intensity{ 5 .0f , 5 .0f , 5 .0f };
165
+ Eigen::Vector3f eye_pos{ 0 .0f , 0 .0f , 10 .0f };
171
166
172
- constexpr static float p = 150 .0f ;
167
+ constexpr float p = 150 .0f ;
173
168
174
169
Eigen::Vector3f normal = payload.normal .normalized ();
175
170
Eigen::Vector3f point = payload.view_pos ;
176
171
Eigen::Vector3f viewDir = (eye_pos - point).normalized ();
177
172
178
- Eigen::Vector3f color = { 0 .0f , 0 .0f , 0 .0f };
173
+ Eigen::Vector3f color{ 0 .0f , 0 .0f , 0 .0f };
179
174
180
175
for (auto &light : lights)
181
176
{
@@ -199,8 +194,8 @@ Eigen::Vector3f texture_fragment_shader(const fragment_shader_payload &payload)
199
194
200
195
Eigen::Vector3f bump_fragment_shader (const fragment_shader_payload &payload)
201
196
{
202
- constexpr static float kh = 0 .2f ;
203
- constexpr static float kn = 0 .1f ;
197
+ constexpr float kh = 0 .2f ;
198
+ constexpr float kn = 0 .1f ;
204
199
205
200
Eigen::Vector3f normal = payload.normal .normalized ();
206
201
@@ -232,28 +227,28 @@ Eigen::Vector3f bump_fragment_shader(const fragment_shader_payload &payload)
232
227
float du = kh * kn * (height_u - height);
233
228
float dv = kh * kn * (height_v - height);
234
229
235
- Eigen::Vector3f localNormal = Eigen::Vector3f { -du, -dv, 1 .0f };
230
+ Eigen::Vector3f localNormal{ -du, -dv, 1 .0f };
236
231
normal = (TBN * localNormal).normalized ();
237
232
238
- return normal * 255 .f ;
233
+ return normal * 255 .0f ;
239
234
}
240
235
241
236
Eigen::Vector3f displacement_fragment_shader (const fragment_shader_payload& payload)
242
237
{
243
- static Eigen::Vector3f ka = Eigen::Vector3f (0 .005f , 0 .005f , 0 .005f );
238
+ Eigen::Vector3f ka = Eigen::Vector3f (0 .005f , 0 .005f , 0 .005f );
244
239
Eigen::Vector3f kd = payload.color ;
245
- static Eigen::Vector3f ks = Eigen::Vector3f (0 .7937f , 0 .7937f , 0 .7937f );
240
+ Eigen::Vector3f ks = Eigen::Vector3f (0 .7937f , 0 .7937f , 0 .7937f );
246
241
247
- static std::vector<light> lights = {
248
- light{{20 .0f , 20 .0f , 20 .0f }, {500 .0f , 500 .0f , 500 .0f }},
249
- light{{-20 .0f , 20 .0f , 0 .0f }, {500 .0f , 500 .0f , 500 .0f }} };
242
+ std::vector<light> lights = {
243
+ light{{ 20 .0f , 20 .0f , 20 .0f }, { 500 .0f , 500 .0f , 500 .0f }},
244
+ light{{ -20 .0f , 20 .0f , 0 .0f }, { 500 .0f , 500 .0f , 500 .0f }} };
250
245
251
- static Eigen::Vector3f amb_light_intensity{ 5 .0f , 5 .0f , 5 .0f };
252
- static Eigen::Vector3f eye_pos{ 0 .0f , 0 .0f , 10 .0f };
246
+ Eigen::Vector3f amb_light_intensity{ 5 .0f , 5 .0f , 5 .0f };
247
+ Eigen::Vector3f eye_pos{ 0 .0f , 0 .0f , 10 .0f };
253
248
254
- constexpr static float kh = 0 .2f ;
255
- constexpr static float kn = 0 .1f ;
256
- constexpr static float p = 150 .0f ;
249
+ constexpr float kh = 0 .2f ;
250
+ constexpr float kn = 0 .1f ;
251
+ constexpr float p = 150 .0f ;
257
252
258
253
Eigen::Vector3f point = payload.view_pos ;
259
254
Eigen::Vector3f normal = payload.normal .normalized ();
@@ -291,11 +286,11 @@ Eigen::Vector3f displacement_fragment_shader(const fragment_shader_payload& payl
291
286
// 将着色点沿着原法线的方向进行位移
292
287
point += kn * normal * height;
293
288
294
- Eigen::Vector3f localNormal = Eigen::Vector3f { -du, -dv, 1 .0f };
289
+ Eigen::Vector3f localNormal{ -du, -dv, 1 .0f };
295
290
normal = (TBN * localNormal).normalized ();
296
291
}
297
292
298
- Eigen::Vector3f color = { 0 .0f , 0 .0f , 0 .0f };
293
+ Eigen::Vector3f color{ 0 .0f , 0 .0f , 0 .0f };
299
294
300
295
for (auto &light : lights)
301
296
{
@@ -316,7 +311,7 @@ Eigen::Vector3f displacement_fragment_shader(const fragment_shader_payload& payl
316
311
color += (ambient + diffuse + specular);
317
312
}
318
313
319
- return color * 255 .f ;
314
+ return color * 255 .0f ;
320
315
}
321
316
322
317
int main ()
@@ -326,10 +321,10 @@ int main()
326
321
Loader.LoadFile (Utils::PathFromAsset (" model/spot/spot_triangulated_good.obj" ));
327
322
for (auto &mesh : Loader.LoadedMeshes )
328
323
{
329
- for (int i = 0 ; i < mesh.Vertices .size (); i += 3 )
324
+ for (size_t i = 0 ; i < mesh.Vertices .size (); i += 3 )
330
325
{
331
326
Triangle* t = new Triangle ();
332
- for (int j = 0 ; j < 3 ; ++j)
327
+ for (size_t j = 0 ; j < 3 ; ++j)
333
328
{
334
329
t->setVertex (j, Vector4f (mesh.Vertices [i + j].Position .X , mesh.Vertices [i + j].Position .Y , mesh.Vertices [i + j].Position .Z , 1.0 ));
335
330
t->setNormal (j, Vector3f (mesh.Vertices [i + j].Normal .X , mesh.Vertices [i + j].Normal .Y , mesh.Vertices [i + j].Normal .Z ));
@@ -342,8 +337,8 @@ int main()
342
337
int key = 0 ;
343
338
int frame_count = 0 ;
344
339
float angle = 140 .0f ;
345
- rst::rasterizer r ( 700 , 700 ) ;
346
- Eigen::Vector3f eye_pos = { 0 .0f ,0 .0f ,10 .0f };
340
+ rst::rasterizer r{ 700 , 700 } ;
341
+ Eigen::Vector3f eye_pos{ 0 .0f ,0 .0f ,10 .0f };
347
342
348
343
// normal_fragment_shader
349
344
// phong_fragment_shader
0 commit comments