-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEscenaIncompleta.cpp
executable file
·434 lines (337 loc) · 9.39 KB
/
EscenaIncompleta.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
422
423
424
425
426
427
428
429
430
431
432
433
434
/** PRACTICA DE TRAZADO DE RAYOS
Prof: R.Vivó, J.Lluch para GPC.etsinf.upv.es 2011
Alumno: **/
#include <iostream>
#include <windows.h>
#include <GL/GL.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "EscenaIncompleta.h"
/** Añade aquí tu código **/
// Clase Objeto -------------------------
// Objeto gráfico con volumen
Objeto::Objeto(void){
colDifuso = Color::ROJO;
ka = 0.5f;
kd = 1.0f;
ks = 0.3f;
m = 1;
}
Objeto::~Objeto(void){
}
void Objeto::setColor(Color cd,Color ce,float Ka,float Kd,float Ks,int em){
colDifuso = cd;
colEspecular = ce;
ka = Ka;
kd = Kd;
ks = Ks;
m = em;
}
Vector Objeto::normal(Punto p) const{
return Vector();
}
int Objeto::rayIntersection(Punto p, Vector v, float &t) const{
t = 0;
return 0;
}
// Clase Esfera -------------------------
// Deriva de Objeto
Esfera::Esfera(){
centro = Punto();
radio = 1.0f;
}
Esfera::Esfera(Punto c, float r, Transformacion t){
centro = c.transform(t);
/* Sólo como aproximación, para la esfera, tengo en cuenta el escalado
del radio */
radio = r*t.elem(0,0);
}
Esfera::~Esfera(void){
return;
}
Vector Esfera::normal(Punto p) const{
return (p-centro).unitary();
}
int Esfera::rayIntersection(Punto p, Vector v, float &t) const{
//Calcula la intersección con el rayo(p,v)
//Devuelve (0:no hay interseccion) (1:interseccion externa) (-1:interseccion interna)
int ni;
float alfa,beta,gamma;
float raiz;
float t1,t2;
Vector pcentro = (p-centro);
// El primer punto, en el caso que haya 2, será el más cercano al inicio del rayo
alfa = v*v;
beta = 2*(pcentro*v);
gamma = (pcentro*pcentro) - radio*radio;
raiz = beta*beta-4*alfa*gamma;
if (raiz<0.0f) return 0; // Raíces imaginarias
raiz = (float)sqrt(raiz);
t2 = (-beta+raiz)/(2*alfa);
t1 = (-beta-raiz)/(2*alfa);
//Debido a que alfa>0, t1<t2
if(t1>0){ //Hay dos intersecciones despues del inicio del rayo
t = t1;
ni = 1;
}
else if(t2>0){ //Hay una interseccion despues del inicio del rayo, estamos dentro
t = t2;
ni = -1;
}
else{ //Las dos intersecciones ocurren antes del inicio del rayo
ni = 0;
};
return ni;
}
// Clase Caja -------------------------
// Deriva de Objeto
Caja::Caja(Transformacion t){
Punto p_puntos[24]= {Punto(-1,-1,-1), Punto(-1, 1,-1), Punto( 1, 1,-1), Punto( 1,-1,-1),
Punto(-1, 1,-1), Punto(-1, 1, 1), Punto( 1, 1, 1), Punto( 1, 1,-1),
Punto( 1, 1,-1), Punto( 1, 1, 1), Punto( 1,-1, 1), Punto( 1,-1,-1),
Punto(-1, 1, 1), Punto(-1,-1, 1), Punto( 1,-1, 1), Punto( 1, 1, 1),
Punto(-1, 1,-1), Punto(-1,-1,-1), Punto(-1,-1, 1), Punto(-1, 1, 1),
Punto(-1,-1,-1), Punto( 1,-1,-1), Punto( 1,-1, 1), Punto(-1,-1, 1)};
Punto p_caras[4];
int n = 0;
for(int i=0; i<6; i++){
for(int j=0; j<4; j++){
p_caras[j] = p_puntos[n].transform(t);
n++;
}
Cuadrilatero c(p_caras);
caras.push_back(c);
}
}
Caja::Caja(Punto caras[24]){
Punto p_caras[4];
int n = 0;
for(int i=0; i<6; i++){
for(int j=0; j<4; j++){
p_caras[j] = caras[n++];
}
}
}
//Caja::Caja(float ladomayor, float ladomenor, Transformacion t=Transformacion()){
Vector Caja::normal(Punto p) const{
int cara = -1;
float D, dist, mindist = INFINITO;
Punto pto;
Vector v1, v2, vn;
for(unsigned int i=0; i<caras.size(); ++i){
pto = caras[i].vertices[0];
v1 = caras[i].vertices[1] - caras[i].vertices[0];
v2 = caras[i].vertices[2] - caras[i].vertices[0];
vn = (v1^v2).unitary();
D = 0;
for(int k=0; k<3; ++k)
D -= vn.elem(k)*pto.elem(k);
dist = 0;
for(int k=0; k<3; ++k)
dist += vn.elem(k)*p.elem(k);
dist = abs(dist + D);
//dist = abs(dist + ((pto.x()*vn.x() + pto.y()*vn.y() + pto.z()*vn.z())));
if(dist < mindist){
cara = i;
mindist = dist;
}
}
pto = caras[cara].vertices[0];
v1 = caras[cara].vertices[1] - caras[cara].vertices[0];
v2 = caras[cara].vertices[2] - caras[cara].vertices[0];
vn = (v1^v2).unitary();
return vn;
}
int Caja::rayIntersection(Punto inicio, Vector direccion, float &t) const{
float te = 0;
float ts = INFINITO;
for(unsigned int i=0; i<caras.size(); ++i){
Vector v1 = caras[i].vertices[1] - caras[i].vertices[0];
Vector v2 = caras[i].vertices[2] - caras[i].vertices[0];
Vector normal = (v1^v2).unitary();
Punto a = caras[i].vertices[0];
float rN = normal * direccion;
if(rN == 0){
if(((inicio-a)*normal) > 0)
return 0;
}else if(rN > 0){
t = (normal*(a-inicio))/rN;
ts = min(ts,t);
}else{
t = (normal*(a-inicio))/rN;
te = max(te,t);
}
}
if(ts<te) return 0;
else{
if (te==0) return -1;
else{
t=te;
return 1;
}
}
}
Caja::~Caja(){
while(!caras.empty())
caras.pop_back();
}
// Clase FuenteLuminosa ---------------------
/********** DESCOMENTAR PARA LA PRACTICA 4.3 ********/
FuenteLuminosa::FuenteLuminosa(){
I = Color(1.0f,1.0f,1.0f);
posicion = Punto();
encendida = ON;
}
FuenteLuminosa::FuenteLuminosa(Color intensidad, Punto pos){
I = intensidad;
posicion = pos;
encendida = ON;
}
Vector FuenteLuminosa::L(Punto p) const{
return (posicion-p).unitary();
}
Color FuenteLuminosa::intensity(Punto p) const{
return I;
}
void FuenteLuminosa::setColor(Color c){
I = c;
}
Punto FuenteLuminosa::position() const{
return posicion;
}
void FuenteLuminosa::setPosition(Punto pos){
posicion = pos;
}
void FuenteLuminosa::switchLight(int ONOFF){
encendida = ONOFF;
}
int FuenteLuminosa::switchOn() const{
return encendida;
}
// Añade aquí tu código
// Iluminacion ambiental
Ambiental::Ambiental(){
I = Color(1.0f,1.0f,1.0f);
posicion = Punto();
encendida = ON;
}
Vector Ambiental::L(Punto p) const{
return Vector(0, 0, 0);
}
// Iluminacion puntual
Puntual::Puntual(){
}
Puntual::Puntual(Color c, Punto pos){
I = c;
posicion = pos;
encendida = ON;
}
// Iluminacion direccional
Direccional::Direccional(){
}
Direccional::Direccional(Color intens, Vector direc){
I = intens;
direccion = direc;
}
void Direccional::setDirection(Vector d){
direccion = d;
}
Vector Direccional::L(Punto p) const{
return (posicion - p).negated().unitary();
}
// Iluminación focalizada
Focalizada::Focalizada(){
}
Focalizada::Focalizada(Color inten, Punto posic, Vector direc, double concentracion, double angulo){
encendida = ON;
I = inten;
posicion = posic;
direccion = direc;
semiapertura = cos(DEG2RAD(angulo/2));
p = concentracion;
}
Color Focalizada::intensity(Punto pt) const{
Vector L = pt - posicion;
float cosgamma = (L*direccion) /
(L.module()*direccion.module());
//printf("%f < %f\n",cosgamma, semiapertura);
if(cosgamma < semiapertura)
return Color(0.0,0.0,0.0,1);
else
return I * pow(cosgamma, (float)p);
}
void Focalizada::setShape(Vector dir, double concentracion, double angulo){
direccion = dir;
p = concentracion;
semiapertura = cos(DEG2RAD(angulo/2));
}
/****** DESCOMENTAR PARA LA PRACTICA 4.3 *******/
// Clase Escena ------------------------
/** Añade aquí tu código **/
int Escena::add(Objeto *o){
/** Añade aquí tu código **/
objetos.push_back(o);
return objetos.size() - 1;
}
Color Escena::rayTrace(Punto inicio, Vector direccion, int numReflex) const{
float t, sht, rft;
float min = INFINITO;
int minpos;
for(unsigned int i=0; i<objetos.size(); ++i){
int ri = objetos[i]->rayIntersection(inicio, direccion, t);
if(ri != 0 && t<min){
min = t;
minpos = i;
}
}
if(min < INFINITO){
Punto corte = inicio + direccion*min;
Vector normal = objetos[minpos]->normal(corte);
Color total = objetos[minpos]->colDifuso *
objetos[minpos]->ka *
luces[0]->intensity(corte); // I = ka*Cd*Ia (Luz ambiental)
for(unsigned int i=1; i<luces.size(); ++i){ //Para cada otra luz
//Sombras
bool inShadow = false; // Comprobación de sombras
if(this->shadowsOn){
for(unsigned int o=0; o<objetos.size(); ++o) {
if(o!=minpos && objetos[o]->rayIntersection(corte, luces[i]->position()-corte, sht)) {
inShadow = true;
break;
}
}
}
if(!inShadow && luces[i]->switchOn()){ //si luz encendida
Vector L = luces[i]->L(corte); //L = rayo desde p a la luz
Vector V = Punto(0,0,3) - corte;
Vector H = (L+V); H = H*(1/H.module());
float NL = 0, NH = 0;
for(int k=0; k<3; ++k){
NL += normal.elem(k)*L.elem(k);
NH += normal.elem(k)*H.elem(k);
}
total = total + (luces[i]->intensity(corte) *
objetos[minpos]->colDifuso *
objetos[minpos]->kd *
NL); // Ii*kd*Cd*(N*L)
total = total + (luces[i]->intensity(corte) *
objetos[minpos]->ks *
pow(NH, objetos[minpos]->m)); // Ii*ks*(N*H)^m
}
// Reflexion
if(this->reflexOn){
if(numReflex < this->maxReflex){
total = total + this->rayTrace(corte+normal*0.01, normal, numReflex + 1);
}
}
}
return total;
}
else //No existe interseccion
return Color(0, 0, 0, 1); //Devolver color de fondo
}
int Escena::addLight(FuenteLuminosa *fl){
luces.push_back(fl);
return luces.size() - 1;
}