Bonjour à tous,
Je me mets aux VBO et j'ai un problème de plantage de l'exe.
J'utilise VC++ 2005 express, la SDL et Glew en static.
glewInit() est appelé juste après l'initialisation video de la SDL et avant toute fonction openGL.
J'affiche correctement un cube sans plantage de l'exe lors tout s'initialise dans la fonction main().
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 #include <SDL/SDL.h> #include <GL/glew.h> #include <math.h> // dimensions de la fenetre #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.05; 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); } } int main(int argc, char **argv) { int loop = 1; // booleen du 'main loop' SDL_Event ev; // structure d'evenement(s) SDL GLuint buf; // identifiant de notre objet tampon GLuint indBuf; #define N_FACE 6 #define N_VERTS 8 #define P_SIZE 3 #define C_SIZE 3 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 }; // initialisation de la SDL en mode OpenGL SDL_Init(SDL_INIT_VIDEO); SDL_SetVideoMode(W, H, 32, SDL_OPENGL); // nom de la fenetre SDL_WM_SetCaption("Vertex Buffer Objects GL", NULL); // 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_STATIC_DRAW); // fin VBO // boucle d'affichage principale while(loop) { // recuperation d'un evenement SDL_PollEvent(&ev); // analyse if(ev.type == SDL_QUIT) loop = 0; if(ev.type == SDL_MOUSEBUTTONDOWN) hold = true; if(ev.type == SDL_MOUSEBUTTONUP) hold = false; if(ev.type == SDL_MOUSEMOTION && hold) OnMouseMotion(ev.motion); glEnable(GL_DEPTH_TEST); 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); // on flip les tampons glFlush(); SDL_GL_SwapBuffers(); } glDeleteBuffers(1, &buf); return 0; }
Mais j'obtiens un plantage de l'exe avec cette erreur lors de l'execution du debugage lors que je tente de mettre mon cube dans une class.
Exception de première chance à 0x00000000 dans projet01_v1.1.exe : 0xC0000005: Violation d'accès lors de la lecture de l'emplacement 0x00000000.
Ceci est secondaire à l'appel de : glGenBuffers(1, &ArrayBuffer);
voici le code avec ma class :
Je ne comprends pas ce qui cloche (en espérant que ce ne soit pas moi la cloche
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 ************************* CModelGeometrique.h ************* #ifndef _CMODELGEOMETRIQUE_H #define _CMODELGEOMETRIQUE_H #include "main.h" #define N_FACE 6 #define N_VERTS 8 #define P_SIZE 3 #define C_SIZE 3 #define BUFFER_OFFSET(a) ((float*)NULL + (a)) class CCube { protected : // 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 *********************** CModelGeometrique.cpp ************ #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; } 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); } 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 << " Affiche cube " << 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(); } CCube::~CCube() { glDeleteBuffers(1, &ArrayBuffer); glDeleteBuffers(1, &IndiceBuffer); })
Partager