m***@bmtmis.demon.co.uk
2008-02-11 09:09:07 UTC
Hi,
Can anyone help me get going here please?
I have Visual Studio 2005 and have installed the DirectX SDK. I hate
including lumps of code in a post BUT all the below code works
perfectly - I end up rendering a traingle which is defined in screen
coordinates. So there's no need to look over the code for any errors.
My question is, how can I render a truly 3D object - if I make the z-
coordinates of any of the traingle vertices anything other than 0 then
those corners dissappear - which makes some sense as I think I must be
some 2d rendering mode. So, here'e my code:
In the program.cs file I simply have:
***********************************************************
static void Main()
{
using (Form1 form = new Form1(true,800,600))
{
if (!form.InitializeGraphics())
{
MessageBox.Show("Unable to initialize DirectX.");
form.Dispose();
return;
}
Application.Idle += new
EventHandler(form.OnApplicationIdle);
Application.Run(form);
}
}
***********************************************************
No problem there. Here's my form's constuctor:
***********************************************************
public Form1(bool bWindowed, int width, int height)
{
InitializeComponent();
m_bWindowed = bWindowed;
this.ClientSize = new Size(width, height);
}
***********************************************************
Here's my InitializeGraphics definition:
***********************************************************
public bool InitializeGraphics()
{
m_displayMode = Manager.Adapters[0].CurrentDisplayMode;
m_caps = Manager.GetDeviceCaps(0, DeviceType.Hardware);
CreateFlags flags;
if (m_caps.DeviceCaps.SupportsHardwareTransformAndLight)
{
flags = CreateFlags.HardwareVertexProcessing;
if (m_caps.DeviceCaps.SupportsPureDevice)
{
flags |= CreateFlags.PureDevice;
}
}
else
{
flags = CreateFlags.SoftwareVertexProcessing;
}
if (!BuildPresentParameters())
{
return false;
}
try
{
m_device = new Device(0, DeviceType.Hardware,
this.Handle, flags, m_pp);
m_vertices = CreateVertexBuffer(m_device);
return true;
}
catch(DirectXException)
{
return false;
}
}
***********************************************************
Here's my BuildPresentParameters definition:
***********************************************************
public bool BuildPresentParameters()
{
m_pp = new PresentParameters();
Format adaptorFormat = (m_bWindowed) ?
m_displayMode.Format : Format.X8R8G8B8;
if (Manager.CheckDeviceFormat(0, DeviceType.Hardware,
adaptorFormat, Usage.DepthStencil, ResourceType.Surface,
DepthFormat.D24S8))
{
m_pp.AutoDepthStencilFormat = DepthFormat.D24S8;
}
else if (Manager.CheckDeviceFormat(0, DeviceType.Hardware,
adaptorFormat, Usage.DepthStencil, ResourceType.Surface,
DepthFormat.D24X8))
{
m_pp.AutoDepthStencilFormat = DepthFormat.D24X8;
}
else if (Manager.CheckDeviceFormat(0, DeviceType.Hardware,
adaptorFormat, Usage.DepthStencil, ResourceType.Surface,
DepthFormat.D16))
{
m_pp.AutoDepthStencilFormat = DepthFormat.D16;
}
else
{
return false;
}
m_pp.BackBufferWidth = (m_bWindowed) ? 0 :
m_displayMode.Width;
m_pp.BackBufferHeight = (m_bWindowed) ? 0 :
m_displayMode.Height;
m_pp.BackBufferFormat = adaptorFormat;
m_pp.BackBufferCount = 1;
m_pp.MultiSample = MultiSampleType.None;
m_pp.MultiSampleQuality = 0;
m_pp.SwapEffect = SwapEffect.Discard;
m_pp.DeviceWindowHandle = this.Handle;
m_pp.Windowed = m_bWindowed;
m_pp.EnableAutoDepthStencil = true;
m_pp.PresentFlag = PresentFlag.DiscardDepthStencil;
m_pp.FullScreenRefreshRateInHz = (m_bWindowed) ? 0 :
m_displayMode.RefreshRate;
m_pp.PresentationInterval = PresentInterval.Immediate;
return true;
}
***********************************************************
And here's my CretateVertexBuffer method:
***********************************************************
protected VertexBuffer CreateVertexBuffer(Device device)
{
device.VertexFormat =
CustomVertex.TransformedColored.Format;
CustomVertex.TransformedColored[] verts = new
CustomVertex.TransformedColored[3];
verts[0] = new
CustomVertex.TransformedColored(this.Width / 2, this.Height / 4, 0.0F,
1,Color.Blue.ToArgb());
verts[1] = new CustomVertex.TransformedColored(this.Width
* 3 / 4, this.Height * 3 / 4, 0.0F, 1,Color.Green.ToArgb());
verts[2] = new
CustomVertex.TransformedColored(this.Width / 4, this.Height * 3 / 4,
0.0F, 1,Color.Red.ToArgb());
VertexBuffer buf = new
VertexBuffer(typeof(CustomVertex.TransformedColored),verts.Length,device,
0,CustomVertex.TransformedColored.Format,Pool.Default);
GraphicsStream stm = buf.Lock(0, 0, 0);
stm.Write(verts);
buf.Unlock();
return buf;
}
***********************************************************
And finally RenderFrame:
***********************************************************
private void RenderFrame()
{
m_device.Clear(ClearFlags.Target, Color.Black, 0.0f, 0);
m_device.BeginScene();
m_device.VertexFormat =
CustomVertex.TransformedColored.Format;
m_device.SetStreamSource(0, m_vertices, 0);
m_device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
m_device.EndScene();
m_device.Present();
}
***********************************************************
That's it. It all works great. 2D Colourful triangle. Let's say I
change all the z-ordinates of the traingle's vertices to -0.5. It
dissappears. Let's say I change the triangles' vertices to
(-1,-1,-0.5),(1,-1,-0.5),(0,1,-0.5) What in the code should I change
to allow me to view this:
I was naively hoping to simply add:
m_device.Transform.World = Matrix.Identity;
Microsoft.DirectX.Vector3 cameraPosition = new
Vector3(0.0f, 1.2f, -2.0f);
Microsoft.DirectX.Vector3 cameraTarget = new Vector3(0.0f,
0.0f, 0.0f);
Microsoft.DirectX.Vector3 cameraUpVector = new
Vector3(0.0f, 1.0f, 0.0f);
m_device.Transform.View = Matrix.LookAtLH(cameraPosition,
cameraTarget,
cameraUpVector);
m_device.Transform.Projection =
Matrix.PerspectiveFovLH((float)System.Math.PI / 4,
4.0f / 3.0f,
1.0f,
100.0f);
Into the render code immediately after device.clear().
Thanks for any help concerning my ignorance.
Mitch.
Can anyone help me get going here please?
I have Visual Studio 2005 and have installed the DirectX SDK. I hate
including lumps of code in a post BUT all the below code works
perfectly - I end up rendering a traingle which is defined in screen
coordinates. So there's no need to look over the code for any errors.
My question is, how can I render a truly 3D object - if I make the z-
coordinates of any of the traingle vertices anything other than 0 then
those corners dissappear - which makes some sense as I think I must be
some 2d rendering mode. So, here'e my code:
In the program.cs file I simply have:
***********************************************************
static void Main()
{
using (Form1 form = new Form1(true,800,600))
{
if (!form.InitializeGraphics())
{
MessageBox.Show("Unable to initialize DirectX.");
form.Dispose();
return;
}
Application.Idle += new
EventHandler(form.OnApplicationIdle);
Application.Run(form);
}
}
***********************************************************
No problem there. Here's my form's constuctor:
***********************************************************
public Form1(bool bWindowed, int width, int height)
{
InitializeComponent();
m_bWindowed = bWindowed;
this.ClientSize = new Size(width, height);
}
***********************************************************
Here's my InitializeGraphics definition:
***********************************************************
public bool InitializeGraphics()
{
m_displayMode = Manager.Adapters[0].CurrentDisplayMode;
m_caps = Manager.GetDeviceCaps(0, DeviceType.Hardware);
CreateFlags flags;
if (m_caps.DeviceCaps.SupportsHardwareTransformAndLight)
{
flags = CreateFlags.HardwareVertexProcessing;
if (m_caps.DeviceCaps.SupportsPureDevice)
{
flags |= CreateFlags.PureDevice;
}
}
else
{
flags = CreateFlags.SoftwareVertexProcessing;
}
if (!BuildPresentParameters())
{
return false;
}
try
{
m_device = new Device(0, DeviceType.Hardware,
this.Handle, flags, m_pp);
m_vertices = CreateVertexBuffer(m_device);
return true;
}
catch(DirectXException)
{
return false;
}
}
***********************************************************
Here's my BuildPresentParameters definition:
***********************************************************
public bool BuildPresentParameters()
{
m_pp = new PresentParameters();
Format adaptorFormat = (m_bWindowed) ?
m_displayMode.Format : Format.X8R8G8B8;
if (Manager.CheckDeviceFormat(0, DeviceType.Hardware,
adaptorFormat, Usage.DepthStencil, ResourceType.Surface,
DepthFormat.D24S8))
{
m_pp.AutoDepthStencilFormat = DepthFormat.D24S8;
}
else if (Manager.CheckDeviceFormat(0, DeviceType.Hardware,
adaptorFormat, Usage.DepthStencil, ResourceType.Surface,
DepthFormat.D24X8))
{
m_pp.AutoDepthStencilFormat = DepthFormat.D24X8;
}
else if (Manager.CheckDeviceFormat(0, DeviceType.Hardware,
adaptorFormat, Usage.DepthStencil, ResourceType.Surface,
DepthFormat.D16))
{
m_pp.AutoDepthStencilFormat = DepthFormat.D16;
}
else
{
return false;
}
m_pp.BackBufferWidth = (m_bWindowed) ? 0 :
m_displayMode.Width;
m_pp.BackBufferHeight = (m_bWindowed) ? 0 :
m_displayMode.Height;
m_pp.BackBufferFormat = adaptorFormat;
m_pp.BackBufferCount = 1;
m_pp.MultiSample = MultiSampleType.None;
m_pp.MultiSampleQuality = 0;
m_pp.SwapEffect = SwapEffect.Discard;
m_pp.DeviceWindowHandle = this.Handle;
m_pp.Windowed = m_bWindowed;
m_pp.EnableAutoDepthStencil = true;
m_pp.PresentFlag = PresentFlag.DiscardDepthStencil;
m_pp.FullScreenRefreshRateInHz = (m_bWindowed) ? 0 :
m_displayMode.RefreshRate;
m_pp.PresentationInterval = PresentInterval.Immediate;
return true;
}
***********************************************************
And here's my CretateVertexBuffer method:
***********************************************************
protected VertexBuffer CreateVertexBuffer(Device device)
{
device.VertexFormat =
CustomVertex.TransformedColored.Format;
CustomVertex.TransformedColored[] verts = new
CustomVertex.TransformedColored[3];
verts[0] = new
CustomVertex.TransformedColored(this.Width / 2, this.Height / 4, 0.0F,
1,Color.Blue.ToArgb());
verts[1] = new CustomVertex.TransformedColored(this.Width
* 3 / 4, this.Height * 3 / 4, 0.0F, 1,Color.Green.ToArgb());
verts[2] = new
CustomVertex.TransformedColored(this.Width / 4, this.Height * 3 / 4,
0.0F, 1,Color.Red.ToArgb());
VertexBuffer buf = new
VertexBuffer(typeof(CustomVertex.TransformedColored),verts.Length,device,
0,CustomVertex.TransformedColored.Format,Pool.Default);
GraphicsStream stm = buf.Lock(0, 0, 0);
stm.Write(verts);
buf.Unlock();
return buf;
}
***********************************************************
And finally RenderFrame:
***********************************************************
private void RenderFrame()
{
m_device.Clear(ClearFlags.Target, Color.Black, 0.0f, 0);
m_device.BeginScene();
m_device.VertexFormat =
CustomVertex.TransformedColored.Format;
m_device.SetStreamSource(0, m_vertices, 0);
m_device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
m_device.EndScene();
m_device.Present();
}
***********************************************************
That's it. It all works great. 2D Colourful triangle. Let's say I
change all the z-ordinates of the traingle's vertices to -0.5. It
dissappears. Let's say I change the triangles' vertices to
(-1,-1,-0.5),(1,-1,-0.5),(0,1,-0.5) What in the code should I change
to allow me to view this:
I was naively hoping to simply add:
m_device.Transform.World = Matrix.Identity;
Microsoft.DirectX.Vector3 cameraPosition = new
Vector3(0.0f, 1.2f, -2.0f);
Microsoft.DirectX.Vector3 cameraTarget = new Vector3(0.0f,
0.0f, 0.0f);
Microsoft.DirectX.Vector3 cameraUpVector = new
Vector3(0.0f, 1.0f, 0.0f);
m_device.Transform.View = Matrix.LookAtLH(cameraPosition,
cameraTarget,
cameraUpVector);
m_device.Transform.Projection =
Matrix.PerspectiveFovLH((float)System.Math.PI / 4,
4.0f / 3.0f,
1.0f,
100.0f);
Into the render code immediately after device.clear().
Thanks for any help concerning my ignorance.
Mitch.