I'm currently building a voxel engine (like Minecraft for example), and I'm using opengl.
The current design of the engine looks like this:
class Map -> contains 2d array of chunks (std::vector of std::vector)
class Chunk -> contains 3d array of block (also using std::vector)
class Block -> contains the vertices of the blocks. (cubes, built from 12 triangles);
What I want to do is make every chunk have a draw() function which will draw all the blocks in the chunk. I thought I'd make a buffer for each chunk using glGenBuffers(). From the tutorials I read should first create a vertex array with glVertexArray and than bind it, than bind a buffer to it with the target GL_ARRAY_BUFFER and fill it with data.
So far I managed to render a single chunk with a number of blocks using a buffer which created in the chunk with glGenBuffers() but in the main function I create glVertexArray and bind it, than call the draw() function in the chunk and it bind the chunk's buffer and drawing using glDrawArrays().
My question is, it is right to create a VBO for each chunk? I need to create a glVertexArray for each chunk too? or I should use one for all the chunks and every time bind another VBO?
I tried to study more about the VAO and the VBO throught tutorials and the opengl wiki but so far I didn't fully understand how it works and how it should be used properly.
boost.multiarray
. – genpfaultdraw
method per Block class. While that's a great example of encapsulation, it has the potential to be very inefficient. In particular, for actions like rendering, you may have to set state (e.g., bind VBOs, change shader uniforms, etc.) redundantly in eachdraw
method, and the encapsulation prevents any global view of how you might optimize that operation. Have a look at the Visitor Pattern which describes a way to coordinate traversal of your data in an organized manner. – radical7glVertexArray()
function you're referring to? OpenGL has vertex arrays, vertex array objects, and vertex buffer objects. None of which use something calledglVertexArray()
. Perhaps you're thinking ofglGenVertexArrays()
? – genpfaultdraw
methods are associated with classes that encapsulate all of the state required to draw the object they represent (of course, that depends on how you implement it). That encapsulation may cause redundant information to be sent to OpenGL, since objects that use the same state (e.g., shaders, textures, etc.) will each need to send (or at least bind) that state. It's just inefficient. For best performance, you need a mechanism that has a more "global" view of all the objects, and can try to minimize state changes. – radical7