Sign in to follow this  

What could make my shader scale the mesh, when I dont apply a scale transform.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to copy the 2009 DX SDK's instancing app but when I load my mesh class the shader seems to apply a scale to it.
I can find any thing wrong with the cpp side of the app and I dont know much about shaders so ill post the shader where i think it may be.
plus the relevant cpp code.



//cpp side code


D3DXMATRIX* g_pTreeInstanceData;//world locations



// set us up for instanced rendering all stuff related to rendering is valid just not put in here.
//this here code is the shaders params needed to render the mesh the mesh data comes from streem 0 and the instancing is in stream 1
D3DXMATRIX WorldView;
D3DXMATRIX WorldViewProj;
D3DXMatrixIdentity(&WorldView);
D3DXMatrixMultiply( &WorldViewProj, &view, &proj );

HRESULT hr = fx->WorldViewProj->SetMatrix( ( float* )&WorldViewProj );//g_pmWorldViewProj
WorldView = view;

//FLOAT det = 0.0f;
//D3DXMatrixInverse(&WorldView, &det, &view);
hr = fx->WorldView->SetMatrix( ( float* )&WorldView);//view );//g_pmWorldView

hr = fx->iNumTrees->SetInt( g_iNumTreesToDraw );


//render here













//setting the instancing world locations




CreateRandomTreeMatrices(void)
{
srand( 100 ); //use the same random seed every time so we can verify output
float fScale = 30.0f;

for( int i = 1; i < MAX_TREE_INSTANCES; i++ )
{
//find a random position
D3DXVECTOR3 pos;
pos.x = RPercent() * 140.0f;
pos.y = 0;//RPercent() * 20.0f - 10.0f;
pos.z = 15.0f + fabs( RPercent() ) * 200.0f;

pos.x *= -1;
pos.z *= -1;

pos *= fScale;

float fRot = RPercent() * D3DX_PI;

D3DXMATRIX mTrans;
D3DXMATRIX mRot;
D3DXMATRIX mScale;
D3DXMatrixIdentity(&mTrans);
D3DXMatrixIdentity(&mRot);
D3DXMatrixIdentity(&mScale);

D3DXMatrixRotationY( &mRot, fRot );
D3DXMatrixTranslation( &mTrans, pos.x, pos.y, pos.z );

//no scale
D3DXMatrixScaling( &mScale, 1.0f, 1.0f, 1.0f );
g_treeInstanceMatrices[i] = mScale * mRot * mTrans;


//maybe need invers not work
//FLOAT d = 0.0f;
//D3DXMatrixInverse(&g_treeInstanceMatrices[i], &d, &g_treeInstanceMatrices[i]);



}


g_pTreeInstanceList = &g_treeInstanceMatrices[0];
}//end CreateRandomTreeMatrices
///////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////





LoadTreeInstanceData(DWORD dwNumTrees )
{
HRESULT hr = S_OK;

// Create a resource with the input matrices
// We're creating this buffer as dynamic because in a game, the instance data could be dynamic... aka
// we could have moving trees.
D3D10_BUFFER_DESC bufferDesc =
{
dwNumTrees * sizeof( D3DXMATRIX ),
D3D10_USAGE_DYNAMIC,
D3D10_BIND_VERTEX_BUFFER,
D3D10_CPU_ACCESS_WRITE,
0
};

hr = pd3dDevice->CreateBuffer( &bufferDesc, NULL, &g_pTreeInstanceData );
if(FAILED(hr))
{
DebugText("failed to Create Buffer",//the message to display
"MyAppWin::LoadTreeInstanceData()",//the location the message was called Eg. what function
true);//bool Usewindowmsgbox = false);//true if you also want to display it in a messagebox

return;
}

D3DXMATRIX* pMatrices = NULL;
( g_pTreeInstanceData )->Map( D3D10_MAP_WRITE_DISCARD, NULL, ( void** )&pMatrices );

memcpy( pMatrices, g_pTreeInstanceList, dwNumTrees * sizeof( D3DXMATRIX ) );

( g_pTreeInstanceData )->Unmap();

return;
}//end LoadTreeInstanceData
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////














//shader code comes out of the sdk 2009 instancing island app


//--------------------------------------------------------------------------------------
// CalcLighting helper function. Calculates lighting from 4 light sources, adds ambient
// and attenuates for depth. Used by all techniques for lighting.
//--------------------------------------------------------------------------------------
float4 CalcLighting( float3 norm, float depth )
{
float4 color = float4(0,0,0,0);

// add the contributions of 4 directional lights
[unroll] for( int i=0; i<4; i++ )
{
color += saturate( dot(g_lights[i].direction,norm) )*g_lights[i].color;
}

// give some attenuation due to depth
float attenuate = depth / 10000.0;
float4 attenColor = float4(0.15, 0.2, 0.3, 0);

// add it all up plus ambient
return (1-attenuate*0.23)*(color + g_ambient) + attenColor*attenuate;
}

//--------------------------------------------------------------------------------------
// Instancing vertex shader. Positions the vertices based upon the matrix stored
// in the second vertex stream.
//--------------------------------------------------------------------------------------
PSSceneIn VSInstmain(VSInstIn input)
{
PSSceneIn output;

//
// Transform by our Sceneance matrix
//
float4 InstancePosition = mul(float4(input.pos, 1), input.mTransform);
float4 ViewPos = mul(InstancePosition, g_mWorldView );

//
// Transform the vert to view-space
//
float4 v4Position = mul(InstancePosition, g_mWorldViewProj);
output.pos = v4Position;

//
// Transfer the rest
//
output.tex = input.tex;

//
// dot the norm with the light dir
//
float3 norm = mul(input.norm,(float3x3)input.mTransform);
output.color = CalcLighting( norm, ViewPos.z );

//
// Dim the color by how far up the tree we are.
// This is a nice way to fake occlusion of the branches by the leaves.
//
output.color *= 1.0f - saturate(input.pos.y/g_occDimHeight);


return output;
}








//--------------------------------------------------------------------------------------
// PS for non-leaf or grass items.
//--------------------------------------------------------------------------------------
float4 PSScenemain(PSSceneIn input) : SV_Target
{
float4 color = g_txDiffuse.Sample( g_samLinear, input.tex ) * input.color;
return color;
}



//--------------------------------------------------------------------------------------
// Render instanced meshes with vertex lighting
//--------------------------------------------------------------------------------------
technique10 RenderInstancedVertLighting
{
pass p0
{
SetVertexShader( CompileShader( vs_4_0, VSInstmain() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );

SetBlendState( NoBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
SetDepthStencilState( EnableDepthTestWrite, 0 );
SetRasterizerState( EnableMSAA );
}
}






Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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