Sign in to follow this  
b3rs3rk

Light Scattering problem [SOLVED] (with screenshot)

Recommended Posts

i'm trying to implement the technique described in this paper: http://www.ati.com/developer/dx9/ATI-LightScattering.pdf using glsl, but i've got some problems. This is the function i use to call the shader:
void Bind_SkyShader(){
	/************ scattering ******************************/
	vec3 lambda(1.0 / 650e-9,1.0 / 570e-9,1.0 / 475e-9);
	vec3 lambda2;
	vec3 lambda4;
	for(int i = 0; i < 3; i++) {
		lambda2[i] = lambda[i] * lambda[i];
		lambda4[i] = lambda2[i] * lambda2[i];
	}
	
	float n = 1.003;
	float N = 2.545e25;
	float pn = 0.035;
	float T = 2.0;
	float c = (6.544 * T - 6.51) * 1e-17;
	
	float tempRay = PI * PI * (n * n - 1.0) * (n * n - 1.0) * (6.0 + 3.0 * pn) / (6.0 - 7.0 * pn) / N;
	vec3 betaRay = lambda4 * 8.0 * tempRay * PI / 3.0;
	
	float tempMie = 0.434 * c * PI * (2 * PI) * (2 * PI) * 0.5;
	vec3 betaMie = vec3(lambda2.x * 0.685,lambda2.y * 0.679,lambda2.z * 0.67) * tempMie;
	
	float greenstein = 0.8 * 1.5;
	float energy = 10.0 * 1.0;

	betaRay *= 0.8 * 1.1;
	betaMie *= 0.2 * 0.8;

	/************ scattering ******************************/

	vec3 color(0.1f, 0.3f, 0.5f);
	vec3 camera = g_Camera.GetPosition();
	vec3 sun_dir = vec3(sin(g_time / 10.0),cos(g_time / 10.0),0.1 + fabs(sin(g_time / 30.0)));
	sun_dir.normalize();

	glUseProgramObjectARB(Sky_Shader);

	GLint loc = glGetUniformLocationARB(Sky_Shader,"time");
	glUniform1fARB(loc,g_time * 0.01f);	
	GLint loc3 = glGetUniformLocationARB(Sky_Shader,"camera");
	glUniform4fARB(loc3,camera.x,camera.y,camera.z,1.0);
	GLint loc4 = glGetUniformLocationARB(Sky_Shader,"dir");
	glUniform4fARB(loc4,-sun_dir.x,-sun_dir.y,-sun_dir.z,1.0);

	GLint loc5 = glGetUniformLocationARB(Sky_Shader,"betaRay");
	glUniform4fARB(loc5,betaRay.x,betaRay.y,betaRay.z,1.0);

	GLint loc6 = glGetUniformLocationARB(Sky_Shader,"betaMie");
	glUniform4fARB(loc6,betaMie.x,betaMie.y,betaMie.z,1.0);

	GLint loc7 = glGetUniformLocationARB(Sky_Shader,"greenstein");
	glUniform4fARB(loc7,-greenstein * 2.0,greenstein * greenstein + 1.0,(1.0 - greenstein) * (1.0 - greenstein),0);

	GLint loc1 = glGetUniformLocationARB(Sky_Shader,"horizon");
	glUniform4fARB(loc1,color.x,color.y,color.z,1);
	GLint loc2 = glGetUniformLocationARB(Sky_Shader,"s_texture_0");
	glUniform1iARB(loc2,0);


}




VERTEX SHADER
uniform float time;
uniform vec4  camera;
uniform vec4  dir;
uniform vec4  betaRay ;
uniform vec4  betaMie;
uniform vec4  greenstein;

void main() {
	gl_Position = ftransform(); 
	
	float CosTheta = dot(normalize(camera.xyz - gl_Vertex.xyz), dir.xyz);
	vec3 RayTheta = 0.0597 * betaRay.xyz * (1 + CosTheta*CosTheta);
	
	float div = pow( (greenstein.y + greenstein.x * CosTheta) , 1.5f);
	vec3 MieTheta = (0.693 * betaMie.xyz * greenstein.z) / div;  
	
	float dist = distance( camera.xyz, gl_Vertex.xyz );
	vec3 BetaSum = (betaRay.xyz + betaMie.xyz);	
		
	vec3 SunEnergy =  -(BetaSum) * dist;
	float neper = 2.7182818284590452353602874713527;
	vec3 En = pow(vec3(neper,neper,neper), SunEnergy);
	
	vec3 Lin = (RayTheta + MieTheta) * (1 - En) / BetaSum.xyz;
	
	gl_TexCoord[0].x  = gl_MultiTexCoord0.x + time*0.66;
        gl_TexCoord[0].y  = gl_MultiTexCoord0.y + time*0.33;
        gl_TexCoord[0].xy *= 6.0;

        gl_TexCoord[0].z = gl_MultiTexCoord0.x + time*1.33;
        gl_TexCoord[0].w = gl_MultiTexCoord0.y + time*1.66;
        gl_TexCoord[0].zw *= 3.0;     
	
	gl_TexCoord[1] = vec4(Lin,1.0);
	gl_TexCoord[2] = vec4(En,1.0);
}



PIXEL SHADER
uniform sampler2D s_texture_0;
uniform half4 horizon;

void main(){
    vec4 clouds = texture2D(s_texture_0,gl_TexCoord[0].xy);
    
    half intensity = 0.6;
	half4 cloudColor = half4((1.0 - intensity)*horizon.x, (1.0 - intensity)*horizon.y, intensity*horizon.z, 0.0); 
	
	gl_FragColor = (cloudColor * (1.0 - clouds.x) + clouds) * gl_TexCoord[2] + gl_TexCoord[1];
}



every thing i get is a black sky -__- Any suggestions? [Edited by - b3rs3rk on November 7, 2005 4:33:13 AM]

Share this post


Link to post
Share on other sites
Solved! here is a screen:

Atmosphere

there was a problem in the pixel shader, the new code is

uniform sampler2D s_texture_0;

void main(){

vec2 bc = vec2(0.10,0.15);
vec4 clouds = texture2D(s_texture_0,gl_TexCoord[0].xy) * 3;

half4 horizon = half4(0.1, 0.3, 0.5, 1) * 2;
half4 TotalColor = (horizon * (1.0 - clouds.x) + clouds) ;

vec4 color = lerp(gl_TexCoord[2],TotalColor,gl_TexCoord[1]);

vec4 temp = color - 0.5;
color = temp * bc.y + color;

gl_FragColor = color + bc.x;

}



hope it helps :)

Share this post


Link to post
Share on other sites

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