【问题标题】:OpenGl Vertex Order for Cube立方体的OpenGL顶点顺序
【发布时间】:2014-03-15 21:01:05
【问题描述】:

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

1 0 |‾‾‾‾| | |指数 = 0,1,2, 0,2,3(一个面 2 个三角形) | | |_ _ _ _| 2 3

但是,我在理解绘制立方体的最佳方法时遇到了一些麻烦。有没有一种特定的方式来绘制立方体?例如,在 front->right->bottom->left->top->back 中绘制面部是最佳顺序还是其他方式。我是否也逆时针绘制立方体面或其他什么?我只需要了解我可以表示模型/立方体的不同方式以及原因。

【问题讨论】:

    标签: opengl-es 3d cube vertices


    【解决方案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);
    };
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-24
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多