1+ #include < vector>
12#include < cmath>
23#include " tgaimage.h"
4+ #include " model.h"
5+ #include " geometry.h"
36
47const TGAColor white = TGAColor(255 , 255 , 255 , 255 );
58const TGAColor red = TGAColor(255 , 0 , 0 , 255 );
9+ Model *model = NULL ;
10+ const int width = 800 ;
11+ const int height = 800 ;
612
713void line (int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
814 bool steep = false ;
@@ -15,35 +21,42 @@ void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
1521 std::swap (x0, x1);
1622 std::swap (y0, y1);
1723 }
18- int dx = x1-x0;
19- int dy = y1-y0;
20- int derror2 = std::abs (dy)*2 ;
21- int error2 = 0 ;
22- int y = y0;
24+
2325 for (int x=x0; x<=x1; x++) {
26+ float t = (x-x0)/(float )(x1-x0);
27+ int y = y0*(1 .-t) + y1*t;
2428 if (steep) {
25- image.set (y, x, TGAColor ( 255 , 1 ) );
29+ image.set (y, x, color );
2630 } else {
27- image.set (x, y, TGAColor (255 , 1 ));
28- }
29- error2 += derror2;
30-
31- if (error2>dx) {
32- y += (y1>y0?1 :-1 );
33- error2 -= dx*2 ;
31+ image.set (x, y, color);
3432 }
3533 }
3634}
3735
3836int main (int argc, char ** argv) {
39- TGAImage image (100 , 100 , TGAImage::RGB);
40- for (int i=0 ; i<1000000 ; i++) {
41- line (13 , 20 , 80 , 40 , image, white);
42- line (20 , 13 , 40 , 80 , image, red);
43- line (80 , 40 , 13 , 20 , image, red);
37+ if (2 ==argc) {
38+ model = new Model (argv[1 ]);
39+ } else {
40+ model = new Model (" obj/african_head.obj" );
4441 }
42+
43+ TGAImage image (width, height, TGAImage::RGB);
44+ for (int i=0 ; i<model->nfaces (); i++) {
45+ std::vector<int > face = model->face (i);
46+ for (int j=0 ; j<3 ; j++) {
47+ Vec3f v0 = model->vert (face[j]);
48+ Vec3f v1 = model->vert (face[(j+1 )%3 ]);
49+ int x0 = (v0.x +1 .)*width/2 .;
50+ int y0 = (v0.y +1 .)*height/2 .;
51+ int x1 = (v1.x +1 .)*width/2 .;
52+ int y1 = (v1.y +1 .)*height/2 .;
53+ line (x0, y0, x1, y1, image, white);
54+ }
55+ }
56+
4557 image.flip_vertically (); // i want to have the origin at the left bottom corner of the image
4658 image.write_tga_file (" output.tga" );
59+ delete model;
4760 return 0 ;
4861}
4962
0 commit comments