Sign in to follow this  

DrawIndexPrimitives Problem

This topic is 4776 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

I'm trying to learn Direct3D to C#. I was going well until I tried to use the DrawIndexedPrimitive function. I simply got nothing. A black screen with no error message. Here is te complete source code to the form:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Surface
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class WinForm : System.Windows.Forms.Form
	{
		private Device device = null;
		private PresentParameters presentParams;
		private bool deviceLost = false;

		private VertexBuffer vertexBuffer = null;
		private CustomVertex.PositionColored[] verts;
		private IndexBuffer indexBuffer = null;
		private int[] indices;

		private float fAngleX = 0f;
		private float fAngleY = 0f;
		private float fAngleZ = 0f;

		private float fCameradX = 0.0f;
		private float fCameradY = 0.0f;
		private float fCameradZ = -5.0f;


		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public WinForm() {
			//
			// Required for Windows Form Designer supresentParamsort
			//
			InitializeComponent();
			this.Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque, true);
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing ) {
			if( disposing && (components != null)) components.Dispose();
			base.Dispose( disposing );
		}

		/// <summary>
		/// Required method for Designer supresentParamsort - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
			// 
			// WinForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(800, 600);
			this.FormBorderstyle = System.Windows.Forms.FormBorderstyle.FixedSingle;
			this.Name = "WinForm";
			this.Text = "DirectX Tutorial";
		}

		public bool setDeviceMode(bool fullScreen, int Width, int Height, Format Depth, int RefreshRate) {
			PresentParameters oldParams = new PresentParameters(presentParams);

			this.ClientSize = new System.Drawing.Size(Width, Height);

			presentParams.Windowed = !fullScreen;
			if (fullScreen) {
				presentParams.BackBufferWidth	= Width;
				presentParams.BackBufferHeight	= Height;
				presentParams.BackBufferFormat	= Depth;
				presentParams.FullScreenRefreshRateInHz = RefreshRate;

				this.FormBorderstyle = System.Windows.Forms.FormBorderstyle.None;
			} else {
				this.FormBorderstyle = System.Windows.Forms.FormBorderstyle.FixedSingle;
			}

			if (device != null) {
				try {
					device.TestCooperativeLevel();
					device.Reset(presentParams);
				} catch(DeviceLostException) {
					presentParams = (PresentParameters)oldParams.Clone();
					deviceLost = true;
					return false;
				} catch {
					presentParams = (PresentParameters)oldParams.Clone();
					device.Reset(presentParams);
					return false;
				}
			}

			return true;
		}

		public void InitializeDevice(bool fullScreen) {
			presentParams = new PresentParameters();

			presentParams.SwapEffect = SwapEffect.Discard;
			presentParams.Windowed = !fullScreen;
			if (fullScreen) {
				presentParams.BackBufferCount	= 1;
				presentParams.BackBufferWidth	= this.ClientSize.Width;
				presentParams.BackBufferHeight	= this.ClientSize.Height;
				presentParams.BackBufferFormat	= Manager.Adapters[0].CurrentDisplayMode.Format;
				presentParams.FullScreenRefreshRateInHz = Manager.Adapters[0].CurrentDisplayMode.RefreshRate;
			}

			device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
			device.DeviceReset += new System.EventHandler(this.OnResetDevice);
			this.OnResetDevice(device, null);
			device.Reset(presentParams);
		}

		public void OnResetDevice(object sender, EventArgs e) {
			Device dev = (Device)sender;
			// Turn off culling, so we see the front and back of the triangle
			dev.RenderState.CullMode = Cull.None;
			// Set Surface to Wireframe to show only the edges
//			dev.RenderState.FillMode = FillMode.WireFrame;
			// Turn off D3D lighting, since we are providing our own vertex colors
			dev.RenderState.Lighting = false;
		}

		
		private void DefineVertex() {
		
//			Vector3 p1 = new Vector3(-1f, -1f, 0f);
//			Vector3 p2 = new Vector3(1f, -1f, 0f);
//			Vector3 p3 = new Vector3(0f, 1f, 0f);
//			Vector3 p4 = new Vector3(0f, 0f, 3f);

//			verts = new CustomVertex.PositionColored[6];

//			verts[0].Position = p1;
//			verts[0].Color = Color.Red.ToArgb();    

//			verts[1].Position = p2;
//			verts[1].Color = Color.Green.ToArgb();

//			verts[2].Position = p3;
//			verts[2].Color = Color.Blue.ToArgb();  

//			verts[3].Position = p4;
//			verts[3].Color = Color.White.ToArgb();  

//			verts[4].Position = p1;
//			verts[4].Color = Color.Red.ToArgb();    

//			verts[5].Position = p2;
//			verts[5].Color = Color.Green.ToArgb();

			verts = new CustomVertex.PositionColored[5];

			verts[0].Position = new Vector3(-1f, 0f, 0f);
			verts[0].Color = Color.White.ToArgb();

			verts[1].Position = new Vector3(0f, 0f, 0f);
			verts[1].Color = Color.White.ToArgb();

			verts[2].Position = new Vector3(1f, 0f, 0f);
			verts[2].Color = Color.White.ToArgb();

			verts[3].Position = new Vector3(0f, 1f, 0f);
			verts[3].Color = Color.White.ToArgb();

			verts[4].Position = new Vector3(1f, 1f, 0f);
			verts[4].Color = Color.White.ToArgb();


			vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 5, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
			vertexBuffer.SetData(verts, 0 ,LockFlags.None);
		}

		private void DefineIndex() {
			indexBuffer = new IndexBuffer(typeof(int), 6, device, 0, Pool.Default);

			indices = new int[6];
			
			indices[0] = 3;
			indices[1] = 1;
			indices[2] = 0;
			indices[3] = 4;
			indices[4] = 2;
			indices[5] = 1;

			indexBuffer.SetData(indices, 0, LockFlags.None);
		}
		
		private void SetupCamera() {
			// For our world matrix, we will just rotate the object about the y-axis.

			// Set up the rotation matrix to generate 1 full rotation (2*PI radians) 
			// every 1000 ms. To avoid the loss of precision inherent in very high 
			// floating point numbers, the system time is modulated by the rotation 
			// period before conversion to a radian angle.
//			int  iTime  = Environment.TickCount % 1000;
//			float fAngle = iTime * (2.0f * (float)Math.PI) / 1000.0f;
//			device.Transform.World = Matrix.Translation(-5,-10*1/3,0)*Matrix.RotationAxis(new Vector3(fAngle*4,fAngle*2,fAngle*3), fAngle);

			// User controled rotation
			device.Transform.World = Matrix.RotationY(fAngleY)*Matrix.RotationX(fAngleX)*Matrix.RotationZ(fAngleZ);

			// Set up our view matrix. A view matrix can be defined given an eye point,
			// a point to lookat, and a direction for which way is up. Here, we set the
			// eye five units back along the z-axis and up three units, look at the
			// origin, and define "up" to be in the y-direction.
			Vector3 origin = new Vector3(fCameradX,fCameradY,fCameradZ);
			Vector3 direction = new Vector3(0f, 0f, 0f);
			Vector3 align = new Vector3(0f, 1f, 0f);
			device.Transform.View = Matrix.LookAtLH(origin, direction, align);

			// For the projection matrix, we set up a perspective transform (which
			// transforms geometry from 3D view space to 2D viewport space, with
			// a perspective divide making objects smaller in the distance). To build
			// a perpsective transform, we need the field of view (1/4 pi is common),
			// the aspect ratio, and the near and far clipping planes (which define at
			// what distances geometry should be no longer be rendered).
			float fieldOfViewY = (float)Math.PI / 4;
			float aspectRadio = this.ClientSize.Width/this.ClientSize.Height;
			float znearPlane = 1.0f;
			float zfarPlane = 100.0f;
			device.Transform.Projection = Matrix.PerspectiveFovLH(fieldOfViewY, aspectRadio, znearPlane, zfarPlane);
		}

		public void Render() {
			if (deviceLost) {
				try {
					device.TestCooperativeLevel();
					device.Reset(presentParams);
					deviceLost = false;
				} catch {
					return;
				}
			}
			
			try {
				device.TestCooperativeLevel();

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

				SetupCamera();

				device.VertexFormat = CustomVertex.PositionColored.Format;
				device.SetStreamSource(0, vertexBuffer, 0);
				device.Indices = indexBuffer;

//				device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, verts);
				device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 5, 0, 2);

				device.EndScene();
				device.Present();
			} catch(DeviceLostException) {
				deviceLost = true;
				return;
			}
		}




		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
			Render();
			this.Invalidate();
		}

		protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e) {
			if (e.KeyCode == System.Windows.Forms.Keys.Escape) this.Close(); // Esc was pressed
//			if (e.KeyCode == System.Windows.Forms.Keys.Left) {
//				fCameradX += 0.1f;
//			}
//			if (e.KeyCode == System.Windows.Forms.Keys.Right) {
//				fCameradX -= 0.1f;
//			}
//			if (e.KeyCode == System.Windows.Forms.Keys.Up) {
//				fCameradY += 0.1f;
//			}
//			if (e.KeyCode == System.Windows.Forms.Keys.Down) {
//				fCameradY -= 0.1f;
//			}
//			if (e.KeyCode == System.Windows.Forms.Keys.PageUp) {
//				fCameradZ += 0.1f;
//			}
//			if (e.KeyCode == System.Windows.Forms.Keys.PageDown) {
//				fCameradZ -= 0.1f;
//			}
			if (e.KeyCode == System.Windows.Forms.Keys.Left) {
				fAngleY -= (float)((10.0/180.0)*Math.PI);
			}
			if (e.KeyCode == System.Windows.Forms.Keys.Right) {
				fAngleY += (float)((10.0/180.0)*Math.PI);
			}
			if (e.KeyCode == System.Windows.Forms.Keys.Up) {
				fAngleX -= (float)((10.0/180.0)*Math.PI);
			}
			if (e.KeyCode == System.Windows.Forms.Keys.Down) {
				fAngleX += (float)((10.0/180.0)*Math.PI);
			}
			if (e.KeyCode == System.Windows.Forms.Keys.PageUp) {
				fAngleZ -= (float)((10.0/180.0)*Math.PI);
			}
			if (e.KeyCode == System.Windows.Forms.Keys.PageDown) {
				fAngleZ += (float)((10.0/180.0)*Math.PI);
			}
		}

		/// <summary>
		/// The main entry point for the apresentParamslication.
		/// </summary>
		[STAThread]
		static void Main() {
		
			using (WinForm f = new WinForm()) {
				f.InitializeDevice(false);
				f.DefineVertex();
				f.DefineIndex();
				f.SetupCamera();
				f.Show();
				while(f.Created) {
					f.Render();
					Application.DoEvents();			
				}
			}
		}
	}
}


Please can you help me into finding where is the error. Thanks. [Edited by - Coder on November 14, 2004 12:56:43 AM]

Share this post


Link to post
Share on other sites

This topic is 4776 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this