- Website design & DirectX code : Riemer Grootjans -
- World Space coordinates and camera view
Last chapter we drew
a triangle, using 'transformed' coordinates. These coordinates
are already 'transformed' so you can directly specify their
position on the screen. However, you will usually use the
untransformed coordinates, the so called World space
coordinates. These allow you to create a whole scene using
simple 3D coordinates, and, most important, to position a
camera and set its viewing point.
So we'll start with
redefining our triangle coordinates in world space. Replace
the code in your OnPaint method with this :
CustomVertex.PositionColored[] vertices = new
CustomVertex.PositionColored[3];
vertices[0].SetPosition(new Vector3(0f, 0f,
0f));
vertices[0].Color
= Color.Red.ToArgb();
vertices[1].SetPosition(new
Vector3(10f, 0f, 0f));
vertices[1].Color
= Color.Green.ToArgb();
vertices[2].SetPosition(new
Vector3(5f, 100f, 0f));
vertices[2].Color
= Color.Yellow.ToArgb();
All you've done here is changed the format from
pretransformed coordinates to 'normal' coordinats. Also let
the device know your format has changed :
device.VertexFormat =
CustomVertex.PositionColored.Format;
Let's run this code.
Very nice, your triangle has disappeared again. Why's that
? Easy, because you haven't told yet where to position the
camera and where to look at! To position your camera, simply
add the following code as the first lines in your OnPaint
method :
device.Transform.Projection =
Matrix.PerspectiveFovLH((float)Math.PI/4,
this.Width/this.Height, 1f, 50f);
device.Transform.View
= Matrix.LookAtLH(new Vector3(0,0,30), new Vector3(0,0,0), new
Vector3(0,1,0));
The first line tells the device what and how the
camera should look at the scene. The first parameter sets the
view angle, 90° in our case. Then comes the view aspect ratio.
The last parameters define the view range. Any objects closer
to the camera than 1f will not be shown. Any object farther
than 50f won't be shown either. The second line actually
positions the camera. The first parameter defines the
position. We position it 30 units above our (0,0,0) point, the
origin. The next parameter set the target the camera is
looking at. We will be looking at our origin. Then we only
need to define which vector will be considered as 'up'. Now
run the code again.
This time, we see a triangle, but it's all black! This is
because world space is a little more advanced than our
previous chapter. Here we are also required to place some
lights. However, these will be handled in a following chapter,
thus here we will simply tell our device to not expect any
lights. Add the following line underneath your camera
definition and you'll see our colored triangle :
device.RenderState.Lighting =
false;
Now everything has been set to use world space coordinates.
One thing you should remark: you'll see the green corner of
the triangle on the LEFT side of the window, while you defined
it on the POSITIVE x-axis. This is because DirectX
uses left-handed coordinates!! So, if you would position
your camera on the negative z-axis:
device.Transform.View = Matrix.LookAtLH(new
Vector3(0,0,-30), new Vector3(0,0,0), new
Vector3(0,1,0));
you would expect to see the green point in the right half
of the window. Try to run this now.
This might again not be exactly what you expected.
Something very important has happened. DirectX only draws
triangles that are facing the camera. DirectX defines that
triangles facing the camera should be drawn
counter-clockwise. If you position the camera on the
negative z-axis, the triangle will be defined clockwise
relative to the camera, and thus will not be drawn! One way to
remove this problem is simply redifining the vertices
counter-clockwise :
vertices[2].SetPosition(new Vector3(0f,
0f, 0f));
vertices[2].Color
= Color.Red.ToArgb();
vertices[1].SetPosition(new
Vector3(10f, 0f, 0f));
vertices[1].Color
= Color.Green.ToArgb();
vertices[0].SetPosition(new
Vector3(5f, 10f, 0f));
vertices[0].Color
= Color.Yellow.ToArgb();
This will indeed draw the triangle with the green point to
the right. The other way is to add the following line after
you camera definition :
device.RenderState.CullMode =
Cull.None;
This will simply draw all triangles, even those not facing
the camera. You should note that this should never be done iin
a final product, because it slows down the drawing process!
However, while designing, it is wise to turn of culling, so
you'll always see everything you draw. Also, I did chose the
background color to be non-black, again because if your
triangle might be wrongly defined and drawn black, you'll
still see it. So, while designing, you should turn culling off
and set a non-black backgroud color.
Here's the complete code again :
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
DirectX_Tutorial
{
public class WinForm :
System.Windows.Forms.Form
{
private Device device;
private System.ComponentModel.Container components =
null;
public WinForm()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.Opaque, true);
}
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(System.Windows.Forms.PaintEventArgs
e)
{
device.Transform.Projection
= Matrix.PerspectiveFovLH((float)Math.PI/4,
this.Width/this.Height, 1f, 50f);
device.Transform.View = Matrix.LookAtLH(new
Vector3(0,0,-30), new Vector3(0,0,0), new
Vector3(0,1,0));
device.RenderState.Lighting =
false;
device.RenderState.CullMode =
Cull.None;
CustomVertex.PositionColored[] vertices = new
CustomVertex.PositionColored[3];
vertices[0].SetPosition(new Vector3(0f, 0f,
0f));
vertices[0].Color =
Color.Red.ToArgb();
vertices[1].SetPosition(new Vector3(10f, 0f,
0f));
vertices[1].Color =
Color.Green.ToArgb();
vertices[2].SetPosition(new Vector3(5f, 10f,
0f));
vertices[2].Color =
Color.Yellow.ToArgb();
device.Clear(ClearFlags.Target, Color.DarkSlateBlue ,
1.0f, 0);
device.BeginScene();
device.VertexFormat =
CustomVertex.PositionColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList,
1, vertices);
device.EndScene();
device.Present();
this.Invalidate();
}
protected override void Dispose (bool
disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
private void
InitializeComponent()
{
this.components = new
System.ComponentModel.Container();
this.Size = new
System.Drawing.Size(500,500);
this.Text = "DirectX
Tutorial";
}
static void Main()
{
using (WinForm our_directx_form = new
WinForm())
{
our_directx_form.InitializeDevice();
Application.Run(our_directx_form);
}
}
}
}
|