How to move object in Directx?

Started by
5 comments, last by Medo Mex 10 years, 7 months ago

in opengl, if I want move an object ,a box, I can type:


glPushMatrix();
glTranslatef(x,y,z);
drawBox();
glPopMatrix();

Can someone tell me how to do the same effect in Directx?

Advertisement

Well you can create a world matrix, which defines the modal space transformations. And then apply it the all the vertices on the current mesh being rendered.

Example:


C++:

D3DXMATRIX worldC, worldT, worldR, worldS;

// Translate Scale Rotate it
D3DXMatrixTranslation(&worldT, tx, ty, tz);
D3DXMatrixRotationYawPitchRoll(&worldR, rx, ry, rz);
D3DXMatrixScaling(&worldS, sx, sy, sz);

worldC = worldR * worldS * worldT;

// Send the matrix to our shader
bff_perObject.worldC = worldC;
.. Send it

VS:
{
    ..
    OUT.position = mul(input.position, worldC);
    OUT.position = mul(input.position, viewMatrix);
    OUT.position = mul(input.position, projMatrix);
}

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

can I use g_pd3dDevice->SetViewport() ?

No. You need to use a model transformation. SetViewport is for defining which portion of the display to draw to.

Either use world matrix approach as described by Migi0027, or use dynamic vertex buffers. I believe that the first one is faster anyway. First method is similar to OpenGL's shader approach if you are familiar with something different from fixed function pipeline.

If you're happier with the stack approach you could use the D3DX library : Matrix Stacks.

If you are using D3D9, you can set your world matrix using:


g_pd3dDevice->SetTransform(D3DTS_WORLD, &mWorld);

However, you will also need to set projection and view matrix.

This topic is closed to new replies.

Advertisement