-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
422 lines (377 loc) · 15.4 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include "include/tgaimage.h"
#include "include/model.h"
#include "include/geometry.h"
#include "include/HzzzImage.h"
#include "include/RayTraceable.h"
#include "include/RayTracer.h"
#include "include/config.h"
#define DEBUG
const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor red = TGAColor(255, 0, 0, 255);
const TGAColor green = TGAColor(0, 255, 0, 255);
Model *model = NULL;
#ifdef HIGH_RES
const int width = 1000;
const int height = 1000;
#else
const int width = 100;
const int height = 100;
#endif
// float zbuffer[width][height];
HzzzImage RenderImage(Model* model, TGAImage& texture)
{
try
{
HzzzImage image(width, height);
for(int i=0; i<model->nfaces(); i++)
// for(int i=0; i<; i++)
{
std::vector<int> face = model->face(i);
std::vector<int> t_face = model->texture_face(i);
Vec3f world_coords[3];
Vec2i screen_coords[3];
Vec2f uv_coords[3];
for(int j=0; j<3; j++)
{
Vec3f v = model->vert(face[j]);
screen_coords[j] = Vec2i((v.x+1.)*width/2., (v.y+1.)*height/2.);
world_coords[j] = v;
uv_coords[j] = model->uv(t_face[j]);
}
image.Triangle(world_coords[0], world_coords[1], world_coords[2], uv_coords[0], uv_coords[1], uv_coords[2], texture);
// image.Line(screen_coords[0], screen_coords[1], white);
// image.Line(screen_coords[1], screen_coords[2], white);
// image.Line(screen_coords[2], screen_coords[0], white);
}
return image;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
std::cout << "Fail to render." << std::endl;
}
}
RayTracer GenCornellBox()
{
RayTracer tracer;
tracer.scene = std::vector<RayTraceable*>();
RayTraceable* object = new RayTraceableInfPlane(0, 0, 1, -3);
object->color = white;
tracer.addSon(object);
object = new RayTraceableInfPlane(1, 0, 0, 1.5);
object->color = red;
tracer.addSon(object);
object = new RayTraceableInfPlane(1, 0, 0, -1.5);
object->color = green;
tracer.addSon(object);
object = new RayTraceableInfPlane(0, 1, 0, 3);
object->color = white;
tracer.addSon(object);
object = new RayTraceableInfPlane(0, 1, 0, 0);
object->color = white;
tracer.addSon(object);
object = new RayTraceableInfPlane(0, 1, 0, -3);
object->color = white;
tracer.addSon(object);
float a = 0.5;
object = new RayTraceableBox(std::vector<Vec3f>{Vec3f(-a, a, a), Vec3f(a, a, a),
Vec3f(-a, a, -a), Vec3f(a, a, -a),
Vec3f(-a, -a, -a), Vec3f(a, -a, -a),
Vec3f(-a, -a, a), Vec3f(a, -a, a)});
object->color = white;
static_cast<RayTraceableBox*>(object)->rotate(0.2, Vec3f(0, 1, 0));
static_cast<RayTraceableBox*>(object)->move(Vec3f(0.3, a, 0));
tracer.addSon(object);
object = new RayTraceableBox(std::vector<Vec3f>{Vec3f(-a, 2*a, a), Vec3f(a, 2*a, a),
Vec3f(-a, 2*a, -a), Vec3f(a, 2*a, -a),
Vec3f(-a, -2*a, -a), Vec3f(a, -2*a, -a),
Vec3f(-a, -2*a, a), Vec3f(a, -2*a, a)});
object->color = white;
static_cast<RayTraceableBox*>(object)->rotate(-0.2, Vec3f(0, 1, 0));
static_cast<RayTraceableBox*>(object)->move(Vec3f(-0.3, 2*a, 2));
tracer.addSon(object);
return tracer;
}
int main()
{
RayTracer rt = GenCornellBox();
HzzzImage image(width, height);
image.setCamera(Vec3f(0, 1, -10));
image.setViewDirection(Vec3f(0, 0, 1));
std::vector<Model*> light_models;
for(int i=0; i<4; i++)
{
light_models.push_back(new Model());
}
image.RayTracingRender(rt.scene, light_models);
image.saveToTGAImage("output.tga");
return 0;
}
int not_main(int argc, char** argv)
{
// TGAImage frame(200, 200, TGAImage::RGB);
// Vec2i pts[3] = {Vec2i(10,10), Vec2i(100, 30), Vec2i(190, 160)};
// triangle(pts[0], pts[1], pts[2], frame, TGAColor(255, 0, 0));
// frame.flip_vertically(); // to place the origin in the bottom left corner of the image
// frame.write_tga_file("framebuffer.tga");
// return 0;
// for(int i=0; i<width; i++)
// {
// for(int j=0; j<height; j++)
// {
// zbuffer[i][j] = -std::numeric_limits<float>::max();
// }
// }
// Model* model_rock = new Model("obj/models/rock/rock.obj");
// if (2==argc)
// {
// model = new Model(argv[1]);
// } else
// {
// // model = new Model("/obj/spot_triangulated.obj");
// model = new Model("obj/new.obj");
// // model->calNorm();
// // std::cout << model->toString() << std::endl;
// // model = new Model("obj/models/rock/rock.obj");
// // model = new Model("obj/african_head.obj");
// std::cout << model->toString() << std::endl;
// std::cout << "model loaded" << std::endl;
// }
model = new Model("obj/new.obj");
TGAImage texture;
// TGAImage texture_rock;
// texture.read_tga_file("obj/african_head_diffuse.tga");
// texture.read_tga_file("obj/models/rock/rock.tga");
texture.read_tga_file("obj/spot_texture.tga");
texture.flip_vertically();
model->setTexture(&texture);
// texture_rock.read_tga_file("obj/models/rock/rock.tga");
// texture.flip_vertically();
// texture_rock.flip_vertically();
// model->setTexture(&texture);
// model_rock->setTexture(&texture_rock);
float d = M_PI/30;
std::vector<Vec3f> light_vector;
std::vector<Model*> light_models;
// model_rock->scale(0.1);
// RayTraceable rt(Vec3f(0, 1, 0), 1);
// RayTraceable sphere(Vec3f(1, 0.2, 0), 0.2);
// RayTraceable plane(0, 1, 0, 0);
// RayTraceable plane2(1, 1, 1, -3);
RayTraceable** rts = new RayTraceable*[10];
RayTracer tracer;
rts[0] = new RayTraceableInfPlane(0, 1, 0, 1);
rts[0]->color = TGAColor(200, 120, 200, 255);
rts[0]->father_tracer = &tracer;
rts[1] = new RayTraceableInfPlane(0, 0, 1, -1.5);
rts[1]->color = TGAColor(200, 200, 120, 255);
rts[1]->father_tracer = &tracer;
rts[2] = new RayTraceableSphere(Vec3f(1, 0.2, 0), 0.2);
rts[2]->color = TGAColor(120, 200, 200, 255);
rts[2]->father_tracer = &tracer;
rts[3] = new RayTraceableSphere(Vec3f(-2, 1, 0), 1);
rts[3]->color = TGAColor(255, 255, 255, 255);
rts[3]->father_tracer = &tracer;
// rts[4] = new RayTraceableModel(model);
float a = 0.3;
rts[4] = new RayTraceableBox(std::vector<Vec3f>{Vec3f(-a, a, a), Vec3f(a, a, a),
Vec3f(-a, a, -a), Vec3f(a, a, -a),
Vec3f(-a, -a, -a), Vec3f(a, -a, -a),
Vec3f(-a, -a, a), Vec3f(a, -a, a)});
static_cast<RayTraceableBox*>(rts[4])->rotate(0.5, Vec3f(0, 1, 0));
static_cast<RayTraceableBox*>(rts[4])->rotate(0.5, Vec3f(1, 0, 0));
static_cast<RayTraceableBox*>(rts[4])->move(Vec3f(0, -0.3, 0));
rts[4]->color = TGAColor(255, 255, 255, 255);
rts[4]->father_tracer = &tracer;
tracer.scene = std::vector<RayTraceable*>();
// tracer.lights = std::vector<Model*>();
for(int i=0; i<5; i++)
{
tracer.scene.push_back(rts[i]);
}
HzzzImage hi(width, height);
hi.setCamera(Vec3f(0, 1, -10));
hi.setViewDirection(Vec3f(0, 0, 1));
for(int i=0; i<4; i++)
{
light_models.push_back(new Model());
}
// hi.RayTracingRender(tracer.scene, light_models);
// HzzzImage hi(width, height);
// hi.setCamera(Vec3f(0, 1, -10));
// hi.setViewDirection(Vec3f(0, 0, 1));
// // std::vector<RayTraceable*> ray_traceables;
// // ray_traceables = std::vector<RayTraceable*>();
// rt.color = TGAColor(255, 255, 255, 255);
// plane.color = TGAColor(200, 120, 200, 255);
// plane2.color = TGAColor(200, 200, 120, 255);
// sphere.color = TGAColor(120, 200, 200, 255);
// tracer.scene = std::vector<RayTraceable*>();
// tracer.scene.push_back(&rt);
// rt.father_tracer = &tracer;
// tracer.scene.push_back(&sphere);
// sphere.father_tracer = &tracer;
// tracer.scene.push_back(&plane);
// plane.father_tracer = &tracer;
// tracer.scene.push_back(&plane2);
// plane2.father_tracer = &tracer;
// // ray_traceables.push_back(&rt);
// // ray_traceables.push_back(&plane);
// // ray_traceables.push_back(&plane2);
// // ray_traceables.push_back(&sphere);
// hi.RayTracingRender(tracer.scene, light_models);
for(int i=0; i<1; i++)
{
HzzzImage hz(width, height);
hz.setCamera(Vec3f(0, 1, -10));
hz.setViewDirection(Vec3f(-0.1, -0.1, 1));
hz.RayTracingRender(tracer.scene, light_models);
// hz.toFile("outfiles/" + std::to_string(i) + std::string(".hzzz"));
TGAImage tga_image = hz.toTGAImage();
tga_image.flip_vertically();
tga_image.write_tga_file("output.tga");
static_cast<RayTraceableBox*>(rts[4])->rotate(d, Vec3f(0, 1, 0));
for(int j=0; j<4; j++)
{
light_models[j]->move(Vec3f(cos(d*i)/4, 0, 0));
}
std::cout << "#" << i << std::endl;
}
// hi.RayTracingRender(&rt, 90, light_models);
// hi.RayTracingRender(&plane, 90, light_models);
// TGAImage tga_image = hi.toTGAImage();
// tga_image.flip_vertically();
// tga_image.write_tga_file("output.tga");
std::cout << "C++ ALL DONE" << std::endl;
// image.RayTracingRender(&rt, 90, light_models);
// for(int i=0; i<60; i++)
// {
// // image.RayTracingRender(model, texture, 90, light_models);
// HzzzImage image(width, height);
// // image.RayTracingRender(&rt, 90, light_models);
// image.setCamera(Vec3f(0, 1, -4));
// image.setViewDirection(Vec3f(0, 0, 1));
// image.RayTracingRender(ray_traceables, light_models);
// image.toFile("outfiles/" + std::to_string(i) + std::string(".hzzz"));
// std::cout << "#" ;
// for(int j=0; j<5; j++)
// {
// light_models[j]->move(Vec3f(-0.05, 0, 0));
// }
// // for(int j=0; j<5; j++)
// // {
// // light_models[j]->scale(1.05);
// // }
// }
// image.RayTracingRender(model, texture, 90, light_models);
// image.RenderModel(model, texture, Vec3f(cos(d*2), sin(d*2), -1));
// image.RenderModel(model_rock, texture_rock, Vec3f(cos(d*2), sin(d*2), -1));
// image.RenderModel(model, Vec3f(cos(d*2), sin(d*2), -1));
// for(int i=0; i<100; i++)
// {
// image.RenderModel(new Model(), Vec3f(cos(d*i), sin(d*i), -1));
// }
// image.RayTracingRender(model, texture, 90, light_vector);
// TGAImage tga_image = image.toTGAImage();
// tga_image.flip_vertically();
// tga_image.write_tga_file("output.tga");
// model->rotate(20*d);
// int video = 1;
// if(video)
// {
// for(int k=0; k<60; k++)
// {
// // HzzzImage image = RenderImage(model, texture);
// // model->rotate(d*k);
// HzzzImage image(width, height);
// image.RenderModel(model, texture, Vec3f(cos(d*k*2), sin(d*k*2), -1));
// image.toFile("outfiles/" + std::to_string(k) + std::string(".hzzz"));
// // TGAImage tgaimage = image.toTGAImage();
// // tgaimage.flip_vertically();
// // tgaimage.write_tga_file(("outputs/" + std::to_string(k) + std::string(".tga")).data());
// model->rotate(d);
// std::cout << "#" ;
// }
// }
// else
// {
// int k = 2;
// HzzzImage image(width, height);
// image.RenderModel(model, texture, Vec3f(cos(d*k*2), sin(d*k*2), -1));
// TGAImage tgaimage = image.toTGAImage();
// tgaimage.flip_vertically();
// // tgaimage.write_tga_file(("outputs/" + std::to_string(k) + std::string(".tga")).data());
// tgaimage.write_tga_file("output.tga");
// }
// TGAImage tgaimage = image.toTGAImage();
// tgaimage.flip_vertically();
// tgaimage.write_tga_file("output.tga");
// TGAImage image(width, height, TGAImage::RGB);
// TGAImage uvmap;
// uvmap.read_tga_file("obj/spot_texture.tga");
// uvmap.read_tga_file("obj/african_head_diffuse.tga");
// uvmap.read_tga_file("obj/models/rock/rock.tga");
// std::cout << "read tga file" << std::endl;
// uvmap.flip_vertically();
// float l = 1.0;
// Vec3f light_dir(0,0,-1); // define light_dir
// std::cout << "nfaces: " << model->nfaces() << std::endl;
// std::cout << "rendering" << std::endl;
// int k=0;
// for (int i=0; i<model->nfaces(); i++)
// {
// std::cout << "#" ;
// std::vector<int> face = model->face(i);
// std::vector<int> full_face = model->full_face(i);
// Vec2i screen_coords[3];
// Vec3f world_coords[3];
// Vec2f uv_coords[3];
// for (int j=0; j<3; j++)
// {
// // std::cout << face[j] << " " ;
// Vec3f v = model->vert(face[j]);
// screen_coords[j] = Vec2i((v.x+1.)*width/2., (v.y+1.)*height/2.);
// world_coords[j] = v;
// uv_coords[j] = model->uv(full_face[3*j+1]);
// // std::cout << full_face[j] << std::endl;
// // std::cout << uv_coords[j].x << " " << uv_coords[j].y << std::endl;
// }
// // Vec3f n = (world_coords[2]-world_coords[0])^(world_coords[1]-world_coords[0]);
// // n.normalize();
// // float intensity = n*light_dir;
// // if (intensity>0)
// if(1)
// {
// // triangle(screen_coords[0], screen_coords[1], screen_coords[2], image, TGAColor(intensity*255, intensity*255, intensity*255, 255));
// mapTriangle(full_face, world_coords, uv_coords, height, width, image, uvmap);
// // k++;
// // if(k == 100)
// // {
// // break;
// // }
// }
// // mytriangle(model->vert(face[0]), model->vert(face[1]), model->vert(face[2]), height, width, image, TGAColor(rand()%255, rand()%255, rand()%255, 255));
// // for (int j=0; j<3; j++) {
// // Vec3f v0 = model->vert(face[j]);
// // Vec3f v1 = model->vert(face[(j+1)%3]);
// // int x0 = (v0.x+1.)*width/2.;
// // int y0 = (v0.y+1.)*height/2.;
// // int x1 = (v1.x+1.)*width/2.;
// // int y1 = (v1.y+1.)*height/2.;
// // // int x0 = (v0.x*(l/(l+std::abs(v0.z+1.)))+1.)*width/2.;
// // // int y0 = (v0.y*(l/(l+std::abs(v0.z+1.)))+1.)*height/2.;
// // // int x1 = (v1.x*(l/(l+std::abs(v1.z+1.)))+1.)*width/2.;
// // // int y1 = (v1.y*(l/(l+std::abs(v1.z+1.)))+1.)*height/2.;
// // line(x0, y0, x1, y1, image, white);
// // }
// }
// image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
// std::cout << uvmap.get_width() << " " << uvmap.get_height() << std::endl;
// image.write_tga_file("output.tga");
delete model;
return 0;
}