Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to draw multiple objects in modern OpenGL?

Tags:

c++

opengl

I'm trying to use modern OpenGL and shaders, instead of the immediate mode I have been using so far. I recently learned about VBOs and VAO, and I'm still trying to get my head round them, but I know that a VBO takes an array of floats that are vertices, which it then passes to the GPU etc

What is the best way to draw multiple objects (which are all identical) but in different positions, using VBOs. Will I have to draw one, then modify the array passed in beforehand, and then draw it again and modify and draw and modify and so on... for all blocks in the screen every frame? Or is there a better way?

I'm trying to achieve this: http://imgur.com/cBgJ0sK

Any help is appreciated - I don't want to learn bad (deprecated, old) immediate mode habits, when I could be learning a more modern way!

like image 264
lbowes Avatar asked Oct 18 '22 19:10

lbowes


1 Answers

You should not modify the vertices in your program, that should be done in the shaders. For this, you will create a matrix that represents the transformation and will use that matrix in the vertex shader.

The main idea is:

You create a VAO holding the information of your VBO (vertices, normals, texture coordinates, tangent information, etc.)

Then, for every different object, you generate a model matrix that holds the information of the position, orientation and scale (and other homogeneous tranformations) and send that to your shader to make the transformations.

The idea is that you bind your VAO just once and then draw all the different objects just sending the information that change (model matrix, may be textures) and draw the objects.

To learn about how to use the model matrix, read tutorials like this: http://ogldev.atspace.co.uk/www/tutorial06/tutorial06.html

There are even better ways to do this, but you can start from here.

Other information that would be good for your case is using instancing. http://ogldev.atspace.co.uk/www/tutorial33/tutorial33.html

Later, you can move on indirect drawing for even better performance. Later...

like image 170
Orlando Aguilar Avatar answered Nov 15 '22 10:11

Orlando Aguilar