|
1 | 1 | #include <cmath> |
| 2 | +#include <tuple> |
| 3 | +#include "geometry.h" |
| 4 | +#include "model.h" |
2 | 5 | #include "tgaimage.h" |
3 | 6 |
|
| 7 | +constexpr int width = 800; |
| 8 | +constexpr int height = 800; |
| 9 | + |
4 | 10 | constexpr TGAColor white = {255, 255, 255, 255}; // attention, BGRA order |
5 | 11 | constexpr TGAColor green = { 0, 255, 0, 255}; |
6 | 12 | constexpr TGAColor red = { 0, 0, 255, 255}; |
@@ -32,23 +38,25 @@ void line(int ax, int ay, int bx, int by, TGAImage &framebuffer, TGAColor color) |
32 | 38 | } |
33 | 39 | } |
34 | 40 |
|
35 | | -int main(int argc, char** argv) { |
36 | | - constexpr int width = 64; |
37 | | - constexpr int height = 64; |
38 | | - TGAImage framebuffer(width, height, TGAImage::RGB); |
| 41 | +std::tuple<int,int> project(vec3 v) { // First of all, (x,y) is an orthogonal projection of the vector (x,y,z). |
| 42 | + return { (v.x + 1.) * width/2, // Second, since the input models are scaled to have fit in the [-1,1]^3 world coordinates, |
| 43 | + (v.y + 1.) * height/2 }; // we want to shift the vector (x,y) and then scale it to span the entire screen. |
| 44 | +} |
39 | 45 |
|
40 | | - int ax = 7, ay = 3; |
41 | | - int bx = 12, by = 37; |
42 | | - int cx = 62, cy = 53; |
| 46 | +int main(int argc, char** argv) { |
| 47 | + if (argc != 2) { |
| 48 | + std::cerr << "Usage: " << argv[0] << " obj/model.obj" << std::endl; |
| 49 | + return 1; |
| 50 | + } |
43 | 51 |
|
44 | | - line(ax, ay, bx, by, framebuffer, blue); |
45 | | - line(cx, cy, bx, by, framebuffer, green); |
46 | | - line(cx, cy, ax, ay, framebuffer, yellow); |
47 | | - line(ax, ay, cx, cy, framebuffer, red); |
| 52 | + Model model(argv[1]); |
| 53 | + TGAImage framebuffer(width, height, TGAImage::RGB); |
48 | 54 |
|
49 | | - framebuffer.set(ax, ay, white); |
50 | | - framebuffer.set(bx, by, white); |
51 | | - framebuffer.set(cx, cy, white); |
| 55 | + for (int i=0; i<model.nverts(); i++) { // iterate through all vertices |
| 56 | + vec3 v = model.vert(i); // get i-th vertex |
| 57 | + auto [x, y] = project(v); // project it to the screen |
| 58 | + framebuffer.set(x, y, white); |
| 59 | + } |
52 | 60 |
|
53 | 61 | framebuffer.write_tga_file("framebuffer.tga"); |
54 | 62 | return 0; |
|
0 commit comments