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

Atmospheric Scattering problem

Started by Eric Friedman Jan 5, 2007 at 9:27 PM 0 replies 1.1k views
Original Post
Eric Friedman
Eric Friedman
Hello all, I have a question regarding atmospheric scattering, and a problem I'm having. I have based my shader (in HLSL) off of the Hoffman/Preetham article found here: http://ati.de/developer/dx9/ATI-LightScattering.pdf This is my shader:

// SkyPlane.fx
// Author: Eric Friedman


//==== Globals
float4x4 g_matWorldViewProj;
float4x4 g_matWorldView;
float4x4 g_matWorld;
float4	 g_vCameraPos;
float4	 g_vLightDir = float4(0.0f, 0.0f, -1.0f, 1.0f);
float	 g_fHDRMultiplyer = 1.0f;

//==== Constants
float3 g_vBetaR = float3(0.00000695, 0.0000118, 0.0000244);
float3 g_vBetaM = float3(0.0000004, 0.0000006, 0.0000024);
float3 g_vSunColor = float3(1.0f, 1.0f, 0.0f);
float  g_fG = 0.4f;
#define BETA_SUM (g_vBetaR + g_vBetaM)
#define PI 3.14159f

struct VS_OUT
{
	float4 vPosition : POSITION;
	float3 vLIn : TEXCOORD0;
	float3 vFEx : TEXCOORD1;
};

struct VS_IN
{
	float4 vPosition : POSITION;
};

//==== Shaders
VS_OUT VS_SkyPlane ( VS_IN IN )
{
	VS_OUT OUT = (VS_OUT)0;
	
	OUT.vPosition = mul(IN.vPosition, g_matWorldViewProj);
	
	//	Calculate the view direction vector
	float3 vView = g_vCameraPos - mul(IN.vPosition, g_matWorld);
	vView = normalize(vView);
	
	//	Calculate Theta, the angle between the view direction and the light direction
	float fCosTheta = dot(vView, normalize(g_vLightDir.xyz));
	
	//	Calculate Raleigh Scattering
	float3 vRaleigh = (3.0f / (16.0f * PI)) * g_vBetaR * ( 1 + pow(fCosTheta,2) );
	
	//	Calculate Mie Scattering
	float3 vMie = (1.0f / (4.0f * PI)) * g_vBetaM * ( (pow( (1 - g_fG), 2)) / pow((1 + pow(g_fG, 2) - 2 * g_fG * fCosTheta), 3.0f/2.0f));
	
	//	Calculate In Scattering
	float fDistance = mul(IN.vPosition, g_matWorldView).z;
	OUT.vLIn = ((vRaleigh + vMie) / (BETA_SUM)) * g_vSunColor * (1 - exp( BETA_SUM * (-fDistance) ));
	
	//	Calculate extinction
	OUT.vFEx = exp( BETA_SUM * (-fDistance) );
	
	return OUT;
}

float4 PS_SkyPlane ( VS_OUT IN ) : COLOR
{	
	return float4( (float3(0.0f, 0.0f, 1.0f)*IN.vFEx + (IN.vLIn)), 1.0f );
}


//==== Techniques
technique SkyPlane
{
	pass p0
	{
		VertexShader = compile vs_1_1 VS_SkyPlane();
		PixelShader = compile ps_2_0 PS_SkyPlane();
	}
}



Well, what seems to be happening is that there are two "suns" if I set the light direction to either (0,0,1) or (0,0,-1). If it is set to (0,1,0) or (0,-1,0) then the sun comes from below the terrain. Is there anything that jumps out at anyone that just looks plane wrong? The shader is being applied to a SkyPlane and not a Dome... I don't see why that would mess anything up, but I am kind of lost. Thank you, Eric Friedman [Edited by - Eric Friedman on January 5, 2007 10:03:55 PM]
Eric Friedman
Eric Friedman
Problem fixed...If I knew how to delete this thread I would.

Topic Locked

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

Sign in to reply to this topic.