Sign in to follow this  

[.net] Rotating a triangle

This topic is 4728 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 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);
			}
		}
	}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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