简体   繁体   English

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

[英]What is the best way to draw a large number of lines using openGL and cocos2D?

I have a series of 2d vertices that represent the lines used to draw a grid. 我有一系列2d顶点,表示用于绘制网格的线条。 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). 有大约900个线段要绘制(网格使用弹簧物理扭曲,这就是为什么我不只为每一行和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. cocos2D有一个内置的ccDrawLine函数,它绘制得很好,但我认为这可能是低效的,因为它为每个线段调用glDrawArrays。 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推荐一个良好的2D绘图实践来源。

Efficient drawing in OpenGL means sending the least information, and fewest batches of information possible. OpenGL中的高效绘图意味着发送最少的信息,并尽可能少地发送信息。 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 really isn't very many, and it sounds like a buffer or shader wouldn't make sense in this context. 900真的不是很多,听起来像缓冲区或着色器在这种情况下是没有意义的。

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 };

(That's x1, y1, x2, y2, etc. You probably want to malloc some memory so that it can be variable length) (那是x1,y1,x2,y2等。你可能想要一些内存,以便它可以是可变长度)

Then you send it to OpenGL and render it: 然后将它发送到OpenGL并渲染它:

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

GL_LINES will draw a line from 1 to 2, 3 to 4, etc. there are a lot of other options. GL_LINES将绘制一条从1到2,3到4等的线。还有很多其他选项。 You can be a bit looser with the memory; 你可以对记忆稍微宽松一点; take a look at the stride parameter (0 above) if you need to. 如果需要,请查看stride参数(上面的0)。 Here's the documentation: 这是文档:

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

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

Cocos2d 2.0具有CCDrawNode ,它批量绘制图元(线条,圆形,......)。

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

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