Jump to content
  • Advertisement
Sign in to follow this  
andyb716

Indices and Vb's in C#

This topic is 5029 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, I'm trying to learn from some tutorials on the net about managed Directx in c#. I got to this tutorial and I can't seem to get it to work. I went through and did a break point on the OnPaint method and it seems to be erroring at the DrawIndexedPrimitive, which leads me to believe that I am not setting something right, but I copied the exact code from the site, I will keep looking through my code but if anyone can see something thats wrong please let me know here is my code.

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;

private System.ComponentModel.Container components = null;
private Device device = null;
private float angle = 0f;
private VertexBuffer vb;
private int[] indices;
private IndexBuffer ib;
private CustomVertex.PositionColored[] verts;

#region "Init Graphics"
public void initGraphics()
{

 PresentParameters presentParameters = new PresentParameters();
 presentParameters.Windowed = true;
 presentParameters.SwapEffect = SwapEffect.Discard;
	
 device = new Device(0, DeviceType.Hardware, this,  CreateFlags.SoftwareVertexProcessing, presentParameters);
			device.RenderState.CullMode = Cull.None;
			device.RenderState.FillMode = FillMode.WireFrame;

}
#endregion

#region "Camera"
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));
}
#endregion

#region "Vertex Declaration"
private void VertexDeclaration()
{
			
	vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 5, device, 
				Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format,
				Pool.Default);

	verts = new CustomVertex.PositionColored[5];

	verts[0].SetPosition(new Vector3(0f, 0f, 0f));
	verts[0].Color = Color.White.ToArgb();
	verts[1].SetPosition(new Vector3(5f, 0f, 0f));
	verts[1].Color = Color.White.ToArgb();
	verts[2].SetPosition(new Vector3(10f, 0f, 0f));
	verts[2].Color = Color.White.ToArgb();
	verts[3].SetPosition(new Vector3(5f, 5f, 0f));
	verts[3].Color = Color.White.ToArgb();
	verts[4].SetPosition(new Vector3(10f, 5f, 0f));
	verts[4].Color = Color.White.ToArgb();

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

}
#endregion

#region "Indices Declaration"
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);

}
#endregion
	
#region "OnPaint"
private void OnPaint(object sender, 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;
}
#endregion

#region "Main"
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() 
{
	using(Form1 Form = new Form1())
	{
		Form.initGraphics();
		Form.CameraPositioning();
		Form.VertexDeclaration();
		Form.IndicesDeclaration();
		Form.Show();
		Application.Run(Form);
	}
}
#endregion "Main"


Share this post


Link to post
Share on other sites
Advertisement
I've done some more testing commenting out some code and it seems to error out when i uncomment this.

device.Indices = ib;

So i am wonder if I am setting the index buffer right. I'm sorry I dont know too much about this, I wish code would always work from tutorials, but they don't. So if anybody can help me, It would be appreciated.

EDIT: thanks JDUK, i tried to put 6 in there and it still gives me one of those Error in appplication Errors, I wish I knew what the error actually was.

Share this post


Link to post
Share on other sites
Ooops ignore my advice you have 6 indices not vertices(of whichyou do have 5)
Sorry about that *Blush*

Somthing i found when u started ManagedDX9 was that recent SKD updates made changes to some functions so some tutorials from books and the web no longer worked. try removing your DirectX and Direct3D references and using version 1.0.1901 or older for tht tutorial?

Share this post


Link to post
Share on other sites
Does anybody know? Or can somebody give me some links to more tutorials. And dont say go to google because thats where I found it. I'll keep searching though, if you know of something please let me know, thanks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!