forked from ssloy/tinyrenderer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
177 lines (149 loc) · 4.48 KB
/
Copy pathmain.cpp
File metadata and controls
177 lines (149 loc) · 4.48 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <vector>
#include <iostream>
#include <cmath>
#include "tgaimage.h"
#include "model.h"
#include "geometry.h"
const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor red = TGAColor(255, 0, 0, 255);
const TGAColor green = TGAColor(0, 255, 0, 255);
const TGAColor blue = TGAColor(0, 0, 255, 255);
const TGAColor yellow = TGAColor(255, 255, 0, 255);
Model *model = NULL;
const int width = 800;
const int height = 800;
const int depth = 255;
void line(Vec3i p0, Vec3i p1, TGAImage &image, TGAColor color) {
bool steep = false;
if (std::abs(p0.x-p1.x)<std::abs(p0.y-p1.y)) {
std::swap(p0.x, p0.y);
std::swap(p1.x, p1.y);
steep = true;
}
if (p0.x>p1.x) {
std::swap(p0, p1);
}
for (int x=p0.x; x<=p1.x; x++) {
float t = (x-p0.x)/(float)(p1.x-p0.x);
int y = p0.y*(1.-t) + p1.y*t+.5;
if (steep) {
image.set(y, x, color);
} else {
image.set(x, y, color);
}
}
}
Vec3f m2v(Matrix m) {
return Vec3f(m[0][0]/m[3][0], m[1][0]/m[3][0], m[2][0]/m[3][0]);
}
Matrix v2m(Vec3f v) {
Matrix m(4, 1);
m[0][0] = v.x;
m[1][0] = v.y;
m[2][0] = v.z;
m[3][0] = 1.f;
return m;
}
Matrix viewport(int x, int y, int w, int h) {
Matrix m = Matrix::identity(4);
m[0][3] = x+w/2.f;
m[1][3] = y+h/2.f;
m[2][3] = depth/2.f;
m[0][0] = w/2.f;
m[1][1] = h/2.f;
m[2][2] = depth/2.f;
return m;
}
Matrix translation(Vec3f v) {
Matrix Tr = Matrix::identity(4);
Tr[0][3] = v.x;
Tr[1][3] = v.y;
Tr[2][3] = v.z;
return Tr;
}
Matrix zoom(float factor) {
Matrix Z = Matrix::identity(4);
Z[0][0] = Z[1][1] = Z[2][2] = factor;
return Z;
}
Matrix rotation_x(float cosangle, float sinangle) {
Matrix R = Matrix::identity(4);
R[1][1] = R[2][2] = cosangle;
R[1][2] = -sinangle;
R[2][1] = sinangle;
return R;
}
Matrix rotation_y(float cosangle, float sinangle) {
Matrix R = Matrix::identity(4);
R[0][0] = R[2][2] = cosangle;
R[0][2] = sinangle;
R[2][0] = -sinangle;
return R;
}
Matrix rotation_z(float cosangle, float sinangle) {
Matrix R = Matrix::identity(4);
R[0][0] = R[1][1] = cosangle;
R[0][1] = -sinangle;
R[1][0] = sinangle;
return R;
}
int main(int argc, char** argv) {
if (2==argc) {
model = new Model(argv[1]);
} else {
model = new Model("obj/cube.obj");
}
TGAImage image(width, height, TGAImage::RGB);
Matrix VP = viewport(width/4, width/4, width/2, height/2);
{ // draw the axes
Vec3f x(1.f, 0.f, 0.f), y(0.f, 1.f, 0.f), o(0.f, 0.f, 0.f);
o = m2v(VP*v2m(o));
x = m2v(VP*v2m(x));
y = m2v(VP*v2m(y));
line(o, x, image, red);
line(o, y, image, green);
}
Vec3f camera(5, 0, 0);
for (int i=0; i<model->nfaces(); i++) {
std::vector<int> face = model->face(i);
for (int j=0; j<(int)face.size(); j++) {
Vec3f wp0 = model->vert(face[j]);
Vec3f wp1 = model->vert(face[(j+1)%face.size()]);
/*
{ // draw the original model
Vec3f sp0 = m2v(VP*v2m(wp0));
Vec3f sp1 = m2v(VP*v2m(wp1));
Vec3f sc = m2v(VP*v2m(camera));
line(sp0, sp1, image, white);
}
*/
{ // draw the deformed model
// Matrix T = zoom(1.5);
/*
Matrix T = Matrix::identity(4);
T[0][1] = -.18;
Matrix T2 = Matrix::identity(4);
T2[1][0] = .34;
Matrix T3 = Matrix::identity(4);
T3[0][1] = -.18;
*/
// Matrix T = translation(Vec3f(.33, .5, 0))*rotation_z(cos(10.*M_PI/180.), sin(10.*M_PI/180.));
// Matrix T = Matrix::identity(4);
// T[0][1] = -.18;
// Matrix R = rotation_z(cos(20.*M_PI/180.), sin(20.*M_PI/180.));
Matrix T = Matrix::identity(4);
T[3][0] = -.2;
std::cerr << T << std::endl;
// std::cerr << v2m(wp0) << std::endl;
Vec3f sp0 = m2v(VP*T*v2m(wp0));
Vec3f sp1 = m2v(VP*T*v2m(wp1));
line(sp0, sp1, image, red);
}
}
break;
}
image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
image.write_tga_file("output.tga");
delete model;
return 0;
}