-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenglwidget.cpp
362 lines (290 loc) · 10.6 KB
/
openglwidget.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
#include "openglwidget.h"
OpenGLWidget::OpenGLWidget(QWidget * parent):QOpenGLWidget(parent)
{
}
void OpenGLWidget::initializeGL ()
{
initializeOpenGLFunctions () ;
qDebug (" OpenGL version : %s", glGetString ( GL_VERSION ) ) ;
qDebug (" GLSL %s", glGetString ( GL_SHADING_LANGUAGE_VERSION ) ) ;
//testa a profundidade para saber que para apresentar o objeto final
//ele tem que apresentar quem está mais próxima da câmera, não o último que foi desenhado
glEnable ( GL_DEPTH_TEST ) ;
//cria o vetor de models
models = std::make_unique <Model[]>(maxModels);
}
//cria um novo model
void OpenGLWidget::criaModel(QString arq)
{
//inicializa o model
models[nModels].inicializa(this);
if (nModels == 0)
//indica que é um personagem do bem
models[nModels].criaPersonagem();
qDebug("\nModel %d:", nModels);
//passa o ponteiro para a classe
models[nModels].readOFFFile ( arq ) ;
//seta a trackball
models[nModels].trackBall . resizeViewport ( width () , height () ) ;
//emite um sinal com o número de vértices e de faces
emit statusBarMessage ( QString ("Vertices:\%1, Faces:\%2").arg ( models[nModels].numVertices ).arg ( models[nModels].numFaces ) ) ;
//indica que o model foi criado
nModels++;
}
void OpenGLWidget::resizeGL (int w , int h )
{
glViewport (0 ,0 ,w , h ) ;
}
void OpenGLWidget::paintGL ()
{
//limpa o buffer de cor e de profundidade
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
QPainter painter(this);
//verifica se estamos morrendo
if(morrendo)
{
//incrementa o tempo que estamos morrendo
morrendo++;
//pinta a tela de vermelho
painter.fillRect(rect(), Qt::red);
//só fica morrendo por uns frames, depois reseta
if(morrendo >= 10)
morrendo = 0;
}
else
{
//verifica se o jogo começou
if(models[0].inicializado(this))
{
int i;
//desenha todos os models
for(i=0;i<maxModels;i++)
{
//se tem modelo
if (models[i].inicializado(this))
{
//verifica se precisa desenhar o modelo
if(!models[i].colidiu)
{
//qDebug("%d", models[i].shaderProgram[models[i].shaderIndex]);
//seta luz do model
int shaderProgramID = models[i].shaderProgram[models[i].shaderIndex];
QVector4D ambientProduct = light.ambient * models[i].material.ambient ;
QVector4D diffuseProduct = light.diffuse * models[i].material.diffuse ;
QVector4D specularProduct = light.specular * models[i].material.specular ;
//seta propriedades da luz
GLuint locProjection = glGetUniformLocation ( shaderProgramID , "projection") ;
GLuint locView = glGetUniformLocation ( shaderProgramID , "view") ;
GLuint locLightPosition = glGetUniformLocation ( shaderProgramID , "lightPosition") ;
GLuint locAmbientProduct = glGetUniformLocation ( shaderProgramID ,"ambientProduct") ;
GLuint locDiffuseProduct = glGetUniformLocation ( shaderProgramID ,"diffuseProduct") ;
GLuint locSpecularProduct = glGetUniformLocation ( shaderProgramID ,"specularProduct") ;
GLuint locShininess = glGetUniformLocation ( shaderProgramID , "shininess") ;
glUseProgram ( shaderProgramID ) ;
glUniformMatrix4fv ( locProjection , 1 , GL_FALSE , camera.projectionMatrix.data () ) ;
//seta a camera
glUniformMatrix4fv ( locView , 1 , GL_FALSE , camera.viewMatrix.data () ) ;
glUniform4fv ( locLightPosition , 1 , &( light.position[0]) ) ;
glUniform4fv ( locAmbientProduct , 1 , &( ambientProduct [0]) ) ;
glUniform4fv ( locDiffuseProduct , 1 , &( diffuseProduct [0]) ) ;
glUniform4fv ( locSpecularProduct , 1 , &( specularProduct [0]) ) ;
glUniform1f ( locShininess , models[i].material.shininess ) ;
}
//desenha o modelo
models[i].drawModel() ;
}
}
}
else
{
painter.setPen(Qt::green);
painter.setFont(QFont("Arial", 50));
painter.drawText(rect(), Qt::AlignCenter, "Clique 2x para começar :)");
}
}
}
//inicia o jogo
void OpenGLWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
//verifica se o jogo já começou
if(!models[0].inicializado(this))
{
//cria o model do personagem principal
criaModel(QString(QCoreApplication::applicationDirPath()+"/formas/cube.off"));
for(int i=1;i< maxModels; i++)
{
//cria os inimigos
criaModel(QString(QCoreApplication::applicationDirPath()+"/formas/sphere.off"));
//coloca em uma posição aleatória
models[i].criaInimigo();
}
//instancia o timer
timer = new QTimer(this);
update();
//começa o timer
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(updateModels()));
timer->start(10);
tempo.start();
qApp->installEventFilter(this);
}
}
void OpenGLWidget::updateModels()
{
QTime tempo = getTime();
if(models[maxModels - 1].inicializado(this))
{
//move todos os inimigos no eixo x
for(int i =1;i<maxModels;i++)
{
models[i].mX += 0.2;
}
update();
//adiciona um ponto se tiver colidido
if (verificaColisao())
{
//soma um ponto
pontos++;
//indica que estamos morrendo
morrendo = 1;
//emitie o ponto
emit somaPonto(QString("Mortes: %1").arg(QString::number(pontos)));
}
}
emit cronometro(QString("%1:%2:%3").arg(tempo.hour(), 2, 10, QChar('0')).arg(tempo.minute(), 2, 10, QChar('0')).arg(tempo.second(), 2, 10, QChar('0')));
}
//verifica se o personagem principal colidiu com algum inimigo
bool OpenGLWidget::verificaColisao()
{
for(int i = 1; i<maxModels; i++)
{
//só roda para inimigos que ainda não colidiram para não
//colidir infinitas vezes sem parar
if(!models[i].colidiu)
{
//modelMatrix(0,3) = mX;
//modelMatrix(1,3) = mY;
//verifica o começo do range de x
double axComeco = models[i].modelMatrix(0,3) - (models[i].invDiag/2);
double bxComeco = models[0].modelMatrix(0,3) - (models[0].invDiag/2);
//verifica o fim do range de x
double axFim = models[i].modelMatrix(0,3) + (models[i].invDiag/2);
double bxFim = models[0].modelMatrix(0,3) + (models[0].invDiag/2);
//verifica o começo do range de y
double ayComeco = models[i].modelMatrix(1,3) - (models[i].invDiag/2);
double byComeco = models[0].modelMatrix(1,3) - (models[0].invDiag/2);
//verifica o fim do range de y
double ayFim = models[i].modelMatrix(1,3) + (models[i].invDiag/2);
double byFim = models[0].modelMatrix(1,3) + (models[0].invDiag/2);
//verifica se estão colidindo
if((axComeco <= bxFim) && (axFim >= bxComeco) && (ayComeco <= byFim) && (ayFim >= byComeco))
{
//seta o inimigo como colidido
models[i].colidiu = true;
return true;
}
}
}
return false;
}
void OpenGLWidget::showFileOpenDialog()
{
QByteArray fileFormat = "off";
QString fileName = QFileDialog::getOpenFileName (this ,
" Open File ", QDir::homePath () ,
QString (" \%1 Files (*.\%2) ").arg ( QString ( fileFormat.toUpper () ) ).arg ( QString ( fileFormat ) ) , 0
# ifdef Q_OS_LINUX
, QFileDialog::DontUseNativeDialog
# endif
) ;
qDebug() << fileName;
if (!fileName.isEmpty() )
{
//cria um modelo que vale para todos os métodos da classe
//model = std::make_shared <Model>( this ) ;
//passa o ponteiro para a classe
//model[]->readOFFFile ( fileName ) ;
//cria um model com o arquivo escolhido
criaModel(fileName);
//emite um sinal com o número de vértices e de faces
emit statusBarMessage ( QString ("Vertices:\%1, Faces:\%2").arg ( models[nModels].numVertices ).arg ( models[nModels].numFaces ) ) ;
}
//chama o paintgl
update () ;
}
//gira o objeto x graus no eixo d
void OpenGLWidget::gira(int x, int d)
{
//se não tem modelo, retorna
if (!models )
return ;
if(d == 0)
{
models[selModel].giraX(x);
}
else
{
models[selModel].giraY(x);
}
//chama o paintgl
update () ;
}
//gira o objeto x graus no eixo d
void OpenGLWidget::giraX(int x)
{
this->gira(x, 0);
}
//gira o objeto x graus no eixo d
void OpenGLWidget::giraY(int y)
{
this->gira(y, 1);
}
void OpenGLWidget :: keyPressEvent ( QKeyEvent * event )
{
switch ( event-> key () )
{
case Qt::Key_Escape :
qApp-> quit () ;
break;
case Qt::Key_Up :
this->gira(1, 0);
break;
case Qt::Key_Down:
this->gira(-1,0);
break;
case Qt::Key_Left:
this->gira(1, -1);
break;
case Qt::Key_Right:
this->gira(-1, 1);
break;
}
}
//quando o mouse se move
void OpenGLWidget::mouseMoveEvent(QMouseEvent *event)
{
QPoint posicao = event->pos();
double xx = (posicao.x() - (width()/2));
double yy = height()/2 - posicao.y();
xx = xx/width() * 10;
yy = yy/height() * 10;
//movimenta o model principal
models[0].mX = xx;
models[0].mY = yy;
//seta a luz para seguir o model principal
light.position = QVector4D(xx, yy, 2, 0);
//update();
}
bool OpenGLWidget::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
mouseMoveEvent(mouseEvent);
}
return false;
}
//retorna o tempo
QTime OpenGLWidget::getTime()
{
return QTime(0,0).addMSecs(tempo.elapsed());
}