News
DirectX
Links
Contact Me

In Association with Amazon.com
In Association with Amazon.ca

$5 via PayPal


Y Rotations without D3DX



Y Rotations without D3DX
Here is our replacement for D3DXMatrixRotationY:
// Procedure: dhMatrixRotationY
// Whazzit:Given an angle (in radians), produces a rotation matrix
//         to perform that transformation.
D3DXMATRIX *dhMatrixRotationY(D3DXMATRIX *pOut, float angle ){
float my_sin, my_cos;


   my_sin=(float)sin(angle);
   my_cos=(float)cos(angle);

   pOut->_11 = my_cos;  pOut->_12 =  0.0f;   pOut->_13 = -my_sin; pOut->_14 = 0.0f;
   pOut->_21 = 0.0f;    pOut->_22 =  1.0f;   pOut->_23 = 0.0f;    pOut->_24 = 0.0f;
   pOut->_31 = my_sin;  pOut->_32 =  0.0f;   pOut->_33 = my_cos;  pOut->_34 = 0.0f;
   pOut->_41 = 0.0f;    pOut->_42 =  0.0f;   pOut->_43 = 0.0f;    pOut->_44 = 1.0f;

   return pOut;
}

Here is our new render() function which shows that our function produces results identical to the D3DX version. The important bits are in red:
void render(void){
D3DXMATRIX matWorld;
D3DXMATRIX trans_matrix;   //Our translation matrix
D3DXMATRIX y_rot_matrix;
static float y_rot=0;


   y_rot+=0.001f;

   //Clear the buffer to our new colour.
   g_d3d_device->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

   //Notify the device that we're ready to render
   if(SUCCEEDED(g_d3d_device->BeginScene())){

      //Vertex shaders are a complex topic, but you can do some amazing things with them
      //For this example we're not creating one, so we tell Direct3D that we're just
      //using a plain vertex format.
      g_d3d_device->SetVertexShader(D3D8T_CUSTOMVERTEX);

      //D3D's rendering functions read from streams.  Here we tell D3D that the
      //VB we created for our triangle is the stream it should read from.
      g_d3d_device->SetStreamSource(0,g_vb,sizeof(my_vertex));

      //Set up the rotation matrix for the triangle
      D3DXMatrixRotationY(&y_rot_matrix,y_rot);

      //Set up the translation matrix (move it over to the left a bit)
      D3DXMatrixTranslation(&trans_matrix,-1.0,0.0f,0.0f);

      //Combine out matrices
      D3DXMatrixMultiply(&matWorld,&y_rot_matrix,&trans_matrix);

      //Set our World Matrix
      g_d3d_device->SetTransform(D3DTS_WORLD,&matWorld );

      //After all that setup, actually drawing the triangle is pretty easy.
      //We tell it what we're giving it (a Triangle List), where it should
      //start reading (0, the beginning), and how many triangles we're drawing(1)
      g_d3d_device->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);

      //Now we go through the same steps as above, this time using our own
      //RotationY function instead of the D3DX version.
      dhMatrixRotationY(&y_rot_matrix,y_rot);
      //Move this one a bit to the right
      D3DXMatrixTranslation(&trans_matrix,1.0,0.0f,0.0f);

      D3DXMatrixMultiply(&matWorld,&y_rot_matrix,&trans_matrix);


      g_d3d_device->SetTransform(D3DTS_WORLD,&matWorld );

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

      //Notify the device that we're finished rendering for this frame
      g_d3d_device->EndScene();
   }

   //Show the results
   g_d3d_device->Present( NULL, NULL, NULL, NULL );

}