-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
285 lines (227 loc) · 8.21 KB
/
main.cpp
File metadata and controls
285 lines (227 loc) · 8.21 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
#include <stdlib.h>
#include <GL/glut.h>
#include <iostream>
#include <math.h>
#include "Cube.h"
#include "CubeView.h"
#include "EchangeurClavier.h"
using namespace std;
/* Prototypes des fonctions evenementielles utilisees par glut */
void redimensionne(int largeur, int hauteur);
void display(void);
void clavier(unsigned char touche, int x, int y);
void numClavier(int key, int x, int y); // touches du numpad
/* Prototype des fonctions tmp */
int rubixConsole(void);
void drawString(char *S);
void drawRepere(void);
/* Variables globales */
Cube unCube;
CubeView cubeView(&unCube);
EchangeurClavier mesTouches;
/*--------------------------------------------------------------------------------*/
/* Point d'entree du programme : fonction main */
int main(int argc, char *argv[]) {
//rubixConsole();
/* Initialisation de glut et creation de la fenetre */
glutInit(&argc, argv);
glutInitWindowSize(640,640);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Projet Rubik's Cube");
glEnable(GL_DEPTH_TEST);
/* Initialisation OpenGL : machine à etats... */
glClearColor(0.0,0.0,0.0,0.0);
/* Enregistrement des fonctions de rappel ("CallBacks" evenementielles) */
glutReshapeFunc(redimensionne);
glutDisplayFunc(display);
glutKeyboardFunc(clavier);
glutSpecialFunc(numClavier);
/* Entree dans la boucle principale glut */
glutMainLoop();
return EXIT_SUCCESS;
}
void drawRepere(void) {
glPushMatrix();
glBegin(GL_LINE_STRIP);
//axe Y
glColor3f(0,1,0);
glVertex3f(0,0,5);
glVertex3f(0,1,5);
//axe X
glColor3f(1,0,0);
glVertex3f(0,0,5);
glVertex3f(1,0,5);
//axe Z
glColor3f(0,0,1);
glVertex3f(0,0,5);
glVertex3f(0,0,6);
glEnd();
glPopMatrix();
glPushMatrix();
glColor3d(0,1,0);
glRasterPos3f(0,1,5);
drawString("Y");
glPopMatrix();
glPushMatrix();
glColor3d(1,0,0);
glRasterPos3f(1,0,5);
drawString("X");
glPopMatrix();
glPushMatrix();
glColor3d(0,0,1);
glRasterPos3f(0,0,6);
drawString("Z");
glPopMatrix();
}
// Exemple d'utilisation du rubix en mode console
int rubixConsole(void) {
cout << "Hello world!" << endl;
Cube unCube;
unCube.afficher();
int num = 42;
int sens;
while(num != -1) {
cout << "Quelle face voulez vous faire tourner ? 0, 1, 2, 3, 4 ou 5" << endl;
cout << "Entrez -1 pour arrêter." << endl;
cin >> num;
if(num !=-1 ) {
cout << "Dans quel sens ? horaire = 1, inverse = 2" << endl;
cin >> sens;
unCube.rotation(num,sens);
unCube.afficher();
}
}
return 0;
}
void display(void) {
/* Variables locales ET statiques pour memorisation entre affichages successifs */
static GLdouble pos_cam_x = 0, pos_cam_y = 0, pos_cam_z = 20, alpha_cam = 90, thetha_cam = 0, dist_cam = 20;
/* Mise a jour des donnees avec les evenements clavier */
while(mesTouches.touchePressee())
{
switch(mesTouches.recupereTouchePressee())
{
case ECHAP : exit(EXIT_SUCCESS);
break;
case ZOOM : dist_cam++;
if(dist_cam >100)dist_cam=100;
break;
case DEZOOM : dist_cam--;
if(dist_cam <1)dist_cam=1;
break;
case GAUCHE : alpha_cam -=2;
if(alpha_cam < 0)alpha_cam=358;
break;
case DROITE : alpha_cam +=2;
if(alpha_cam >358)alpha_cam=0;
break;
case BAS : thetha_cam -=2;
if(thetha_cam < 0)thetha_cam=360;
break;
case HAUT : thetha_cam +=2;
if(thetha_cam >358)thetha_cam=0;
break;
case ARROW_DOWN : cubeView.mooveCube(DOWN);
break;
case ARROW_UP : cubeView.mooveCube(UP);
break;
case ARROW_RIGHT : cubeView.mooveCube(RIGHT);
break;
case ARROW_LEFT : cubeView.mooveCube(LEFT);
break;
case DEBUT : alpha_cam = 90;
thetha_cam = 0;
break;
}
pos_cam_x = dist_cam * cos(alpha_cam*3.14/180) * cos(thetha_cam*3.14/180);
pos_cam_z = dist_cam * sin(alpha_cam*3.14/180) * cos(thetha_cam*3.14/180);
pos_cam_y = dist_cam * sin(thetha_cam*3.14/180);
}
/* Effacement de l'image avec la couleur de fond */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Selection / Initialisation de la matrice de transformation des objets */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* Placement de la camera */
gluLookAt(pos_cam_x, pos_cam_y, pos_cam_z, 0,0,0, 0,1,0);
/* Objets 3D de la scene */
glPushMatrix();
drawRepere();
cubeView.update();
glPopMatrix();
/* On affiche la scene nouvellement calculee (double buffer) */
glutSwapBuffers();
glutPostRedisplay();
}
/* Gestion du redimensionnement de la fenetre + projections de la scene */
void redimensionne(int largeur, int hauteur) {
glViewport(0, 0, largeur, hauteur);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, largeur/(GLdouble)hauteur , 1.0, 100.);
}
void clavier(unsigned char touche, int x, int y) {
switch (touche)
{
case 27 : /* La touche Esc permet de quitter le programme */
mesTouches.ajouteTouchePressee(ECHAP);
break;
case 'z':
case 'Z': mesTouches.ajouteTouchePressee(HAUT);
break;
case 's':
case 'S': mesTouches.ajouteTouchePressee(BAS);
break;
case 'q':
case 'Q': mesTouches.ajouteTouchePressee(GAUCHE);
break;
case 'd':
case 'D': mesTouches.ajouteTouchePressee(DROITE);
break;
case 'a':
case 'A': mesTouches.ajouteTouchePressee(ZOOM);
break;
case 'e':
case 'E': mesTouches.ajouteTouchePressee(DEZOOM);
break;
}
glutPostRedisplay();
}
void numClavier(int key, int x, int y) {
switch (key)
{
case GLUT_KEY_F1 : break;
case GLUT_KEY_F2 : break;
case GLUT_KEY_F3 : break;
case GLUT_KEY_F4 : break;
case GLUT_KEY_F5 : break;
case GLUT_KEY_F6 : break;
case GLUT_KEY_F7 : break;
case GLUT_KEY_F8 : break;
case GLUT_KEY_F9 : break;
case GLUT_KEY_F10 : break;
case GLUT_KEY_F11 : break;
case GLUT_KEY_F12 : break;
case GLUT_KEY_LEFT : mesTouches.ajouteTouchePressee(ARROW_LEFT);
break;
case GLUT_KEY_UP : mesTouches.ajouteTouchePressee(ARROW_UP);
break;
case GLUT_KEY_RIGHT : mesTouches.ajouteTouchePressee(ARROW_RIGHT);
break;
case GLUT_KEY_DOWN : mesTouches.ajouteTouchePressee(ARROW_DOWN);
break;
case GLUT_KEY_PAGE_UP : break;
case GLUT_KEY_PAGE_DOWN : break;
case GLUT_KEY_HOME : mesTouches.ajouteTouchePressee(DEBUT); //remet la camera en position initial
break;
case GLUT_KEY_END : break;
case GLUT_KEY_INSERT : break;
}
glutPostRedisplay();
}
void drawString(char *S) {
for(unsigned int i =0; i<strlen(S); i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10,S[i]);
}
}