Hello
This is not a tiny post, and I am not encountering technical or coding issues with OpenGL. But I am new to 3D coding and OpenGL and I just would like to be sure I understand well the use of Buffer Objects. I would be grateful for anyone who will take the time to read my post and guide me
I am learning OpenGL using to the Red Book (7th edition).
I try to understand Buffer Objects. What I ask is "do I code it the good way ? Are there improvements (always using Buffer Objects of course).
I won’t give raw code (at least for gl______() calls), but only the ideas of how i understand the use of BOs.
My C++ code contains a class representing an editable Mesh, which is a collection of vertices and indices :
#include "Vector3.hpp"
class Mesh
{
// ....
ListOf<Vector3> Vertices;
ListOf<Uint> Indices;
// ....
};
Vector3 is just a {X,Y,Z}-float structure.
That way if I wish to represent a box meshed with triangles :
Vertices = {
{-1,-1,-1},
{1,-1,-1},
{1,1,-1},
{-1,1,-1},
{-1,-1,1},
{1,-1,1},
{1,1,1},
{-1,1,1}
};
Indices = {
0,1,2,
0,2,3,
1,5,6,
1,6,2,
4,0,3,
4,3,7,
5,4,7,
5,7,6,
3,2,6,
3,6,7,
4,5,1,
4,1,0
};
My Mesh class also has two methods :
[ul][li]Draw : Asks for rendering my mesh;[*]UpdateBuffers : If no buffer object created yet for this mesh, create them. Then send vertices and indices in the BO[/ul][/li]
For that purpose, my methods are designed as below :
VERTICES = 0
INDICES = 1
Buffers = Array of 2 GLuint; (will contains the Buffer Object names)
Method "Draw"
{
IF [Buffer Objects] are not "UP TO DATE" THEN
[CALL UpdateBuffers Method]
END IF
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Buffers[INDICES]);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, SIZE(Indices), GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
}
Method "UpdateBuffers"
{
glEnableClientState(GL_VERTEX_ARRAY);
IF [Buffer Objects] == NOTHING THEN
// ...
glGenBuffers(2, BUFFERS);
// ...
END IF
[... Create the array of vertices, of type : GLfloat[] from Vertices ...] ---> VerticesArray
[... Create the array of indices, of type : GLfloat[] from Indices ...] ---> IndicesArray
glBindBuffer(GL_ARRAY_BUFFER, Buffers[VERTICES]);
glBufferData(GL_ARRAY_BUFFER, SIZE(VerticesArray), VerticesArray, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Buffers[INDICES]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, SIZE(IndicesArray), IndicesArray, GL_DYNAMIC_DRAW);
[... Free VerticesArray and IndicesArray ...]
}
I know two performance “TODOs” :
- Do not update a whole array but only update modified parts of my BO (using glMapBufferRange or glSubData I suppose) ;
- Only update the modified BO (either Vertices or Indices, not both).
But before going further, I’d like to know if my coding design is doing the “good way” :).