-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglwidget.cpp
More file actions
344 lines (290 loc) · 8.12 KB
/
Copy pathglwidget.cpp
File metadata and controls
344 lines (290 loc) · 8.12 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
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
#include "glwidget.h"
#include <QFileDialog>
#include <stdlib.h>
#define _USE_MATH_DEFINES 1
#include <cmath>
// Constructora amb format per defecte
GLWidget::GLWidget(QWidget * parent)
: QGLWidget (parent)
{
// Cal activar el timer per generar animacions a l'escena
connect( &timer, SIGNAL(timeout()), this, SLOT(updateGL()) );
timer.start(0);
xClick = yClick = 45;
lastZoom = 60; //default angle
DoingInteractive = NONE;
llumglobal=true;
llumdofi=false;
carregat=false;
dibuixa=false;
puja=false;
use_cam_first = 0;
id_dofi=-1;
}
// initializeGL() - Aqui incloem les inicialitzacions del context grafic.
void GLWidget::initializeGL()
{
glClearColor(0.4f, 0.4f, 0.8f, 1.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glEnable(GL_DEPTH_TEST);
// inicialitzem també l'escena
scene.Init();
scene.computeTransformedBoxes();
// dimensions escena i camera inicial
scene.CalculaEsfera(radi, centreEscena);
cam.computeDefaultCamera(radi, centreEscena);
}
// paintGL() - callback cridat cada cop que cal refrescar la finestra.
void GLWidget::paintGL( void ) {
// Esborrem els buffers
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//cerr << "painting with cam " << use_cam_first << endl;
if(use_cam_first) {
camFirst.setProjection(width(), height());
camFirst.setModelview();
} else {
cam.setProjection(width(), height());
cam.setModelview();
}
// dibuixar eixos aplicacio
glBegin(GL_LINES);
glColor3f(1,0,0); glVertex3f(0,0,0); glVertex3f(20,0,0); // X
glColor3f(0,1,0); glVertex3f(0,0,0); glVertex3f(0,20,0); // Y
glColor3f(0,0,1); glVertex3f(0,0,0); glVertex3f(0,0,20); // Z
glEnd();
// pintem l'escena: terra, objectes i el que calgui
scene.Render(id_dofi,dibuixa, llumglobal, llumdofi);
}
// resizeGL() - Cridat quan es canvia el tamany del viewport.
void GLWidget::resizeGL (int width, int height) {
glViewport (0, 0, width, height);
}
// help() - Surt per la terminal des de la que hem
// engegat el programa. En la versio amn interficie
// hauria de crear una nova finestra amb la informacio...
void GLWidget::help( void ){
cout<<"Tecles definides: \n";
cout<<"f Pinta en filferros\n";
cout<<"s Pinta amb omplert de polígons\n";
cout<<"h,H,? Imprimir aquesta ajuda\n";
// Completar amb altres tecles que definiu...
//
}
/*--------------------
*
* teclat()
*
* callback per quan hom prem una tecla.
*
*/
void GLWidget::keyPressEvent(QKeyEvent *e)
{
ObjectInstance* dofi;
dofi = scene.getObjectInstance(id_dofi);
switch( e->key() )
{
case Qt::Key_F: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
updateGL();
break;
case Qt::Key_S: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
updateGL();
break;
case Qt::Key_Z:
if(carregat) {
cerr << "camera changed: Using 1st person view cam" << endl;
use_cam_first = 1;
camFirst.resetOrientation();
camFirst.setModelview();
scene.getObjectInstance(id_dofi)->setVisible(false);
updateGL();
}
break;
case Qt::Key_X:
if(carregat) {
cerr << "camera changed: Using default cam" << endl;
use_cam_first = 0;
cam.setModelview();
scene.getObjectInstance(id_dofi)->setVisible(true);
updateGL();
}
break;
case Qt::Key_Left:
if(carregat) {
cerr << "Left!" << endl;
dofi->Rotate(scene, 3);
if(use_cam_first) camFirst.resetOrientation();
}
break;
case Qt::Key_Right:
if(carregat) {
cerr << "Right!" << endl;
dofi->Rotate(scene, -3);
if(use_cam_first) camFirst.resetOrientation();
}
break;
case Qt::Key_Up:
if(carregat && !puja) {
cerr << "Up!" << endl;
dofi->MoveFront(scene);
}
else if(carregat && puja){
cout << "Up+Puja!" << endl;
dofi->Puja(scene);
if(use_cam_first) camFirst.resetCam();
}
break;
case Qt::Key_Down:
if(carregat && !puja) {
cerr << "Down!" << endl;
dofi->MoveBack(scene);
}
else if(carregat && puja){
cout << "Up+Baixa!" << endl;
dofi->Baixa(scene);
if(use_cam_first) camFirst.resetCam();
}
break;
case Qt::Key_Control:
puja=true;
break;
case 'h' : case 'H' : case '?' : help();
break;
default: e->ignore(); // el propaguem cap al pare...
}
//if(use_cam_first) {
// camFirst.resetOrientation();
//}
}
void GLWidget::keyReleaseEvent(QKeyEvent *e){
switch( e->key() )
{
case Qt::Key_Control:
puja=false;
break;
}
}
/*--------------------
* mousePressEvent()
*/
void GLWidget::mousePressEvent( QMouseEvent *e){
xClick = e->x();
yClick = e->y();
if (e->button()&Qt::LeftButton &&
! (e->modifiers()&(Qt::ShiftModifier|Qt::AltModifier|Qt::ControlModifier)))
{
DoingInteractive = ROTATE;
}
else if (e->button()&Qt::LeftButton && e->modifiers() &Qt::ShiftModifier)
{
DoingInteractive = ZOOM;
}
}
/*--------------------
*
* mouseReleaseEvent()
*
* Callback Qt cridat quan es deixa anar el botó del
* ratolí.
*
*/
void GLWidget::mouseReleaseEvent( QMouseEvent *) {
DoingInteractive = NONE;
}
/*--------------------
*
* mouseMoveEvent()
*
* Callback Qt cridat quan s'arrosega el ratoli amb
* algun boto premut.
*
*/
void GLWidget::mouseMoveEvent(QMouseEvent *e) {
// Aquí cal que es calculi i s'apliqui la rotació o el zoom
// com s'escaigui...
if (DoingInteractive==ROTATE) {
// Rotar la camera
if(!use_cam_first) {
cam.userRotate((xClick - e->x()), (yClick - e->y()));
} else if(carregat) {
camFirst.userRotate((xClick - e->x()), (yClick - e->y()));
}
xClick = e->x();
yClick = e->y();
}
if (DoingInteractive==ZOOM) {
// Fer zoom
cerr << "zoom! " << e->y() << endl;
if(!use_cam_first) {
cam.userZoom(lastZoom - e->y());
} else if(carregat) {
camFirst.userZoom(lastZoom - e->y());
}
lastZoom = e->y();
}
updateGL();
}
void GLWidget::resetCam() {
if(!use_cam_first) {
cam.resetCam();
} else if(carregat) {
camFirst.resetCam();
}
updateGL();
}
void GLWidget::switchCam() {
if(carregat){
if(!use_cam_first) {
cerr << "camera changed: Using 1st person view cam" << endl;
use_cam_first = 1;
camFirst.resetOrientation();
camFirst.setModelview();
scene.getObjectInstance(id_dofi)->setVisible(false);
} else {
use_cam_first = 0;
cam.setModelview();
scene.getObjectInstance(id_dofi)->setVisible(true);
}
updateGL();
}
}
void GLWidget::loadModel() {
QString path = QFileDialog::getOpenFileName(this, "Choose a model to load", ".", "Obj (*.obj)");
cerr << "path = " << path.toAscii().data() << endl;
if(path==QString::null or path.toAscii().data()=="") {
cerr << "invalid path!" << endl;
return;
}
if(path.toAscii().contains("dolphin")){
Model mod("dofi");
mod.readObj(path.toAscii().data(), Scene::matlib);
mod.updateBoundingBox();
int id = scene.AddModel(mod);
cout << "Added model with id " << id << endl;
/*
Point p(centreEscena.x, 0, centreEscena.z);
p.y+=2.5; //para que no atraviese suelo
*/
Point p(centreEscena.x, centreEscena.y, centreEscena.z);
id_dofi = scene.AddInstance(id, 20, p, -90);
cameraFirst c(scene.getObjectInstance(id_dofi));
camFirst=c;
camFirst.computeDefaultCamera(radi, centreEscena);
carregat=true;
scene.computeTransformedBoxes();
}
else{
Model mod("altre");
mod.readObj(path.toAscii().data(), Scene::matlib);
mod.updateBoundingBox();
int id = scene.AddModel(mod);
cerr << "Added model with id " << id << endl;
Point p(centreEscena.x, centreEscena.y, centreEscena.z);
cerr<<"punt inici " << p << endl;
id_dofi = scene.AddInstance(id, 20, p, 0);
cameraFirst c(scene.getObjectInstance(id_dofi));
camFirst=c;
camFirst.computeDefaultCamera(radi, centreEscena);
carregat=true;
cerr << "punt dofi:" << p << endl;
}
}