Hi all.
I’m stuck with calculating per vertex normals. I’ve checked this http://www.java-gaming.org/topics/reopened-calculating-normal-vectors/33838/view.html, but got no new information.
I render two objects (see screenshot), the cube is exported from blender and the other one is constructed manually from code. As you see, the blender cube is OK. Also, if I create the same object from blender and export it with normals, it’s also OK.
So I think, something wrong with my manual code. Screenshot: https://drive.google.com/open?id=1hsAFJMJSH9atgcxQNe6o_YyzXqgr2NYh
How triangles constructed: https://drive.google.com/open?id=1YrF6IiHIg9ieNZ1Us9pvKsCp-a4Z6lcm
float[] vertices = new float[]{
0, 0, 0,
0, 0, 1,
1, 0, 1,
1, 0.5f, 0,
//1,0.5f,0,
//1,0,1,
2, 0, 1,
2, 0, 0
};
int[] indices = new int[]{
1, 2, 0,
2, 3, 0,
2, 5, 3,
2, 4, 5
};
float[] normals=new float[vertices.length];
int normalsIndex=0;
Vector3f[] triangleVertices=new Vector3f[3];
for (int i=0;i<indices.length;i++){
int index=indices[i];
Vector3f vertexPos=new Vector3f(
vertices[index*3],
vertices[index*3+1],
vertices[index*3+2]
);
if(i%3==0){
triangleVertices[0]=vertexPos;
}
else if(i%3==1){
triangleVertices[1]=vertexPos;
}
else if(i%3==2){
triangleVertices[2]=vertexPos;
// Nf = (↑B - ↑A) × (↑C - ↑A)
Vector3f normal=triangleVertices[1].sub(triangleVertices[0]).cross(triangleVertices[2].sub(triangleVertices[0]));
normal.normalize();
normals[normalsIndex++]=normal.x;
normals[normalsIndex++]=normal.y;
normals[normalsIndex++]=normal.z;
}
}