有人可以解释如何使用 glDrawElements (iPhone) 吗?

发布于 2024-09-06 19:31:57 字数 1351 浏览 2 评论 0原文

抱歉,重复了,但我已经在谷歌上搜索了几个小时,没有任何结果。

我有从转换器导出的简单立方体的(优化)数据:

// 8 Verticies
// 4 Texture Coordinates
// 6 Normals
// 12 Triangles

static GLshort cubeFace_indicies[12][9] = {
    // Box001
    {2,0,3 ,0,0,0 ,0,1,2 }, {1,3,0 ,0,0,0 ,3,2,1 }, {5,4,7 ,1,1,1 ,1,3,0 },
    {6,7,4 ,1,1,1 ,2,0,3 }, {1,0,5 ,2,2,2 ,1,3,0 }, {4,5,0 ,2,2,2 ,2,0,3 },
    {3,1,7 ,3,3,3 ,1,3,0 }, {5,7,1 ,3,3,3 ,2,0,3 }, {2,3,6 ,4,4,4 ,1,3,0 },
    {7,6,3 ,4,4,4 ,2,0,3 }, {0,2,4 ,5,5,5 ,1,3,0 }, {6,4,2 ,5,5,5 ,2,0,3 }
};
static GLfloat cubeVertices [8][3] = {
    {-100.0f,-100.0f,-100.0f},{100.0f,-100.0f,-100.0f},{-100.0f,100.0f,-100.0f},
    {100.0f,100.0f,-100.0f},{-100.0f,-100.0f,100.0f},{100.0f,-100.0f,100.0f},
    {-100.0f,100.0f,100.0f},{100.0f,100.0f,100.0f}
};
static GLfloat cubeNormals [6][3] = {
    {0.0f,0.0f,1.0f},{0.0f,0.0f,-1.0f},{0.0f,1.0f,0.0f},
    {-1.0f,0.0f,0.0f},{0.0f,-1.0f,0.0f},{1.0f,0.0f,0.0f}
};
static GLfloat cubeTextures [4][2] = {
    {1.0f,2.0f},{1.0f,1.0f},{0.0f,2.0f},
    {0.0f,1.0f}
};

我设置了纹理,我想在屏幕上看到一些东西。我最近的绘图代码如下:

glTexCoordPointer(2, GL_FLOAT, 0, cubeTextures);
glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
glNormalPointer(GL_FLOAT, 0, cubeNormals);
glBindTexture(GL_TEXTURE_2D, textures[0]);

    glDrawElements(GL_TRIANGLES , 12, GL_SHORT, cubeFace_indicies);

Sorry for the duplicaiton, but I've been googlin' for hours now without any result.

I have this (optimized) data of a simple cube exported from a converter:

// 8 Verticies
// 4 Texture Coordinates
// 6 Normals
// 12 Triangles

static GLshort cubeFace_indicies[12][9] = {
    // Box001
    {2,0,3 ,0,0,0 ,0,1,2 }, {1,3,0 ,0,0,0 ,3,2,1 }, {5,4,7 ,1,1,1 ,1,3,0 },
    {6,7,4 ,1,1,1 ,2,0,3 }, {1,0,5 ,2,2,2 ,1,3,0 }, {4,5,0 ,2,2,2 ,2,0,3 },
    {3,1,7 ,3,3,3 ,1,3,0 }, {5,7,1 ,3,3,3 ,2,0,3 }, {2,3,6 ,4,4,4 ,1,3,0 },
    {7,6,3 ,4,4,4 ,2,0,3 }, {0,2,4 ,5,5,5 ,1,3,0 }, {6,4,2 ,5,5,5 ,2,0,3 }
};
static GLfloat cubeVertices [8][3] = {
    {-100.0f,-100.0f,-100.0f},{100.0f,-100.0f,-100.0f},{-100.0f,100.0f,-100.0f},
    {100.0f,100.0f,-100.0f},{-100.0f,-100.0f,100.0f},{100.0f,-100.0f,100.0f},
    {-100.0f,100.0f,100.0f},{100.0f,100.0f,100.0f}
};
static GLfloat cubeNormals [6][3] = {
    {0.0f,0.0f,1.0f},{0.0f,0.0f,-1.0f},{0.0f,1.0f,0.0f},
    {-1.0f,0.0f,0.0f},{0.0f,-1.0f,0.0f},{1.0f,0.0f,0.0f}
};
static GLfloat cubeTextures [4][2] = {
    {1.0f,2.0f},{1.0f,1.0f},{0.0f,2.0f},
    {0.0f,1.0f}
};

I have a texture set up, and I want to see something on the screen. My recent drawing code below:

glTexCoordPointer(2, GL_FLOAT, 0, cubeTextures);
glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
glNormalPointer(GL_FLOAT, 0, cubeNormals);
glBindTexture(GL_TEXTURE_2D, textures[0]);

    glDrawElements(GL_TRIANGLES , 12, GL_SHORT, cubeFace_indicies);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

战皆罪 2024-09-13 19:31:57

OpenGL ES(和常规 OpenGL)中的索引数据无法按照您使用的方式工作。您无需为每个顶点提供位置、法线和纹理坐标的单独索引,而是提供单个索引 i,OpenGL ES 从每个启用的顶点获取元素 i数组。这意味着您需要为您在当前表示中创建的所有顶点-法线-纹理坐标组合使用单独的索引,这可能需要稍微复制您的数据。

如果您的模型可以使用平面着色进行绘制(即在绘制之前调用 glShadeModel(GL_FLAT)),您也许可以不必指定较少的唯一点。在这种情况下,三角形中最后一个顶点的法线将用于整个三角形。当然,这需要仔细排序索引。

Index data in OpenGL ES (and regular OpenGL) doesn’t work the way you’re using it. Rather than providing separate indices per vertex for the position, normal, and texture coordinate, you provide a single index i, and OpenGL ES takes element i from each of the enabled vertex arrays. This means that you’ll need separate indices for all the vertex-normal-texcoord combinations you’ve created in your current representation, which may require duplicating your data a bit.

You might be able to get away with specifying fewer unique points if your model can be drawn with flat shading (i.e. calling glShadeModel(GL_FLAT) before drawing). In that case, the normal from the last vertex in a triangle will be used for the entire triangle. This requires careful ordering of your indices, of course.

臻嫒无言 2024-09-13 19:31:57

要使用glDrawArrays,您必须使用glEnableClientState(GL_VERTEX_ARRAY)启用GL_VERTEX_ARRAY状态,然后使用glVertexPointer(3, GL_FLOAT, 0, )创建每个顶点

to use glDrawArrays you must enable the GL_VERTEX_ARRAY state with glEnableClientState(GL_VERTEX_ARRAY) and then create each vertex point with glVertexPointer(3, GL_FLOAT, 0, )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文