Eric van Feggelen

Hello there,

I've been playing around with drawing primitives in XNA, and I've found it difficult to light my own meshes. I've tried using a BasicEffect with default lightning, but that doesn't work because I don't have any vertex normals.

I need some more information on vertex normals because I'm having a little trouble understanding the difference from face normals. I just can't seem to get the normal calculations straight.

I've used a custom vertex format called VertexPositionNormalColor that has normal information. Then, I've created a simple quad with 4 vertices and 6 indices to render it:

// Init vertexList
VertexPositionNormalColor[] vertexList = new VertexPositionNormalColor[4];

// Create vertices
vertexList[0] = new VertexPositionNormalColor(new Vector3(-1.0f, 1.0f, 0.0f), Color.Red, ...);
vertexList[1] =
new VertexPositionNormalColor(new Vector3(1.0f, 1.0f, 0.0f), Color.Green, ...);
vertexList[2] =
new VertexPositionNormalColor(new Vector3(1.0f, -1.0f, 0.0f), Color.Blue, ...);
vertexList[3] =
new VertexPositionNormalColor(new Vector3(-1.0f, -1.0f, 0.0f), Color.White, ...);

// Create vertex buffer
this.vertexBuffer = new VertexBuffer(graphicsDevice, VertexPositionNormalColor.SizeInBytes * vertexList.Length, ResourceUsage.None);

// Init index list
short[] indexList = new short[ 6] {0, 1, 2, 0, 2, 3};

// Create the index buffer
this.indexBuffer = new IndexBuffer(graphicsDevice, sizeof(short) * indexList.Length, ResourceUsage.None, IndexElementSize.SixteenBits);

To what should I set the normals of the 4 vertices Thanks everyone!

Re: XNA Framework Calculating vertex normals



Vertex normals are typically going to be the (normalized) vector which is the average of the face normal vectors of the faces to which the vertex is attached. This produces a smooth surface lighting appearance between the faces. If you wanted a hard edge break (also known as a continuity break) along certain edges (to create the appearance of a ridge on the object where the direction of the reflection changes abruptly), the vertices for those edges would need to be duplicated so that the vertices which connect to the faces on one side of the edge break can have a different set of normals from the duplicated vertices (which have the same spatial positions as the original vertices) which connect to the faces on the other side of the break.

For a simple quad as you have shown in the example code, all of the vertices would have the same normal vector of (0,0,1) since the faces point toward the positive Z axis. However if you want to creat objects with even a few triangles and any of those triangles are not aligned with the world space axes, it quickly becomes impractical to try to calculate the normals by hand. Therefore, triangle primitives are not really suited for creating objects of even minor complexity from lists of vertex positions and normals. You could create your own tool to calculate the normals or perhaps even create your own tool to allow visual modeling of your vertex positions, but really the easiest and best thing to do is to use some 3rd party 3D modeling software (such as Softimage XSI, Maya, 3D Studio Max, Lightwave 3D, Cinema 4D, TrueSpace, etc etc) which supports exporting objects as X or FBX files. The exported files would already have the normals calculated and added in for you, and then you would import these objects into your game as models instead of using triangle primitives. There are many different 3D modeling programs on the market and unfortunately most are quite expensive. However, as luck would have it, there is a free version of Softimage XSI 4.2 called the XSI Mod Tool, which was made as a free tool for hobbyists to use for creating their models. It of course lacks many of the features of the full version of XSI, and it has a number of limitations imposed on it such as the inability to export models containing more than 7500 triangles. Personally I consider the polygonal modeling tools in XSI (and therefore the Mod Tool) to be among the best I've seen, so I definitely recommend checking it out, as the Mod Tool is probably adequate for most hobbyist projects: