-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
337 lines (275 loc) · 8.15 KB
/
Main.cpp
File metadata and controls
337 lines (275 loc) · 8.15 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
/**
Base sur les fichiers fournis pour les TP d'INF2705 a Polytechnique
*/
#include <iostream>
#include "BasicTimer.h"
#include "inf2705.h"
#include "MassSpringSystem.h"
// variables pour l'utilisation des nuanceurs
ProgNuanceur progBase; // le programme de nuanceurs de base
GLint locVertex;
GLint locColor;
// matrices de du pipeline graphique
MatricePipeline matrModel;
MatricePipeline matrVisu;
MatricePipeline matrProj;
// les formes
FormeCube *cubeFil = NULL;
// variables pour définir le point de vue
double thetaCam = 0.0; // angle de rotation de la caméra (coord. sphériques)
double phiCam = 0.0; // angle de rotation de la caméra (coord. sphériques)
double distCam = 0.0; // distance (coord. sphériques)
// variables d'état
bool enmouvement = false; // le modèle est en mouvement automatique ou non
bool afficheAxes = true; // indique si on affiche les axes
const double dimBoite = 0.25; // la dimension de la boite
const glm::vec3 FocusPoint(0.f, 0.f, 0.1f);
const GLdouble thetaInit = 270.0;
const GLdouble phiInit = 80.0;
const GLdouble distInit = 0.45;
MassSpringSystem* MSS = NULL;
uint MSSnX = 2;
uint MSSnY = 2;
uint MSSnZ = 2;
glm::vec3 MSSsize(0.1f, 0.1f, 0.1f);
bool bStepOnce = false;
// vérifier que les angles ne débordent pas les valeurs permises
void verifierAngles()
{
if ( thetaCam > 360.0 )
thetaCam -= 360.0;
else if ( thetaCam < 0.0 )
thetaCam += 360.0;
const GLdouble MINPHI = 0.01, MAXPHI = 180.0 - 0.01;
if ( phiCam > MAXPHI )
phiCam = MAXPHI;
else if ( phiCam < MINPHI )
phiCam = MINPHI;
}
void ReInitMSS()
{
if (MSS) { delete MSS; MSS = NULL; }
MSS = new MassSpringSystem();
MSS->GenerateCube(MSSnX, MSSnY, MSSnZ, MSSsize);
MSS->InitGL(locVertex, locColor);
MSS->InitCuda();
}
void calculerPhysique( float DeltaT )
{
if ( enmouvement || bStepOnce )
{
DeltaT = 0.01667f;
//std::cout << "DeltaT = " << DeltaT << std::endl;
const int NumIter = 18;
for (int i = 0; i < NumIter; i++)
{
//ComputeForces();
//IntegrateParticles(DeltaT / NumIter);
MSS->UpdateSystem(DeltaT / NumIter);
if (bStepOnce)
{
bStepOnce = false;
break;
}
}
}
}
void chargerNuanceurs()
{
// charger le nuanceur de base
progBase.creer( );
progBase.attacher( GL_VERTEX_SHADER, "nuanceurSommetsBase.glsl" );
progBase.attacher( GL_FRAGMENT_SHADER, "nuanceurFragmentsBase.glsl" );
progBase.lier( );
locVertex = progBase.obtenirAttribLocation( "Vertex" );
locColor = progBase.obtenirAttribLocation( "Color" );
}
void initialiser()
{
// positionnement de la caméra: angle et distance de la caméra à la base du bras
thetaCam = thetaInit;
phiCam = phiInit;
distCam = distInit;
// donner la couleur de fond
glClearColor( 0.0, 0.0, 0.0, 1.0 );
// activer les etats openGL
glEnable( GL_DEPTH_TEST );
// charger les nuanceurs
chargerNuanceurs();
// créer quelques autres formes
progBase.utiliser( );
cubeFil = new FormeCube( 1.0, false );
ReInitMSS();
}
void conclure()
{
delete cubeFil;
}
void afficherSysteme()
{
matrModel.PushMatrix();{ // sauvegarder la tranformation courante
progBase.assignerUniformMatrix4fv( "matrModel", matrModel );
MSS->Draw();
}matrModel.PopMatrix(); // revenir à la transformation sauvegardée
}
void definirCamera()
{
matrVisu.LookAt( distCam*cos(glm::radians(thetaCam))*sin(glm::radians(phiCam)) + FocusPoint.x,
distCam*sin(glm::radians(thetaCam))*sin(glm::radians(phiCam)) + FocusPoint.y,
distCam*cos(glm::radians(phiCam)) + FocusPoint.z,
FocusPoint.x, FocusPoint.y, FocusPoint.z,
0., 0., 6. );
}
void FenetreTP::afficherScene()
{
// effacer l'ecran et le tampon de profondeur
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
progBase.utiliser( );
// définir le pipeline graphique
matrProj.Perspective( 45.0, (GLdouble) largeur_ / (GLdouble) hauteur_, 0.05, 300.0 );
progBase.assignerUniformMatrix4fv( "matrProj", matrProj ); // informer la carte graphique des changements faits à la matrice
definirCamera();
progBase.assignerUniformMatrix4fv( "matrVisu", matrVisu ); // informer la carte graphique des changements faits à la matrice
matrModel.LoadIdentity();
progBase.assignerUniformMatrix4fv( "matrModel", matrModel ); // informer la carte graphique des changements faits à la matrice
// afficher les axes
if ( afficheAxes ) FenetreTP::afficherAxes();
// tracer la boite englobante
glVertexAttrib3f( locColor, 1.0, 0.5, 0.5 ); // équivalent au glColor() de OpenGL 2.x
matrModel.PushMatrix();{
matrModel.Translate( 0, 0, dimBoite/2 );
matrModel.Scale( dimBoite, dimBoite, dimBoite );
progBase.assignerUniformMatrix4fv( "matrModel", matrModel );
cubeFil->afficher();
}matrModel.PopMatrix();
// tracer la bestiole à pattes
afficherSysteme();
}
void FenetreTP::redimensionner( GLsizei w, GLsizei h )
{
glViewport( 0, 0, w, h );
}
void FenetreTP::clavier( TP_touche touche )
{
int tmpSize = 2;
switch ( touche )
{
case TP_ECHAP:
case TP_q: // Quitter l'application
if (MSS) { delete MSS; MSS = NULL; }
quit();
break;
case TP_x: // Activer/désactiver l'affichage des axes
afficheAxes = !afficheAxes;
std::cout << "// Affichage des axes ? " << ( afficheAxes ? "OUI" : "NON" ) << std::endl;
break;
case TP_v: // Recharger les fichiers des nuanceurs et recréer le programme
MSS->ToggleVelocityDisplay();
break;
case TP_i: // Réinitiliaser le point de vue
phiCam = phiInit; thetaCam = thetaInit; distCam = distInit;
break;
case TP_SOULIGNE:
case TP_MOINS: // Reculer la caméra
distCam += 0.2;
break;
case TP_PLUS: // Avancer la caméra
case TP_EGAL:
if ( distCam > 1.0 )
distCam -= 0.2;
break;
case 'a':
case TP_ESPACE: // Mettre en pause ou reprendre l'animation
enmouvement = !enmouvement;
break;
case 's':
bStepOnce = true;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
tmpSize = (touche - '0') * 2;
if (tmpSize > 5) tmpSize *= 1.5f;
MSSnX = MSSnY = MSSnZ = tmpSize;
// Fall through
case 'r':
ReInitMSS();
break;
case 't':
MSS->TogglePrintTime();
break;
default:
std::cout << " touche inconnue : " << (char) touche << std::endl;
break;
}
}
int dernierX, dernierY;
static bool pressed = false;
void FenetreTP::sourisClic( int button, int state, int x, int y )
{
// button est un parmi { TP_BOUTON_GAUCHE, TP_BOUTON_MILIEU, TP_BOUTON_DROIT }
// state est un parmi { TP_PRESSE, DL_RELEASED }
pressed = ( state == TP_PRESSE );
switch ( button )
{
case TP_BOUTON_GAUCHE: // Déplacer (modifier angles) la caméra
dernierX = x;
dernierY = y;
break;
}
}
void FenetreTP::sourisWheel( int x, int y )
{
//const int sens = +1;
//std::cout << "wheel " << x << ", " << y << std::endl;
if (y > 0)
{
distCam -= 0.06f;
}
else
{
distCam += 0.06f;
}
}
void FenetreTP::sourisMouvement( int x, int y )
{
if ( pressed )
{
int dx = x - dernierX;
int dy = y - dernierY;
thetaCam -= dx / 3.0;
phiCam -= dy / 3.0;
dernierX = x;
dernierY = y;
verifierAngles();
}
}
int main( int argc, char *argv[] )
{
// créer une fenêtre
FenetreTP fenetre( "INF2705 TP" );
// allouer des ressources et définir le contexte OpenGL
initialiser();
BasicTimer SimTimer;
SimTimer.Start();
bool boucler = true;
while ( boucler )
{
// mettre à jour la physique
calculerPhysique( SimTimer.GetTimeSinceLastCheck() );
// affichage
fenetre.afficherScene();
fenetre.swap();
// récupérer les événements et appeler la fonction de rappel
boucler = fenetre.gererEvenement();
}
// détruire les ressources OpenGL allouées
conclure();
return 0;
}