XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   
My XNA Book
      
       Go to section on this site

Additional info


Latest Forum posts

 Account settings
  Posted by: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


Ads

Rotation and Translation

Now you have your triangle drawn by DirectX, the next step is to let it rotate. Up till now we have seen two ways to achieve this: we can rotate the camera around our triangle, but the logic way is to simply rotate the triangle. To do this, we have to rotate our World coordinate system. This is made very simple by DirectX. First we need a global variable which we use to store the current rotation of the triangle, and which we are going to increase every iteration of our game loop. So first declare this variable at the top of your file:

 float flt_Angle = 0;

Every iteration of your game loop, the DrawScene method is called. It is this method that we are going to change this chapter. When the method is called, the first thing we are going to do is increase the angle variable:

 flt_Angle += (float)0.05;

Now, immediately before you draw your triangle, we are going to tell DirectX to rotate its coordinate system by these lines:

 D3DXMATRIX m_Rotation;
 D3DXMatrixRotationY(&m_Rotation, flt_Angle);
 p_dx_Device->SetTransform(D3DTS_WORLD, &m_Rotation);

You first create a matrix, in which you store a rotation simply by calling the D3DXMatrixRotationY method. Then you pass this matrix to the device, and you indicate that you are changing its World coordinate system.

Compiling and running this code will already show you a rotating triangle! As you can see, the triangle is rotated around its left point. This is because this is locatated in the origin of the coordinate system, (0,0,0). If we would like to rotate through the center of the triangle, we should first move (=translate) the coordinate system to the center of the triangle, and the rotate. This is done by the following code:

 D3DXMATRIX m_Rotation;
 D3DXMatrixRotationY(&m_Rotation, flt_Angle);
 D3DXMATRIX m_Translation;
 D3DXMatrixTranslation(&m_Translation,-5,0,0);
 
 D3DXMATRIX m_World;
 D3DXMatrixMultiply(&m_World, &m_Translation, &m_Rotation);
 p_dx_Device->SetTransform(D3DTS_WORLD, &m_World);

First you define a rotation around the Y axis and a translation to the center of the bottomline of the triangle. Then we combine both actions into one matrix, m_World, which is done by multiplying both matrixes. In the end we pass this m_World matrix to the device. Now compile and run this code, DirectX should have moved the triangle to the center of the screen and the triangle should be rotation around its center axis!

Notice that the order of both actions is crucial: if you would first rotate the triangle, and the translate it, the result would be different. You can check this by using this line:

 D3DXMatrixMultiply(&m_World, &m_Rotation, &m_Translation);

Of course, instead of D3DXMatrixRotationY, you can also use D3DXMatrixRotationX, D3DXMatrixRotationZ or define a rotation axis yourself by using D3DXMatrixRotationAxis. Or you can combine rotation around the X, Y and Z axes by using D3DXMatrixRotationYawPitchRoll:

 D3DXMatrixRotationYawPitchRoll(&m_Rotation, flt_Angle,flt_Angle/2, flt_Angle/3);

These three terms are used in mechanics to describe the movements of larger objects. This is what you should see:




DirectX Tutorial 8 - Rotation - Translation

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



Click here to go to the forum on this chapter!


Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home1/riemers/public_html/eng/Tutorials/DirectX/C++/Series1/tut8.php(9) : eval()'d code(35) : eval()'d code on line 303



Remember culling ? While the triangle is rotating, during one half of the cycle the triangle should be culled by DirectX. To show this, you can remove the line that turns off culling. Remember to turn culling off again before moving on to the next chapter!

The code for this chapter:


#include<windows.h>
#include<d3d9.h>
#include<d3dx9.h>
struct OURCUSTOMVERTEX
{
    float x,y,z;
    DWORD color;
};


 int int_AppRunning = 1;
 float flt_Angle = 0;
 
 LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM parameter1,LPARAM parameter2)
 {
     switch(uint_Message)
     {
         case WM_KEYDOWN:
         {
             int_AppRunning = 0;
             break;
         }
         break;
     }
 
     return DefWindowProc(han_Wind,uint_Message,parameter1,parameter2);
 }
 
 HWND NewWindow(LPCTSTR str_Title,int int_XPos, int int_YPos, int int_Width, int int_Height)
 {
     WNDCLASSEX wnd_Structure;
 
     wnd_Structure.cbSize = sizeof(WNDCLASSEX);
     wnd_Structure.style = CS_HREDRAW | CS_VREDRAW;
     wnd_Structure.lpfnWndProc = OurWindowProcedure;
     wnd_Structure.cbClsExtra = 0;
     wnd_Structure.cbWndExtra = 0;
     wnd_Structure.hInstance = GetModuleHandle(NULL);
     wnd_Structure.hIcon = NULL;
     wnd_Structure.hCursor = NULL;
     wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
     wnd_Structure.lpszMenuName = NULL;
     wnd_Structure.lpszClassName = "WindowClassName";
     wnd_Structure.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
 
     RegisterClassEx(&wnd_Structure);
 
     return CreateWindowEx(WS_EX_CONTROLPARENT, "WindowClassName", str_Title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, int_XPos, int_YPos, int_Width, int_Height, NULL, NULL, GetModuleHandle(NULL), NULL);
 }
 
 LPDIRECT3DDEVICE9 InitializeDevice(HWND han_WindowToBindTo)
 {
     LPDIRECT3D9 p_dx_Object;
     LPDIRECT3DDEVICE9 p_dx_Device;
 
     p_dx_Object = Direct3DCreate9(D3D_SDK_VERSION);
     if (p_dx_Object == NULL)
     {
         MessageBox(han_WindowToBindTo,"DirectX Runtime library not installed!","InitializeDevice()",MB_OK);
     }
 
     D3DPRESENT_PARAMETERS dx_PresParams;
     ZeroMemory( &dx_PresParams, sizeof(dx_PresParams) );
     dx_PresParams.Windowed = TRUE;
     dx_PresParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
     dx_PresParams.BackBufferFormat = D3DFMT_UNKNOWN;
 
     if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, han_WindowToBindTo, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))
     {
         if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, han_WindowToBindTo, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))
         {
             MessageBox(han_WindowToBindTo,"Failed to create even the reference device!","InitializeDevice()",MB_OK);
         }
  }
 
     p_dx_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
     p_dx_Device->SetRenderState(D3DRS_LIGHTING,false);
 
     return p_dx_Device;
 }
 
 void DrawScene(LPDIRECT3DDEVICE9 p_dx_Device, LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer)
 {
     flt_Angle += (float)0.05;
 
     p_dx_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(72,61,139), 1.0f, 0);
     p_dx_Device->BeginScene();
 
     D3DXMATRIX m_Rotation;
     D3DXMatrixRotationYawPitchRoll(&m_Rotation, flt_Angle,flt_Angle/2, flt_Angle/3);
     D3DXMATRIX m_Translation;
     D3DXMatrixTranslation(&m_Translation,-5,0,0);
 
     D3DXMATRIX m_World;
     D3DXMatrixMultiply(&m_World, &m_Rotation, &m_Translation);
     p_dx_Device->SetTransform(D3DTS_WORLD, &m_World);
 
     p_dx_Device->SetStreamSource(0, p_dx_VertexBuffer, 0, sizeof(OURCUSTOMVERTEX));
     p_dx_Device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
     p_dx_Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
 
     p_dx_Device->EndScene();
     p_dx_Device->Present(NULL, NULL, NULL, NULL);
 }
 
 LPDIRECT3DVERTEXBUFFER9 FillVertices(HWND han_Window, LPDIRECT3DDEVICE9 p_dx_Device)
 {
     OURCUSTOMVERTEX cv_Vertices[3];
     LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer;
 
     cv_Vertices[0].x = 5.0f;
     cv_Vertices[0].y = 10.0f;
     cv_Vertices[0].z = 0.0f;
     cv_Vertices[0].color = 0xffff0000;
 
     cv_Vertices[1].x = 10.0f;
     cv_Vertices[1].y = 0.0f;
     cv_Vertices[1].z = 0.0f;
     cv_Vertices[1].color = 0xff00ff00;
 
     cv_Vertices[2].x = 0.0f;
     cv_Vertices[2].y = 0.0f;
     cv_Vertices[2].z = 0.0f;
     cv_Vertices[2].color = 0xff00ffff;
 
     if (FAILED(p_dx_Device->CreateVertexBuffer(3*sizeof(OURCUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &p_dx_VertexBuffer, NULL)))
     {
         MessageBox(han_Window,"Error while creating VertexBuffer","FillVertices()",MB_OK);
     }
 
     VOID* p_Vertices;
     if (FAILED(p_dx_VertexBuffer->Lock(0, 3*sizeof(OURCUSTOMVERTEX), (void**)&p_Vertices, 0)))
     {
         MessageBox(han_Window,"Error trying to lock","FillVertices()",MB_OK);
     }else{
         memcpy(p_Vertices, cv_Vertices, 3*sizeof(OURCUSTOMVERTEX));
         p_dx_VertexBuffer->Unlock();
     }
 
     return p_dx_VertexBuffer;
 }
 
 void SetUpCamera(LPDIRECT3DDEVICE9 p_dx_Device)
 {
     D3DXVECTOR3 m_EyePos(0, 0, -30);
     D3DXVECTOR3 m_TargetPos(0, 0, 0);
     D3DXVECTOR3 m_UpVector(0, 1, 0);
     D3DXMATRIXA16 m_View;
     D3DXMatrixLookAtLH(&m_View, &m_EyePos, &m_TargetPos, &m_UpVector);
     p_dx_Device->SetTransform(D3DTS_VIEW, &m_View);
 
     D3DXMATRIX m_Projection;
     D3DXMatrixPerspectiveFovLH(&m_Projection, D3DX_PI/4, 500/500, 1, 50);
     p_dx_Device->SetTransform(D3DTS_PROJECTION, &m_Projection);
 }
 
 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow)
 {
     MSG msg_Message;
 
     HWND han_Window = NewWindow("DirectX C++ Tutorial",100,100,500,500);
     LPDIRECT3DDEVICE9 p_Device = InitializeDevice(han_Window);
     LPDIRECT3DVERTEXBUFFER9 p_dx_VB = FillVertices(han_Window, p_Device);
     SetUpCamera(p_Device);
 
     while(int_AppRunning)
     {
         if(PeekMessage(&msg_Message,han_Window,0,0,PM_REMOVE))
         {
             DispatchMessage(&msg_Message);
         }
         DrawScene(p_Device, p_dx_VB);
     }
 
     p_Device->Release();
     DestroyWindow(han_Window);
 
     return 0;
 }


Google
 
Webwww.riemers.net


If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



- Website design & XNA + DirectX code : Riemer Grootjans -
©2003 - 2011 Riemer Grootjans
Translations

This site in English
This site in Korean
This site in Czech

Microsoft MVP Award



2007 - 2011 MVP Award
DirectX - XNA

Contents

News
Home
Forum
XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Downloads
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
XNA 4.0 using C# (89)
DirectX using C# (54)
DirectX using C++ (15)
Series 1: Terrain (15)
Opening a window
Ending the game loop
Linking to the Device
Clearing your window
Drawing a triangle
Culling
Camera
Rotation - Translation
Indices
Terrain creation
Terrain from file
DirectInput
Importing .bmp files
Adding colors
DirectX Light basics
DirectX using VB (2)
-- Expand all --


Thank you!

Support this site --
any amount is welcome !

Stay up-to-date

I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!