My below code will compile and create a form with blue inside. I'm currently trying to draw a triangle to the screen but when I call "device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);" inside of my render function the program fails. Let me know if I'm doing anything wrong, I'm still in the process of learning directx.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Second
{
	/// This is the main class of my Direct3D application
	public class MainClass : Form
	{
		/// <summary>
		/// The rendering device
		/// </summary>
		Device device = null;
		VertexBuffer vertexBuffer = null;
		
		public MainClass()
		{
			this.ClientSize = new System.Drawing.Size(640, 480);
			this.Text = "Hopefully a triangle";
		}
		
		public bool InitializeGraphics()
		{
			try {
				// Direct3D stuff
				PresentParameters presentParams = new PresentParameters();
				presentParams.Windowed   = true;
				presentParams.SwapEffect = SwapEffect.Discard;
				
				// Create the device
				device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
				
				// Setup the event handlers for the device
				device.DeviceLost     += new EventHandler(this.InvalidateDeviceObjects);
				device.DeviceReset    += new EventHandler(this.RestoreDeviceObjects);
				device.Disposing      += new EventHandler(this.DeleteDeviceObjects);
				device.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);
		
				return true;
			} catch (DirectXException) {
				return false;
			}
		}
		
		protected virtual void InvalidateDeviceObjects(object sender, EventArgs e)
		{
		}
		
		protected virtual void RestoreDeviceObjects(object sender, EventArgs e)
		{		
		    Device dev = (Device)sender;
		    vertexBuffer =
	            new VertexBuffer(typeof(CustomVertex.TransformedColored),
	            4,
	            dev,
	            0,
	            CustomVertex.TransformedColored.Format,
	            Pool.Default);
		    
			    GraphicsStream stm = vertexBuffer.Lock(0, 0, 0);
				CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[3];
				
				verts[0].X=150;
				verts[0].Y=50;
				verts[0].Z=0.0f;
				verts[0].Rhw=1;
				verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
				verts[1].X=250;
				verts[1].Y=250;
				verts[1].Z=0.0f;
				verts[1].Rhw=1;
				verts[1].Color = System.Drawing.Color.Brown.ToArgb();
				verts[2].X=50;
				verts[2].Y=250;
				verts[2].Z=0.0f;
				verts[2].Rhw=1;
				verts[2].Color = System.Drawing.Color.LightPink.ToArgb();
				stm.Write(verts);
				vertexBuffer.Unlock();
		}
		
		protected virtual void DeleteDeviceObjects(object sender, EventArgs e)
		{
		}
		
		protected virtual void EnvironmentResizing(object sender, CancelEventArgs e)
		{
		}
		
		/// <summary>
		/// This method moves the scene
		/// </summary>
		protected virtual void FrameMove()
		{
			// TODO : Frame movement
		}
		
		/// <summary>
		/// This method renders the scene
		/// </summary>
		protected virtual void Render()
		{
			if (device != null) {
				device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
				device.BeginScene();
				
				device.SetStreamSource( 0, vertexBuffer, 0);
			        device.VertexFormat = CustomVertex.TransformedColored.Format;
				//device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
				
				device.EndScene();
				device.Present();
			}
		}
		
		/// <summary>
		/// Our mainloop
		/// </summary>
		public void Run()
		{
			// While the form is still valid, render and process messages
			while (Created) {
				FrameMove();
				Render();
				Application.DoEvents();
			}
		}
		
		protected override void OnPaint(PaintEventArgs e)
		{
			this.Render();
		}
		
		protected override void OnKeyPress(KeyPressEventArgs e)
		{
			base.OnKeyPress(e);
			if ((int)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) {
				this.Close(); 
			}
		}
		
		/// <summary>
		/// The main entry point for the application
		/// </summary>
		static void Main()
		{
			using (MainClass mainClass = new MainClass()) {
				if (!mainClass.InitializeGraphics()) {
					MessageBox.Show("Error while initializing Direct3D");
					return;
				}
				mainClass.Show();
				mainClass.Run();
			}
		}
	}
}

Use this code:

device.DrawPrimitives(PrimitiveType.TriangleList, 0, verts.Length/3)

Let me know if that works.

Thanks

Use this code:

device.DrawPrimitives(PrimitiveType.TriangleList, 0, verts.Length/3)

Let me know if that works.

Thanks

already fixed it using

device.DrawUserPrimitives(PrimitiveType.TriangleList, 0, vertexes);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.