[.net] Rotating a triangle

Started by
4 comments, last by White Scorpion 19 years, 3 months ago
Hi everyone ! I was reading a tutorial and now I have a problem that they don't explain. I have the following code and the only bug is that it's not rotating even though everything seems correct to me.
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DirectX_Tutorial
{
	public class WinForm : Form
	{
		private Device device;
		private float Theta = 0f;
		private CustomVertex.PositionColored[] Vertices;

		public WinForm()
		{
			this.ClientSize = new Size(640, 480);
			this.Name = "WinForm";
			this.Text = "DirectX Tutorial";
			InitializeDevice();
			InitializeVertices();
			CameraPositioning();
		}

		public void InitializeDevice()
		{
			PresentParameters PresParams = new PresentParameters();
			PresParams.Windowed = true;
			PresParams.SwapEffect = SwapEffect.Discard;
			device = new Device(0, DeviceType.Hardware, 
				this, CreateFlags.SoftwareVertexProcessing, PresParams); 
		}

		public void InitializeVertices()
		{
			Vertices = new CustomVertex.PositionColored[3];
			Vertices[0].Position = new Vector3(0f, 0f, 0f);
			Vertices[0].Color = Color.Yellow.ToArgb();
			Vertices[1].Position = new Vector3(10f, 0f, 0f);
			Vertices[1].Color = Color.Blue.ToArgb();
			Vertices[2].Position = new Vector3(5f, 10f, 0f);
			Vertices[2].Color = Color.Red.ToArgb();
		}

		public void CameraPositioning()
		{
			device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4,
				this.Width/this.Height, 1f, 50f);
			device.Transform.View = Matrix.LookAtLH(new Vector3(0f, 0f, 30f), new Vector3(0f, 0f, 0f),
				new Vector3(0f, 1f, 0f));
			device.RenderState.Lighting = false;
			device.RenderState.CullMode = Cull.None;
		}

		protected override void OnPaint(PaintEventArgs args)
		{
			device.Clear(ClearFlags.Target, Color.DarkBlue, 1.0f, 0);

			device.BeginScene();
			device.VertexFormat = CustomVertex.PositionColored.Format;
			device.Transform.World = Matrix.RotationX(Theta);
			device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, Vertices);
			device.EndScene();

			device.Present();
			Theta += 0.05f;
		}

		static void Main() 
		{
			using (WinForm our_dx_form = new WinForm())
			{
				Application.Run(our_dx_form);
			}
		}
	}
}

Advertisement
Try adding on this.Invalidate(); at the very end of your OnPaint method. My guess is that the control just paints itself once and then has no reason to paint itself again. If you invalidate the form at the end of your paint method it will tack another request for the control to paint itself in the message queue.
The whole screen flickers now :(
But does it rotate ?
http://sourceforge.net/projects/pingux/ <-- you know you wanna see my 2D Engine which supports DirectX and OpenGL or insert your renderer here :)

Add the following to the Constructor for your form

this.Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque, true)

Mykre - BlogVirtual Realm :- XNA News and Resources from Down Under** For those Interested in an Australian XNA User Group Contact me though my site.
Thank you, yes it was rotating and now nothing flickers :D. Thanks a lot guys.

This topic is closed to new replies.

Advertisement