saving matrix state

Started by
5 comments, last by BlackWind 15 years, 8 months ago
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..
Advertisement
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.
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!
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?
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,
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();
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!

This topic is closed to new replies.

Advertisement