使用openGL和cocos2D绘制大量行的最好方法是什么? [英] What is the best way to draw a large number of lines using openGL and cocos2D?

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

问题描述

我有一系列的2d顶点,代表用来绘制网格的线。有大约900个线段来绘制(网格使用弹簧物理学来扭曲,这就是为什么我不只是为每一行和col绘制一条线)。 cocos2D有一个内置的ccDrawLine函数,它绘制精细,但我认为这可能是低效的,因为它为每个线段调用glDrawArrays。如何有效地绘制大量的线段?

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中的高效绘图意味着发送最少的信息,以及可能的最少批次的信息。和一切一样,它取决于情况,你应该尝试各种事情和基准的情况。但是根据经验法则,最有效的方式(如果顶点是固定的)是将卡上的顶点存储在卡上一次(在缓冲器中)并且呈现多次,次最佳(当它做出时)感觉)是使用几何着色器来生成卡上的大多数顶点,其次最好是一次发送所有顶点,最好是发送批次,最后最糟糕的是一次做一个。

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绘制大量行的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆