J'ai fait un premier projet dans lequel j'affiche un cube avec une couleur par sommet, les données pour ce cube etant transmisent par VBO.
projet 1 : un seul fichier pas de problème
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <vector> #include <string> #include <math.h> #include <SDL/SDL.h> #include <GL/glew.h> using namespace std; #define DEBUG 1 #define FPS 50 #define N_FACE 6 #define N_VERTS 8 #define P_SIZE 3 #define C_SIZE 3 #define W 800 #define H 600 #define BUFFER_OFFSET(a) ((char*)NULL + (a)) #define PI 3.141 //#define PI 3.1411592654 bool hold = false; float _angleX = 0, _angleY = 0; float _positionX = 5, _positionY = 5, _positionZ = 5; float _distance = 8.66, _rayonY = 1, _motionSensivity = 0.5; GLuint buf; // identifiant de notre objet tampon GLuint indBuf; GLuint modelTexture; GLuint solTexture; // Texture des models de la scene SDL_Event ev; float pos[N_VERTS*P_SIZE] = { 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f }; float colors[N_VERTS*C_SIZE] = { 1.0f, 0.0f, 0.0f, // rouge 0.0f, 0.0f, 1.0f, // bleu 0.0f, 1.0f, 0.0f, // vert 1.0f, 1.0f, 1.0f, // blanc 1.0f, 0.0f, 1.0f, // magenta 0.0f, 1.0f, 1.0f, // cyan 1.0f, 1.0f, 0.0f, // jaune 0.5f, 0.5f, 0.5f // gris }; GLuint Indice[N_FACE*6] = { 0,1,2,0,2,3, 7,6,5,5,4,7, 0,3,4,0,4,5, 7,2,1,1,6,7, 0,5,6,0,6,1, 7,4,3,3,2,7 }; double cosinus(double angle) { double cosinus = cos(angle*PI/180); return cosinus; } double sinus(double angle) { double sinus = sin(angle*PI/180); return sinus; } void OnMouseMotion(const SDL_MouseMotionEvent & _event) { if (hold) //si nous maintenons le bouton gauche enfoncé { double rayonY; // rotation autour de X <=> inclinaison de la camera _angleX += _event.yrel*_motionSensivity;//; if (_angleX > 360.0) _angleX -= 360.0; else if (_angleX < 0.0) _angleX += 360.0; _positionY = _distance*sinus(_angleX); _rayonY = _distance*cosinus(_angleX); // rotation autour de Y <=> place la camera sur un cercle de _rayonY autour de Y en _positionY _angleY += _event.xrel*_motionSensivity;//; if (_angleY > 360.0) _angleY -= 360.0; else if (_angleY < 0.0) _angleY += 360.0; _positionX = _rayonY*cosinus(_angleY); _positionZ = _rayonY*sinus(_angleY); } } void initialisationVideo() { if (SDL_Init(SDL_INIT_VIDEO) == -1) { cerr << "Erreur d'initialisation de la SDL\n"; exit(EXIT_FAILURE); } SDL_WM_SetCaption("SDL GL Application", NULL); SDL_SetVideoMode( W, H, 32, SDL_OPENGL); // nom de la fenetre SDL_WM_SetCaption("Vertex Buffer Objects GL", NULL); } void initOpenGL() { // Définition de la Matrice et du type de vue glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective(70,(float)W/(float)H,1,100); // Active le Z_Buffer et le Texturage glEnable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); } void majAffichageGL() { // Mise à jour affichage glFlush(); SDL_GL_SwapBuffers(); } void boucleSDL() { // boucle infinie while(1) { // Boucle d'evenement while(SDL_PollEvent(&ev)) { switch(ev.type) { case SDL_QUIT: exit(0); break; case SDL_KEYDOWN: switch (ev.key.keysym.sym) { case SDLK_ESCAPE: exit(0); break; default : break; } break; case SDL_MOUSEMOTION: OnMouseMotion(ev.motion); break; case SDL_MOUSEBUTTONUP: hold = false; break; case SDL_MOUSEBUTTONDOWN: hold = true; break; }// fin switch event.type }// fin while(SDL_PollEvent(&ev)) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // On va définir la matrice de projection glMatrixMode(GL_PROJECTION); // Initialisation avec la matrice identité glLoadIdentity(); GLfloat ratio = (float) W / (float) H; gluPerspective(70.0f, ratio, 0.1f, 10.0f); gluLookAt( _positionX, _positionY, _positionZ, 0, 0, 0, 0, 1, 0); // on passe en mode VBO glBindBuffer(GL_ARRAY_BUFFER, buf); glVertexPointer(P_SIZE, GL_FLOAT, 0, BUFFER_OFFSET(0)); glColorPointer(C_SIZE, GL_FLOAT, 0, BUFFER_OFFSET(N_VERTS*P_SIZE*sizeof *pos)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indBuf); // activation des tableaux de sommets glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); // desactivation des tableaux de sommet glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); // Mise à jour affichage majAffichageGL(); }// fin while(1) SDL_Quit(); } int main(int argc, char *argv[]) { // redirige cout et cerr --> fichier ofstream Erreur("erreur.txt"); streambuf* oldErrBuff = cerr.rdbuf(Erreur.rdbuf()) ; cerr << " *** Redirection de cerr *** " << endl; // Initialisation Video, Création de la fenetre de l'application initialisationVideo(); // initialisation de glew glewInit(); // creation d'un objet tampon et recuperation de son identifiant glGenBuffers(1, &buf); glGenBuffers(1, &indBuf); // creation de notre VBO // on bind le buffer glBindBuffer(GL_ARRAY_BUFFER, buf); // on alloue un espace glBufferData(GL_ARRAY_BUFFER, // target (N_VERTS * P_SIZE * sizeof *pos) + // taille des positions (N_VERTS * C_SIZE * sizeof *colors),// taille des couleurs NULL, // ... GL_STREAM_DRAW); // mode // on specifie les donnees glBufferSubData(GL_ARRAY_BUFFER, 0, // emplacement des donnees dans le VBO (N_VERTS * P_SIZE * sizeof *pos), // taille des donnees pos); // adresse des donnees glBufferSubData(GL_ARRAY_BUFFER, (N_VERTS*P_SIZE*sizeof *pos), // emplacement (N_VERTS*C_SIZE*sizeof *colors),// taille colors); // donnees // Buffer d'indices glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indBuf); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indice), Indice, GL_STREAM_DRAW); // fin creation de notre VBO // Activation de la repetition de touche SDL_EnableKeyRepeat(100, 100); // initialisation openGL initOpenGL(); // boucle infinie boucleSDL(); // Fin de main glDeleteBuffers(1, &buf); glDeleteBuffers(1, &indBuf); SDL_Quit(); // Restauration du streambuf initial de cerr (affichage sur la console) cerr.rdbuf(oldErrBuff); return 0; }
Second projet : même données (cube) enfermées dans une class, mais là rien ne s'affiche !!!
main.h
main.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #ifndef _MAIN_H #define _MAIN_H #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <vector> #include <string> #include <math.h> #include <SDL/SDL.h> #include <GL/glew.h> using namespace std; #define DEBUG 1 #define FPS 50 #define W 800 #define H 600 #define N_FACE 6 #define N_VERTS 8 #define P_SIZE 3 #define C_SIZE 3 double cosinus(double angle); double sinus(double angle); #endif //_MAIN_H fin de main.h
CModelGeometrique.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "main.h" #include "CModelGeometrique.h" #define BUFFER_OFFSET(a) ((char*)NULL + (a)) #define PI 3.141 //#define PI 3.1411592654 bool hold = false; float _angleX = 0, _angleY = 0; float _positionX = 5, _positionY = 5, _positionZ = 5; float _distance = 8.66, _rayonY = 1, _motionSensivity = 0.5; SDL_Event ev; CCube *cube; double cosinus(double angle) { double cosinus = cos(angle*PI/180); return cosinus; } double sinus(double angle) { double sinus = sin(angle*PI/180); return sinus; } void OnMouseMotion(const SDL_MouseMotionEvent & _event) { if (hold) //si nous maintenons le bouton gauche enfoncé { double rayonY; // rotation autour de X <=> inclinaison de la camera _angleX += _event.yrel*_motionSensivity;//; if (_angleX > 360.0) _angleX -= 360.0; else if (_angleX < 0.0) _angleX += 360.0; _positionY = _distance*sinus(_angleX); _rayonY = _distance*cosinus(_angleX); // rotation autour de Y <=> place la camera sur un cercle de _rayonY autour de Y en _positionY _angleY += _event.xrel*_motionSensivity;//; if (_angleY > 360.0) _angleY -= 360.0; else if (_angleY < 0.0) _angleY += 360.0; _positionX = _rayonY*cosinus(_angleY); _positionZ = _rayonY*sinus(_angleY); } } void initialisationVideo() { if (SDL_Init(SDL_INIT_VIDEO) == -1) { cerr << "Erreur d'initialisation de la SDL\n"; exit(EXIT_FAILURE); } SDL_SetVideoMode( W, H, 32, SDL_OPENGL); // nom de la fenetre SDL_WM_SetCaption("Vertex Buffer Objects GL", NULL); } void initOpenGL() { // Définition de la Matrice et du type de vue glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective(70,(float)W/(float)H,1,100); // Active le Z_Buffer et le Texturage glEnable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); } void majAffichageGL() { // Mise à jour affichage glFlush(); SDL_GL_SwapBuffers(); } void boucleSDL() { // boucle infinie while(1) { // Boucle d'evenement while(SDL_PollEvent(&ev)) { switch(ev.type) { case SDL_QUIT: exit(0); break; case SDL_KEYDOWN: switch (ev.key.keysym.sym) { case SDLK_ESCAPE: exit(0); break; default : break; } break; case SDL_MOUSEMOTION: OnMouseMotion(ev.motion); break; case SDL_MOUSEBUTTONUP: hold = false; break; case SDL_MOUSEBUTTONDOWN: hold = true; break; }// fin switch event.type }// fin while(SDL_PollEvent(&ev)) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // On va définir la matrice de projection glMatrixMode(GL_PROJECTION); // Initialisation avec la matrice identité glLoadIdentity(); GLfloat ratio = (float) W / (float) H; gluPerspective(70.0f, ratio, 0.1f, 10.0f); gluLookAt( _positionX, _positionY, _positionZ, 0, 0, 0, 0, 1, 0); cube->Affiche(); // Mise à jour affichage majAffichageGL(); }// fin while(1) } int main(int argc, char *argv[]) { // redirige cout et cerr --> fichier ofstream Erreur("erreur.txt"); streambuf* oldErrBuff = cerr.rdbuf(Erreur.rdbuf()) ; cerr << " *** Redirection de cerr *** " << endl; // Initialisation Video, Création de la fenetre de l'application initialisationVideo(); // initialisation de glew glewInit(); cube = new CCube; // Activation de la repetition de touche SDL_EnableKeyRepeat(100, 100); // initialisation openGL initOpenGL(); // boucle infinie boucleSDL(); delete cube; SDL_Quit(); // Restauration du streambuf initial de cerr (affichage sur la console) cerr.rdbuf(oldErrBuff); return 0; }
CModelGeometrique.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #ifndef _CMODELGEOMETRIQUE_H #define _CMODELGEOMETRIQUE_H #include "main.h" //#include "3Dmath.h" //#include "sdlglutils.h" //#include "CObjet3D.h" #define BUFFER_OFFSET(a) ((float*)NULL + (a)) class CCube { protected : float Cote; // Buffers GLuint ArrayBuffer; // VBO GLuint IndiceBuffer; // IBO // Arrays GLfloat * VertexArray; GLfloat * ColorArray; GLuint * IndiceArray; // Methodes de dessin des objets du decor void InitArray(); void InitVBO(); public: void Affiche(); CCube(); ~CCube(); }; #endif
Les deux projet étant compiler avec les fichier glew.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "CModelGeometrique.h" void CCube::InitArray() { GLfloat pos[N_VERTS * P_SIZE] = { 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f }; GLfloat colors[N_VERTS * C_SIZE] = { 1.0f, 0.0f, 0.0f, // rouge 0.0f, 0.0f, 1.0f, // bleu 0.0f, 1.0f, 0.0f, // vert 1.0f, 1.0f, 1.0f, // blanc 1.0f, 0.0f, 1.0f, // magenta 0.0f, 1.0f, 1.0f, // cyan 1.0f, 1.0f, 0.0f, // jaune 0.5f, 0.5f, 0.5f // gris }; GLuint indice[N_FACE * 6] = { 0,1,2,0,2,3, 7,6,5,5,4,7, 0,3,4,0,4,5, 7,2,1,1,6,7, 0,5,6,0,6,1, 7,4,3,3,2,7 }; VertexArray = pos; ColorArray = colors; IndiceArray = indice; /* if(DEBUG) { int i = 0, j = 0; while (j < 36) { cerr << " IndiceArray " << j << " : " << IndiceArray[j] << endl; if (i < 24) { cerr << " Vertex " << i/3 << " : " << VertexArray[i] << " | " << VertexArray[i+1]<< " | " << VertexArray[i+2] << endl; } i += 3; ++j; } } */ if(DEBUG) cerr << " Initialisation tableau : OK " << endl; } void CCube::InitVBO() { // Génération des buffers glGenBuffers(1, &ArrayBuffer); // on bind le buffer glBindBuffer(GL_ARRAY_BUFFER, ArrayBuffer); // on alloue un espace glBufferData(GL_ARRAY_BUFFER, // target (N_VERTS * P_SIZE * sizeof *VertexArray) + // taille des positions (N_VERTS * C_SIZE * sizeof *ColorArray), // taille des couleurs NULL, // ... GL_STREAM_DRAW); // mode // on specifie les donnees glBufferSubData(GL_ARRAY_BUFFER, 0, // emplacement des donnees dans le VBO (N_VERTS * P_SIZE *sizeof *VertexArray), // taille des donnees VertexArray); // adresse des donnees glBufferSubData(GL_ARRAY_BUFFER, (N_VERTS * P_SIZE * sizeof *VertexArray), // emplacement (N_VERTS * C_SIZE * sizeof *ColorArray), // taille ColorArray); // donnees //glBindBuffer(GL_ARRAY_BUFFER, 0); // Buffer d'indices glGenBuffers(1, &IndiceBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndiceBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(IndiceArray), IndiceArray, GL_STREAM_DRAW); //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); if(DEBUG) cerr << " Creation VBO : OK " << endl; if(DEBUG) cerr << " IndiceBuffer " << IndiceBuffer << " ArrayBuffer " << ArrayBuffer << endl; } void CCube::Affiche() { // Utilisation des données des buffers glBindBuffer(GL_ARRAY_BUFFER, ArrayBuffer); glVertexPointer(P_SIZE, GL_FLOAT, 0, BUFFER_OFFSET(0)); glColorPointer(C_SIZE, GL_FLOAT, 0, BUFFER_OFFSET(N_VERTS * P_SIZE * sizeof *VertexArray)); //glVertexPointer( 3, GL_FLOAT, 6 * sizeof(float), 0 ); //glColorPointer( 3, GL_FLOAT, 6 * sizeof(float), ((float*)NULL + (3)) ); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndiceBuffer); // Activation d'utilisation des tableaux glEnableClientState( GL_VERTEX_ARRAY ); glEnableClientState( GL_COLOR_ARRAY ); // Rendu de notre géométrie glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, BUFFER_OFFSET(0)); if(DEBUG) cerr << " Affichage du cube : OK " << endl; //glBindBuffer(GL_ARRAY_BUFFER, 0); //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glDisableClientState( GL_COLOR_ARRAY ); glDisableClientState( GL_VERTEX_ARRAY ); } CCube::CCube() { VertexArray = NULL; ColorArray = NULL; IndiceArray = NULL; InitArray(); InitVBO(); if(DEBUG) cerr << " Creation cube : OK " << endl; } CCube::~CCube() { glDeleteBuffers(1, &ArrayBuffer); glDeleteBuffers(1, &IndiceBuffer); if(DEBUG) cerr << " Destruction cube : OK " << endl; }
Je ne comprends pas pourquoi rien ne s'affiche. Quelqu'un pourait il m'aider.
Merci d'avance
Partager