C# XNA Normals Question
Posted
by
Wade
on Game Development
See other posts from Game Development
or by Wade
Published on 2010-12-23T17:47:08Z
Indexed on
2010/12/28
1:02 UTC
Read the original article
Hit count: 498
Hello all!
I have been working on some simple XNA proof of concept for a game idea I have as well as just to further my learning in XNA. However, i seem to be stuck on these dreaded normals, and using the BasicEffect with default lighting i can't seem to tell if my normals are being calculated correctly, hence the question.
I'm mainly drawing cubes at the moment, I'm using a triangle list and a VertexBuffer to get the job done. The north face of my cube has two polygons and 6 vectors:
Vector3 startPosition = new Vector3(0,0,0);
corners[0] = startPosition; // This is the start position. Block size is 5.
corners[1] = new Vector3(startPosition.X, startPosition.Y + BLOCK_SIZE, startPosition.Z);
corners[2] = new Vector3(startPosition.X + BLOCK_SIZE, startPosition.Y, startPosition.Z);
corners[3] = new Vector3(startPosition.X + BLOCK_SIZE, startPosition.Y + BLOCK_SIZE, startPosition.Z);
verts[0] = new VertexPositionNormalTexture(corners[0], normals[0], textCoordBR);
verts[1] = new VertexPositionNormalTexture(corners[1], normals[0], textCoordTR);
verts[2] = new VertexPositionNormalTexture(corners[2], normals[0], textCoordBL);
verts[3] = new VertexPositionNormalTexture(corners[3], normals[0], textCoordTL);
verts[4] = new VertexPositionNormalTexture(corners[2], normals[0], textCoordBL);
verts[5] = new VertexPositionNormalTexture(corners[1], normals[0], textCoordTR);
Using those coordinates I want to generate the normal for the north face, I have no clue how to get the average of all those vectors and create a normal for the two polygons that it makes.
Here is what i tried:
normals[0] = Vector3.Cross(corners[1], corners[2]);
normals[0].Normalize();
It seems like its correct, but then using the same thing for other sides of the cube the lighting effect seems weird, and not cohesive with where i think the light source is coming from, not really sure with the BasicEffect.
Am I doing this right? Can anyone explain in lay mans terms how normals are calculated. Any help is much appreciated.
Note: I tried going through Riemers and such to figure it out with no luck, it seems no one really goes over the math well enough. Thanks!
© Game Development or respective owner