|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
The Transformation Pipeline In our last tutorial, we created a triangle by specifying in screen coordinates where we wanted to put it. This basic assignment multiplies the running speed by the amount of time elapsed since the last update. TheoryWhilst useful, the transformation pipeline can often be hard to understand.
This shows a triangle with its coordinates defined relatively. The center point is defined as coordinate (0,0), and the three vertices being based off that. Now, somone places this box in a game world. The box is scaled by 3 to make it bigger, and to be moved into the game world it has to be translated into a new position. Basically, a matrix is a grid of numbers we can perform mathematical operations on to dynamically change the position of a primitive in DirectX. NOTE: In DirectX, we use the word “Transformed’ to describe vertices which have already been declared in screen-space coordinates (ie our last tutorial) and Positioned to describe those which have been declared in object space (relative) coordinates. The new cObject class: class cPosObject
{
public CustomVertex.PositionColored[] itsVerts;
public Matrix itsTransform;
public bool isActive;
}
As you can see, this is very similar to our previous class, with the exception that out CustomVertex has been changed to a PositionColored as opposed to TransformedColored. This is explained in the note above. We will assign this Matrix object in the constructor of our new cPosTriangle class. The class will have two methods and no members. The methods will be its constructor and the render method. The ability to render itself will be very useful when dealing with large objects. You will be able to see an example of this in the tutorial. public cPosTriangle(bool ac, Vector3 Translation, Vector3 Scale, Vector3 Rotation,Color pColor)
Ok, calm down and move the mouse away from the large X at the top right of your screen. Because we now have a better system, you no longer need to input a set of vertices into the method, instead we can define the points as Vector3’s and our scaling matrix can make any triangle from them. And finally, our Render() method: public void Render(Device device)
{
device.Transform.World = itsTransform;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, itsVerts);
}
The only change here is the assignment of itsTransform to device.Transform.World. This sets how the device will render all primitives until it is re-assigned. The next step is to change our OnPaint event. We need to call some methods in the rendering loop to set how we look at the scene. device.Transform.View = Matrix.LookAtLH(new Vector3(0, -40, 300), new Vector3(0, -5, 0), new Vector3(0, 1, 0));
This method defines the properties of our view point, or camera. The Matrix.LookAtLH is defined as follows: The first paramater is fairly self explanatory, a coordinate for the position of the camera. The second coordinate is also pretty obvious, the target (ie where the camera is looking at). This is used with the cameras position to create a vector-line from the camera to its target so DX can calculate where the camera is looking. As the player moves the mouse, the camera moves along with the crosshair. Although these are not directly linked it’s a good way to see how the parameters would be used in a real-life application (well not real life as such, but..) The camera position would be the player moving around the screen. Now feast your eyes upon my latest diagram. Although not usually of a high standard, this one should be even more interesting as my laptop (which I am currently using) does not have any image editing software for which the trial has not run out except for the world-(in?)famous MS Paint.
Ok, here you see a bad guy, representing the camera target (what the player is looking at) and three possible camera positions (where the player is standing). The last stage in setting up a position-coordinate based system is to change the Projection. This is performed in a similar way to setting up the camera stage. Matrix.PerspectiveFovLH(Field of View,Aspect Ratio, Near Plane, Far Plane);
The first parameter is our “viewing cone” as discussed earlier. This will usually be PI / 4 (quarter-circle) to mimic the human eye’s vision.
The red area denotes the area which will be rendered. Any objects before our near plane, after our far plane or outside of our viewing cone will be clipped (ignored). The actual calls to set our camera and projection in the OnPaint method are as follows: device.Transform.View = Matrix.LookAtLH(new Vector3(0, -40, 300), new Vector3(0, -5, 0), new Vector3(0, 1, 0));
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, Width / Height, 1f, 500f);
Feel free to play around with these variables after you have read the tutorial to see the different reactions you get by changing them. As you can see, besides the color parameter, the only thing that changes is the Translation vector. This increases globally by one each time. foreach (cTriangle Triangle in Triangles)
if (Triangle != null)
Triangle.Render(device);
As you can see, this loops through every triangle in the array, tests whether or not they are null, and if not it calls its Render() method. I encourage you to play with the camera and triangle variables – playing with scaling, rotating, translating, camera targets etc. FeedbackI am always open for answering questions, whether through MSN (jamespraveen@aol.com), email (james@magclan.cwhnetworks.com) or through the message board attatched to this article. History21/02/06: First submitted article 4 to Codeproject 22/02/06: Re-formatted code syntax
Previous ArticlesPart 1: Setting Up DirectX
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||