# Need help - why are my normals messing up?

I have problems with normals, I’ll try to describe here what I do

declarations:

ArrayList vertices = new ArrayList(); // points

ArrayList normals = new ArrayList(); // normals

ArrayList texCoord = new ArrayList(); // tex cords

ArrayList indexes = new ArrayList(); // indexes

I am building cube here, face by face so left, right, top, bottom, front and back faces/quads what ever you want to call them

I create 8 points of cube like this:

[java]

Vector3f pa = new Vector3f(bx-halfbsize, by-halfbsize, bz+halfbsize);

Vector3f pb = new Vector3f(bx+halfbsize, by-halfbsize, bz+halfbsize);

Vector3f pc = new Vector3f(bx-halfbsize, by+halfbsize, bz+halfbsize);

Vector3f pd = new Vector3f(bx+halfbsize, by+halfbsize, bz+halfbsize);

Vector3f pe = new Vector3f(bx-halfbsize, by-halfbsize, bz-halfbsize);

Vector3f pf = new Vector3f(bx+halfbsize, by-halfbsize, bz-halfbsize);

Vector3f pg = new Vector3f(bx-halfbsize, by+halfbsize, bz-halfbsize);

Vector3f ph = new Vector3f(bx+halfbsize, by+halfbsize, bz-halfbsize);

[/java]

I create some normals like this:

[java]

Vector3f normalUp = new Vector3f(0, 1, 0);

Vector3f normalDown = new Vector3f(0, -1, 0);

Vector3f normalRight = new Vector3f(1, 0, 0);

Vector3f normalLeft = new Vector3f(-1, 0, 0);

Vector3f normalFront = new Vector3f(0, 0, 1);

Vector3f normalBack = new Vector3f(0, 0, -1);

[/java]

for each side I create a face here example for right face:

[java]

// x = +

if(faces[rightFace] == 1)

{

/

/

}
[/java]

I go like that thru all the faces and build them up, add normal, texCoord, indices.

next I do this
[java]
// convert lists to arrays
Vector3f[] v3 = vertices.toArray(new Vector3f[vertices.size()]);
Vector3f[] n3 = normals.toArray(new Vector3f[normals.size()]);
Vector2f[] v2 = texCoord.toArray(new Vector2f[texCoord.size()]);
int indx[] = convertIntegers(indexes);

mapmesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(v3));
mapmesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(v2));
mapmesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indx));
mapmesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(n3));
mapmesh.updateBound();
[/java]

this is result:
with matDefName = "Common/MatDefs/Light/Lighting.j3md";
http://i.imgur.com/bHhTv.png

and with matDefName = "Common/MatDefs/Misc/ShowNormals.j3md";
http://i.imgur.com/fkcsA.png

I also tried to do:
TangentBinormalGenerator.generate(mapmesh);

it for some reason gives me an error:

[java]
java.lang.IndexOutOfBoundsException: 210
at java.nio.DirectFloatBufferU.get(DirectFloatBufferU.java:212)
at com.jme3.util.BufferUtils.populateFromBuffer(BufferUtils.java:259)
at com.jme3.util.TangentBinormalGenerator.processTriangleData(TangentBinormalGenerator.java:404)
at com.jme3.util.TangentBinormalGenerator.generate(TangentBinormalGenerator.java:181)
at com.jme3.util.TangentBinormalGenerator.generate(TangentBinormalGenerator.java:145)
at mygame.Main.simpleInitApp(Main.java:156)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:219)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:188)
[/java]

What am I doing wrong here, I asumed that normals would be correct and point outwar from the triangel if I build it counter clockwise which I do, but since my normals were messed up I then started assigning each face its normal.

What am I doing wrong here? btw I am using point light in scene.

Just glanced at your code quickly… every vertex needs a normal. For a given side they will all be facing in the same direction… but every vertex needs one.

ohh thats how it works, thanks alot I’ll test that

wow that did it, I tottally missunderstood how this works. now it works a charm and I dont have to even call TangentBinormalGenerator.generate on the mesh.

Thank you so much!

You’re welcome.

re: TangentBinormalGenerator, you only need that if you are using normal maps or bump maps for your textures…and even then, only if creating them by hand is difficult.

For a cube, tangent vectors are trivial to create.