使用openGL和cocos2D绘制大量线条的最佳方法是什么?

What is the best way to draw a large number of lines using openGL and cocos2D?(使用openGL和cocos2D绘制大量线条的最佳方法是什么?)

本文介绍了使用openGL和cocos2D绘制大量线条的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列代表用于绘制网格的线的二维顶点.大约有 900 条线段要绘制(网格使用弹簧物理来扭曲,这就是为什么我不只为每一行和每一列画一条线).cocos2D 内置了一个 ccDrawLine 函数,可以很好地绘制,但我认为这可能效率低下,因为它为每个线段调用 glDrawArrays.如何高效地绘制大量线段?作为奖励,请推荐一个使用 OpenGL 的良好 2D 绘图实践来源.

I have a series of 2d vertices that represent the lines used to draw a grid. There are about 900 line segments to draw (the grid uses spring physics to distort, which is why I don't just draw a single line for each row and col). cocos2D has a ccDrawLine function built in, which draws fine, but I think this may be inefficient since it is calling glDrawArrays for each line segment. How do you efficiently draw a large number of line segments? As a bonus, please recommend a source of good 2D drawing practices with openGL.

推荐答案

OpenGL 中的高效绘图意味着发送最少的信息和尽可能少的信息批次.与所有事情一样,这取决于情况,您应该尝试各种事情并针对您的情况进行基准测试.但是作为经验法则,最有效的方法(如果顶点是静止的)是将顶点存储在卡片上一次(在缓冲区中)并渲染多次,其次最好(当它使sense) 是使用几何着色器生成卡片上的大多数顶点,次优是一次发送所有顶点,次优是批量发送,最后最坏的是一次发送一个.

Efficient drawing in OpenGL means sending the least information, and fewest batches of information possible. As with everything, it depends on the situation and you should try various things and benchmark for your situation. But as a rule of thumb, the most efficient way (if the vertices are stationary) is to store the vertices on the card once (in a buffer) and render many times, next best (when it makes sense) is to use a geometry shader to generate most vertices on the card, next best is to send all vertices at once, next best is to send batches, and finally the worst is to do one at a time.

900 确实不是很多,听起来缓冲区或着色器在这种情况下没有意义.

900 really isn't very many, and it sounds like a buffer or shader wouldn't make sense in this context.

要批量发送,你需要把你的顶点放在顺序内存中,例如:

To send in a batch, you need to put your vertices in sequential memory, such as:

float coords[] = { 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0 };

(即 x1、y1、x2、y2 等.您可能想要 malloc 一些内存,以便它可以是可变长度)

(That's x1, y1, x2, y2, etc. You probably want to malloc some memory so that it can be variable length)

然后你将它发送到 OpenGL 并渲染它:

Then you send it to OpenGL and render it:

glVertexPointer( 2, GL_FLOAT, 0, coords ); // 2 = dimensions
glDrawArrays( GL_LINES, 0, 4 ); // 4 = number of points, => 2 lines

GL_LINES 将从 1 到 2、3 到 4 等绘制一条线.还有很多其他选项.记忆力可以稍微宽松一点;如果需要,请查看 stride 参数(上面的 0).这是文档:

GL_LINES will draw a line from 1 to 2, 3 to 4, etc. there are a lot of other options. You can be a bit looser with the memory; take a look at the stride parameter (0 above) if you need to. Here's the documentation:

http://www.opengl.org/sdk/docs/man2/xhtml/glVertexPointer.xml

http://www.opengl.org/sdk/docs/man2/xhtml/glDrawArrays.xml

这篇关于使用openGL和cocos2D绘制大量线条的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用openGL和cocos2D绘制大量线条的最佳方法是什么?

基础教程推荐