Estoy aprendiendo a utilizar directX en C# y al usar los indices me muestra un error.
El codigo de la aplicacion es el sgte.
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 float angle = 0f;
private CustomVertex.PositionColored[] vertices;
private VertexBuffer vb;
private int[] indices;
private IndexBuffer ib;
private System.ComponentModel.Container components = null;
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]=0;
indices[4]=0;
indices[5]=0;
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.Transform.World = Matrix.Translation(-5,-10*1/3,0)*Matrix.RotationAxis(new Vector3(angle*4,angle*3,angle*2),angle);
device.SetStreamSource(0,vb,0);
device.Indices = ib;
//device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
//device.DrawIndexedPrimitives(PrimitiveType.TriangleList,0,0,5,0,2);
device.EndScene();
device.Present();
this.Invalidate();
angle += .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 dx = new WinForm())
{
dx.InitializeDevice();
dx.CameraPositioning();
dx.VertexDeclaration();
dx.IndicesDeclaration();
Application.Run(dx);
}
}
}
}