I must be doing something wrong. I’m trying to explore how to manipulate Meshes at the vertex level, so I wrote up a quick debugging method to print out the position and normals of vertices in a Mesh (using a Box as an example), but I’m getting weird results (or I don’t understand vertex normals).
Here’s the code:
[java]
Mesh m = new Box(1f,1f,1f);
VertexBuffer pb = m.getBuffer(Type.Position);
VertexBuffer nb = m.getBuffer(Type.Normal);
IndexBuffer ib = m.getIndicesAsList();
int tCount = ib.size() / 3; //triangle count
FloatBuffer pfb = (FloatBuffer) pb.getData();
FloatBuffer pnb = (FloatBuffer) nb.getData();
for (int i = 0; i < tCount; i++) {
// acquire triangle's vertex indices
System.out.println("p"+ib.get(i*3)+": {"+pfb.get(ib.get(i*3))+", "+pfb.get(ib.get(i*3+1))+", "+pfb.get(ib.get(i*3+2))+"}, n"+ib.get(i*3)+": {"+pnb.get(ib.get(i*3))+", "+pnb.get(ib.get(i*3+1))+", "+pnb.get(ib.get(i*3+2))+"}");
}
[/java]
And here are the results:
[java]
p2: {-1.0, -1.0, -1.0}, n2: {-1.0, 0.0, 0.0}
p3: {1.0, -1.0, -1.0}, n3: {0.0, -1.0, 0.0}
p6: {1.0, -1.0, -1.0}, n6: {0.0, -1.0, 0.0}
p7: {1.0, 1.0, -1.0}, n7: {0.0, 0.0, 0.0}
p10: {1.0, -1.0, -1.0}, n10: {0.0, 0.0, -1.0}
p11: {-1.0, 1.0, -1.0}, n11: {-1.0, 0.0, -1.0}
p14: {-1.0, -1.0, 1.0}, n14: {0.0, 0.0, 1.0}
p15: {1.0, -1.0, 1.0}, n15: {1.0, 0.0, 1.0}
p18: {1.0, 1.0, -1.0}, n18: {1.0, 0.0, 0.0}
p19: {1.0, 1.0, -1.0}, n19: {0.0, 1.0, 0.0}
p22: {1.0, 1.0, 1.0}, n22: {0.0, 1.0, 0.0}
p23: {-1.0, 1.0, 1.0}, n23: {0.0, 0.0, 0.0}
[/java]
The first thing I noticed is that not all vertices are referenced from the indexBuffer. This may be normal, since when I ignore the indexBuffer, I get a lot of duplicate vertices.
Second thing is, according to my understanding of vertex normals (which might be wrong), the normal for a vertex on this cube, centered at origin, should essentially be the normalized position vector. So for a position of (1,1,1), the normal would be sqrt(3) / 3 = (0.5773502691896258, 0.5773502691896258, 0.5773502691896258) or thereabouts, and not… whatever it is I'm getting out of the normal buffer (looks like face normals?).
So: Am I misunderstanding Vertex normals, using the indexbuffer incorrectly, or what?