Hello everyone,

I've been playing around with XNA for a while now, and I can't seem to get the lightning right on a mesh i'm dynamically creating. I've created a simple GameComponent that initializes a VertexBuffer and an IndexBuffer in the LoadGraphicsContent method. The Draw method uses a BasicEffect (with default lightning enabled) to draw the vertices using the TriangleList primitive. I've used the DrawIndexedPrimitives method for this.

The lightning on the mesh is totally wrong, i've uploaded a screenshot at the following location to see the effect here:

http://www.fegelein.com/projects/xna_fegelein/WeirdLightningTorus.png

Also, you can download the complete sourcecode here:

http://www.fegelein.com/projects/xna_fegelein/07 - Creating a Torus mesh.rar

The code to create the vertexbuffer and indexbuffer is as follows:

protected void InitializeVertexBuffer(GraphicsDevice graphicsDevice) { // Localize properties (as float for easy math) float radius = this.SegmentRadius; float tradius = this.TubeRadius; float segments = this.Segments; float tubes = this.Tubes; // Create doubles for our xyz coordinates double x = 0; double y = 0; double z = 0; // Init vertexList and indexList List verticesList = new List(); List indexList = new List(); // Init temp lists with tubes and segments List<LIST> tubeList = new List<LIST>(); List segmentList = new List(); // Calculate slice offset for float tubeSlice = MathHelper.Pi / (tubes / 2); float segmentSlice = MathHelper.Pi / (segments / 2); // Loop through number of tubes for (int i = 0; i < tubes; i++) { // Create a new segment loopup list segmentList = new List(); // Loop through number of segments for (int j = 0; j < segments; j++) { // Calculate X, Y, Z .. x = (tradius + radius * Math.Cos(j * segmentSlice)) * Math.Cos(i * tubeSlice); y = (tradius + radius * Math.Cos(j * segmentSlice)) * Math.Sin(i * tubeSlice); z = radius * Math.Sin(j * segmentSlice); // Create a new vertex element VertexPositionColor vertex = new VertexPositionColor(new Vector3((float)x, (float)y, (float)z), Color.White); // Add the vertex to global vertex list verticesList.Add(vertex); segmentList.Add(vertex); } tubeList.Add(segmentList); } //for (int n = 1, i = 0; i < tubeList.Count; n = ++i % tubeList.Count) for (int i = 0; i < tubeList.Count; i++) { int n = (i + 1) % tubeList.Count; List currentSegment = tubeList; List nextSegment = tubeList; for (int j = 0; j < currentSegment.Count; j++) { int m = (j + 1) % currentSegment.Count; indexList.Add((short)verticesList.IndexOf(currentSegment[j])); indexList.Add((short)verticesList.IndexOf(nextSegment[m])); indexList.Add((short)verticesList.IndexOf(currentSegment[m])); indexList.Add((short)verticesList.IndexOf(currentSegment[j])); indexList.Add((short)verticesList.IndexOf(nextSegment[j])); indexList.Add((short)verticesList.IndexOf(nextSegment[m])); } } // Create vertex buffer VertexPositionColor[] vertexData = new VertexPositionColor[this.totalVertices]; verticesList.CopyTo(vertexData); this.vertexBuffer = new VertexBuffer(graphicsDevice, VertexPositionColor.SizeInBytes * this.totalVertices, ResourceUsage.None); this.vertexBuffer.SetData(vertexData); short[] indexData = new short[this.totalIndices]; indexList.CopyTo(indexData); // Create the index buffer this.indexBuffer = new IndexBuffer(graphicsDevice, sizeof(short) * this.totalIndices, ResourceUsage.None, IndexElementSize.SixteenBits); this.indexBuffer.SetData(indexData); }

If you have any suggestions or comments, thanks very much in advance!!