Skip to content

Commit 6d3acdb

Browse files
committed
screen clipping
1 parent c6ef780 commit 6d3acdb

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int main(int argc, char** argv) {
7676
triangle(clip_vert, shader, framebuffer, zbuffer); // actual rasterization routine call
7777
}
7878
}
79-
framebuffer.write_tga_file("framebuffer.tga"); // the vertical flip is moved inside the function
79+
framebuffer.write_tga_file("framebuffer.tga");
8080
return 0;
8181
}
8282

our_gl.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <limits>
21
#include "our_gl.h"
32

43
mat<4,4> ModelView;
@@ -10,7 +9,7 @@ void viewport(const int x, const int y, const int w, const int h) {
109
}
1110

1211
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}}};
1413
}
1514

1615
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
3231
vec4 pts[3] = { Viewport*clip_verts[0], Viewport*clip_verts[1], Viewport*clip_verts[2] }; // triangle screen coordinates before persp. division
3332
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
3433

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};
3836
for (int i=0; i<3; i++)
3937
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]));
4240
}
4341
#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)});
4745
vec3 bc_clip = {bc_screen.x/pts[0][3], bc_screen.y/pts[1][3], bc_screen.z/pts[2][3]};
4846
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
4947
double frag_depth = vec3{clip_verts[0][2], clip_verts[1][2], clip_verts[2][2]}*bc_clip;

0 commit comments

Comments
 (0)