Direct3D Problem

sh0

Newcomer
Joined
Jul 26, 2003
Messages
1
I write this program based on Direct3D Tutorial 03 from DxSDK
but when Im run it i see only black screen. Whats is wrong ?. Thx for help.

Code:
/************************************************************************/
using System;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
/************************************************************************/
namespace mdx
{
	class Direct3DTutor : Form
	{
		//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
		private Device			device			= null;
		private	VertexBuffer	vertexBuffer	= null;
		//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
		public Direct3DTutor()
		{
			this.ClientSize	= new System.Drawing.Size(400, 300);
			this.Text		= "Direct3D Tutorials";
		}
		//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
		private bool InitializeDirect3D()
		{
			try
			{
				PresentParameters presentParams = new PresentParameters();
				presentParams.SwapEffect	= SwapEffect.Discard;
				presentParams.Windowed		= true;

				device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
				device.DeviceCreated	+= new EventHandler(this.OnDeviceCreated);
				device.DeviceReset		+= new EventHandler(this.OnDeviceReset);
				this.OnDeviceCreated(device, null);
				this.OnDeviceReset(device, null);

				return true;
			}
			catch(DirectXException e)
			{
				MessageBox.Show("InitializeDirect3D(): " + e.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
		}
		//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
		private void OnDeviceCreated(Object sender, EventArgs e)
		{
			Device dev = (Device)sender;
			
			vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 3, dev, 0, CustomVertex.PositionColored.Format, Pool.Default);
			vertexBuffer.Created += new EventHandler(this.OnVertexBufferCreated);
			this.OnVertexBufferCreated(vertexBuffer, null);
		}
		//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
		private void OnDeviceReset(Object sender, EventArgs e)
		{
			Device dev = (Device)sender;

			dev.RenderState.CullMode	= Cull.None;
			dev.RenderState.Lighting	= false;
		}
		//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
		private void OnVertexBufferCreated(Object sender, EventArgs e)
		{
			VertexBuffer vb = (VertexBuffer)sender;
			CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
			GraphicsStream gs = vb.Lock(0, 0, 0);

			verts[0].X		= 150;
			verts[0].Y		= 50;
			verts[0].Z		= 0.5f;
			verts[0].Color	= System.Drawing.Color.SpringGreen.ToArgb();

			verts[1].X		= 250;
			verts[1].Y		= 250;
			verts[1].Z		= 0.5f;
			verts[1].Color	= System.Drawing.Color.SkyBlue.ToArgb();

			verts[2].X		= 50;
			verts[2].Y		= 250;
			verts[2].Z		= 0.5f;
			verts[2].Color	= System.Drawing.Color.Silver.ToArgb();

			gs.Write(verts);

			vb.Unlock();
		}
		//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
		private void SetupMatrices()
		{
			device.Transform.World	= Matrix.RotationY(Environment.TickCount / 150.0f);
			device.Transform.View	= Matrix.LookAtLH(new Vector3(0.0f, 3.0f,-5.0f),
													  new Vector3(0.0f, 0.0f, 0.0f),
													  new Vector3(0.0f, 1.0f, 0.0f));
			device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);
		}
		//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
		private void Render()
		{
			if(device == null)
				return;

			device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);

			device.BeginScene();
				SetupMatrices();
				device.SetStreamSource(0, vertexBuffer, 0);
				device.VertexFormat = CustomVertex.PositionColored.Format;
				device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
			device.EndScene();
			device.Present();
		}
		//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
		[STAThread]
		static void Main()
		{
			Direct3DTutor frm = new Direct3DTutor();

			if(!frm.InitializeDirect3D())
			{
				MessageBox.Show("Direct3D Initialization Failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			frm.Show();

			while(frm.Created)
			{
				frm.Render();
				Application.DoEvents();
			}
		}
		//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	}
}
 
Your Camera view is looking down,
You are clipping off the back of your triangle in the projection view. You need to think about position of the camera.

You are cutting off the image, Think about your settings for a second.

You are creating a Triangle vertex
150,50,.5
250,250,.5
50,250,.5

Then your view you have defined as follows as it place the camera
below your triangle, looking at a downward angle

device.Transform.View= Matrix.LookAtLH(
new Vector3(0.0f, 3.0f,-5.0f), //This put Camer Up 3 , back 5
new Vector3(0.0f, 0.0f, 0.0f), // Looking at 0,0,0 below triangle
new Vector3(0.0f, 1.0f, 0.0f)); // Looking up 1

So try this...It positons the camera up 250 and back 700
then it changes what it is looking at to be 150 units up the Y axis

device.Transform.View= Matrix.LookAtLH(
Matrix.LookAtLH(new Vector3(0.0f, 250.0f,-700.0f),
new Vector3(0.0f,150.0f, 0.0f),
new Vector3(0.0f, 1.0f, 0.0f));

Next your Projection cuts off the back so change it to

device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 1500.0f);

These lines will make it visable so you can play with the setting and decide you you want it to work...
 
Back
Top