-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraytrace.cpp
217 lines (183 loc) · 5.3 KB
/
raytrace.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
#include "raytrace.h"
#include <stdio.h>
#include <string.h>
#include <omp.h>
#define PARALLEL
//height - i - 1 => isso com que o a coordenada na imagem (0,0) seja no canto inferior esquerdo
//isso eh necessario pois eh a referencia do opengl
#define IMG(i,j,c) (data[(height - i - 1)*width*3 + (j)*3 + (c)])
#define MAXDOUBLE 99999999
float ImageRGBf::operator()(uint i, uint j, uint k){
int n = i*width*3 + j*3 + k;
if(n >= width) return -1;
return data[n];
}
ImageRGBf::ImageRGBf(){
data = NULL;
width = 0;
height= 0;
}
ImageRGBf::ImageRGBf(uint width, uint height){
// puts("criando img");
this->width = width;
this->height = height;
data = new float[width*height*3];
}
ImageRGBf::~ImageRGBf(){
if(data != NULL)
delete[] data;
}
void ImageRGBf::resize(uint newWidth,uint newHeight){
if(data != NULL)
delete[] data;
float *newData = new float[newWidth*newHeight*3];
data = newData;
this->width = newWidth;
this->height = newHeight;
}
void ImageRGBf::setColor(uint i, uint j, const Vec color){
IMG(i, j, 0) = color[0];//set RED
IMG(i, j, 1) = color[1];//set GREEN
IMG(i, j, 2) = color[2];//set BLUE
}
void
RayTracer::rayTrace(ImageRGBf &img, int numReflection)const
{
#ifndef PARALLEL
for(int lin = 0; lin < img.height; lin++)
{
for (int col = 0; col < img.width; col++)
{
Vec ray_dir = viewer.pixelToWorld(col, lin);
{
Vec color = trace(Ray(viewer.camera.pos, ray_dir), numReflection);
img.setColor(lin, col, color);
}
}
}
#else
int size = img.height*img.width;
Vec *rayList;
rayList = new Vec[size];
for(int i = 0; i < img.height; i++)
for(int j = 0; j < img.width; j++)
{
rayList[i*img.width + j] = viewer.pixelToWorld(j, i);
}
#pragma omp parallel for schedule(runtime) default(none) \
shared(img, rayList) firstprivate(numReflection) collapse (2)
for(int lin = 0; lin < img.height; lin++)
{
for (int col = 0; col < img.width; col++)
{
Vec color = trace(Ray(viewer.camera.pos, rayList[lin*img.width + col]), numReflection);
img.setColor(lin, col, color);
}
}
delete[] rayList;
#endif
}
Vec
RayTracer::trace(const Ray ray, int num_reflection)const
{
Ray ray_reflected;
bool reflection;
Vec point_intersection;
Vec result_color(0.0);
Object* obj=NULL;
//Caso base
//calcular o ponto de intersecao mais proximo
if(closestPoint(ray, point_intersection, &obj) == false)
return world.bgColor;
for(auto &lsource : world.lights)
{
result_color += shade(ray,
lsource,
point_intersection,
obj,
ray_reflected,
reflection);
if((obj->material.kr != 0.0) && reflection && (num_reflection != 0))
{
result_color += obj->material.kr*trace(ray_reflected, num_reflection-1);
}
}
return result_color;
}
bool
RayTracer::closestPoint(/*in*/const Ray ray,
/*out*/Vec &point_intersec,
/*out*/Object **obj)const
{
double smallerDistance = MAXDOUBLE;
bool intersec = false;
/*itera os objetos, calcula o ponto de inteseccao e pega o menor deles*/
for(auto it = world.objs.begin();it != world.objs.end();it++){
Vec pointTmp;
double distTmp;
if((*it)->intersectRay(ray, pointTmp, distTmp)){// ===> TODO
if( (distTmp < smallerDistance) && (distTmp > 0.0)){
smallerDistance = distTmp;
*obj = *it;
point_intersec = pointTmp;
intersec = true;
}
}
}
return intersec;
}
Vec
RayTracer::shade(/*in*/const Ray ray,
/*in*/const LightSource &source,
/*in*/const Vec point, //ponto sobre a superficie do objeto
/*in*/const Object *obj,
/*out*/Ray &ray_reflected,
/*out*/bool& reflection)const
{
reflection = false;
Vec N, L, V, R;
Vec color;
Vec tmpP;
Object* objTmp = NULL;
//vetor L
L = glm::normalize(source.pos - point);
//Observador
V = -ray.dir;
//calcula a normal
obj->normalAt(point, N);
//raio refletido R
R = (2.0f*N)*glm::dot(N,L) - L;
//Equacao de iluminacao
float fatt = 1.0;
double cosTetha, cosPhi;
cosTetha = glm::dot(N,L);
cosPhi = glm::dot(R,V);
#define FABS(x) (((x)<0.0)?-(x):(x))
cosPhi = FABS(cosPhi);
if(cosTetha <= 0.0)
{
// ray_reflected = Ray(point, N);
return world.getVeclightEnv()*obj->material.color*(obj->material.kd);
}
reflection = true;
ray_reflected = Ray(point, R);
//testes para considerar objetos entre o ponto e a fonte de luz
//efeito de sombra
Ray rayL(point, L);
double distTmp;
Vec pointCopy = point;
if(closestPoint(rayL, tmpP, &objTmp))
{
objTmp->intersectRay(rayL, tmpP, distTmp);
if( (distTmp < glm::distance(pointCopy, world.lights.begin()->pos )) && (objTmp != obj))
return world.getVeclightEnv()*obj->material.color*(obj->material.kd);
}
// #define SATURADOR(x) (((x) < 0.0)?world.lightEnv*world.ka*obj->material.color[ch]:(x))
for(int ch = 0; ch < 3; ch++)
{
color[ch] = world.lightEnv*world.ka*obj->material.color[ch] +
fatt*source.color[ch]*(obj->material.kd*obj->material.color[ch]* cosTetha +
obj->material.ks*pow(cosPhi,obj->material.n_shiny));
}
return color;
}