Using index buffers

I want to draw cube. And I want specify only 8 vertices, and then specify their order in special array. As I know index buffers allow to do so. But I can't find example. Please, write example of using index buffers.




Answer this question

Using index buffers

  • mikeBearb

    I write my program exactly as Andypike. But it doesn't works. Why

  • Ti L H

    Andypike has exactly the kind of sample you are looking for. Although in DirectX8 and in C++. The explanation is good and should give you a bump in the right direction.

    I hope this helps.
    Take care.


  • flyernut

    I wrote following program:

    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
    namespace ProgaX
    {
    class Forma : Form
    {
    #region Variables
    Device device = null;
    VertexBuffer buf = null;
    IndexBuffer ibuf = null;
    PresentParameters par = new PresentParameters();
    #endregion
    void init()
    {
    par.Windowed = true;
    par.SwapEffect = SwapEffect.Discard;
    device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, par);
    Size = new Size(300, 300);
    buf = new VertexBuffer(typeof(CustomVertex.PositionOnly), 3, device, 0, CustomVertex.PositionOnly.Format, Pool.Default);
    buf.Created += new EventHandler(OnCreateVertex);
    ibuf = new IndexBuffer(typeof(int), 3, device, 0, Pool.Default);
    ibuf.Created += new EventHandler(OnCreateIndex);
    device.DeviceReset += new EventHandler(OnResetDevice);
    OnResetDevice(device, null);
    OnCreateVertex(buf, null);
    OnCreateIndex(ibuf, null);
    }
    void OnResetDevice(object d, EventArgs e)
    {
    Device dev = (Device)d;
    dev.RenderState.CullMode = Cull.None;
    dev.RenderState.Lighting = false;
    }
    void OnCreateVertex(Object sender, EventArgs e)
    {
    VertexBuffer vb = (VertexBuffer)sender;
    CustomVertex.PositionOnly[] v = (CustomVertex.PositionOnly[])vb.Lock(0, 0); ;
    v[0].Position = new Vector3(1, 1, 1);
    v[1].Position = new Vector3(-1, -1, -1);
    v[2].Position = new Vector3(1, -1, 1);
    vb.Unlock();
    }
    void OnCreateIndex(Object sender, EventArgs e)
    {
    IndexBuffer ib = (IndexBuffer)sender;
    int[] i = (int[])ib.Lock(0, 0);
    i[0] = 0;
    i[1] = 1;
    i[2] = 2;
    ib.Unlock();
    }
    void Render()
    {
    if (device == null) return;
    device.Clear(ClearFlags.Target, Color.Black, 1, 0);
    device.BeginScene();
    device.Transform.World = Matrix.RotationY(Environment.TickCount * (2.0f * (float)Math.PI) / 5000.0f);
    device.Transform.View = Matrix.LookAtLH(new Vector3(0, 3f, -5f), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
    device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1, 1, 100);
    device.SetStreamSource(0, buf, 0);
    device.VertexFormat = CustomVertex.PositionOnly.Format;
    device.Indices = ibuf;
    device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 3, 0, 1);
    device.EndScene();
    device.Present();
    }
    static void Main()
    {
    Forma Form1 = new Forma();
    Form1.init();
    Form1.Show();
    while (Form1.Created)
    {
    Form1.Render();
    Application.DoEvents();
    }
    }
    }
    }

    But it doesn't work. When I run it, computer suddenly reboots (like when I press reset). Where is a mistake



  • mame

    I understand. I used 32-bit indicies (int), but my video card doesn't support them. With 16-bit indicies my program works correctly. Thanks!

  • Vikram Kumar

    Yes, I can run all the tutorials, but they don't use index buffers.

  • Mr Leaf

    Tough to know - do all the programs crash your computer

    Can you run any of the tutorial 1-6 in the SDK How about other SDK samples



  • swYap

    Sorry, I thought one of them used it.

    Try Chads tutorial here http://www.c-unit.com/tutorials/mdirectx/ t=33



  • Darren Blackett

    I also copied program from http://users.pandora.be/riemer/indices.html it doesn't work too. I think it is because my video card may not support this feature (I have VIA/S3G UniChromeII Graphics video card). If you know something about it, please, tell me.



  • Using index buffers