Sign in to follow this  
BlackWind

OpenGL saving matrix state

Recommended Posts

hi, im new to directx, and im playing with some matrix transformations. currently, i have an object rotating in a world, and the camera implemented. The problem, is that the whole world rotates along with the camera. this is a sample
g3D->setViewMatrix( gCamera ); /*set the new view and look at for the camera*/
g3D->begin(); /* Begin the scene*/
g3D->clear(); /* Clear viewport*/
DrawGrid( CPos(0,-2,0), 32, D3DCOLOR_ARGB(255,255,0,0) ); /*draw the floor of the world*/

D3DXMATRIX wMatrix;
D3DXMatrixRotationZ(&wMatrix, DEG2RAD(angle)); /* rotate the obj around z*/
g3D->setWorldMatrix(&wMatrix); /* Set our world transformation (call setTransform(D3DTS_WORLD, matrix)*/
DrawTriangle(); /* Draw a triangle*/
g3D->end(); /* End the scene */


that works almost fine, except that instead of rotating only the triangle, it rotates everything. In OpenGL, i used to do something like this to only affect the objects that i want:
glPushMatrix() /*save the matrix state*/
 glRotate(x,y,z,angle); /* rotate the objects to draw*/
 draw() /*draw something*/
glPopMatrix(); /* finish working with that matrix*/


what would be the equivalent in dx? thanks a lot..

Share this post


Link to post
Share on other sites
Typically in D3D for an object your rendering on screen, you'll store a world matrix that represents its position and orientation. So you'd have one for your grid (probably set to identity), and then one for your triangle. Then right before you draw each object, you'd set the corresponding world matrix.

D3DX has a helper class which emulates the OpenGL matrix stack functionality, called ID3DXMATRIXStack. It has methods that are pretty much equivalent to the GL functions you used to use. Only difference is you need to call IDirect3DDevice::SetTransform and pass the result of ID3DXMATRIXStack::GetTop before you draw.

Share this post


Link to post
Share on other sites
hey, thanks a lot. I solved the problem with the fist option, i did this:

D3DXMATRIX wMatrix2;
D3DXMatrixIdentity(&wMatrix2);
g3D->setWorldMatrix(&wMatrix2);
DrawGrid( CPos(0,-2,0), 32, D3DCOLOR_ARGB(255,255,0,0) );



and works great.

But i didnt understand very well the second option, can you give me a simple example using ID3DXMATRIXStack ??

thanks again!

Share this post


Link to post
Share on other sites
Quote:
Original post by BlackWind

But i didnt understand very well the second option, can you give me a simple example using ID3DXMATRIXStack ??

thanks again!


besides of that, i have another problem. What do i need to render first an object mapped (texture) and then a color object?

i have something like this:

drawColorPimitive();
drawMappedPrimitive();


by separate, the work fine, but together, it only draws the first time the colorprimitive, and then only the mapped primitve. How can i solve that?

Share this post


Link to post
Share on other sites
well, problem solved.
i only needed to use the method:
IDirect3DDevice9::SetTexture (0, NULL); before calling the color primitives.

But i still would like to see a simple example using the ID3DXMATRIXStack.

cheers,

Share this post


Link to post
Share on other sites
ID3DXMATRIXStack is pretty simple, it's a lot like the OpenGL functions.


// At startup, create the stack
ID3DXMATRIXStack* stack = NULL;
D3DXCreateMatrixStack(&stack);

// In your rendering loop, use the stack to work with transforms
// (This does the same thing as the code you originally posted for
// drawing the triangle)
stack->Push();

stack->RotateAxis(&D3DXVECTOR3(0, 0, 1), DEG2RAD(angle));
g3D->setWorldMatrix(stack->GetTop());
DrawTriangle();

stack->Pop();


Share this post


Link to post
Share on other sites
thanks a lot again! only it was a little little bit different from the way you posted the initialization. I'll post it in case someone needs it in the future...


ID3DXMatrixStack * stack = NULL;
D3DXCreateMatrixStack(0, &stack);



now i have another questions, but i think those would be better in another thread...

cheers!

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