[.net] [DirectX]IndexBuffer vs TriangleList

Started by
3 comments, last by turnpast 19 years, 3 months ago
Hi everyone ! I am now attempting the tutorial on IndexBuffer? and VertexBuffer?. I understood most of the tutorial but now I have another error. The code compiles fine, but instead of drawing 2 triangles side-by-side, it draws only one Here's the link where you can find the original source code and picture of the result: http://users.pandora.be/riemer/indices.html Now here's the code I used, what did I do wrong ?
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Test_DirectX
{
	class DirectXHandler : Form
	{
		private Device D3DDevice;
		private float Theta = 0f;
		private CustomVertex.PositionColored[]	Vertices;
		private VertexBuffer VBuf;
		private int[] Indices;
		private IndexBuffer IBuf;

		public DirectXHandler()
		{
			PresentParameters PresParams = new PresentParameters();
			PresParams.SwapEffect = SwapEffect.Discard;
			PresParams.Windowed = true;
			D3DDevice = new Device(0, DeviceType.Hardware, this, 
						CreateFlags.SoftwareVertexProcessing, PresParams);
		}

		public void WindowSetUp()
		{
			this.ClientSize = new Size(640, 480);
			this.Name = "DirectXHandler";
			this.Text = "DirectX";
			this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
		}

		public void CameraSetUp()
		{
			D3DDevice.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4, 
	                              this.Width/this.Height, 1f, 100f);
			D3DDevice.Transform.View = Matrix.LookAtLH(new Vector3(0f, 0f, -30f), new Vector3(0f, 0f, 0f),
	                              new Vector3(0f, 1f, 0f));
			D3DDevice.RenderState.Lighting = false;
			D3DDevice.RenderState.CullMode = Cull.None;
			D3DDevice.RenderState.FillMode = FillMode.WireFrame;
		}

		protected  override void OnPaint(PaintEventArgs e)
		{
			D3DDevice.Clear(ClearFlags.Target, Color.DarkBlue, 1f, 0);

			D3DDevice.BeginScene();
			D3DDevice.VertexFormat = CustomVertex.PositionColored.Format;
			D3DDevice.SetStreamSource(0, VBuf, 0);
			D3DDevice.Indices = IBuf;
			D3DDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 5, 0, 2);
			D3DDevice.EndScene();

			D3DDevice.Present();
			this.Invalidate();
			Theta += 0.05f;
		}

		public void VertexBufferSetUp()
		{
			VBuf = new VertexBuffer(typeof(CustomVertex.PositionColored), 5, D3DDevice,
				Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);

			Vertices = new CustomVertex.PositionColored[5];
			Vertices[0].Position = (new Vector3(0f, 0f, 0f));
			Vertices[0].Color = Color.White.ToArgb();
			Vertices[1].Position = (new Vector3(5f, 0f, 0f));
			Vertices[1].Color = Color.White.ToArgb();
			Vertices[2].Position = (new Vector3(10f, 0f, 0f));
			Vertices[2].Color = Color.White.ToArgb();
			Vertices[3].Position = (new Vector3(5f, 5f, 0f));
			Vertices[3].Color = Color.White.ToArgb();
			Vertices[4].Position = (new Vector3(10f, 5f, 0f));
			Vertices[4].Color = Color.White.ToArgb();

			VBuf.SetData(Vertices, 0, LockFlags.None);
		}

		public void IndexBufferSetUp()
		{
			IBuf = new IndexBuffer(typeof(int), 6, D3DDevice, Usage.WriteOnly, Pool.Default);
			Indices = new int[6];

			Indices[0]=3;
			Indices[1]=1;
			Indices[2]=0;
			Indices[3]=4;
			Indices[4]=2;
			Indices[5]=1;

			IBuf.SetData(Indices, 0, LockFlags.None);
		}

		public static void Main(string[] args)
		{
			using(DirectXHandler DXHandler = new DirectXHandler())
			{
				DXHandler.WindowSetUp();
				DXHandler.CameraSetUp();
				DXHandler.VertexBufferSetUp();
				DXHandler.IndexBufferSetUp();
				Application.Run(DXHandler);
			}
		}
	}
}



Advertisement
I managed to "make it work"... but there are anormal things though... I had to specify that I was drawing 3 primitives instead of 2 and the other things is... it's supposed to look like this but in my program the 2nd triangle is drawn as 4-2-0 instead of 4-2-1, why ?
Does your graphics card support 32 bit indexes? Many older cards do not. Alot of people have trouble with this in managed DX. Try switching your index buffer to use shorts instead of ints.

hope this helps.
Thank you mate ! Are there any other issues like this one that I might want to be aware of ?
Backface culling and winding order cause a lot of trouble for people. Just make sure you triangles are indexed clockwise.

The only other issue I can think of that seems to get a almost everyone is z-fighting. That is atrifacts on overlapping objects deep in the scene and it is usually because the range between the near plane and the far plane it too big. You should be fine with the range you have (1,100) though.

cheers.

This topic is closed to new replies.

Advertisement