-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrazador.cpp
executable file
·253 lines (201 loc) · 6.2 KB
/
Trazador.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
/** PRACTICA DE TRAZADO DE RAYOS
Prof: R.Vivó, J.Lluch para GPC.etsinf.upv.es 2011
Código de generación de Rayos Primarios y relleno
del framebuffer con el resultado del trazador.
Alumno: Mario Sanz **/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/Glut.h>
#include "EscenaIncompleta.h"
void myinit(void);
void myReshape(GLsizei w, GLsizei h);
void display(void);
// Variables globales
// Vector que almacenará en los valores del raster, usando RGB
unsigned char *raster = NULL;
// Tamaño del raster en píxeles
int ancho=500, alto=400;
// Flag que indica si hay que redibujar la imagen
int sucio;
// Definición de la escena
Escena *escena = NULL;
// Parámetros de la cámara
double AspectRatio, fov = 65.0;
// Posición de la cámara sobre el eje Z
float pos_z = 3.0f;
// Control de la iluminacion con el teclado
bool iluminacion = false, sombras = false, reflexiones = false, antialiasing = false;
// Funciones auxiliares
double calc_b(float pos_z, double fov){
return 2*pos_z*tan(DEG2RAD(fov)/2);
}
double calc_x1(double a, int ancho, int i){
return ((a*i)/ancho) - a/2;
}
double calc_y1(double b, int alto, int j){
return ((j*b)/alto) - b/2;
}
void crear_escena(void) {
Esfera *amarilla, *blanca, *roja, *verde, *azul;
float radio = 0.59673710213f;
// Creamos la escena
if (escena==NULL)
escena = new Escena();
//Esfera
Esfera *esfera = new Esfera(Punto(0,0.1,-2), 0.8);
esfera->setColor(Color(1,0,0), Color(1,1,1), 0.3, 0.3, 0.7, 40);
escena->add(esfera);
//Caja suelo
Transformacion t1;
t1.translation(Real4(0.0, -1.0, -1.5));
t1.scale(2, 0.1, 2);
Caja *cajasuelo = new Caja(t1);
cajasuelo->setColor(Color(1,1,1), Color::BLANCO, 0.1, 0.3, 0.9, 100);
escena->add(cajasuelo);
//Caja cubo
Transformacion t2;
t2.translation(Real4(-1.0, -0.3, -0.3));
t2.rotation(60, Vector(1,1,1));
t2.scale(0.5, 0.5, 0.5);
Caja *cajacubo = new Caja(t2);
cajacubo->setColor(Color(0,1,0), Color::BLANCO, 0.1, 0.8, 0.1, 1);
escena->add(cajacubo);
// Esfera blanca (detrás)
/*blanca = new Esfera(Punto(0.0f, 0.0f, -2.0f), radio);
blanca->setColor(Color::BLANCO);
escena->add(blanca);*/
// Esfera amarilla (NO)
/*amarilla = new Esfera(Punto(-radio, radio,0.0f),radio);
amarilla->setColor(Color::VERDE+Color::ROJO);
escena->add(amarilla);*/
// Esfera roja (SO)
/*roja = new Esfera(Punto(-radio,-radio,0.0f), radio);
roja->setColor(Color::ROJO);
escena->add(roja);
// Esfera azul (SE)
/*azul = new Esfera(Punto(radio, -radio,0.0f), radio);
azul->setColor(Color::AZUL);
escena->add(azul);/*
// Esfera verde (NE)
/*verde = new Esfera(Punto(radio, radio, 0.0f), radio);
verde->setColor(Color::VERDE);
escena->add(verde);*/
/*Transformacion t1;
//t1.translation(Real4(2,2,2,0));
t1.rotation(30, Vector(0,1,0));
t1.rotation(-15, Vector(1,0,0));
t1.scale(0.5, 0.4, 0.3);
Caja *cajaverde = new Caja(t1);
cajaverde->setColor(Color::VERDE);
escena->add(cajaverde);*/
//escena->add(amarilla);
/*for(int i=0;i<6;i++){
for(int j=0;j<4;j++){
printf("%.2f %.2f ", cajaverde->caras[i].vertices[j].x(), cajaverde->caras[i].vertices[j].y());
}
printf("\n");
}*/
}
void crear_iluminacion(void){
FuenteLuminosa *ambiental, *puntual, *direccional, *focalizada;
ambiental = new Ambiental();
escena->addLight(ambiental);
puntual = new Puntual(Color(1,1,1), Punto(1,0.5,0));
escena->addLight(puntual);
focalizada = new Focalizada(Color(1,1,0), Punto(0,2,-1), Vector(0,-1,0), 8, 60);
escena->addLight(focalizada);
//direccional = new Direccional(Color(1,0,1), Vector(0,-1,0));
//escena->addLight(direccional);
}
void traza(void) {
int i, j;
Color color;
double x1, y1, a, b;
unsigned char *t;
//Si es la primera vez, o hay que redibujar la escena...
if(raster==NULL){
raster = (unsigned char *)malloc(sizeof(unsigned char)*3*ancho*alto);
if (raster==NULL){
fprintf(stderr, "Sin memoria\n");
exit(-1);
}
}
b = calc_b(pos_z, fov);
a = AspectRatio * b;
t = raster;
for(j=0; j<alto; j++){
y1 = calc_y1(b, alto, j);
for(i=0; i<ancho; i++){
x1 = calc_x1(a, ancho, i);
color = escena->rayTrace(Punto(0, 0, pos_z), Vector(x1, y1, -pos_z), 0);
*t++ = (unsigned char)(color.r()*255);
*t++ = (unsigned char)(color.g()*255);
*t++ = (unsigned char)(color.b()*255);
}
}
sucio = 0;
}
void myinit(void){
glClearColor (0.0, 0.0, 0.0, 0.0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
crear_escena();
crear_iluminacion();
}
void display(void){
if (raster==NULL || sucio)
traza();
glRasterPos2i(0,0);
glDrawPixels(ancho, alto, GL_RGB, GL_UNSIGNED_BYTE, raster);
glFlush();
}
void myReshape(GLsizei w, GLsizei h){
h = (h == 0) ? 1 : h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLfloat)w, 0.0, (GLfloat)h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
ancho = MAX(w, 1);
alto = MAX(h, 1);
free(raster);
raster = NULL;
AspectRatio = (GLdouble)ancho/alto;
}
void myKeyboard(unsigned char tecla, int x, int y){
switch(tecla) {
/* INTRODUCE AQUÍ TU CÓDIGO */
/* Mayúsculas activan, minúsculas desactivan */
// L,l: iluminación
// S,s: sombras
// R,r: reflexiones
// A,a: antialiasing
case 'L': escena->getLight(1)->switchLight(ON);
escena->getLight(2)->switchLight(ON); break;
case 'l': escena->getLight(1)->switchLight(OFF);
escena->getLight(2)->switchLight(OFF); break;
case 'S': escena->switchShadows(ON); break;
case 's': escena->switchShadows(OFF); break;
case 'R': escena->switchReflex(ON); break;
case 'r': escena->switchReflex(OFF); break;
case 27: exit(0);
default:;
};
sucio = 1;
display();
return;
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(500, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("Trazador de rayos");
myinit();
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutKeyboardFunc(myKeyboard);
glutMainLoop();
return(0);
}