绘制多个相同对象的最有效方法?
我想用许多立方体制作一个游戏,并计划将其放在移动平台上,并使用 webgl 放在网络上。我的问题是,当我对每个立方体进行绘图元素调用时,帧速率会受到影响。有没有办法可以对 opengl es 进行一次绘制调用来绘制它们?立方体之间的唯一区别是位置和颜色。
I would like to make a game out of many cubes and am planning on putting it on mobile platforms and also on the web using webgl. My problem is when I make a drawelements call per cube I take a hit on the frame rate. Is there a way I can make a single draw call to opengl es to draw them? The only difference between the cubes would be position and color.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我自己也遇到了同样的问题。我昨天问了类似的问题并自己回答了。看看:
在 OpenGL ES 中绘制的高效方法
你想要的使用 gl.bufferData 创建顶点缓冲区并将其上传到显卡内存一次。然后每次执行 gl.drawElements 或 gl.drawArrays 时都使用对该缓冲区的引用。在 3D 场景的内容下次更改之前,您可以使用上传到显卡内存中的此缓冲区。
I ran into same problem myself. I asked similar question and answered it myself yesterday. Take a look at:
Efficient way of drawing in OpenGL ES
You want to create your vertex buffers and upload them to graphics card memory only once using gl.bufferData. Then use reference to that buffer each time you do gl.drawElements or gl.drawArrays. Until the next time the content of your 3D scene changes, you can use this buffer that is uploaded in graphics card memory.
遵循 UITableView 模型如何使单元出队。我将构建一个对象来跟踪您绘制的对象并将它们链接到标识符。然后您可以简单地使用所述标识符将它们出列。如果您知道要绘制同一对象的多个版本,请使用该对象来最大限度地减少渲染/分配。
Follow the model of UITableView in how they dequeue cells. I would build an object that keeps track of objects you've drawn and links them to an Identifier. Then you can simply dequeue them with said identifier. If you know you're going to draw many versions of the same object, use that object to minimize rendering/allocations.
如果您在 glDrawElements() 中使用顶点数组,我建议您改用顶点缓冲区对象。这将存储数据服务器端(在 GRAM 中)而不是客户端(在系统 RAM 中)。这样,您就可以使用更少的 CPU<->GPU 数据传输开销来调用 glDrawElements()。
或者,您可以将多维数据集存储在显示列表中。这样您就可以使用 glTranlate() 来移动立方体,然后只需调用显示列表来渲染它。使用显示列表的唯一警告是,您在显示列表中执行的任何操作都是不可变的;如果不完全重新编译它,则无法更改显示列表中的调用。
If you're using Vertex Arrays for your glDrawElements(), I'd suggest instead using Vertex Buffer Objects. This will store the data server side (in GRAM) instead of client side (in system RAM). That way you can make glDrawElements() calls with much less CPU<->GPU data transfer overhead.
Alternatively, you can store you're cubes in display lists. This way you could use glTranlate() to move a cube around and then just call the display list to render it. The only caveat to using display lists is that whatever you do in the display list is immutable; you can't change the calls in the display list without completely recompiling it.