-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
110 lines (97 loc) · 3.34 KB
/
Copy pathmain.cpp
File metadata and controls
110 lines (97 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2026 <miguel>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "./model.h"
#include "./tgaimage.h"
constexpr int width = 800;
constexpr int height = 800;
constexpr TGAColor white = {255, 255, 255, 255}; // attention, BGRA order
constexpr TGAColor green = {0, 255, 0, 255};
constexpr TGAColor red = {0, 0, 255, 255};
constexpr TGAColor blue = {255, 128, 64, 255};
constexpr TGAColor yellow = {0, 200, 255, 255};
struct Vec3 {
float x, y, z;
};
struct Face {
int a[3], b[3], c[3];
};
void drawLine(TGAImage& framebuffer, const TGAColor& c, int ax, int ay, int bx,
int by) {
// if line is more vertical than horizontal,we iterate over y instead of x
bool steep = std::abs(ax - bx) < std::abs(ay - by);
if (steep) {
std::swap(ax, ay);
std::swap(bx, by);
}
// swap coordinates so we draw from left to right
if (ax > bx) {
std::swap(ax, bx);
std::swap(ay, by);
}
for (int x = ax; x <= bx; x++) {
float t = (x - ax) / static_cast<float>(bx - ax);
int y = std::round(ay + t * (by - ay));
if (steep) {
framebuffer.set(y, x, c);
} else {
framebuffer.set(x, y, c);
}
}
}
double signedTriangleArea(int ax, int ay, int bx, int by, int cx, int cy) {
return .5 * ((by - ay) * (bx + ax) + (cy - by) * (cx + bx) +
(ay - cy) * (ax + cx));
}
void drawTriangle(int ax, int ay, int bx, int by, int cx, int cy,
TGAImage& framebuffer, const TGAColor& c) {
int bbminx = std::min(std::min(ax, bx), std::min(bx, cx));
int bbminy = std::min(std::min(ay, by), std::min(by, cy));
int bbmaxx = std::max(std::max(ax, bx), std::max(bx, cx));
int bbmaxy = std::max(std::max(ay, by), std::max(by, cy));
double fullArea = signedTriangleArea(ax, ay, bx, by, cx, cy);
if (fullArea < 1) return;
#pragma omp parallel for
for (int x = bbminx; x < bbmaxx; x++) {
for (int y = bbminy; y < bbmaxy; y++) {
double alpha = signedTriangleArea(x, y, bx, by, cx, cy) / fullArea;
double beta = signedTriangleArea(x, y, cx, cy, ax, ay) / fullArea;
double gamma = signedTriangleArea(x, y, ax, ay, bx, by) / fullArea;
if (alpha < 0 || beta < 0 || gamma < 0) continue;
framebuffer.set(x, y, c);
}
}
}
std::tuple<int, int> project(vec3 v) {
return {(v.x + 1.) * width / 2, (v.y + 1.) * height / 2};
}
int main(int argc, char** argv) {
TGAImage framebuffer(width, height, TGAImage::RGB);
std::string path = "./obj/african_head/african_head.obj";
std::vector<Vec3> vertices;
std::vector<Face> faces;
Model model(path);
for (int i = 0; i < model.nfaces(); i++) {
auto [ax, ay] = project(model.vert(i, 0));
auto [bx, by] = project(model.vert(i, 1));
auto [cx, cy] = project(model.vert(i, 2));
TGAColor randomColor;
for (int i = 0; i < 3; i++) randomColor[i] = std::rand() % 255;
drawTriangle(ax, ay, bx, by, cx, cy, framebuffer, randomColor);
}
// for (int i = 0; i < model.nverts(); i++) {
// vec3 v = model.vert(i);
// auto [x, y] = project(v);
// framebuffer.set(x, y, white);
// }
// drawTriangle(7, 45, 35, 100, 45, 60, framebuffer, red);
// drawTriangle(120, 35, 90, 5, 45, 110, framebuffer, white);
// drawTriangle(115, 83, 80, 90, 85, 120, framebuffer, green);
framebuffer.write_tga_file("framebuffer.tga");
return 0;
}