0

我有一个金字塔,它有 5 个顶点和 18 个索引。因为我想为每个面添加法线,所以我刚刚找到了每个顶点法线的解决方案。这意味着我不能使用索引来定义我的金字塔我需要有 18 个顶点(空间中同一点的相同顶点的 3 倍)。

必须有一个解决方案,不是在顶点基础上而是在索引基础上使用法线。

一些代码(javascript):

var vertices = [
    -half, -half,  half, // 0 front left
     half, -half,  half, // 1 front right
     half, -half, -half, // 2 back right
    -half, -half, -half, // 3 back left
      0.0,  Math.sqrt((size * size) - (2 * (half * half))) - half,   0.0  // 4 top
];

var vertexNormals = [
    // front face
     normaleFront[0],  normaleFront[1],  normaleFront[2],
     normaleFront[0],  normaleFront[1],  normaleFront[2],
     normaleFront[0],  normaleFront[1],  normaleFront[2],

    // back face
     normaleBack[0],  normaleBack[1],  normaleBack[2],
     normaleBack[0],  normaleBack[1],  normaleBack[2],
     normaleBack[0],  normaleBack[1],  normaleBack[2],

    // left face
     normaleLeft[0],  normaleLeft[1],  normaleLeft[2],
     normaleLeft[0],  normaleLeft[1],  normaleLeft[2],
     normaleLeft[0],  normaleLeft[1],  normaleLeft[2],

    // right face
     normaleRight[0],  normaleRight[1],  normaleRight[2],
     normaleRight[0],  normaleRight[1],  normaleRight[2],
     normaleRight[0],  normaleRight[1],  normaleRight[2],

    // bottom face
     0.0, -1.0, 0.0,
     0.0, -1.0, 0.0,
     0.0, -1.0, 0.0,
     0.0, -1.0, 0.0,
     0.0, -1.0, 0.0,
     0.0, -1.0, 0.0,
];

var pyramidVertexIndices = [
    0, 1, 4, // Front face
    2, 3, 4, // Back face
    3, 0, 4, // Left face
    1, 2, 4, // Right face
    0, 1, 2,   2, 3, 0, // Bottom face
];
4

1 回答 1

1

金字塔的每个顶点都有三个不同的法线,具体取决于它属于哪个面。因此,您需要将每个顶点传递 3 次,每次使用不同的法线,才能使用glDrawElements.

或者你可以打电话

// Face 1
glNormal
glVertex
glVertex
glVertex

// Face 2 glNormal glVertex glVertex glVertex

// ...

于 2011-01-02T22:37:12.460 回答