Original Post
Please help to write shaders for volumetric fog. I have an error. But I do not know where.
float4x4 g_wvp;
//depth.vsh m_pFogShaderDepth
void vs_depth( inout float4 pos : POSITION ,
out float depth : TEXCOORD0 )
{
pos = mul( pos, g_wvp );
depth = pos.z;
}
//depth.psh m_pFogShaderDepth
float4 ps_depth( in float depth : TEXCOORD0 ) : COLOR
{
return float4( depth, 0.f, 0.f, 0.f );
}
sampler2D s_fog_back;
sampler2D s_fog_front;
sampler2D s_scene_depth;
//fog.ps m_pPixShader
float4 Main_ps( in float2 tex : TEXCOORD0 ) : COLOR
{
float fog_back = tex2D( s_fog_back , tex ).r;
float fog_front = tex2D( s_fog_front , tex ).r;
float scene_depth = tex2D( s_scene_depth , tex ).r;
float k = fog_back - fog_front;
k -= fog_back - clamp( scene_depth, 0, fog_back );
return float4(0.5f,0.5f,0.5f, k*0.5f );
}
Code C++
p_d3d_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
p_d3d_Device->SetTexture(0,m_pEndFogTexture);
//********************************
D3DXMATRIX V;
TheCamera.getViewMatrix(&V);
D3DXMATRIX matRes;
//matRes=mxWorld*mxView*mxProj;
matRes=mxWorld*V*mxProj;
VertShaderConstTable->SetMatrix(p_d3d_Device, MatrixHandle, &matRes);
//------------------------------
p_d3d_Device->SetPixelShader( NULL );
//reverse the culling order to get the back side
p_d3d_Device->SetRenderState( D3DRS_ZFUNC , D3DCMP_GREATER );
p_d3d_Device->Clear(NULL, NULL, D3DCLEAR_TARGET| D3DCLEAR_ZBUFFER , D3DCOLOR_ARGB(0, 0, 0, 0), 0.0, 0);
p_d3d_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
p_d3d_Device->SetTexture(0,m_pEndFogTexture);
p_d3d_Device->SetVertexShader( m_pFogShaderDepth );
for( DWORD i=0; i<g_dwNumMaterials; i++ )
{
m_pMeshFloor->DrawSubset( i );
}
p_d3d_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
p_d3d_Device->SetTexture(1,m_pStartFogTexture);
p_d3d_Device->SetVertexShader( m_pFogShaderDepth );
for( DWORD i=0; i<g_dwNumMaterials; i++ )
{
m_pMeshFloor->DrawSubset( i );
}
p_d3d_Device->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
p_d3d_Device->SetVertexShader( NULL);
p_d3d_Device->SetPixelShader( m_pPixShader );
for( DWORD i=0; i<g_dwNumMaterials; i++ )
{
m_pMeshFloor->DrawSubset( i );
}
//---------------------------------
p_d3d_Device->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
//**********************************
p_d3d_Device->EndScene ();
p_d3d_Device->Present (NULL, NULL, NULL, NULL);
as a result I have displayed a blue cube, textured very wrong, on a gray background. Read the article Russian programmer http://timai-ru.blogspot.com/