Sign in to follow this  
lildude_alex

transformation matrices

Recommended Posts

hello i`m asking about transformation in Direct X SDK the 6th tutorial , i want to know how can i multiply matices so i can rotate, scale and translation thanx in advance

Share this post


Link to post
Share on other sites
There are D3D matrix functions for scaling, translation and rotation...
You just make your matrix and use one of those function on it, like:


D3DXMATRIX rotationY; float angle = 0.1f;
D3DXMatrixRotationY ( &rotationY, angle );


You can then set the transformation with the SetTransform method of the device.


// Assuming _device is a pointer to an IDirect3DDevice9 interface
_device->SetTransform ( D3DTS_WORLD, &rotationY );


In the documention you can look up more D3DXMatrix functions. To use more than one transformations at once you can multiply the matrices you create with these functions, and then apply all transformations at once with SetTransform.

Share this post


Link to post
Share on other sites
Hi alex,

This can be a little confusing and I advise waiting for the tutorials to get you this far since they will go more in detail than I, but here is something to get you started:

(NOTE: I am assuming you understand what the 3 transformations are and do)

First, you will need to add the
C:\Program Files\Microsoft DirectX SDK (June 2006)\Lib\x86\d3dx9.lib
library to your project.

Next, in the file that you want to do the transformations, include the d3dx9.h file. This file contains commands that will greatly help you in preforming the matrix operations. And for the rest of my explaination take note that types preceeded by D3DX come from this library.

Now lets start with some code:

/*
NOTE: I am assuming the following:
1) that your rotation angles are stored in:
xAngle, yAngle, zAngle
2) that your positions are stored in:
xPos, yPos, zPos
3) that your scales are in:
xScale, yScale, zScale
4) WNDWIDTH is a macro defined to be the width of your window
WNDHEIGHT is a macro defined to be the height of your window
5) g_pD3DDevice is the name of your initialized
IDirect3DDevice9 object
*/


/* code already here such as defining the vertices
creating a vertex buffer, and initializing the above
values if they are not already initialized */


// declare the matrices needed for calculation
D3DXMATRIX matWorld; // our world trans matrix
D3DXMATRIX matView; // our view matrix
D3DXMATRIX matProj; // our projection matrix
D3DXMATRIX matRotX, matRotY, matRotZ; // the rotation matrices
D3DXMATRIX matTrans; // the transformation matrix
D3DXMATRIX matScale; // the scale matrix

/* World Transformation Matrix */
// create the rotation matrices
D3DXMatrixRotationX( &matRotX, xAngle );
D3DXMatrixRotationY( &matRotY, yAngle );
D3DXMatrixRotationZ( &matRotZ, zAngle );
// create the translation matrix
D3DXMatrixTranslation( &matTrans, xPos, yPos, zPos );
// create the scale matrix
D3DXMatrixScaling( &matScale, xScale, yScale, zScale );
// start the world matrix as identity, always do this!
D3DXMatrixIdentity( &matWorld );
// combine all of the matrices into the world trans matrix
// note that the order we do this is unique to the world
// transformation and must be kept this way
D3DXMatrixMultiply( &matWorld, &matWorld, &matScale );
D3DXMatrixMultiply( &matWorld, &matWorld, &matRotX );
D3DXMatrixMultiply( &matWorld, &matWorld, &matRotY );
D3DXMatrixMultiply( &matWorld, &matWorld, &matRotZ );
D3DXMatrixMultiply( &matWorld, &matWorld, &matTrans );

/* View Transformation Matrix */
// create the rotation matricies, note that we use opposite
// values here from what the viewpoint is really at
D3DXMatrixRotationX( &matRotX, -xAngle );
D3DXMatrixRotationY( &matRotY, -yAngle );
D3DXMatrixRotationZ( &matRotZ, -zAngle );
// Create the translation matrix, note the opposite values
D3DXMatrixTranslation( &matTrans, -xPos, -yPos, -zPos );
// start the view matrix as identity, always do this!
D3DXMatrixIdentity( &matView );
// combine all to create the view matrix, note the order
// of combination is unique and must be done this way
D3DXMatrixMultiply( &matView, &matView, &matTrans );
D3DXMatrixMultiply( &matView, &matView, &matRotZ );
D3DXMatrixMultiply( &matView, &matView, &matRotY );
D3DXMatrixMultiply( &matView, &matView, &matRotX );

/* Projection Transformation Matrix */
// find the aspect to use by dividing the window width
// by the window height
float aspect = (float)WNDWIDTH / (float)WNDHEIGHT;
// create the projection transformation matrix
// with help from the d3dx9 library function
D3DXMatrixPerspectiveFovLH( &matProj, // the projection matrix
D3DX_PI/4, // the view range in radians
aspect, // aspect of the screen ratio
1.0f, // the near clip plane
1000.0f ); // the far clip plane

/* Attaching the new transformations to Direct3D */
// set the world transform
if( FAILED(g_pD3DDevice->SetTransform( D3DTS_WORLD, &matWorld ))){
// error occurred world trans matrix not set
}
// set the view transform
if( FAILED(g_pD3DDevice->SetTransform( D3DTS_VIEW, &matView ))){
// error occurred view trans matrix not set
}
// set the projection transform
if( FAILED(g_pD3DDevice->SetTransform( D3DTS_PROJECTION, &matProj ))){
// error occurred proj trans matrix not set
}

/* more code here such as clearing the backbuffer
and beginning the scene */


// END OF SOURCE
[/SOURCE]


I hope this helps you. But again I strongly advise you read up on it so you have some idea of what is all going on, if you dont already. Also, if there are any symbols you don't understand or functions you are wondering what do, like SetTransform or D3DTS_PROJECTION, look them up in the DirectX SDK, it is your friend.

Goodluck yo ya lildude, I'll check back to see if you have any questions or comments. And if anyone finds an error in my code or logic please let me know!

Thanks,

-sevans







Share this post


Link to post
Share on other sites
woooooow thanx so much especially sevans ur code works properly
that wat i`m really call help

but i have another question:

how can i get .X files 4 meshes to put`em in my game??????????

Share this post


Link to post
Share on other sites
That I don't know :(

I am still currently learning C++ and DirectX and I just got to that point in my book but haven't started it yet. I do advise getting a good book though. I am using "Roleplaying Game Programming with DirectX", and it is just awesome. I got it for 10$ on half.com ^.^d

Goodluck to ya,

-sevans

Share this post


Link to post
Share on other sites
Quote:
Original post by lildude_alex
how can i get .X files 4 meshes to put`em in my game??????????


You can load a .x file using D3DXLoadMeshFromX(), and draw it to the screen with mesh->DrawSubset(i), where mesh is a pointer to your mesh, and i ranges from 0 < i < number of materials in your model

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this