# OpenGL saving matrix state

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 on other sites
Quote:
 Original post by BlackWindBut 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 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 on other sites
ID3DXMATRIXStack is pretty simple, it's a lot like the OpenGL functions.

// At startup, create the stackID3DXMATRIXStack* 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 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!

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002275
×