Repository navigation
Expand file tree
/
Copy pathigvInterfaz.cpp
More file actions
278 lines (242 loc) · 9.1 KB
/
Copy pathigvInterfaz.cpp
File metadata and controls
278 lines (242 loc) · 9.1 KB
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
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <Windows.h>
#include "igvInterfaz.h"
#define PI 3.14159265358979323846
#include "orientacion.h"
extern igvInterfaz interfaz; // los callbacks deben ser estaticos y se requiere este objeto para acceder desde
// ellos a las variables de la clase
// Metodos constructores -----------------------------------
igvInterfaz::igvInterfaz () {}
igvInterfaz::~igvInterfaz () {}
// Metodos publicos ----------------------------------------
void igvInterfaz::crear_mundo(void) {
// crear cámaras
interfaz.camara.set(IGV_PERSPECTIVA, igvPunto3D(25,7,25),igvPunto3D(25,7,24),igvPunto3D(0,1,0),
60.0, 1.0 , 0.2, -1*3);
interfaz.minimapa.set(IGV_PERSPECTIVA, igvPunto3D(25, 25, 25), igvPunto3D(25, 7, 25), igvPunto3D(-1, 0, 0),
60.0, 1.0, 0.2, -1*3);
interfaz.minimapa.setCamaraASeguir(&interfaz.camara);
interfaz.actual = &interfaz.camara;
}
void igvInterfaz::configura_entorno(int argc, char** argv,
int _ancho_ventana, int _alto_ventana,
int _pos_X, int _pos_Y, string _titulo){
// inicialización de las variables de la interfaz
ancho_ventana = _ancho_ventana;
alto_ventana = _alto_ventana;
//PlaySound( (LPCWSTR)".\\sound\\caramel.wav", NULL, SND_ASYNC | SND_FILENAME | SND_LOOP);
// inicialización de la ventana de visualización
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(_ancho_ventana,_alto_ventana);
glutInitWindowPosition(_pos_X,_pos_Y);
glutCreateWindow(_titulo.c_str());
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
glEnable(GL_DEPTH_TEST); // activa el ocultamiento de superficies por z-buffer
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glClearColor(1.0,1.0,1.0,0.0); // establece el color de fondo de la ventana
glEnable(GL_LIGHTING); // activa la iluminacion de la escena
glEnable(GL_NORMALIZE); // normaliza los vectores normales para calculo iluminacion
glEnable(GL_TEXTURE_2D);
crear_mundo(); // crea el mundo a visualizar en la ventana
}
void igvInterfaz::inicia_bucle_visualizacion() {
glutMainLoop(); // inicia el bucle de visualizacion de OpenGL
}
void igvInterfaz::set_glutKeyboardFunc(unsigned char key, int x, int y) {
switch (key) {
case 'w':
interfaz.camara.moverAdelante(interfaz.dt);
interfaz.minimapa.seguir(interfaz.dt);
interfaz.camara.aplicar();
interfaz.minimapa.aplicar();
break;
case 'a':
interfaz.camara.moverIzquierda(interfaz.dt);
interfaz.minimapa.seguir(interfaz.dt);
interfaz.camara.aplicar();
interfaz.minimapa.aplicar();
break;
case 's':
interfaz.camara.moverAtras(interfaz.dt);
interfaz.minimapa.seguir(interfaz.dt);
interfaz.camara.aplicar();
interfaz.minimapa.aplicar();
break;
case 'd':
interfaz.camara.moverDerecha(interfaz.dt);
interfaz.minimapa.seguir(interfaz.dt);
interfaz.camara.aplicar();
interfaz.minimapa.aplicar();
break;
case 'p':
if (interfaz.actual == &interfaz.camara) interfaz.actual = &interfaz.minimapa;
else interfaz.actual = &interfaz.camara;
break;
case 'l':
interfaz.escena.cambiarRGB();
break;
case 'n':
interfaz.escena.tipo = NORMAL;
break;
case 'c':
interfaz.escena.tipo = SELCHUNK;
break;
case 'k':
interfaz.escena.tipo = SELBLOQUE;
break;
case 'f':
interfaz.escena.tipo = SELCARA;
break;
case '1':
interfaz.seleccionPl = TIERRA;
break;
case '2':
interfaz.seleccionPl = CESPED;
break;
case '3':
interfaz.seleccionPl = PIEDRA;
break;
case '4':
interfaz.seleccionPl = MADERA;
break;
case 27: // tecla de escape para SALIR
exit(1);
break;
}
glutPostRedisplay(); // renueva el contenido de la ventana de vision
}
void igvInterfaz::set_glutReshapeFunc(int w, int h) {
// dimensiona el viewport al nuevo ancho y alto de la ventana
// guardamos valores nuevos de la ventana de visualizacion
interfaz.set_ancho_ventana(w);
interfaz.set_alto_ventana(h);
// establece los parámetros de la cámara y de la proyección
interfaz.camara.aplicar();
}
void igvInterfaz::set_glutDisplayFunc() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // borra la ventana y el z-buffer
// se establece el viewport
// Apartado A: antes de aplicar las transformaciones de cámara y proyección hay que comprobar el modo para sólo visualizar o seleccionar:
if (interfaz.modo == SELECCIONAR) {
// Apartado A: Para que funcione habrá que dibujar la escena sin efectos, sin iluminación, sin texturas ...
glDisable(GL_LIGHTING); // desactiva la iluminacion de la escena
glDisable(GL_DITHER);
glDisable(GL_TEXTURE_2D);
glDisable(GL_CULL_FACE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, interfaz.get_ancho_ventana(), interfaz.get_alto_ventana());
// Apartado A: Reestablece los colores como no seleccionado
// Apartado A: aplica la cámara
interfaz.actual->aplicar();
// Apartado A: visualiza los BV cada uno de un color
interfaz.escena.visualizarMundoSeleccion();
// Apartado A: Obtener el color del pixel seleccionado
GLubyte pixel[3];
glReadPixels(interfaz.cursorX, interfaz.cursorY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
std::cout << "\nColor seleccionado Chunk: " << (float)pixel[0] << "," << (float)pixel[1] << "," << (float)pixel[2] << "\n";
igvColor pxColor((float)pixel[0], (float)pixel[1], (float)pixel[2]);
Chunk** frontera = interfaz.escena.getFronteraByColor(pxColor);
interfaz.escena.selChunk = frontera;
/*if (frontera != nullptr) {
for (int i = 0; i < 9; i++)
{
if(frontera[i] != nullptr) frontera[i]->marcar();
}
}*/
if (frontera != nullptr) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
interfaz.actual->aplicar();
frontera[CENTRO]->drawChunkSeleccionCubo();
glReadPixels(interfaz.cursorX, interfaz.cursorY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
//std::cout << "\nColor seleccionado Cubo: " << (float)pixel[0] << "," << (float)pixel[1] << "," << (float)pixel[2] << "\n";
igvColor pxColor((float)pixel[0], (float)pixel[1], (float)pixel[2]);
Cubo* cuboSeleccinado = frontera[CENTRO]->getCubo(pxColor);
interfaz.escena.selCubo = cuboSeleccinado;
if (cuboSeleccinado != nullptr) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
interfaz.actual->aplicar();
cuboSeleccinado->visualizaCarasCuboSeleccion();
glReadPixels(interfaz.cursorX, interfaz.cursorY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
//std::cout << "\nColor seleccionado Cara cubo: " << (float)pixel[0] << "," << (float)pixel[1] << "," << (float)pixel[2] << "\n";
igvColor pxColor((float)pixel[0], (float)pixel[1], (float)pixel[2]);
if (interfaz.accionActual == PONER) frontera[CENTRO]->colocarCubo(pxColor, frontera, cuboSeleccinado, interfaz.seleccionPl);
else if (interfaz.accionActual == QUITAR) frontera[CENTRO]->quitarCubo(cuboSeleccinado);
}
}
interfaz.modo = IGV_VISUALIZAR;
interfaz.accionActual = NADA;
glEnable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE);
}
else {
glViewport(0, 0, interfaz.get_ancho_ventana(), interfaz.get_alto_ventana());
interfaz.camara.aplicar();
interfaz.escena.visualizar();
glViewport(interfaz.get_ancho_ventana() - interfaz.get_ancho_ventana()/4, interfaz.get_alto_ventana() - interfaz.get_alto_ventana()/4, interfaz.get_ancho_ventana()/4, interfaz.get_alto_ventana()/4);
interfaz.minimapa.aplicar();
interfaz.escena.visualizar();
glutSwapBuffers();
}
}
void igvInterfaz::set_glutMouseFunc(GLint boton, GLint estado, GLint x, GLint y) {
switch (boton) {
case GLUT_LEFT_BUTTON:
if (estado == GLUT_DOWN) {
interfaz.cursorX = x;
interfaz.cursorY = interfaz.alto_ventana - y;
interfaz.modo = SELECCIONAR;
interfaz.accionActual = PONER;
}
break;
case GLUT_RIGHT_BUTTON:
if (estado == GLUT_DOWN) {
interfaz.cursorX = x;
interfaz.cursorY = interfaz.alto_ventana - y;
interfaz.modo = SELECCIONAR;
interfaz.accionActual = QUITAR;
}
break;
default:
interfaz.accionActual = NADA;
break;
}
glutPostRedisplay();
}
void igvInterfaz::set_glutPassiveMotionFunc(GLint x, GLint y) {
if (interfaz.skipEvento) {
interfaz.skipEvento = false;
return;
}
if (interfaz.actual == &interfaz.camara) {
glutWarpPointer(interfaz.ancho_ventana / 2, interfaz.alto_ventana / 2);
double nuevox = (x - (interfaz.ancho_ventana / 2)) * interfaz.dt;
double nuevoy = ((interfaz.alto_ventana / 2) - y) * interfaz.dt;
interfaz.camara.mirar(nuevox, nuevoy);
interfaz.camara.aplicar();
interfaz.skipEvento = true;
}
glutPostRedisplay();
}
void igvInterfaz::set_timer(int)
{
if(interfaz.actual == &interfaz.camara) glutWarpPointer(interfaz.ancho_ventana / 2, interfaz.alto_ventana / 2);
int t = glutGet(GLUT_ELAPSED_TIME);
interfaz.dt = (t - interfaz.tUltimoFotograma) / 1000.0;
interfaz.tUltimoFotograma = t;
glutPostRedisplay();
glutTimerFunc(1000 / 60, set_timer, 0);
}
void igvInterfaz::inicializa_callbacks() {
glutKeyboardFunc(set_glutKeyboardFunc);
glutReshapeFunc(set_glutReshapeFunc);
glutDisplayFunc(set_glutDisplayFunc);
glutPassiveMotionFunc(set_glutPassiveMotionFunc);
glutMouseFunc(set_glutMouseFunc);
glutTimerFunc(1000/60,set_timer,0);
}