多维数据集的OpenGl顶点顺序

时间:2014-03-15 21:01:05

标签: opengl-es 3d cube vertices

我一直在研究OpenGL并潜入3D世界。这个问题一般与OpenGL有关,但我一直在使用WebGL(如果我相信的话,它就是OpenGL ES)。我在理解如何绘制立方体时遇到了问题。我知道要绘制四边形,你可以在(默认)CCW顺序中创建两个三角形,如

1       0 
|‾ ‾ ‾ ‾|
|       |    Indices = 0,1,2, 0,2,3 (2 triangles one face)
|       |
|_ _ _ _|
2       3  

然而,我在理解绘制立方体的最佳方法时遇到了一些麻烦。有没有一种特定的方式我应该绘制立方体?例如,正在 front-> right-> bottom-> left-> top-> back 绘制最佳顺序的面部,或者是其他方式。我是否逆时针绘制立方体面或某些东西?我只需要了解我可以表示模型/多维数据集的不同方式以及原因。

1 个答案:

答案 0 :(得分:1)

可以使用2个三角形条带来完成,但是条带通常用索引三角形替换以获得更复杂的几何体,因为当您需要多个条带时它们会变得更有效。我认为如果使用退化三角形来连接两者,也可以使用单个条带完成,但对于较大的几何体,索引通常更好。四元组只是转换成三角形。

只有在启用背面剔除时,缠绕方向才很重要。此外,当您为照明添加纹理坐标和曲面法线时,事情变得更加复杂。法线可用于使立方体看起来具有刻面或平滑阴影,这对于较大的模型(如球体)来说确实如此。这是我多年前为OpenGL ES 2.0编写的教程:

/******************************************************************************

  Function      DrawCubeSmooth

  Return        None

  Description   Draw a cube using Vertex and NormalsPerVertex Arrays and
                glDrawArrays with two triangle strips.  Because normals are
                supplied per vertex, all the triangles will be smooth shaded.
                Triangle strips are used instead of an index array.  The first
                strip is texture mapped but the second strip is not.

******************************************************************************/

void Cube2::DrawCubeSmooth(void)
{
    static GLfloat Vertices[16][3] =
    {   // x     y     z
        {-1.0, -1.0,  1.0}, // 1  left    First Strip
        {-1.0,  1.0,  1.0}, // 3
        {-1.0, -1.0, -1.0}, // 0
        {-1.0,  1.0, -1.0}, // 2
        { 1.0, -1.0, -1.0}, // 4  back
        { 1.0,  1.0, -1.0}, // 6
        { 1.0, -1.0,  1.0}, // 5  right
        { 1.0,  1.0,  1.0}, // 7
        { 1.0,  1.0, -1.0}, // 6  top     Second Strip
        {-1.0,  1.0, -1.0}, // 2
        { 1.0,  1.0,  1.0}, // 7
        {-1.0,  1.0,  1.0}, // 3
        { 1.0, -1.0,  1.0}, // 5  front
        {-1.0, -1.0,  1.0}, // 1
        { 1.0, -1.0, -1.0}, // 4  bottom
        {-1.0, -1.0, -1.0}  // 0
    };
    static GLfloat NormalsPerVertex[16][3] =    // One normal per vertex.
    {   // x     y     z
        {-0.5, -0.5,  0.5}, // 1  left          First Strip
        {-0.5,  0.5,  0.5}, // 3
        {-0.5, -0.5, -0.5}, // 0
        {-0.5,  0.5, -0.5}, // 2
        { 0.5, -0.5, -0.5}, // 4  back
        { 0.5,  0.5, -0.5}, // 6
        { 0.5, -0.5,  0.5}, // 5  right
        { 0.5,  0.5,  0.5}, // 7
        { 0.5,  0.5, -0.5}, // 6  top           Second Strip
        {-0.5,  0.5, -0.5}, // 2
        { 0.5,  0.5,  0.5}, // 7
        {-0.5,  0.5,  0.5}, // 3
        { 0.5, -0.5,  0.5}, // 5  front
        {-0.5, -0.5,  0.5}, // 1
        { 0.5, -0.5, -0.5}, // 4  bottom
        {-0.5, -0.5, -0.5}  // 0
    };
    static GLfloat TexCoords[8][2] =
    {   // x   y
        {0.0, 1.0}, // 1  left                  First Strip
        {1.0, 1.0}, // 3
        {0.0, 0.0}, // 0
        {1.0, 0.0}, // 2
        {0.0, 1.0}, // 4  back
        {1.0, 1.0}, // 6
        {0.0, 0.0}, // 5  right
        {1.0, 0.0}  // 7
    };

    glEnableVertexAttribArray(VERTEX_ARRAY);
    glEnableVertexAttribArray(NORMAL_ARRAY);
    glEnableVertexAttribArray(TEXCOORD_ARRAY);

    // Set pointers to the arrays
    glVertexAttribPointer(VERTEX_ARRAY, 3, GL_FLOAT, GL_FALSE, 0, Vertices);
    glVertexAttribPointer(NORMAL_ARRAY, 3, GL_FLOAT, GL_FALSE, 0, NormalsPerVertex);
    glVertexAttribPointer(TEXCOORD_ARRAY, 2, GL_FLOAT, GL_FALSE, 0, TexCoords);

    // Draw first triangle strip with texture map
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 8);

    // Draw second triangle strip without texture map
    glDisableVertexAttribArray(TEXCOORD_ARRAY);
    glDrawArrays(GL_TRIANGLE_STRIP, 8, 8);

    glDisableVertexAttribArray(VERTEX_ARRAY);
    glDisableVertexAttribArray(NORMAL_ARRAY);
};

我希望这会有所帮助。