Skip to main content
GameDev.net gamedev.net
🔒 Locked

Volumetric fog does not work

Started by kurlyak Jan 5, 2010 at 9:52 AM 2 replies 2.1k views
Original Post
kurlyak
kurlyak
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/
Windows programming is like going to the dentist: You know it’s good for you, but no one likes doing it.- Andre LaMothe.
Big_Brain
Big_Brain
Hi did you read this?
http://developer.download.nvidia.com/SDK/9.5/Samples/DEMOS/Direct3D9/src/
FogPolygonVolumes3/docs/FogPolygonVolumes3.pdf

[Edited by - Big_Brain on February 2, 2010 2:52:47 AM]
Sergio666
Sergio666
Still troubles with volumetic fog? Why you created three smilar threads? More threads is not path to win!!
Копай SDK
Zahlman
Zahlman
1) Use source tags. I made a sticky about this for a reason.

2) We're not going to just read over your whole source for you and find the problem.

3) This thread keeps getting stealth-bumped by another user, which is naughty.

In short, I've had enough of this thread.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.