www.xbdev.net
xbdev - software development
Thursday March 28, 2024
home | contact | Support | DirectX.. Its doing all the hard work for us...
>>

PrtX. A taste of some matrix basics and a rotating triangle..
by bkenwright@xbdev.net

Matrix's matrix!  There not that bad... (download full source code).

 

Woww... a rotating triangle!  Yup this is the start of the real power in 3D.  As where going to show the simple basics of how matrix's are used to apply to vertices.  As the principles are the same...whether your rotating a single triangle as we are doing here... or a whole 2000 vertice 3D animated alien.

Now for the code!

/***************************************************************************/

/*                                                                         */

/* File: game.cpp                                                          */

/*                                                                         */

/***************************************************************************/

 

/***************************************************************************/

 /***   ***    ****    *******  *******   ***  ***  ***   ******************

  **** ****   ******   *******  ********  ***   ******      *******

  *********  ***  ***    ***    ***  ***  ***     ***         *******

  *** * ***  ********    ***    *******   ***    *****           *****

  ***   ***  ***  ***    ***    ***  ***  ***   *** ***           *********

  ***   ***  ***  ***    ***    ***  ***  ***  ***   ***  ******************/

/***************************************************************************/

 

// Don't let your fears get the worst of you!...lol... (its all in your mind)..

// takes me back to the film the matrix that does..heheh.

// Anyhow, matrix's are just one way of moving our shapes and world around!

// For example if you wanted to move an object of 200 (x,y,z) vertices away

// from you, you could alternativly jsut add a fixed value to all the z values.

 

// A better method is to use matrixes!  Which works on a similar principle.

// You generate a matrix which will do everything at once.  For example you

// could do a rotation and movement with one matrix, which is just applied

// to all the vertices of your shape once!  Saves doing it twice, once for

// rotating your points and once for moving it!... well thats the principle

// anyhow... lets put together a small piece of code that shows you just that.

 

#include <d3dx8.h>

#include <d3d8.h>

 

// ~~~1~~~ Create a function which draws a triangle.

// you would call this function from within...

// g_pD3DDevice->Clear( .... );

// call draw_triangle here!

// g_pD3DDevice->Present(..).

void draw_triangle()

{

      struct my_vertex

      {

            FLOAT x, y, z;  // D3DFVF_XYZ

            DWORD colour;   // D3DFVF_DIFFUSE

      };

      my_vertex v[] =

      {   // Three points make up the face of the triangle.

            {-1.0f,-1.0f, 0.0f, 0xffffff00},  //[1]

            {0.0f, 1.0f, 0.0f, 0xffff2200},   //[2]

            {1.0f, -1.0f, 0.0f, 0xffffff00}   //[3]

      };

      UINT my_vertex_description = (D3DFVF_XYZ | D3DFVF_DIFFUSE);

 

      IDirect3DVertexBuffer8 * DX_vb;

      g_pD3DDevice->CreateVertexBuffer( 3*sizeof(my_vertex), 0, my_vertex_description, D3DPOOL_MANAGED, &DX_vb );

 

      // Copy our array which is in computer memory over to the directX memory.. using that pointer we

      // just created etc.

      unsigned char *temp_pointer_vb;

      DX_vb->Lock(0,0, &temp_pointer_vb, 0);

      memcpy(temp_pointer_vb, v, sizeof(v) );

      DX_vb->Unlock();

 

      g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

      g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);

      g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_DIFFUSE);

 

      // Draw our triangle.

      g_pD3DDevice->SetStreamSource(0, DX_vb, sizeof(my_vertex));

      g_pD3DDevice->SetVertexShader(my_vertex_description);

      g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

 

      DX_vb->Release();

}

 

 

// Not really done much matrix stuff yet!.. but be patient!

void Render()

{

      if(!g_pD3DDevice)return;

 

      static float angle = 0.0f;

      angle += 0.001f;

 

      // Clear the back buffer to a blue color

      g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

 

      //~~~*~~~~ Create a matrix

      D3DXMATRIX mx;

      //~~~*~~~~ Do somthing to our empty matrix.

      D3DXMatrixRotationZ(&mx, angle ); // angle in radians...eg. 1 degree = PI/180

      //~~~*~~~~ Use the matrix! No use having a matrix if we don't use it!

      g_pD3DDevice->SetTransform(D3DTS_WORLD, &mx);

 

 

      //?? Well what this means is that this matrix we have just filled in and put into

      // our device...is going to be applied to everything we draw! So everything will

      // be rotated by our angle a bit then drawn to the screen!  So if we draw 3 triangles

      // each will be rotated by the angle we just specified.

 

      // CALL OUR TRIANGLE FUNCTION HERE!!...IT WILL DRAW OUR TRIANGLE!

      draw_triangle();

      // After rendering the scene we display it.

      g_pD3DDevice->Present( NULL, NULL, NULL, NULL );

}

 

void mainloop()

{

      Render();

}

It may not be apparent at first but the only lines above which are used for the matrix are these three:

      //~~~*~~~~ Create a matrix

      D3DXMATRIX mx;

      //~~~*~~~~ Do somthing to our empty matrix.

      D3DXMatrixRotationZ(&mx, angle ); // angle in radians...eg. 1 degree = PI/180

      //~~~*~~~~ Use the matrix! No use having a matrix if we don't use it!

      g_pD3DDevice->SetTransform(D3DTS_WORLD, &mx);

We create an empty matrix (which is a 4x4 matrix), then I apply a modifier to it... got to do something with it... we could then have also applied a movement matrix and a Y rotation as well...etc...and a scalling etc... if we wanted.

The matrix is then put into our Screen Device (e.g. g_pD3DDevice), and our matrix is applied to everything that we draw from then onwards.

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.