Please click on the link below to go to my new pages:

http://www.riemers.net

See you there!!

Set up
Contact
  • Overview 
  • Device
  • Vertices
  • Camera
  • Rotation
  • Indices
  • Terrain
  • Landscape
  • Keyboard
  • - Website design & DirectX code : Riemer Grootjans -


    - Recycling vertices using inidices

    The triangle was nice, but what about a lot of triangles ? You would need to specify 3 vertices for each triangle. Consider next example:

    Only 4 out of 6 vertices are unique. So the other 2 are a waste of bandwidth to you AGP slot. It would be better to define the 4 vertices in an array from 0 to 3, and to define triangle 1 as vertices 1,2 and 3 and triangle 2 as vertices 2,3 and 4. This is exactly the idea behind IndexBuffers. Suppose we would like to draw these 2 triangles :

    Normally we would have to define 6 vertices, now only 5. So change our VertexDeclaration method as follows:

      private void VertexDeclaration()

      {

        vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 5, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);

     

        vertices = new CustomVertex.PositionColored[5];

        vertices[0].SetPosition(new Vector3(0f, 0f, 0f));

        vertices[0].Color = Color.White.ToArgb();

        vertices[1].SetPosition(new Vector3(5f, 0f, 0f));

        vertices[1].Color = Color.White.ToArgb();

        vertices[2].SetPosition(new Vector3(10f, 0f, 0f));

        vertices[2].Color = Color.White.ToArgb();

        vertices[3].SetPosition(new Vector3(5f, 5f, 0f));

        vertices[3].Color = Color.White.ToArgb();

        vertices[4].SetPosition(new Vector3(10f, 5f, 0f));

        vertices[4].Color = Color.White.ToArgb();

     

        vb.SetData(vertices, 0 ,LockFlags.None);

      }

    The middle part of this method is easy: we simply define our 5 needed vertices to draw the 2 triangles. However, the first and last lines are new. The first one creates a new VertexBuffer, which will later be needed to link our indices to. The first argument tells the buffer what vertex format to expect. Then the numbers of vertices, our device and some default settings. The last line links our vertex array to the vertex buffer. Of course, you first have to declare vb before this will compile :

      private VertexBuffer vb;

    You can already declare our array of indices we are going to fill next, togerther with its IndexBuffer, ib :

      private int[] indices;

      private IndexBuffer ib;

    Next, create this IndicesDeclaration method:

      private void IndicesDeclaration()

      {

        ib = new IndexBuffer(typeof(int), 6, device, 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;

     

        ib.SetData(indices, 0, LockFlags.None);

      }

    As with our VertexDefinition method, the first line declares the buffer that will be used to draw triangles from. As you can see, we now will need 6 indices, since 1 triangle is defined by 3 indices. Next, our indices array is initiated. As you can see, vertex number 1 is used twice, this was our initial goal. Now, the profit is rather small, but in real-life application this is the way to go. Also note that the triangles have been defined in a counter-clockwise order! The last line attaches the array to the buffer. Make sure to call this method from our Main method :

      our_directx_form.IndicesDeclaration();

    All that's left for this chapter is to draw the triangles from our buffer! Make the following changes to your OnPaint method :

      device.BeginScene();

      device.VertexFormat = CustomVertex.PositionColored.Format;

     

      device.SetStreamSource(0, vb, 0);

      device.Indices = ib;

     

      device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 5, 0, 2);

     

      device.EndScene();

    You still have to indicate what format is to be expected. Then you set your sources, the VertexBuffer and the IndexBuffer. Finally, you call the DrawIndexedPrimitives method. We still offer a list of separate triangles. The first zero indicates at which index to start counting in you indexbuffer. Then you indicate the minimum amount of used indices. We give 0, which will bring no speed optimalisation. Then the amount of used vertices and the starting point in our vertexbuffer. Finally, we have to indicate how many primitives (=triangles) we want to be drawn.

    That's it! When you run the program, you'll see 2 white triangles next to each other. Try putting this line directly after your device creation :

      device.RenderState.FillMode = FillMode.WireFrame;

    This will only draw the lines of our triangles, instead of solid triangles.

     

    Here's the whole code this far:

    using System;

    using System.Drawing;

    using System.Collections;

    using System.ComponentModel;

    using System.Windows.Forms;

    using System.Data;

    using Microsoft.DirectX;

    using Microsoft.DirectX.Direct3D;

     

    namespace DirectX_Tutorial

    {

     

          public class WinForm : System.Windows.Forms.Form

          {

                private Device device;

                private System.ComponentModel.Container components = null;

                private float angle = 0f;

                private CustomVertex.PositionColored[] vertices;

                private VertexBuffer vb;

                private int[] indices;

                private IndexBuffer ib;

     

                public WinForm()

                {

                      InitializeComponent();

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

                }

     

                public void InitializeDevice()

                {

                      PresentParameters presentParams = new PresentParameters();

                      presentParams.Windowed = true;

                      presentParams.SwapEffect = SwapEffect.Discard;

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

                      device.RenderState.CullMode = Cull.None;

                      device.RenderState.FillMode = FillMode.WireFrame;

                }

     

                private void CameraPositioning()

                {

                      device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4, this.Width/this.Height, 1f, 50f);

                      device.Transform.View = Matrix.LookAtLH(new Vector3(0,0,-30), new Vector3(0,0,0), new Vector3(0,1,0));

                      device.RenderState.Lighting = false; 

                }

     

                private void VertexDeclaration()

                {

                      vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 5, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);

     

                      vertices = new CustomVertex.PositionColored[5];

                      vertices[0].SetPosition(new Vector3(0f, 0f, 0f));

                      vertices[0].Color = Color.White.ToArgb();

                      vertices[1].SetPosition(new Vector3(5f, 0f, 0f));

                      vertices[1].Color = Color.White.ToArgb();

                      vertices[2].SetPosition(new Vector3(10f, 0f, 0f));

                      vertices[2].Color = Color.White.ToArgb();

                      vertices[3].SetPosition(new Vector3(5f, 5f, 0f));

                      vertices[3].Color = Color.White.ToArgb();

                      vertices[4].SetPosition(new Vector3(10f, 5f, 0f));

                      vertices[4].Color = Color.White.ToArgb();

     

                      vb.SetData(vertices, 0 ,LockFlags.None);

                }

     

                private void IndicesDeclaration()

                {

                      ib = new IndexBuffer(typeof(int), 6, device, 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;

     

                      ib.SetData(indices, 0, LockFlags.None);

                }

     

                protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)

                {

                      device.Clear(ClearFlags.Target, Color.DarkSlateBlue , 1.0f, 0);

     

                      device.BeginScene();

                      device.VertexFormat = CustomVertex.PositionColored.Format;

     

                      device.SetStreamSource(0, vb, 0);

                      device.Indices = ib;

     

                      device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 5, 0, 2);

     

                      device.EndScene();

     

                      device.Present();

     

                      this.Invalidate();

                      angle += 0.05f;

                }

     

                protected override void Dispose (bool disposing)

                {

                      if (disposing)

                      {

                            if (components != null)

                            {

                                 components.Dispose();

                            }

                      }

                      base.Dispose(disposing);

                }

     

                private void InitializeComponent()

                {

                      this.components = new System.ComponentModel.Container();

                      this.Size = new System.Drawing.Size(500,500);

                      this.Text = "DirectX Tutorial";

                }

         

                static void Main()

                {

                      using (WinForm our_directx_form = new WinForm())

                      {

                            our_directx_form.InitializeDevice();

                            our_directx_form.CameraPositioning();

                            our_directx_form.VertexDeclaration();

                            our_directx_form.IndicesDeclaration();

                            Application.Run(our_directx_form);

                      }

                }

          }

    }