# OpenGL saving matrix state

## Recommended Posts

BlackWind    212
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
MJP    19755
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
BlackWind    212
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
BlackWind    212
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
BlackWind    212
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
MJP    19755
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
BlackWind    212
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!

## Create an account

Register a new account

• ### Similar Content

• Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).

• Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
Skype: Mangodoor408
• By tyhender
Hello, my name is Mark. I'm hobby programmer.
So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine).
And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
Sorry for late replies.
I mostly give more information when people PM me,but this post is REALLY short,even for me =D
So here's few more points:
Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me.

• By Zaphyk
I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?

• I'm trying to get some legacy OpenGL code to run with a shader pipeline,
The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
I've got a version 330 vertex shader to somewhat work:
#version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
Question:
What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?

Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.

• 11
• 14
• 26
• 16
• 19