Question about world transform!NEEED HELP!

Started by
3 comments, last by unbird 12 years, 7 months ago
I am reading Introduction to 3d game programming with directx 9:A shader Approach!
The code style is defferent from Introduction to 3d game programming with directx 9
And I want to transform an X file model as usual...
But Failed...This is my Code:
void XFileDemo::drawScene(){
// Clear the backbuffer and depth buffer.
HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffeeeeee, 1.0f, 0));
HR(gd3dDevice->BeginScene());
HR(mFX->SetValue(mhLight, &mLight, sizeof(DirLight)));
HR(mFX->SetMatrix(mhWVP, &(mWorld*mView*mProj)));
D3DXMATRIX worldInvTrans;
D3DXMatrixInverse(&worldInvTrans, 0, &mWorld);
D3DXMatrixTranspose(&worldInvTrans, &worldInvTrans);
HR(mFX->SetMatrix(mhWorldInvTrans, &worldInvTrans));
HR(mFX->SetMatrix(mhWorld, &mWorld));

HR(mFX->SetTechnique(mhTech));
UINT numPasses = 0;
HR(mFX->Begin(&numPasses, 0));
HR(mFX->BeginPass(0));
D3DXMATRIX F; // I add code here
D3DXMatrixTranslation(&F,100.0f,100.0f,100.0f); // but it doesn't work
gd3dDevice->SetTransform(D3DTS_WORLD,&F); // The x model's position doesn't change
for(int j = 0; j < mMtrl.size(); ++j)
{
HR(mFX->SetValue(mhMtrl, &mMtrl[j], sizeof(Mtrl))); // If there is a texture, then use.
if(mTex[j] != 0)
{
HR(mFX->SetTexture(mhTex, mTex[j]));
}
// But if not, then set a pure white texture. When the texture color
// is multiplied by the color from lighting, it is like multiplying by
// 1 and won't change the color from lighting.
else
{
HR(mFX->SetTexture(mhTex, mWhiteTex));
}
HR(mFX->CommitChanges());
HR(mMesh->DrawSubset(j));
}
HR(mFX->EndPass());
HR(mFX->End());
mGfxStats->display();
HR(gd3dDevice->EndScene());
// Present the backbuffer.
HR(gd3dDevice->Present(0, 0, 0, 0));}

Any help will be appreciated!!!Thanks!
My english is very poor!
Advertisement

//...
HR(mFX->SetMatrix(mhWorld, &mWorld)); // shader
//...
gd3dDevice->SetTransform(D3DTS_WORLD,&F); // fixed function pipeline
//...

Ring any bells ?


//...
HR(mFX->SetMatrix(mhWorld, &mWorld)); // shader
//...
gd3dDevice->SetTransform(D3DTS_WORLD,&F); // fixed function pipeline
//...

Ring any bells ?


Yes.......it works...
D3DXMatrixTranslation(&mWorld,100.0f,100.0f,100.0f);gd3dDevice->SetTransform(D3DTS_WORLD,&mWorld);
.........Thanks.....
My english is very poor!
[color="#1C2837"]Another question...
[color="#1C2837"]I changed my code to
[color="#1C2837"]D3DXMatrixTranslation(&mWorld,100.0f,100.0f,100.0f);
[color="#1C2837"]gd3dDevice->SetTransform(D3DTS_WORLD,&mWorld);

[color="#1C2837"]It works...But It Moved the whole world...
[color="#1C2837"]I had added another model...and those two medel changed their position together...
[color="#1C2837"]I don't quite understand : HR(mFX->SetMatrix(mhWorld, &mWorld));
[color="#1C2837"]How should I do?Thanks!
My english is very poor!

gd3dDevice->SetTransform(D3DTS_WORLD,&mWorld);

It works...

Actually, No.

SetTransform is fixed function pipeline, but you render your mesh with a bound shader (through Begin/BeginPass), so this call to SetTransform will have no effect at all. The reason it does "work" is because mWorld is a member variable of the class, so its value will persist to the next frame, where it will be used again to set the shader parameters (SetMatrix).

You have to initialize/change the world matrix every time before you calculate the other dependent values (worldInvTrans, mWorld*mView*mProj) and for every mesh separately. Have a look at the Lighting chapter again where he renders several different meshes multiple times, with different world transformations, that is.

This topic is closed to new replies.

Advertisement