Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing multiple models using multiple openGL VBO's

Tags:

opengl

vbo

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?

like image 820
user3263338 Avatar asked Feb 02 '14 17:02

user3263338


3 Answers

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!

like image 136
user3787572 Avatar answered Oct 30 '22 19:10

user3787572


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

like image 42
ratchet freak Avatar answered Oct 30 '22 21:10

ratchet freak


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:

  1. Create buffers (let's say buffer 1 is for vertex data, buffer 2 for index data).
  2. Bind buffer 1 as an array buffer.
  3. Fill buffer with vertex data.
  4. Repeat 2 and 3 for buffer 2 as element array buffer and index data, respectively.

And my drawing (again, of one object):

  1. Bind buffer 1 as an array buffer.
  2. Use glVertexAttribPointer to specify that this buffer goes to a specific attribute.
  3. Bind buffer 2 as an element array buffer.
  4. Call DrawElements

For the second object, you have to repeat everything.

like image 12
vesan Avatar answered Oct 30 '22 19:10

vesan