[java] private void computeNormals() {
if (numOfObjects <= 0) {
return;
}
Vector3f vector1 = new Vector3f();
Vector3f vector2 = new Vector3f();
Vector3f vector3 = new Vector3f();
// Go through each of the objects to calculate their normals
for (int index = 0; index < numOfObjects; index++) {
// Get the current object
ASEObject object = objectList.get(index);
// Here we allocate all the memory we need to calculate the normals
Vector3f[] tempNormals = new Vector3f[object.faces.length];
Vector3f[] normals = new Vector3f[object.tm.getVertexCount()];
// Go though all of the faces of this object
for (int i = 0; i < object.faces.length; i++) {
BufferUtils.populateFromBuffer(vector1, object.tm.getVertexBuffer(), object.faces[i].vertIndex[0]);
BufferUtils.populateFromBuffer(vector2, object.tm.getVertexBuffer(), object.faces[i].vertIndex[1]);
BufferUtils.populateFromBuffer(vector3, object.tm.getVertexBuffer(), object.faces[i].vertIndex[2]);
vector1.subtractLocal(vector3);
tempNormals[i] = vector1.cross(vector3.subtract(vector2)).normalizeLocal();
}
Vector3f sum = new Vector3f();
int shared = 0;
for (int i = 0; i < object.tm.getVertexCount(); i++) {
for (int j = 0; j < object.faces.length; j++) {
if (object.faces[j].vertIndex[0] == i
|| object.faces[j].vertIndex[1] == i
|| object.faces[j].vertIndex[2] == i) {
sum.addLocal(tempNormals[j]);
shared++;
}
}
normals[i] = sum.divide((-shared)).normalizeLocal();
sum.zero(); // Reset the sum
shared = 0; // Reset the shared
}
object.tm.setNormalBuffer(BufferUtils.createFloatBuffer(normals));
}
}[/java]
There is a method above to compute the normals of an object(as well as a mesh object) for smooth shading. I think it belongs to jme and I need to change some to adapt this method to jme3. Can anyone help me on this issue?