-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
261 lines (223 loc) · 6.59 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
#include <iostream>
#include <stdlib.h> /* atoi */
#include <stdio.h>
#include <GL/glut.h>
#include <raytrace.h>
#include <glm/gtc/matrix_transform.hpp>
#include <cmath>
#include <omp.h>
float angX = 0.0;
float angY = 0.0;
float angZ = 0.0;
#define WIDTH 640
#define HEIGHT 480
int numReflection = 0;
RayTracer raytracer;
ImageRGBf img(WIDTH,HEIGHT);
void display(void)
{
// double start,end, printed;
// start = omp_get_wtime();
raytracer.rayTrace(img, numReflection);
// end = omp_get_wtime();
glDrawPixels(WIDTH ,HEIGHT,
GL_RGB, GL_FLOAT, img.data);
// printed = omp_get_wtime();
// printf("Raytracer: %fs \t Printed:%fs\n", end - start, printed-end);
glutSwapBuffers();
};
//inicializa o universo
/***** Informacoes para calculo de projecao ****/
/*
* Informacoes ficam em RayTracer.viewerData, essas informacoes possuem um default
* caso nao seja setado apos a construcao de um objeto RayTracer, sera:
* znear(default = 0.01), zfar (default = 10000.0)
* Camera: from(0.0 ,0.0, -0.5), lookAt(0.0, 0.0, 0.0), vUp(0.0, 1.0, 0.0)
* angulo de abertura do pespective (default = 60.0)
* dimensoes da tela: 640x480;
* posicionamento e orientacao da Camera;
*/
//Cria e posiciona os Objetos
//Cria e posiciona os pontos de luz
void init(int win_width, int win_height)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
Material material;
Vec pos, normal;
float radius = 1.0;
//Configurando a matriz de projecao
raytracer.viewer.setWindowSize(win_width, win_height); //atualiza dimensoes da tela
raytracer.viewer.moveCamera(Vec(0.0, 4.0, -10.0));
// //Configurando luz ambiente e background
raytracer.world.bgColor = Vec(0, 0, 0);
raytracer.world.lightEnv= 1.0;
raytracer.world.ka = 0.0;
material.n_shiny = 200.0;
material.color = Vec(1.0, 1.0, 1.0);
material.setKs(0.5, raytracer.world.ka);
material.kr = 0.6;
pos = Vec(0, 0, 0);
radius = 2.0;
raytracer.world.objs.push_back(new Sphere(material, pos, radius));
material.kr = 0.0;
material.n_shiny = 10.0;
material.color = Vec(1.0, 0.0, 0.0);
material.setKs(0.5, raytracer.world.ka);
pos = Vec(-4.0, 0, 0);
radius = 1.0;
raytracer.world.objs.push_back(new Sphere(material, pos, radius));
material.kr = 0.0;
material.n_shiny = 50.0;
material.color = Vec(0.0, 1.0, 0.0);
material.setKs(0.4, raytracer.world.ka);
pos = Vec(4.0, 0, 0);
radius = 1.0;
raytracer.world.objs.push_back(new Sphere(material, pos, radius));
material.n_shiny = 10.0;
material.kr = 0.0;
material.color = Vec(0.0, 0.0, 1.0);
material.setKs(0.1, raytracer.world.ka);
pos = Vec(0, 0, -4.0);
radius = 1.0;
raytracer.world.objs.push_back(new Sphere(material, pos, radius));
material.n_shiny = 30.0;
material.kr = 1.0;
material.color = Vec(1.0, 1.0, 0.0);
material.setKs(0.1, raytracer.world.ka);
pos = Vec(0, 0, 4.0);
radius = 1.0;
raytracer.world.objs.push_back(new Sphere(material, pos, radius));
material.n_shiny = 60.0;
material.kr = 0.1;
material.color = Vec(0.6, 0.6, 0.6);
material.setKs(0.7, raytracer.world.ka);
pos = Vec(0, 0, 90.0);
normal = Vec(0, 0, -1.0);
raytracer.world.objs.push_back(new Plane(material, pos, normal));
material.kr = 0.0;
material.n_shiny = 30.0;
material.color = Vec(1.0, 1.0, 1.0);
material.setKs(0.1, raytracer.world.ka);
pos = Vec(90, 0, 0.0);
normal = Vec(-1, 0, 0);
raytracer.world.objs.push_back(new Plane(material, pos, normal));
material.kr = 0.0;
material.n_shiny = 30.0;
material.color = Vec(1.0, 1.0, 0.0);
material.setKs(0.1, raytracer.world.ka);
pos = Vec(-90, 0, 0.0);
normal = Vec(1, 0, 0);
raytracer.world.objs.push_back(new Plane(material, pos, normal));
material.kr = 0.0;
material.n_shiny = 30.0;
material.color = Vec(0.0, 0.0, 1.0);
material.setKs(0.6, raytracer.world.ka);
pos = Vec(0, 0, -90.0);
normal = Vec(0, 0, 1);
raytracer.world.objs.push_back(new Plane(material, pos, normal));
material.kr = 0.0;
material.n_shiny = 30.0;
material.color = Vec(0.0, 1.0, 0.0);
material.setKs(0.1, raytracer.world.ka);
pos = Vec(0, 90, 0.0);
normal = Vec(0, -1, 0);
raytracer.world.objs.push_back(new Plane(material, pos, normal));
material.kr = 0.0;
material.n_shiny = 100.0;
material.color = Vec(1.0, 0.0, 0.0);
material.setKs(0.3, raytracer.world.ka);
pos = Vec(0, -90, 0.0);
normal = Vec(0, 1, 0);
raytracer.world.objs.push_back(new Plane(material, pos, normal));
raytracer.world.lights.push_back( LightSource(Vec(5.0,5.0,-5.0), Vec(1.0,1.0,1.0)));
};
void spinLight()
{
#define STEP_LIGHT_R 0.1f //5 graus
static Vec axisY(0.0, 1.0, 0.0);
glm::vec4 posLight(raytracer.world.lights.begin()->pos, 1.0);
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, STEP_LIGHT_R, axisY);
posLight = model*posLight;
Vec aux(posLight.x,posLight.y,posLight.z);
raytracer.world.lights.begin()->pos = aux;
display();
}
void keyboard (unsigned char key, int x, int y){
// #define MOVE_LIGHT
#define STEP_R 0.1f //5 graus
#define STEP_D 2.0f
static Vec axisX(1.0, 0.0, 0.0);
static Vec axisY(0.0, 1.0, 0.0);
static Vec axisZ(0.0, 0.0, 1.0);
glm::vec4 posCam(raytracer.viewer.camera.pos, 1.0);
glm::vec4 posLight(raytracer.world.lights.begin()->pos, 1.0);
glm::mat4 model = glm::mat4(1.0f);
switch (key){
case 'x':
model = glm::rotate(model, STEP_R, axisX);
break;
case 'y':
model = glm::rotate(model, STEP_R, axisY);
break;
case 'z':
model = glm::rotate(model, STEP_R, axisZ);
break;
case 'X':
model = glm::rotate(model, -STEP_R, axisX);
break;
case 'Y':
model = glm::rotate(model, -STEP_R, axisY);
break;
case 'Z':
model = glm::rotate(model, -STEP_R, axisZ);
break;
case 'd':
#ifndef MOVE_LIGHT
posCam.z-=STEP_D;
#else
#endif
break;
case 'D':
#ifndef MOVE_LIGHT
posCam.z+=STEP_D;
#else
#endif
break;
case 'o':
posCam= glm::vec4(0.0, 4.0, -10.0, 1.0);
break;
case 27:
exit(0);
break;
default:
break;
}
#ifndef MOVE_LIGHT
posCam = model*posCam;
raytracer.viewer.moveCamera(Vec(posCam.x,posCam.y, posCam.z));
#else
posLight = model*posLight;
raytracer.world.lights.begin()->pos = posLight;
#endif
display();
}
int main(int argc, char **argv)
{
if(argc == 2)
{
numReflection = atoi(argv[1]);
}
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (WIDTH, HEIGHT);
glutInitWindowPosition (0, 0);
glutCreateWindow ("Ray Tracing Project - UFRN - CG - 2019.2");
init(WIDTH, HEIGHT);
glutIdleFunc(spinLight);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}