开发者

Could somebody explain how to use glDrawElements (iPhone)?

开发者 https://www.devze.com 2023-01-04 17:09 出处:网络
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:

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, cube开发者_JAVA技巧Textures);
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);


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.


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, )

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号