Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Managed DirectX Problem, Please Help!

Started by ninjaindark Aug 9, 2005 at 9:06 PM 9 replies 1.6k views
Original Post
ninjaindark
ninjaindark
Hello, I wrote a simple Managed DirectX code using C#. The program basically displays a triangle in 3D space. However, when I run this program, it shows nothing but a colored background. I counldn't figure out why it is like that. By the way, the SDK I use is the latest version (August 2005). Can anyone have a quick look at the following code and point out the problem for me? Thanks


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;

namespace DirectX1
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class DirectXForm : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		
		private Device device;
		private System.ComponentModel.Container components = null;

		public DirectXForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
		}

		/// <summary>
		/// Initialize the DirectX Device
		/// </summary>
		public void InitializeDevice()
		{
			PresentParameters presentParams = new PresentParameters();

			presentParams.Windowed = true;
			presentParams.SwapEffect = SwapEffect.Discard;

			device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
		}


		protected override void OnPaint(PaintEventArgs e)
		{
			
			// Set the Camera
			device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
			device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, 5.0f), new Vector3(), new Vector3(0.0f, 1.0f, 0.0f));

			device.RenderState.Lighting = false;
			device.RenderState.CullMode = Cull.None;

            // Clear the Scene
			device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0);

			// Vertex Buffer
			CustomVertex.PositionColored[] verts= new CustomVertex.PositionColored[3];
						
			verts[0].Position = new Vector3(0f, 1f, 1f);
			verts[0].Color = Color.Red.ToArgb();
			
			verts[1].Position = new Vector3(-1f, -1f, 1f);
			verts[1].Color = Color.Green.ToArgb();

			verts[2].Position = new Vector3(1f, -1f, 1f);
			verts[2].Color = Color.Yellow.ToArgb();
				
			
			// Draw the scene
			device.BeginScene();
			device.VertexFormat = CustomVertex.PositionColored.Format;
			device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
			device.EndScene();

			device.Present();

			//base.OnPaint (e);

			this.Invalidate();
		}


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

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// DirectXForm
			// 
			this.components = new Container();
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(500, 500);
			this.Text = "DirectXForm";

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			using(DirectXForm dxf = new DirectXForm())
			{
				dxf.Show();
				dxf.InitializeDevice();
				Application.Run(dxf);
			}
		}
	}
}


GroZZleR
GroZZleR
Although I can't tell you why I can tell you what. For some reason, it doesn't like the size of your window. Try maximizing the window and it works fine.

Alternatively, change
this.ClientSize = new System.Drawing.Size(500, 500);
to
this.ClientSize = new System.Drawing.Size(640, 480);
and it works.

I've played with it for 15 minutes and I can't figure out why it doesn't like your window size.
ernow
ernow
The Paint event doesn't get automagically triggered.
When you resize it gets triggers so then it Paints; it has nothing to do with 500x500.

Just call this.Invalidate() in the form's load event... that should start the infinit loop.

BTW: check out this for more on the gameloop

Cheers
GroZZleR
GroZZleR
You obviously didn't bother to actually run his program.

"Application.Run(dxf)" will call the OnPaint method where he contiously invalidates the window. If you resize the window up, the triangle will show. If you resize it down, it disappears. The entire time, the background is being painted his Color.CornflowerBlue.
Sr_Guapo
Sr_Guapo
The code looks fine to me. However, In noticed that you never set a world matrix. I don't know if it is required, but definbately worth a shot. Add the code:

device.SetTransform(TransformType.World, Matrix.Identity);


either during the initialization (after the device creation of course) or during the onpaint method...

Edit: hmm... I tried out the code at I had the same results (with and without the world matrix part). I am using the june 2005 update. I tried some transformed verts and they worked fine, but the PostionColored won't work...

It does indeed work once the window is resized. It would seem that it wants the window to be wider than it is tall. It will not appear when the window is taller than it is wide. That is very interesting, never noticed that before...

[Edited by - Sr_Guapo on August 10, 2005 2:02:09 AM]
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
remigius
remigius
I have had the same thing happen to me every once in a while. Sometimes MDX will just happely display nothing when the Window size is very small, but there seems to be no pattern to it at all. For me it only happened for windows smaller than about 200x100 pixels, which I never really needed so I didn't bother to investigate...

No solution alas, just sympathy :)
ramy
ramy
hey, you didnt put ur vertices in the vertex buffer, or try putting them in there.
ninjaindark
ninjaindark
Thanks everyone.

I didn't notice it shows the triangle when the window is resized.
I have referenced some Managed DirectX books and none of them point out this issue.

This program works fine if I use CustomVertex.TransformedColored.
remigius
remigius
Finally got it :)

You're calculating the aspect ratio for the projection matrix using integers. When the height > the width, the aspect ratio will be 0 and things will be screwed.

I got it working by changing this line:

-----
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
-----

to this:

-----
float aspectRatio = ((float)this.Width) / ((float)this.Height);
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, aspectRatio, 1.0f, 100.0f);
-----
Muhammad Haggag
Muhammad Haggag
For future reference, please use a descriptive title for your threads. Something like "[MDX] Triangle not showing" would've been more appropriate.

That, and please read the Forum FAQ [smile]

ninjaindark
ninjaindark
Thanks for Remigius and everyone who help.

I have made it working and can proceed with my Managed DirectX study :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.