1
- #include < limits>
2
1
#include " our_gl.h"
3
2
4
3
mat<4 ,4 > ModelView;
@@ -10,7 +9,7 @@ void viewport(const int x, const int y, const int w, const int h) {
10
9
}
11
10
12
11
void projection (const double f) { // check https://en.wikipedia.org/wiki/Camera_matrix
13
- Projection = {{{1 ,0 ,0 ,0 }, {0 ,-1 ,0 ,0 }, {0 ,0 ,1 ,0 }, {0 ,0 ,-1 /f,0 }}}; // P[1,1] = -1; does vertical flip
12
+ Projection = {{{1 ,0 ,0 ,0 }, {0 ,-1 ,0 ,0 }, {0 ,0 ,1 ,0 }, {0 ,0 ,-1 /f,0 }}};
14
13
}
15
14
16
15
void lookat (const vec3 eye, const vec3 center, const vec3 up) { // check https://github.com/ssloy/tinyrenderer/wiki/Lesson-5-Moving-the-camera
@@ -32,18 +31,17 @@ void triangle(const vec4 clip_verts[3], IShader &shader, TGAImage &image, std::v
32
31
vec4 pts[3 ] = { Viewport*clip_verts[0 ], Viewport*clip_verts[1 ], Viewport*clip_verts[2 ] }; // triangle screen coordinates before persp. division
33
32
vec2 pts2[3 ] = { proj<2 >(pts[0 ]/pts[0 ][3 ]), proj<2 >(pts[1 ]/pts[1 ][3 ]), proj<2 >(pts[2 ]/pts[2 ][3 ]) }; // triangle screen coordinates after perps. division
34
33
35
- vec2 bboxmin{ std::numeric_limits<double >::max (), std::numeric_limits<double >::max ()};
36
- vec2 bboxmax{-std::numeric_limits<double >::max (), -std::numeric_limits<double >::max ()};
37
- vec2 clamp{image.width ()-1 , image.height ()-1 };
34
+ int bboxmin[2 ] = {image.width ()-1 , image.height ()-1 };
35
+ int bboxmax[2 ] = {0 , 0 };
38
36
for (int i=0 ; i<3 ; i++)
39
37
for (int j=0 ; j<2 ; j++) {
40
- bboxmin[j] = std::max ( 0 ., std:: min (bboxmin[j], pts2[i][j]));
41
- bboxmax[j] = std::min (clamp[j], std:: max (bboxmax[j], pts2[i][j]));
38
+ bboxmin[j] = std::min (bboxmin[j], static_cast < int >( pts2[i][j]));
39
+ bboxmax[j] = std::max (bboxmax[j], static_cast < int >( pts2[i][j]));
42
40
}
43
41
#pragma omp parallel for
44
- for (int x=( int ) bboxmin. x ; x<=( int ) bboxmax. x ; x++) {
45
- for (int y=( int ) bboxmin. y ; y<=( int ) bboxmax. y ; y++) {
46
- vec3 bc_screen = barycentric (pts2, {x, y });
42
+ for (int x=std::max ( bboxmin[ 0 ], 0 ) ; x<=std::min ( bboxmax[ 0 ], image. width ()- 1 ) ; x++) {
43
+ for (int y=std::max ( bboxmin[ 1 ], 0 ) ; y<=std::min ( bboxmax[ 1 ], image. height ()- 1 ) ; y++) {
44
+ vec3 bc_screen = barycentric (pts2, {static_cast < double >(x), static_cast < double >(y) });
47
45
vec3 bc_clip = {bc_screen.x /pts[0 ][3 ], bc_screen.y /pts[1 ][3 ], bc_screen.z /pts[2 ][3 ]};
48
46
bc_clip = bc_clip/(bc_clip.x +bc_clip.y +bc_clip.z ); // check https://github.com/ssloy/tinyrenderer/wiki/Technical-difficulties-linear-interpolation-with-perspective-deformations
49
47
double frag_depth = vec3{clip_verts[0 ][2 ], clip_verts[1 ][2 ], clip_verts[2 ][2 ]}*bc_clip;
0 commit comments