Sign in to follow this  

[.net] [DirectX]IndexBuffer vs TriangleList

This topic is 4729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
			}
		}
	}
}



Share this post


Link to post
Share on other sites
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 [url=http://users.pandora.be/riemer/indices2.jpg]this[/url] but in my program the 2nd triangle is drawn as 4-2-0 instead of 4-2-1, why ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this