简体   繁体   English

带有顶点缓冲对象的OpenGL快速纹理绘制。 这是这样做的方法吗?

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

I am making a 2D game with OpenGL. 我正在用OpenGL制作2D游戏。 I would like to speed up my texture drawing by using VBOs. 我想通过使用VBO来加速纹理绘制。

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. 我当时在想,用VBO制作具有纹理边缘的顶点且不包含偏移的VBO最快吗,所以我可以使用glTranslate,glScale和glRotate移动纹理的绘制位置。 Then I can use the same VBO with no changes to draw the texture each time. 然后,我可以使用相同的VBO,而无需更改即可每次绘制纹理。 I could only change the VBO when I need to add coordinates for the rounded corners. 我仅在需要为圆角添加坐标时才能更改VBO。

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? 在现代图形卡中使用GL_TRIANGLES而不是GL_QUADS真的最快吗?

Thank you for any answer. 谢谢您的回答。

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. 为了获得更好的性能,您不应使用立即模式渲染,而应使用顶点缓冲区(在CPU或GPU上)并使用glDrawArrays等类似方法进行绘制。 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. 这里最重要的瓶颈是将每个顶点的数据(例如颜色,位置和纹理坐标)传输到GPU,这就是GPU上的顶点缓冲对象可以提供极大帮助的地方。

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. 最后,GL_QUADS在现代OpenGL规范中也已弃用。 They do not offer much functionality anyway, so you should switch QUADS to triangles or triangle strips/fans. 无论如何,它们并没有提供太多功能,因此您应该将QUADS切换为三角形或三角形带/风扇。

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). 如果要使用VBO绘制带纹理的多边形,则不仅要为顶点创建缓冲区,还需要为纹理坐标(每个顶点一个UV坐标)以及顶点颜色(如果要进行顶点照明)创建缓冲区(使用glColor相反,如果没有)。 Use glClientActiveTexture(GL_TEXTUREn) before binding the intended texture. 绑定所需的纹理之前,请使用glClientActiveTexture(GL_TEXTUREn)。 Don't forget to use glEnableClientState() for each buffer type you are using before issuing the render call. 发出渲染调用之前,请不要忘记对要使用的每种缓冲区类型使用glEnableClientState()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM