Bonsoir,
Je me décide a poster ce message car il me reste plus beaucoup de cheveux sur la tête a cause d'un probleme simple je l'espere. Je suis novice dans l'utilisation d'opengl et j'ai tout d'abord pris le temps de lire les documentations sur le sujet. Pour optimiser mon application je choisis d'utiliser les VBO. J'ai créé des objets de base (cube, cylindre) et après quelque maux de tête j'ai finis par les faire fonctionner. Puis je suis passé a la sphere, et la sa se complique. J'arrive a faire ma sphere avec les vertex array, mais avec les vbo sa marche mal, tres mal.
Je m'explique sa marche en ce que je vais appeler "low detail" c'est a dire n=8 (nombre de vertex pour ma sphere est n*(n-1)+2 , le 2 correspond a mes "sommets") or des que je passe a n=10 j'ai une erreur memoire type
./opengl: malloc(): memory corruption: 0x0883bb28
Alors j'ai voulu tester si le même probleme se posait avec le cylindre. Et la petite larme est arrivée en montant le detail de mon cylindre, crash meme probleme. Pour mon cylindre qui est moins complexe que la sphere a n=100 il marche encore a 150 il ne marche plus.
Je sais vous aller me dire que sa sert a rien de faire un cylindre aussi detaillé mais pour la sphere n=10 c'est loin d être detaillé. Donc je pense que je dois mal initialiser mes VBO puisque qu avec les vertex array je n ai aucun probleme.
J'espere que quelqu un pourra m aider parce que sa fait un bon moment que je suis dessus et rien a faire.
Je vous presente mon code sur le cylindre car celui de la sphere c'est le carnage tellement j ai voulu trouver le probleme les VBO etant initialiser de la meme maniere de toute facon.
cylindre.cpp
shape.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 void Cylindre::GenerateCylindre(const Vector& c, float r, float h, float proportion) { float theta=0.0; int a=0, b=0, d=0, n=150; GLfloat VertexArray[12*n]; GLuint Indice[6*n]; for(int i=0;i<n;i++) { theta=proportion*i*M_PI/n; float x = cos(theta); float z = sin(theta); VertexArray[a]= c[0] + x*r; //Vertex bas cylindre VertexArray[a+1]= c[1]; VertexArray[a+2]= c[2] + z*r; VertexArray[a+3]= x; //Normal VertexArray[a+4]= 0.0f; VertexArray[a+5]= z; VertexArray[a+6]= c[0] + x*r; //Vetex haut cylindre VertexArray[a+7]= c[1] + h; VertexArray[a+8]= c[2] + z*r; VertexArray[a+9]= x; //Normal VertexArray[a+10]= 0.0f; VertexArray[a+11]= z; a=a+12; } for (int ii=0;ii<n-1;ii++) { Indice[b]=d; Indice[b+1]=d+2; Indice[b+2]=d+1; Indice[b+3]=d+1; Indice[b+4]=d+2; Indice[b+5]=d+3; b=b+6; d=d+2; } Indice[b]=d; Indice[b+1]=0; Indice[b+2]=d+1; Indice[b+3]=d+1; Indice[b+4]=0; Indice[b+5]=1; for (int j=0;j<12*n;j++) { CylindreArray[j]=VertexArray[j]; } for (int jj=0;jj<6*n;jj++) { IndiceArray[jj]=Indice[jj]; } } void Cylindre::InitVBO() { // Génération des buffers glGenBuffers(2, CylindreBuffers); // Buffer d'informations de vertex glBindBuffer(GL_ARRAY_BUFFER, CylindreBuffers[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(CylindreArray), CylindreArray, GL_STATIC_DRAW); // Buffer d'indices glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, CylindreBuffers[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(IndiceArray), IndiceArray, GL_STATIC_DRAW); } void Cylindre::Affiche() { // Utilisation des données des buffers glBindBuffer(GL_ARRAY_BUFFER, CylindreBuffers[0]); glVertexPointer( 3, GL_FLOAT, 6 * sizeof(float), 0 ); glNormalPointer( GL_FLOAT, 6 * sizeof(float), ((float*)NULL + (3)) ); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, CylindreBuffers[1]); // Activation d'utilisation des tableaux glEnableClientState( GL_VERTEX_ARRAY ); glEnableClientState( GL_NORMAL_ARRAY ); // Rendu de notre géométrie glDrawElements(GL_TRIANGLES, 6*N_VER_CY, GL_UNSIGNED_INT, 0); glDisableClientState( GL_NORMAL_ARRAY ); glDisableClientState( GL_VERTEX_ARRAY ); } Cylindre::Cylindre(const Vector& c, float r, float h, float proportion) { GenerateCylindre(c,r,h,proportion); InitVBO(); } Cylindre::~Cylindre() { glDeleteBuffers(2, CylindreBuffers); }
et le 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 #define N_VER_CY 150 class Cylindre { protected : // Buffers VBO GLuint CylindreBuffers[2]; //Array GLfloat CylindreArray[12*N_VER_CY]; GLuint IndiceArray[6*N_VER_CY]; void GenerateCylindre(const Vector& c, float r, float h, float proportion); void InitVBO(); public: void Affiche(); Cylindre(const Vector& c, float r, float h, float proportion); ~Cylindre(); };
C'est la premiere fois que je post un message ici j'espere avoir suivis les regle et surtout
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 // Angle de rotation des objects float alpha=0.0f; int Mousekey=0,Mousex,Mousey; int light=1; int myrotate=1; float kavarr= 0.0f; float kgaudroi=0.0f; //Sphere * c=NULL; Cylindre * a=NULL; // Identifiant de fenetre int window; // Gestion du clavier. void Keyboard(unsigned char key, int x, int y) { // Si ESC if (key==27) { glutDestroyWindow(window); exit(0); } // Gestion des lumieres if ((key=='l') || (key=='L')) { light = 1-light; if (!light) { glDisable(GL_LIGHTING); } else { glEnable(GL_LIGHTING); } } if ((key=='r') || (key=='R')) { myrotate = 1-myrotate; } if ((key=='z') || (key=='Z')) { kavarr = kavarr + 1.0f; } if ((key=='s') || (key=='S')) { kavarr = kavarr - 1.0f; } if ((key=='q') || (key=='Q')) { kgaudroi = kgaudroi + 1.0f; } if ((key=='d') || (key=='D')) { kgaudroi = kgaudroi - 1.0f; } glutPostRedisplay(); } void Resize(int width, int height) { // Evite de divisser par 0 en cas de changement de taille if (height==0) { height=1; } // Fenetrage glViewport (0, 0, width, height); // Definition de la matrice de vue glMatrixMode (GL_PROJECTION); glLoadIdentity (); // Camera gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); // Rebascule dans le repere du modele glMatrixMode(GL_MODELVIEW); } // Permet de definir une matiere a partir d'une couleur void GlutShade(GLfloat r,GLfloat v,GLfloat b) { // Couleur sans lumieres glColor3f(0.8,0.9,0.6); // Couleur avec lumieres GLfloat color[4]; // La couleur diffuse sera egale a 25% de la couleur color[0]=0.75f*r; color[1]=0.75f*v; color[2]=0.75f*b; color[3]=1.0; glMaterialfv(GL_FRONT, GL_DIFFUSE, color); // La couleur ambiante sera egale a 25% de la couleur color[0]=0.25f*r; color[1]=0.25f*v; color[2]=0.25f*b; color[3]=1.0; glMaterialfv(GL_FRONT, GL_AMBIENT, color); // GL_AMBIENT_AND_DIFFUSE color[0]=1.0f; color[1]=0.0f; color[2]=0.0f; color[3]=1.0; glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, color); } // Affichage void GlutRendering() { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); //glTranslatef(0.0f,0.0f,kavarr); glTranslatef(0.0f,0.0f,-20.0f); glRotatef(kgaudroi,0.0f,1.0f,0.0f); glRotatef(alpha,1.0f,1.0f,1.0f); //glRotatef(kavarr,1.0f,0.0f,0.0f); a->Affiche(); //glCallList(a); glutSwapBuffers(); } void MouseMove(int x, int y) { Mousex = x; Mousey = y; } void MousePush(int button, int state, int x, int y) { } // Rafraichissement void GlutIdle(void) { if (myrotate) { alpha+=0.005; } GlutRendering(); } //!Initialise OpenGL void InitGlut(int width,int height) { glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA | GLUT_DEPTH); // Fenetre initiale glutInitWindowSize(512, 512); glutInitWindowPosition(0, 0); window=glutCreateWindow("Real Time Rendering"); glutDisplayFunc(GlutRendering); //glutFullScreen(); // Rafraichissement glutIdleFunc(&GlutIdle); // Changement de taille glutReshapeFunc(Resize); // Clavier glutKeyboardFunc(Keyboard); //glutSpecialFunc(&SpecialKeyboard); // Souris glutMouseFunc(MousePush); glutMotionFunc(MouseMove); // Initialise les parametres de rendu glClearColor (0.3f, 0.4f, 0.6f, 1.0); glClearDepth(1.0); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glShadeModel(GL_SMOOTH); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Reset The Projection Matrix gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); // Placement des lumieres dans l'espace du modele glMatrixMode(GL_MODELVIEW); GLfloat light_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat light_position[] = { 0.0,0.0, 10.0,0.0 }; glLightfv (GL_LIGHT1, GL_AMBIENT, light_ambient); glLightfv (GL_LIGHT1, GL_DIFFUSE, light_diffuse); glLightfv (GL_LIGHT1, GL_POSITION, light_position); glEnable(GL_LIGHT1); glEnable(GL_LIGHTING); glewInit(); // Elimination des facettes arriere //glCullFace(GL_BACK); //glEnable(GL_CULL_FACE); } int main(int argc,char **argv) { glutInit(&argc, argv); InitGlut(512,512); //c=new Cube(Vector(-4.2,-0.5,-1.0),Vector(4.2,-3.0,2.0)); a=new Cylindre(Vector(0.0,0.0,0.0),2.0,5.0,2.0); glutMainLoop(); //delete c; return 0; }
aider moi avant d avoir la boule a zero![]()
Partager