使用OpenGL ES 2.0渲染多个对象 [英] Render multiple objects with OpenGL ES 2.0

查看:161
本文介绍了使用OpenGL ES 2.0渲染多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习OpenGL ES 2.0以进行一些iPhone游戏开发.我已经通读了多个教程和一些OpenGL ES 2.0规范.我所看到的所有示例都创建了一个网格,将其加载到顶点缓冲区中,然后进行渲染(具有预期的平移,旋转,渐变等).

I am trying to learn OpenGL ES 2.0 to do some iPhone game development. I have read through multiple tutorials and some of the OpenGL ES 2.0 spec. All of the examples I have seen have created a single mesh, loaded it into a vertex buffer and then rendered it (with the expected translation, rotation, gradient, etc.)

我的问题是:如何在场景中渲染具有不同网格并独立移动的多个对象?例如,如果我有一辆汽车和摩托车,是否可以创建2个顶点缓冲区并为每个渲染调用保留两个顶点的网格数据,然后为每个对象的着色器发送不同的矩阵?还是我需要以某种方式平移网格,然后将它们组合成单个网格,以便可以在一次通过中渲染它们?我正在寻找更多的高级策略/程序结构,而不是代码示例.我认为我对这种方法的工作方式有错误的认识.

My question is this: how do you render multiple objects in your scene that have different meshes and are moving independently? If I have a car and a motorcycle for example, can I create 2 vertex buffers and keep the mesh data for both around for each render call, and then just send in different matrices for the shader for each object? Or do I need to somehow translate the meshes and then combine them into a single mesh so that they can be rendered in one pass? I'm looking for more of the high-level strategy / program structure rather than code examples. I think I just have the wrong mental modal of how this works.

谢谢!

推荐答案

您可以为不同的对象维护单独的顶点/索引缓冲区.例如,您可能有一个RenderedObject类,并且每个实例都有自己的顶点缓冲区.一个RenderedObject可能来自房屋网格的顶点,一个可能来自角色网格的顶点,等等.

You maintain separate vertex/index buffers for different objects, yes. For example, you might have a RenderedObject class, and each instance would have it's own vertex buffer. One RenderedObject might take it's vertices from a house mesh, one might come from a character mesh, etc.

在渲染期间,可以为要使用的顶点缓冲区设置适当的变换/旋转/阴影,例如:

During rendering you set the appropriate transform/rotation/shading for the vertex buffer you're working with, perhaps something like:

void RenderedObject::render()
{
    ...
    //set textures/shaders/transformations

    glBindBuffer(GL_ARRAY_BUFFER, bufferID);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexCount);
    ...
}

正如在其他答案中提到的那样,bufferID只是GLuint,而不是缓冲区的全部内容.如果您需要更多有关创建顶点缓冲区并将其填充数据的详细信息,我也很乐意添加这些细节.

As mentioned in there other answer, the bufferID is just a GLuint not the entire contents of the buffer. If you need more details on creating vertex buffers and filling them with data, I'm happy to add those as well.

这篇关于使用OpenGL ES 2.0渲染多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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