views:

414

answers:

2

Hello. 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.

+1  A: 

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.

tersyon
Thank you. I've managed to get VBOS working for some line code but not for texture drawing. I haven't got a clue how to do it. Everything I tried failed. I guess I need another question.
Matthew Mitchell
Do you see your objects on where you want them to be? Is it only the in-correct texturing of your models? If you see your objects in the correct position, just keep in mind that texture coordinate values are just like vertex position values. Start with something easy (do not modify texture coordinates using glTranslate etc in texture matrix mode), then try to extend it to achieve what you finally want to see.
tersyon
At current it's rendering in the right place, it just isn't being rendered properly. It fails for both GL_QUADS and GL_TRIANGLES. I'll make a new question with my VBO code.Is adding colour information to the VBO that important? Is "glColor" going to be much of a problem?Thank you for your answer and help.
Matthew Mitchell
You cannot mix glColor and like with vertex buffer based draw commands (glDrawElements and glDrawArrays basically). You should use glColorPointer, glVertexPointer and relatex methods do set pre-defined vertex semantics. If your texture coordinates are OK and if you still do not observe any texturing, check if texturing is enabled, and also lighting can affect the result. Once you can clearly see a texture tiled on your mesh, modifications will be easier.
tersyon
Quads are deprecated since four vertices could not be on the same plane, while three vertices (a triangle) always defines a plane.
Luca
A: 

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.

karx11erx