struct InputVS
{
float4 position : POSITION ;
float4 normal : NORMAL ;
} ;
struct OutputVS
{
float4 position : POSITION ;
float4 diffuse : COLOR ;
} ;
uniform extern float4x4 mtrxWVP : WorldViewProjection ;
uniform extern float4x4 mtrxWorld_IT : WorldInverseTranspose ;
// General light attributes
const float4 vLightDir = { 0.0f, 0.0f, 1.0f, 0.0f } ;
// Ambient lighting attributes
uniform extern float4 ligAmbient ;
uniform extern float4 mtrlAmbient ;
// Diffuse lighting attributes
uniform extern float4 ligDiffuse ;
uniform extern float4 mtrlDiffuse ;
OutputVS MainVS( InputVS input )
{
OutputVS output = (OutputVS)0 ;
float3 ambient, diffuse ;
float diffuse_co = 0.0f ;
input.position.w = 1.0f ;
input.normal.w = 0.0f ;
output.position = mul( input.position, mtrxWVP ) ;
input.normal = mul( input.normal, mtrxWorld_IT ) ;
input.normal = normalize( input.normal ) ;
diffuse_co = max( dot( vLightDir, input.normal ), 0 ) ;
ambient = (ligAmbient * mtrlAmbient).rgb ;
diffuse = diffuse_co * (ligDiffuse * mtrlDiffuse).rgb ;
output.diffuse.rgb = ambient + diffuse ;
output.diffuse.a = 1.0f ;
return output ;
}
float4 MainPS( float4 diffuse : COLOR0 ) : COLOR
{
return diffuse ;
}
technique MainFX
{
pass p0
{
AlphaBlendEnable = TRUE ;
Ambient = float4( 1.0f, 1.0f, 1.0f, 1.0f ) ;
CullMode = CCW ;
DestBlend = INVSRCALPHA ;
FillMode = SOLID ;
Lighting = FALSE ;
NormalizeNormals = FALSE ;
ShadeMode = GOURAUD ;
SpecularEnable = FALSE ;
SrcBlend = SRCALPHA ;
StencilEnable = FALSE ;
ZEnable = TRUE ;
ZFunc = LESSEQUAL ;
VertexShader = compile vs_2_0 MainVS() ;
PixelShader = compile ps_2_0 MainPS() ;
}
}
void CDemo::DrawSpheres( float fCount, float fSpacing )
{
float fRoot = sqrt( fCount ) ;
if( !isWholeNumber( fRoot ) )
return ;
D3DXVECTOR4 mtrlAmbient( 0.5f, 0.0f, 0.0f, 1.0f ) ;
_pEffect->SetVector( _hmtrlAmbient, &mtrlAmbient ) ;
D3DXVECTOR4 mtrlDiffuse( 0.9f, 0.0f, 0.0f, 1.0f ) ;
_pEffect->SetVector( _hmtrlDiffuse, &mtrlDiffuse ) ;
D3DXMATRIX mtrxWorldViewProj, mtrxWorld_IT ;
D3DXMATRIX mtrxWorld, mtrxView, mtrxProj ;
_pGDevice->GetTransform( D3DTS_VIEW, &mtrxView ) ;
_pGDevice->GetTransform( D3DTS_PROJECTION, &mtrxProj ) ;
for( unsigned int j = 0 ; j < (unsigned int)fRoot ; j++ )
{
for( unsigned int k = 0 ; k < (unsigned int)fRoot ; k++ )
{
D3DXMatrixTranslation( &mtrxWorld, k * (5.0f + fSpacing), 2.5f, j * (5.0f + fSpacing) ) ;
mtrxWorldViewProj = mtrxWorld * mtrxView * mtrxProj ;
_pEffect->SetMatrix( _hmtrxWVP, &mtrxWorldViewProj ) ;
D3DXMatrixInverse( &mtrxWorld_IT, 0, &mtrxWorld ) ;
D3DXMatrixTranspose( &mtrxWorld_IT, &mtrxWorld_IT ) ;
_pEffect->SetMatrix( _hmtrxWorld_IT, &mtrxWorld_IT ) ;
_pEffect->CommitChanges() ;
_mshBall->DrawSubset( 0 ) ;
}
}
}
//************************************************************
This 2nd screenshot shows the application at work while the shader variable, 'vLightDir', is set to {0, 0, -1}. Note how the spheres are lit, very unusual. Also note how the first sphere in the grid (lower left corner) is perfectly lit; very bizarre.
[Edited by - Yeshua666 on March 18, 2009 7:50:06 PM]