Home Articles Tutorials Resources About

Tutorial 3 - Spinning a triangle


By Pieter Germishuys, January 4 2006

Welcome back.
I hope you had some fun drawing/rendering some triangles. Well hopefully after today you will know a little more about moving things around. Yep, that's it. Moving/Rotating. In the Graphic development world we have a 2 dimensional array known as matrices that will help us in Translating (The term for moving things)/Rotating objects around.
So what do we need to know?
Well let's start off with an explanation of what really happens when you create an object. This object is known to be in a space. Space? What?s a space? A Space is simply the area where you define this object. Maybe with a example I can explain this a bit better. Also an understanding of coordinate systems. Left/Right handed coordinate systems. If you want a good reference. The DX SDK Docs has a nice explenation of what they are.
Let me explain what is going on in that figure. When we define the object?s dimensions. We are defining it in object space. This means that these coordinates/dimensions we give the object is relative to the object. Then through a World Transform which I will explain in a second we place it in the world. The figure shows that very clearly.So that?s cool. So let?s move on. What else do we need? A View space offcourse. We need to be able to see this object in the world. So then we also need to be able to define a pertrution/perspective. What is this? This is basically how to tell direct3d that the further away objects are the smaller they are and also gives them depth like the box that you see in fig1.0 is defined with orthogonal projection. Example in fig 1.1


Is that it you ask? Yep that?s it. Let?s look at some code shall we?
Three matrices. One for our world, one for projection and finally one for our view(eye). We also then create 3 vectors. What is a vector you ask? It is a quantity that has both magnitude and direction. We need to tell direct3d where the eye(camera) is in this case we push the camera -1.0f closer to us., where to look at and finally. What is up? We will specify Y as being up in our world.

private Matrix worldMatrix = new Matrix();
private Matrix projMatrix = new Matrix();
private Matrix viewMatrix = new Matrix();
private Vector3 eye = new Vector3(0.0f, 0.0f, -1.0f);
private Vector3 lookAt = new Vector3(0.0f, 0.0f, 0.0f);
private Vector3 up = new Vector3(0.0f, 1.0f, 0.0f);

Let's see what we do here. First off is the view matrix. We build a Left handed view matrix with the values that we specified. Next is the petrusion/projection matrix. We build the projection matrix with the left handed coordinate system. The field of view (What is our angle of view.) Normally it's PI/4. Secondly it's the aspect ratio which we define as one. It's 1 for a square. It's the width divided by the height. So with a rectangular monitor with a resolution of 800/600 it would be 1.3. Third is the near clipping plane. Anything small than the near clipping plane will not get rendered and lastly is the far clipping plane which will do the same for object further than that value.
Then we just set the matrices.
viewMatrix = Matrix.LookAtLH(eye, lookAt, up); //Set the view matrix to be a left handed coordinate system.
direct3d.XilathDevice.Transform.View = viewMatrix; //set the view matrix
projMatrix = Matrix.PerspectiveFovLH( (float)Math.PI/4, 1.0f, 1.0f, 1000.0f ); //build the projection matrix
direct3d.XilathDevice.Transform.Projection = projMatrix; //set the projection matrix
You are probably asking/thinking where on earth that world matrix is hiding. Well I saved the best for last. Let's rotate this triangle.
angle += 0.01f; //increase the angle each frame. This can be any variable
worldMatrix.RotateZ(angle); //rotate the world matrix with this angle
direct3d.XilathDevice.Transform.World = worldMatrix; //set the world matrix. Basically telling direct3d how this object is behaving in the world
That's it. Really simple isn't it? What do you want next? Do not hesitate to contact me. I am happy to hear your views. If you have any problems/comments/critism about the code or anything concept related. Do not hesitate. Till next time. Happy spinning.




Files for this tutorial

Filename Size
? tut3.rar 120.8 KB
?
MDX info is an initiative by vector4. All content is copyright ? 2005-2006 by its respective authors | About MDX info | Terms of Use |
Coming soon!