Skip to content

Commit e1a3f2b

Browse files
committed
backface culling + gouraud shading
1 parent 9afc1e1 commit e1a3f2b

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

main.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <vector>
22
#include <cmath>
3-
#include <cstdlib> // for rand
43
#include "tgaimage.h"
54
#include "model.h"
65
#include "geometry.h"
@@ -62,14 +61,22 @@ int main(int argc, char** argv) {
6261
}
6362

6463
TGAImage image(width, height, TGAImage::RGB);
64+
Vec3f light_dir(0,0,-1);
6565
for (int i=0; i<model->nfaces(); i++) {
6666
std::vector<int> face = model->face(i);
6767
Vec2i screen_coords[3];
68+
Vec3f world_coords[3];
6869
for (int j=0; j<3; j++) {
69-
Vec3f world_coords = model->vert(face[j]);
70-
screen_coords[j] = Vec2i((world_coords.x+1.)*width/2., (world_coords.y+1.)*height/2.);
70+
Vec3f v = model->vert(face[j]);
71+
screen_coords[j] = Vec2i((v.x+1.)*width/2., (v.y+1.)*height/2.);
72+
world_coords[j] = v;
73+
}
74+
Vec3f n = (world_coords[2]-world_coords[0])^(world_coords[1]-world_coords[0]);
75+
n.normalize();
76+
float intensity = n*light_dir;
77+
if (intensity>0) {
78+
triangle(screen_coords[0], screen_coords[1], screen_coords[2], image, TGAColor(intensity*255, intensity*255, intensity*255, 255));
7179
}
72-
triangle(screen_coords[0], screen_coords[1], screen_coords[2], image, TGAColor(rand()%255, rand()%255, rand()%255, 255));
7380
}
7481

7582
image.flip_vertically(); // i want to have the origin at the left bottom corner of the image

0 commit comments

Comments
 (0)