# world/view/projection transform bug in c++ [solved]

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

## Recommended Posts

##### Share on other sites
It sounds like you need to restore the world matrix after you're done drawing each object.
D3DXMATRIX oldWorld;dev->GetTransform(D3DTS_WORLD,&oldWorld);dev->SetTransform(D3DTS_WORLD,&thisObjectsMat); // set the transform here!... // do the renderingdev->SetTransfrom(D3DTS_WORLD,&oldWorld);

It's probably clearer (to others and yourself) if you set the world transform just before you render, not in another function.

##### Share on other sites
I tried your(Buckeye) approach but to no avail. When I implemented:

D3DXMATRIX oldWorld;
dev->GetTransform(D3DTS_WORLD,&oldWorld);
dev->SetTransform(D3DTS_WORLD,&thisObjectsMat); // set the transform here!
... // do the rendering
dev->SetTransfrom(D3DTS_WORLD,&oldWorld);

no transformation was done at all.

Is there a way to get access to the matrix for a specific model only. As I understand it the device in use only has one D3DTS_WORLD. But all objects loaded in are loaded using the one device thus making all the world transform relative to each other, or not?

What the code does now is:

Backup old world -> transform the model a bit -> display -> restore the old world AND the old model.

Thus the entire old world is restored and the next transformation will be voided, since the old world is continuously restored.

I guess I am missing something... but idk what thats the big question....

##### Share on other sites
You can't use the device to store the transform state of a model instance. As you've noticed you can only have one world transform set onto the device at a time.

Typically you'll have "instances" of a particular model, where an instance is one occurrence of a particular model in your scene. So if you wanted to draw your room model 5 times in the scene with different positions, you would have 5 instances but only the one model.

The simplest version of instance can just store a world matrix, and a pointer to the model. Then when you want to move or rotate an instance you alter the matrix that belongs to that instance, and when you want to draw the instance you set its world matrix onto the device and render the model.

Oh and if you want to post code here, you can use the "code" or "source" tags. See the faq.

##### Share on other sites
Quote:
 As I understand it the device in use only has one D3DTS_WORLD. But all objects loaded in are loaded using the one device thus making all the world transform relative to each other, or not?

Loading a file does not effect the world transform currently in use by the device. They're totally unrelated, provided you mean you use D3DXLoadMeshFromX or some similar function to load objects.

When you call mesh->DrawSubset(), the rendering is done with whatever viewport, view matrix and projection matrix the device currently has set.

Did you remove the SetTransform calls from your move/rotateMesh functions? If you didn't, the next mesh will use the device-world matrix from whatever you last set it to.

##### Share on other sites
ok as i understand it now:

basic code flow follows:

1. INIT all
1.1. Load 2 *.x model (model A and model B).
1.2. Set D3DTS_WORLD to (0,0,0).

2. LOOP
2.1. Begin frame (using pd3dDevice->BeginScene() ).
2.2. Set new D3DTS_WORLD for model A.
2.3. Display model A. (using DrawSubset() ).
2.4. Set new D3DTS_WORLD for model B (in this case D3DTS_WORLD to (0,0,0) ).
2.5. Display model B. (using DrawSubset() ).
2.6. End frame(using pd3dDevice->EndScene() ).
2.7. display Buffer (pd3dDevice->Present(NULL,NULL,NULL,NULL) ).

k well i tried the above approach and it still does not work... The entire frame is stationary model A does not move at all.

Do any of you have a simple complete example that i could look at? this is probably a trivial problem but i fail to correct my error.

By the way thanks for the advice so far ;)

##### Share on other sites
D3DXMATRIX aMat, bMat;D3DXMatrixTranslation(&aMat,1,1,1); // at x=1, y=1, z=1D3DXMatrixTranslation(&bMat,0,0,0); // at the originfloat aAngle = 0;float bAngle = 0;LOOP:  dev->BeginScene();  D3DXMATRIX tmp, world;  aAngle += 0.1f;  bAngle -= 0.2f;  D3DXMatrixRotationX(&tmp,aAngle);  world = tmp * aMat;  dev->SetTransform(D3DTS_WORLD,&world);  objA->DrawSubset(..);  D3DXMatrixRotationY(&tmp,bAngle);  world = tmp * bMat;  dev->SetTransform(D3DTS_WORLD,&world);  objB->DrawSubset(..);  dev->EndScene();  dev->Present(...);// end LOOP

That's all off the top of my head, but it should result in objA at (1,1,1) rotating about it's local x-axis. objB, at the origin, should be rotating twice as fast as objA about the y-axis at the origin.

This all assumes you have a projection and view set up to be able to see those two points, etc., etc.

##### Share on other sites
Buckeye you are my hero!!!!!!!!! IT IS ALIVE!!!

I understand where my error was now. I multiplied the matrices incorrectly. I guess I'll have to review my 3D math algebra again...

thank you thank you thank you thank you thank you thank you

• 18
• 11
• 16
• 9
• 50
• ### Forum Statistics

• Total Topics
631396
• Total Posts
2999783
×