Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

T1mb0

Strange Indexbuffer behaviour

This topic is 5217 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

I am trying to draw simple polygons with Managed DirectX (C#), and get very strange results. Polygons are missing or drawn between other vertices than the ones selected by the index buffer, and it seems to be connected to my PC (Radeon 9700 inside), as it works fine on another computer with a GeForce4. The following source code shows the problem (on my machine): It creates a vertex buffer with a grid of 3x3 vertices and defines an index buffer of 2 triangles that shall conneect some of those vertices. However, it seems like every index i select above 6 will be drawn as if i selected 6... Has anyone encountered such a problem before and knows how i can possibly solve it?
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Chapter4Code
{
	/// <summary>

	/// Summary description for Form1.

	/// </summary>

	public class Form1 : System.Windows.Forms.Form
	{
        private Device device = null;
        private VertexBuffer vb = null;
        private IndexBuffer ib = null;


		public Form1()
		{
			//

			// Required for Windows Form Designer support

			//

			//InitializeComponent();


            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
		}

		/* vertices:
		 * 0  1  2
		 * 3  4  5
		 * 6  7  8
		 */
		private short[] indices = {
			0, 2, 6,
			6, 2, 8,
		};

        /// <summary>

        /// We will initialize our graphics device here

        /// </summary>

        public void InitializeGraphics()
        {
            // Set our presentation parameters

            PresentParameters presentParams = new PresentParameters();

            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;

            // Create our device

            device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);			

            vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 9, device, 0, CustomVertex.PositionColored.Format, Pool.Managed);
			CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[9];

			// Vertices: 3x3 grid of vertices in a plane

			verts[0] = new CustomVertex.PositionColored(-1.0f, -1.0f, 0.0f, Color.Green.ToArgb());
			verts[1] = new CustomVertex.PositionColored( 0.0f, -1.0f, 0.0f, Color.Red.ToArgb());
			verts[2] = new CustomVertex.PositionColored( 1.0f, -1.0f, 0.0f, Color.Yellow.ToArgb());
			verts[3] = new CustomVertex.PositionColored(-1.0f,  0.0f, 0.0f, Color.Blue.ToArgb());
			verts[4] = new CustomVertex.PositionColored( 0.0f,  0.0f, 0.0f, Color.Purple.ToArgb());
			verts[5] = new CustomVertex.PositionColored( 1.0f,  0.0f, 0.0f, Color.Gold.ToArgb());
			verts[6] = new CustomVertex.PositionColored(-1.0f,  1.0f, 0.0f, Color.CornflowerBlue.ToArgb());
			verts[7] = new CustomVertex.PositionColored( 0.0f,  1.0f, 0.0f, Color.Aqua.ToArgb());
			verts[8] = new CustomVertex.PositionColored( 1.0f,  1.0f, 0.0f, Color.White.ToArgb());
			vb.SetData(verts, 0, LockFlags.None);

            ib = new IndexBuffer(typeof(short), indices.Length, device, 0, Pool.Managed);
			ib.SetData( indices, 0, LockFlags.None );

			device.RenderState.Lighting = false;
			device.RenderState.CullMode = Cull.None;
			device.RenderState.FillMode = FillMode.WireFrame;
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);

			device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
			device.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 5.0f), new Vector3(), new Vector3(0,1,0));

            device.BeginScene();
            device.VertexFormat = CustomVertex.PositionColored.Format;
            device.SetStreamSource(0, vb, 0);
			device.Indices = ib;

			device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length, 0, indices.Length / 3 );

			device.EndScene();
            device.Present();

            this.Invalidate();
        }

		/// <summary>

		/// The main entry point for the application.

		/// </summary>

        static void Main() 
        {
            using (Form1 frm = new Form1())
            {
                // Show our form and initialize our graphics engine

                frm.Show();
                frm.InitializeGraphics();
                Application.Run(frm);
            }
        }
	}
}

Share this post


Link to post
Share on other sites
Advertisement
In your DrawIndexedPrimitive call, you''re specifying indices.Length (6) for the number of vertices (fourth param). You should be specifying verts.Length (9).

I like pie.

Share this post


Link to post
Share on other sites
Thx!

Seems like the example i took to build that thing had a little bug that just didnt show up because it initially had more indices that vertices... and the nvidia driver is more tolerant towards that :-)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!