-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
322 lines (297 loc) · 8.48 KB
/
main.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <string>
#include <vector>
#include <iostream>
#include <functional>
#include "3rdParty/model.h"
#include "renderer/renderer.h"
#include "helper/interpolationHelper.h"
using namespace std;
class Lesson
{
public:
virtual void start() = 0;
};
class Lesson1 : public Lesson
{
const TGAColor red = TGAColor(255, 0, 0, 255);
const TGAColor white = TGAColor(255, 255, 255, 255);
public:
void start() override
{
// 红点
{
TGAImage image(100, 100, TGAImage::RGB);
image.set(40, 50, red);
image.flip_vertically();
image.write_tga_file("output/lesson1/point.tga");
}
{
TGAImage image(100, 100, TGAImage::RGB);
Interpolation0({ 13, 20 }, { 80, 40 }, [&](float2 p) { image.set(p.x, p.y, white); });
Interpolation0({ 20, 13 }, { 40, 80 }, [&](float2 p) { image.set(p.x, p.y, red); });
Interpolation0({ 80, 40 }, { 13, 20 }, [&](float2 p) { image.set(p.x, p.y, red); });
image.flip_vertically();
image.write_tga_file("output/lesson1/temp.tga");
}
// 一条红线
{
TGAImage image(100, 100, TGAImage::RGB);
Interpolation({10, 10}, {90, 90}, [&](float2 p) { image.set(p.x, p.y, red); });
Interpolation({ 20, 10 }, { 20, 90 }, [&](float2 p) { image.set(p.x, p.y, red); });
Interpolation({ 10, 10 }, { 9, 90 }, [&](float2 p) { image.set(p.x, p.y, red); });
image.flip_vertically();
image.write_tga_file("output/lesson1/redline.tga");
}
// 模型
{
const float width = 1000;
const float height = 1000;
Model model("resource/african_head/african_head.obj");
TGAImage image(width, height, TGAImage::RGB);
for (int i = 0; i<model.nfaces(); i++)
{
std::vector<int> face = model.face(i);
for (int j = 0; j<3; j++)
{
float3 v1 = model.vert(face[j]);
float3 v2 = model.vert(face[(j + 1) % 3]);
float2 p1((v1.x + 1.0) * width / 2.0, (v1.y + 1.0) * height / 2.0);
float2 p2((v2.x + 1.0) * width / 2.0, (v2.y + 1.0) * height / 2.0);
Interpolation(p1, p2, [&](float2 p)
{
image.set(p.x, p.y, white);
});
}
}
image.flip_vertically();
image.write_tga_file("output/lesson1/model.tga");
}
}
};
class Lesson2 : public Lesson
{
const TGAColor red = TGAColor(255, 0, 0, 255);
const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor green = TGAColor(0, 255, 0, 255);
void triangle(float2 p1, float2 p2, float2 p3, function<void(float2)> handler)
{
Interpolation(p1, p2, handler);
Interpolation(p1, p3, handler);
Interpolation(p2, p3, handler);
}
public:
void start() override
{
// 镂空三角形
{
float2 t0[3] = { float2(10, 70), float2(50, 160), float2(70, 80) };
float2 t1[3] = { float2(180, 50), float2(150, 1), float2(70, 180) };
float2 t2[3] = { float2(180, 150), float2(120, 160), float2(130, 180) };
TGAImage image(200, 200, TGAImage::RGB);
triangle(t0[0], t0[1], t0[2], [&](float2 p)
{
image.set(p.x, p.y, red);
});
triangle(t1[0], t1[1], t1[2], [&](float2 p)
{
image.set(p.x, p.y, white);
});
triangle(t2[0], t2[1], t2[2], [&](float2 p)
{
image.set(p.x, p.y, green);
});
image.flip_vertically();
image.write_tga_file("output/lesson2/simpleTriangle.tga");
}
// 三角形
{
float2 t0[3] = { float2(10, 70), float2(50, 160), float2(70, 80) };
float2 t1[3] = { float2(180, 50), float2(150, 1), float2(70, 180) };
float2 t2[3] = { float2(180, 150), float2(120, 160), float2(130, 180) };
TGAImage image(200, 200, TGAImage::RGB);
Interpolation(t0[0], t0[1], t0[2], [&](float2 p)
{
image.set(p.x, p.y, red);
});
Interpolation(t1[0], t1[1], t1[2], [&](float2 p)
{
image.set(p.x, p.y, white);
});
Interpolation(t2[0], t2[1], t2[2], [&](float2 p)
{
image.set(p.x, p.y, green);
});
image.flip_vertically();
image.write_tga_file("output/lesson2/triangle.tga");
}
// 随机颜色模型
{
const float width = 1000;
const float height = 1000;
Model model("resource/african_head/african_head.obj");
TGAImage image(width, height, TGAImage::RGB);
for (int i = 0; i<model.nfaces(); i++)
{
std::vector<int> face = model.face(i);
float2 screen_coords[3];
for (int j = 0; j<3; j++)
{
float3 v = model.vert(face[j]);
screen_coords[j] = float2(int((v.x + 1)*width / 2.0), int((v.y + 1)*height / 2.0));
}
TGAColor randomColor(rand() % 255, rand() % 255, rand() % 255, 255);
Interpolation(screen_coords[0], screen_coords[1], screen_coords[2], [&](float2 p)
{
image.set(p.x, p.y, randomColor);
});
}
image.flip_vertically();
image.write_tga_file("output/lesson2/randomColorModel.tga");
}
// 简单光照
{
const float width = 1000;
const float height = 1000;
float3 lightDir(0, 0, -1);
Model model("resource/african_head/african_head.obj");
TGAImage image(width, height, TGAImage::RGB);
for (int i = 0; i<model.nfaces(); i++)
{
std::vector<int> face = model.face(i);
float2 screen_coords[3];
float3 world_coords[3];
for (int j = 0; j<3; j++)
{
float3 v = model.vert(face[j]);
screen_coords[j] = float2(int((v.x + 1)*width / 2.0), int((v.y + 1)*height / 2.0));
world_coords[j] = v;
}
float3 a = world_coords[2] - world_coords[0];
float3 b = world_coords[1] - world_coords[0];
float3 n = a.cross(b);
float diffuse = n.normalize().dot(lightDir);
if (diffuse > 0)
{
TGAColor lightColor(diffuse * 255, diffuse * 255, diffuse * 255, 255);
Interpolation(screen_coords[0], screen_coords[1], screen_coords[2], [&](float2 p)
{
image.set(p.x, p.y, lightColor);
});
}
}
image.flip_vertically();
image.write_tga_file("output/lesson2/lightModel.tga");
}
}
};
class Lesson3 : public Lesson
{
const TGAColor red = TGAColor(255, 0, 0, 255);
const TGAColor blue = TGAColor(0, 0, 255, 255);
const TGAColor green = TGAColor(0, 255, 0, 255);
public:
void start() override
{
// z-buffer
{
const int width = 500;
const int height = 500;
float3 lightDir(0, 0, -1);
Model model("resource/african_head/african_head.obj");
TGAImage image(width, height, TGAImage::RGB);
float zBuffer[width][height];
for(int i = 0; i < width; ++i)
for(int j = 0; j < height; ++j)
zBuffer[i][j] = -std::numeric_limits<float>::max();
for (int i = 0; i<model.nfaces(); i++)
{
std::vector<int> face = model.face(i);
float3 screen_coords[3];
float3 world_coords[3];
for (int j = 0; j<3; j++)
{
float3 v = model.vert(face[j]);
screen_coords[j] = float3(int((v.x + 1)*width / 2.0 + 0.5), int((v.y + 1)*height / 2.0 + 0.5), v.z);
world_coords[j] = v;
}
float3 a = world_coords[2] - world_coords[0];
float3 b = world_coords[1] - world_coords[0];
float3 n = a.cross(b);
float diffuse = n.normalize().dot(lightDir);
if (diffuse > 0)
{
TGAColor lightColor(diffuse * 255, diffuse * 255, diffuse * 255, 255);
Interpolation(screen_coords[0], screen_coords[1], screen_coords[2], [&](float3 p)
{
if (p.z > zBuffer[int(p.x)][int(p.y)])
{
image.set(p.x, p.y, lightColor);
zBuffer[int(p.x)][int(p.y)] = p.z;
}
});
}
}
image.flip_vertically();
image.write_tga_file("output/lesson3/zBufferModel.tga");
image.clear();
for (int i = 0; i < width; ++i)
{
for (int j = 0; j < height; ++j)
{
int gray = (zBuffer[i][j] + 1) / 2 * 255;
image.set(i, j, TGAColor(gray, gray, gray, 255));
}
}
image.flip_vertically();
image.write_tga_file("output/lesson3/zBuffer.tga");
}
}
};
class Lesson4 : public Lesson
{
public:
void start() override
{
auto renderer = new Renderer();
Model model("resource/african_head/african_head.obj");
std::vector<vertex> vertexes;
vertexes.reserve(model.nfaces()*3);
for (int i = 0; i < model.nfaces(); i++)
{
for (int j = 0; j < 3; j++)
{
vertex v;
v.position = model.vert(i, j) * 400;
v.uv = model.uv(i, j);
v.normal = model.normal(i, j);
vertexes.push_back(v);
}
}
renderer->setVertexes(vertexes);
renderer->setVertAndFrag(&Lesson4::vert, &Lesson4::frag);
renderer->render("output/lesson4/pipeline.tga");
}
static v2f vert(appdata i)
{
v2f o;
o.position = i.vertex;
return o;
}
static float4 frag(v2f i)
{
return float4(1, 1, 1, 1);
}
};
int main(int argc, char** argv)
{
vector<Lesson*> lessons = { new Lesson1(), new Lesson2(), new Lesson3(), new Lesson4() };
lessons[3]->start();
for (auto lesson : lessons)
{
delete lesson;
}
lessons.clear();
cout << "Finish!" << endl;
//cin.get();
return 0;
}