Sign in to follow this  
xtheendx

Rotating Meshes Independently

Recommended Posts

xtheendx    127
Hey guys I am trying to render three teapot and get them to rotate them independently, but once I get three on the screen using D3DXMatrixTranslation the rotation stops. Only way I can get it to rotate is to just draw one. Why is this? Can it be done in shader code?
void render_frame(void)
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

    // set the view transform
    D3DXMATRIX matView;    // the view transform matrix
    D3DXMatrixLookAtLH(&matView,
    &D3DXVECTOR3 (0.0f, 3.0f, 9.0f),    // the camera position
    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),    // the look-at position
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
    effect->SetMatrix("View", &matView);

    // set the projection transform
    D3DXMATRIX matProjection;    // the projection transform matrix
    D3DXMatrixPerspectiveFovLH(&matProjection,
                               D3DXToRadian(45),    // the horizontal field of view
                               (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
                               1.0f,    // the near view-plane
                               100.0f);    // the far view-plane

    effect->SetMatrix("Projection", &matProjection);

	// set the world transform
    static float index = 0.0f; index+=0.03f;    // an ever-increasing float value
    D3DXMATRIX matRotateY;    // a matrix to store the rotation for each triangle
    D3DXMatrixRotationY(&matRotateY, index);    // the rotation matrix
	effect->SetMatrix("World", &matRotateY);

	for(int count = 0; count < 3; count++)
    {
		static D3DXMATRIX matCopy;
        D3DXMatrixTranslation(&matCopy, -3.0f + (3*count), 0.0f, 0.0f);
        effect->SetMatrix("World", &matCopy);

		effect->Begin(NULL, NULL);    // begin using the effect
			effect->BeginPass(0);    // begin the pass

				// render whatever
				meshTeapot->DrawSubset(0);

			effect->EndPass();    // end the pass
		effect->End();    // end the effect
	}
    d3ddev->EndScene(); 

    d3ddev->Present(NULL, NULL, NULL, NULL);

    return;
}

simple vertex shader I am using.
float4x4 World;
float4x4 View;
float4x4 Projection;

struct VertexOut
{
    float4 Pos : POSITION;
    float4 Color : COLOR;
};

VertexOut VShader(float4 Pos : POSITION)
{
    VertexOut Vert = (VertexOut)0;
    float4x4 Transform;

    Transform = mul(World, View);
    Transform = mul(Transform, Projection);
    Vert.Pos = mul(Pos, Transform);

    Vert.Color = float4(1, 1, 1, 1);

    return Vert;
}

technique FirstTechnique
{
    pass FirstPass
    {
        Lighting = FALSE;
        ZEnable = TRUE;

        VertexShader = compile vs_2_0 VShader();
    }
}

Share this post


Link to post
Share on other sites
MJP    19786
If you want to combine a rotation and a translation, then you need to concatenate the matrices (AKA multiply them). Try this:


static float index = 0.0f; index+=0.03f; // an ever-increasing float value
D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix

for(int count = 0; count < 3; count++)
{
D3DXMATRIX matCopy;
D3DXMatrixTranslation(&matCopy, -3.0f + (3*count), 0.0f, 0.0f);
matCopy = matRotateY * matCopy;
effect->SetMatrix("World", &matCopy);

...
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this