Sign in to follow this  
zephia

[DirectX] Transforming Multiple Objects ( Cubes )

Recommended Posts

Hello,

I recently started learning DirectX and Direct3D 10 ( I know, not 11, but please bear with me ^^ ) by reading the Ultimate Game Programming as well as Luna's books. I feel like I have missed a glaringly obvious detail, in that I still do not know how to rotate / transform vertices independently? What I mean is, I currently have a program which simply displays 2 cubes, and what I'd like to do is rotate one a certain angle in the y-direction, and the other one a certain angle in the x-direction.

I feed in a transformation matrix into my shader ( made from using the MatrixRotationY and MatrixRotationX functions ), and I change that matrix before each object's draw call, yet no matter what, I end up with the two cubes oriented the exact same way. Can someone explain what I am doing wrong?

This is my shader file:
[source lang="plain"]cbuffer Variables {
matrix World;
matrix View;
matrix Projection;
matrix Transform;
};

struct PS_INPUT
{
float4 Pos : SV_POSITION;
float4 Color : COLOR0;
};

PS_INPUT VS( float4 Pos : POSITION, float4 Color : COLOR )
{
PS_INPUT psInput;

Pos = mul ( Pos, Transform );
Pos = mul ( Pos, World );
Pos = mul ( Pos, View );

psInput.Pos = mul( Pos, Projection );
psInput.Color = Color;

return psInput;
}

PS_INPUT VS2( float4 Pos : POSITION, float4 Color : COLOR )
{

}

float4 PS( PS_INPUT psInput ) : SV_Target
{
return psInput.Color;
}

technique10 Render
{
pass P0
{
SetVertexShader ( CompileShader( vs_4_0, VS() ) );
SetGeometryShader ( NULL );
SetPixelShader ( CompileShader( ps_4_0, PS() ) );
}
}[/source]

And here are parts of my main file:
I build my effect and vertex layouts here
[source lang="cpp"]void InitDirect3DApp::buildFX()
{
DWORD shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
shaderFlags |= D3D10_SHADER_DEBUG;
shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION;
#endif

ID3D10Blob* compilationErrors = 0;
HRESULT hr = 0;
hr = D3DX10CreateEffectFromFile(L"shadertest.fx", 0, 0,
"fx_4_0", shaderFlags, 0, md3dDevice, 0, 0, &mFX, &compilationErrors, 0);
if(FAILED(hr))
{
if( compilationErrors )
{
MessageBoxA(0, (char*)compilationErrors->GetBufferPointer(), 0, 0);
ReleaseCOM(compilationErrors);
}
DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX10CreateEffectFromFile", true);
}

mTech = mFX->GetTechniqueByName("Render");

pViewMatrixEffectVariable = mFX->GetVariableByName( "View" )->AsMatrix();
pProjectionMatrixEffectVariable = mFX->GetVariableByName( "Projection" )->AsMatrix();
pWorldMatrixEffectVariable = mFX->GetVariableByName( "World" )->AsMatrix();
pTransformationMatrixEffectVariable = mFX->GetVariableByName( "Transform" )->AsMatrix();
}

void InitDirect3DApp::buildVertexLayouts()
{
// Create the vertex input layout.
D3D10_INPUT_ELEMENT_DESC vertexDesc[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0}
};
[/source]
here is where I try to draw my cubes, with different orientations:
[source lang="cpp"]void InitDirect3DApp::drawScene()
{
D3DApp::drawScene();

md3dDevice->OMSetDepthStencilState(0, 0);
float blendFactors[] = { 0.0f, 0.0f, 0.0f, 0.0f };
md3dDevice->OMSetBlendState(0, blendFactors, 0xffffffff);
md3dDevice->IASetInputLayout(mVertexLayout);
md3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

static float r;
D3DXMATRIX w;
D3DXMatrixIdentity(&w);
//D3DXMatrixRotationY(&w, r);
//r += 0.001f;

mWVP = mView * mProj;
pWorldMatrixEffectVariable->SetMatrix(w);
pViewMatrixEffectVariable->SetMatrix(mView);
pProjectionMatrixEffectVariable->SetMatrix(mProj);
pTransformationMatrixEffectVariable->SetMatrix(mTransform);

D3D10_TECHNIQUE_DESC techDesc;
mTech->GetDesc( &techDesc );
for (UINT p = 0; p < techDesc.Passes; ++p)
{
mTech->GetPassByIndex( p )->Apply(0);
D3DXMatrixRotationY(&mTransform, -.2f);
mShape1.draw( 0.5f, 0.3f, 0.0f );
D3DXMatrixRotationX(&mTransform, .3f);
mShape2.draw( 1.0f, -0.3f, 0.5f );
}

// We specify DT_NOCLIP, so we do not care about width/height of the rect.
RECT R = {50, 50, 0, 0};
mFont->DrawText(0, mFrameStats.c_str(), -1, &R, DT_NOCLIP, BLACK);

mSwapChain->Present(0, 0);
}[/source]

Thank you so much for any help you can give. And I apologize if I made some pretty obvious mistakes ( which I feel like I did ).

Share this post


Link to post
Share on other sites
You change matrix's value in the RAM, but not on GPU.

[i]D3DXMatrixRotationY(&mTransform, -.2f);[/i] changes matrix on CPU, but to actually send it to GPU you need to use [i]pTransformationMatrixEffectVariable->SetMatrix(mTransform);[/i]

If you wish to draw 2 cubes with different transformations it'll look about this:
[CODE]
D3D10_TECHNIQUE_DESC techDesc;
mTech->GetDesc( &techDesc );
D3DXMatrixRotationY(&mTransform, -.2f); // create matrix
pTransformationMatrixEffectVariable->SetMatrix(mTransform); // send it to shader/GPU
for (UINT p = 0; p < techDesc.Passes; ++p)
{
mTech->GetPassByIndex( p )->Apply(0);
mShape1.draw( 0.5f, 0.3f, 0.0f ); // draw something using it
}
D3DXMatrixRotationX(&mTransform, .3f); // create new matrix
pTransformationMatrixEffectVariable->SetMatrix(mTransform); // send it
for (UINT p = 0; p < techDesc.Passes; ++p)
{
mTech->GetPassByIndex( p )->Apply(0);
mShape2.draw( 0.5f, 0.3f, 0.0f ); // draw again
}
[/CODE]

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