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
| // Exemple, notre vertex shader a deux attributs :
in vec3 Position;
in vec2 TextureUV;
// Nous avons un VBO contenant à la fois les positions et les coordonnées de texture séquentiellement, comme ceci :
// pos.x pos.y pos.z uv.s uv.t pos.x pos.y pos.z uv.s uv.t ...
// A l'initialisation, on récupère l'identifiant des attributs
int indexPosition = glGetAttribLocation (program, "Position");
int indexTextureUV = glGetAttribLocation (program, "TextureUV");
// Toujours à l'initialisation (c'est là l'avantage des VAO), on créé le VAO, on le bind, et on fait tous les appels
// d'envoie d'attributs
vertexArrayObject.Bind ();
// On bind le VBO contenant les données
vertexBuffer.Bind ();
// On active le tableau d'attribut correspondant, et on envoie les données
glEnableVertexAttribArray (indexPosition);
glVertexAttribPointer (indexPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray (indexTextureUV);
glVertexAttribPointer (indexTextureUV, 2, GL_FLOAT, GL_FALSE, 3, 0);
// Unbind le VBO
vertexBuffer.Unbind ();
// On désactive les tableaux d'attributs
glDisableVertexAttribArray (indexPosition);
glDisableVertexAttribArray (indexTextureUV);
// Enfin on unbind le VAO
vertexArrayObject.Unbind ();
// Maintenant, chaque fois qu'on voudra dessiner, on aura juste à écrire :
vertexArrayObject.Bind (); // A enregistrer tout ce qu'on lui a dit à l'initialisation
glDrawElements (...);
vertexArrayObject.Unbind (); |
Partager