Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL fast texture drawing with vertex buffer objects. Is this the way to do it?

I am making a 2D game with OpenGL. I would like to speed up my texture drawing by using VBOs.

Currently I am using the immediate mode. I am generating my own coordinates when I rotate and scale a texture. I also have the functionality of rounding the corners of a texture, using the polygon primitive to draw those.

I was thinking, would it be fastest to make a VBO with vertices for the sides of the texture with no offset included so I can then use glTranslate, glScale and glRotate to move the drawing position for my texture. Then I can use the same VBO with no changes to draw the texture each time. I could only change the VBO when I need to add coordinates for the rounded corners.

Is that the best way to do this? What things should I look out for while doing it? Is it really fastest to use GL_TRIANGLES instead of GL_QUADS in modern graphics cards?

Thank you for any answer.

like image 406
Matthew Mitchell Avatar asked May 31 '10 15:05

Matthew Mitchell


2 Answers

For better performance, you should not use immediate mode rendering, but instead use vertex buffer (on CPU or GPU) and draw using glDrawArrays etc like methods. Scaling/rotating the texture coordinates by modifying texture matrix multiplier is not a performance issue, you just set some small values for the entire mesh. The most important bottleneck here is to transfer per-vertex data (such as color, position AND texture coordinate(s)) to GPU, and that's what vertex buffer objects on the GPU can greatly help.

If the mesh that you want to draw to screen is static, that is, you set it once and do not need to modify (parts of) it later, you can generate the texture coordinates for the entire mesh, including all corners) only once. You can later modify texture coordinates for the entire mesh using texture matrix multiplier. However, if you want to modify different parts of the mesh using different transformations, you will have to divide the mesh into parts. From what you have described, I understand that you want to draw a rounded-corner rectangle which holds a picture inside, which can be accomplished using a single static mesh (a list of per-vertex data).

Lastly, GL_QUADS are also deprecated in modern OpenGL specifications. They do not offer much functionality anyway, so you should switch QUADS to triangles or triangle strips/fans.

like image 126
AdilYalcin Avatar answered Sep 22 '22 01:09

AdilYalcin


If you want to draw a textured polygon using VBOs, you need to create buffers not only for the vertices, but also for the texture coordinates (one UV coordinate per vertex) and for the vertex colors if you want to do vertex lighting (use glColor instead if not). Use glClientActiveTexture(GL_TEXTUREn) before binding the intended texture. Don't forget to use glEnableClientState() for each buffer type you are using before issuing the render call.

like image 42
Razzupaltuff Avatar answered Sep 24 '22 01:09

Razzupaltuff