Multible vertex buffer objects (VBO) in lwjgl - opengl

I was able to get VBO's working for a single mesh.
First I set up opengl:
//....
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
For each mesh I generate the buffer (two calls to this function per mesh):
vertBuffer = GL15.glGenBuffers( BufferUtils.createIntBuffer(1));
//same idea for faceBuffer
I put the mesh data into FloatBuffers and IntBuffers.
Then I have to send these over to opengl:
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertBuffer)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, myVertPositions,GL15.GL_STATIC_DRAW);
//same idea for faces.
Note: All of the above steps only are done once.
Finally there is rendering:
//...
int stride = 0; //0 = compact arrays.
int offset = 0; //Each mesh would need a different offset?
GL11.glVertexPointer(3, GL11.GL_FLOAT, stride, offset);
GL12.glDrawRangeElements(GL11.GL_TRIANGLES, 0, numVerts,
numFaces*3, GL11.GL_UNSIGNED_INT, offset);
//Using the "buffer versions" of these functions throws
//"Cannot use Buffers when Array Buffer Object is enabled"
//Whether or not glEnableClientState is called.
Any new mesh I add overwrites the old mesh. Putting ALL of my meshes into the same buffer doesn't sound efficient.
How else can I get multiple meshes to coexist?

Putting ALL of my meshes into the same buffer doesn't sound efficient.
Actually it is: Switching VBOs is more expensive, than setting some offset. Especially for a lot of small (in terms of number of vertices) objects, putting them into one VBO is recommended.
Anyway: You can use as many VBOs as you like. glGenBuffers gives you an ID, which allows you to identify those buffers and target them with glBindBuffer.

this is a quite simple issue.
In order to use multiple VBOs, you have to keep the ID assigned by the function glGenBuffers as your reference to the Mesh.
then, when you want to draw that specific mesh, you need to bind it like this (an excerpt of my engine's code):
glBindBuffer(GL_ARRAY_BUFFER, Object_To_Draw->PATRIA_Model->MODEL_VBO_ID);
It's like you have 3 meshes, apple, pear and orange, each one has its own ID.
If you want to use the apple, you bind the ID of the apple (in simple words you say to the GPU, take the geometry data from the memory storing the apple.... :)), if you want the pear you point that and oso on.
I hope it helped.

Related

Can I set VAO before VBO in OpenGL?

I have a Sphere class that generate the VBO for creating given input radius and some more parameters.
Each Sphere VBO share the same memory layout (let's say vertex indice 0 = vertices, 1 = colors).
I may getting wrong, but if a understand correctly, state is what VAO store. But I don't know if VAO reminds which VBO was bound or use the currently bound VBO. But I think it use the VBO bound when modifying it (so it imply that reconfiguring the VAO for each Sphere render)
Question #1
Is it possible to store on VAO for all of the spheres? Sharing one VAO for multiple VBO.
Question #2
Can we set up the VAO independently from the VBO ? I wanna say, before even creating the VBO and without VBO bound, for example in a static function before even creating Spheres.
Question #3
This question may have non sens and be case-specific, but should I use one VAO and one VBO for each Sphere or share one VAO for all the Spheres (if it's possible) ?
Using the separate attribute format API, it is easy to set up a VAO independently from any particular set of source buffer objects. The vertex format information (component type, count, etc) can all be established via glVertexAttribFormat without a buffer object. You can even set up the relationship between attribute indices and buffer binding points, so that you can read interleaved attributes from a single buffer binding. All without ever binding a buffer object.
Now at the end of the data, you will have to attach buffer objects to the VAO when it comes time to render (via glBindVertexBuffer(s)). And the VAO will store the buffers it was last set with. But you should essentially ignore this. Treat a VAO as if it were just the vertex format state, with buffer binding being something that you do right before you render with it.
And yes, having one VAO and multiple potential buffers that get read with that vertex format (or better yet, just one buffer with multiple mesh data in it that all share the same vertex format, with mesh selection being done via the baseVertex parameter of glDrawElementsBaseVertex) is the right way to go.
All that being said, you also should remember that all spheres are the same mesh. They may appear in different locations with different sizes, but that's just a matter of providing a different transform of a unit sphere. The only reason to use more than one sphere mesh is if you need different resolutions of spheres (more polygons vs. fewer).

Using Index buffers in DirectX 11; how does it know?

Let's say I create two vertex buffers, for two different meshes.
(I'm assuming creating separate buffers for separate meshes is how it's usually done)
Now, let's say I want to draw one of the meshes using an index buffer.
Looking at the book Practical Rendering and Computation with Direct3D 11 it doesnt seem like the creation of an index buffer in any way references a vertex buffer, so how does the index buffer know (during input assembly) what vertex buffer to act on?
I've done some googling without answers, which leads me to assume there's something obvious about it that I'm missing.
You are right, index buffers do not reference specific vertex buffers. During DrawIndexed active index buffer is used to supply indices into active vertex buffers (the ones you set using SetIndexBuffer/SetVertexBuffers).
Indeed, Index Buffers and Vertex Buffers are completely independent.
Index buffer will know about VertexBuffer at draw time (eg: when both as bound to the pipeline)
You can think of Index Buffer as a "Lookup Table", where you keep a list or elements indices to draw.
That also means you can attach two completely "logically unrelated" buffers to the pipeline and draw it, nothing will prevent you from doing that, but you will of course have some strange visual results.
Decoupling both has many advantages, here are a few examples:
You can reuse an index buffer (for example, two displaced grids with identical resolution can share the same index buffer). That can be a decent memory gain.
You can draw your Vertex buffer on it's own and do some processing per vertex (draw a point list for sprites for example, or apply skinning/displacement into a Stream Output buffer , then draw the resulting vertex buffer using DrawIndexed)
Both Vertex/Index buffers can also be bound as ByteAddressBuffer, so you can also process your geometry in Compute Shader, and build another optimized index buffer, with culled triangles for example, then process the Indexed Draw with the optimized buffer. Applying those culls in with indices instead of vertices is often faster than vertex, as you will move much less memory.
This is a niche case, but sometimes I have to draw a mesh as a set of triangles, but then draw as a set of lines (some form of wireframe). If as example, you take a single Box, you will not want to draw the diagonals as lines, so I have a shared Vertexbuffer with box vertices, then one IndexBuffer dedicated to draw triangle list, and another to draw line list. In case of large models, this can also be an effective memory gain.

What is the purpose of VAO in OpenGL

I'm starting to learn modern OpenGL, and as the title says, I just wanted to be sure of the purpose of VAO's in the rendering pipeline.
When rendering we use VBO to store datas, and then we use OpenGL functions like: glAttribe to say to the GPU that we are going to use this datas "in That way", like: the first 3 floats in the vertices that we passes through vbo are in fact positions, and the next 3 floats are colors etc... So then I readed that we need some VAO that stores the descriptions of the vertices but what's the goal there ?
Thanks in advance.
Vertex array objects store a set of buffer names (usually vertex and index buffers) to get vertex data from, as well as how the vertices are layed out in the vertex buffers.
Their main purpose is so that when you want to render a different model from a different set of buffers, instead of binding each buffer and then setting the vertex attribute formats each time, you just bind a different VAO, and all the buffers and attributes are set up for you.
Not only is this more convenient for the programmer, it reduces the number of OpenGL calls required and thus CPU usage, which can clear up a CPU bottleneck.

Drawing multiple models using multiple openGL VBO's

rather than post lots of code, I will phrase this question intuitively. I hope you understand what I am getting at.
I am making a game and in the code I have a model class that loads a model and sets up the VBO for it.
In the loading function, it generates a new VBO ID and loads the vertex data from a file into that buffer by binding it etc. (This works fine)
At the beginning of the program I make one model object and load a .obj file.
In the render function, I simply call the DrawArrays() function (with the neccessary extra stuff) and as I said, everything works fine as the model appears on the screen with no problems.
The problem however, is when I make two model objects at the beginning of the program and load a different .obj file into each one.
The issue is that when I play, only the second model is drawn on screen.
The problem is because I don't properly understand how VBO's work.
This is how I "think" VBO's work.
You can generate as many VBO ID's as you want.
You can bind each ID to make it the active buffer.
You can load vertex data into that buffer.
You now effectively have many different ID's each referring to a "set of vertex data".
By calling the DrawArray function it draws every buffer you have generated (effectively displaying all your models on screen)
Now I know this is wrong because in my limited understanding I can't see how I could turn models on/off. But no matter how many tutorials I look at I can't answer this question because they all focus on displaying that ONE FIRST VBO, which incidentally I can do.
So... if that made sense, could anyone enlighten me?
You're doing it mostly right, but the drawing command does not draw every buffer you have generated. If you want to draw 2 objects with data in different buffers, you'll have to issue 2 drawing commands.
Let me explain, based on how I do it:
You create buffers using glGenBuffers. Before you can use a buffer, you have to "bind" it, that is, make it active using glBindBuffer. When a buffer is active, you can do stuff with it (populate with data for example). Note that there are different bind targets, so you can, for example, have one active array buffer (for vertex data) and one active element array buffer (for index data).
When you want to draw stuff, you have to specify what exactly you want to draw (I'll assume now that you're using your own shader) Usually you specify at least your vertex data and index data. To do this, you first bind your buffer with your vertex data (as an array buffer), then call glVertexAttribPointer with you attribute id. This tells OpenGL that the bound buffer is now the source for the current attribute. Then, you bind your index buffer as the element array buffer and call glDrawElements (not sure how glDrawArrays would work here).
You cannot use multiple VBOs at once for the same attribute - the call to bind another buffer just overwrites the previous call and makes the second buffer active, which, I assume, is why only your second object gets drawn.
You can also use VAOs to simplify the drawing - basically you assign an id to binding commands, then you can execute them with one line (think display lists).
So, my loading (of one object) looks like this:
Create buffers (let's say buffer 1 is for vertex data, buffer 2 for index data).
Bind buffer 1 as an array buffer.
Fill buffer with vertex data.
Repeat 2 and 3 for buffer 2 as element array buffer and index data, respectively.
And my drawing (again, of one object):
Bind buffer 1 as an array buffer.
Use glVertexAttribPointer to specify that this buffer goes to a specific attribute.
Bind buffer 2 as an element array buffer.
Call DrawElements
For the second object, you have to repeat everything.
Follow these steps to render your models, assuming model1 vertices are stored in vertexBuffer and model2 vertices in vertexBuffer2:
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, <no. of vert>);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer2);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, <no. of vert>);
//swap buffers depending on your windowing toolkit
Make sure to not perform glClear in the middle of the two model renders.
All the best!
what you want to do is set the vertexArrayAttribute back to the first object data before each call to drawArrays
the draw arrays function uses the bindings stored in the VAO to find the data needed to render everything
so to render 2 models you create a second VAO bind it and call glVertexAttribPointer as needed. for drawing you bind the VAO and call drawArrays for each model

OpenGL vertex array pointers, different buffers per component

A bit of context :
I'm working on a GPU emulator (the NV2A if you want to know) at the push-buffer level, and I'm trying to implement the drawing using OpenGL. The GPU commands that I have to emulate contain separate pointers for each vertex component (so positions are in an entirely different memory address than fog coordinates, colors, texture coordinates, etc.)
Other data, like vertex component size, type and stride are also present in the push-buffer, but those are not really relevant to this question.
I've been reading about Vertex Array Objects, but as far as my tests go, the pointers you can set with glVertexAttribPointer should all be relative to a Vertex Buffer Object - something I would like to avoid, as I've already got a copy of the data in memory.
The question :
Is it possible in OpenGL to draw vertices using separate pointers (not managed by any OpenGL API) per vertex component? And how would the code look like, roughy?
PS: Since I'm emulating a GPU, I have to take vertex shader programs into account too. I haven't worked on these yet, so any suggestion on that is welcome too. TIA!
You don't need to use VBOs, glVertexAttribPointer takes a normal CPU-pointer if no VBO is bound (you can call glBindBuffer(GL_ARRAY_BUFFER, 0) to make sure). And yes, you can set up one address per attribute stream.