Normali per indice?
-
14-10-2019 - |
Domanda
Ho una piramide che ha 5 vertice e 18 indici. Come Voglio aggiungere normali per ogni faccia ho appena trovato la soluzione per le normali per ogni vertice. Ciò significa che non posso usare indici per definire la piramide devo avere 18 vertice (e 3 volte lo stesso vertice per lo stesso punto nello spazio).
Ci deve essere una soluzione per uso normali non sulla base di vertice ma sulla base di indice.
Alcuni codice (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
];
Soluzione
Ogni vertice di una piramide ha tre differenti normali, a seconda di quale faccia appartiene. Quindi è necessario passare ciascuno dei vertici tre volte, ogni volta con un diverso normale, ad uso glDrawElements
.
In alternativa si potrebbe chiamare
// Face 1
glNormal
glVertex
glVertex
glVertex
// Face 2
glNormal
glVertex
glVertex
glVertex
// ...