Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

gpu gems 2 atmosphere

This topic is 4078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

yeah... im still working with the atmosphere... as a last implementation i thought id try the one described in gpu gems 2... however im having some trouble with it.. all i get is gray color... ive also tried copy pasting the code supplied with the chapter (from nvidias site).. and ive tried running their demo directly... also tried different radius... all gives the same result... any ideas what might eb causing this? EDIT:: the camera is located at the ground
		void SendParameters(GLSLProgram* Shader, vec3 vCamera)
		{
			vec3 m_vLight = vec3(0.0f, 0.0f, 1000.0f);
			vec3 m_vLightDirection = normalize(m_vLight);
			
			float m_nSamples = 3;		// Number of sample rays to use in integral equation
			float m_Kr = 0.0025f;		// Rayleigh scattering constant
			float m_Kr4PI = m_Kr*4.0f*PI;
			float m_Km = 0.0010f;		// Mie scattering constant
			float m_Km4PI = m_Km*4.0f*PI;
			float m_ESun = 20.0f;		// Sun brightness constant
			float m_g = -0.990f;		// The Mie phase asymmetry factor
			float m_fExposure = 2.0f;

			float m_fInnerRadius = 10.0f;
			float m_fOuterRadius = 10.25f;
			float m_fScale = 1 / (m_fOuterRadius - m_fInnerRadius);

			float m_fWavelength[3];
			float m_fWavelength4[3];
			m_fWavelength[0] = 0.650f;		// 650 nm for red
			m_fWavelength[1] = 0.570f;		// 570 nm for green
			m_fWavelength[2] = 0.475f;		// 475 nm for blue
			m_fWavelength4[0] = powf(m_fWavelength[0], 4.0f);
			m_fWavelength4[1] = powf(m_fWavelength[1], 4.0f);
			m_fWavelength4[2] = powf(m_fWavelength[2], 4.0f);

			float m_fRayleighScaleDepth = 0.25f;
			float m_fMieScaleDepth = 0.1f;
			//m_pbOpticalDepth.MakeOpticalDepthBuffer(m_fInnerRadius, m_fOuterRadius, m_fRayleighScaleDepth, m_fMieScaleDepth);


			Shader->sendUniform("v3CameraPos", vCamera.x, vCamera.y, vCamera.z);
			Shader->sendUniform("v3LightPos", m_vLightDirection.x, m_vLightDirection.y, m_vLightDirection.z);
			Shader->sendUniform("v3InvWavelength", 1.0f/m_fWavelength4[0], 1.0f/m_fWavelength4[1], 1.0f/m_fWavelength4[2]);
			Shader->sendUniform("fCameraHeight", length(vCamera));
			Shader->sendUniform("fCameraHeight2", length(vCamera)*length(vCamera));
			Shader->sendUniform("fInnerRadius", m_fInnerRadius);
			Shader->sendUniform("fInnerRadius2", m_fInnerRadius*m_fInnerRadius);
			Shader->sendUniform("fOuterRadius", m_fOuterRadius);
			Shader->sendUniform("fOuterRadius2", m_fOuterRadius*m_fOuterRadius);
			Shader->sendUniform("fKrESun", m_Kr*m_ESun);
			Shader->sendUniform("fKmESun", m_Km*m_ESun);
			Shader->sendUniform("fKr4PI", m_Kr4PI);
			Shader->sendUniform("fKm4PI", m_Km4PI);
			Shader->sendUniform("fScale", 1.0f / (m_fOuterRadius - m_fInnerRadius));
			Shader->sendUniform("fScaleDepth", m_fRayleighScaleDepth);
			Shader->sendUniform("fScaleOverScaleDepth", (1.0f / (m_fOuterRadius - m_fInnerRadius)) / m_fRayleighScaleDepth);
			Shader->sendUniform("g", m_g);
			Shader->sendUniform("g2", m_g*m_g);
			Shader->sendUniform("s2Test", 0);

		}

///// VERTEX


uniform vec3 v3CameraPos;		// The camera's current position
uniform vec3 v3LightPos;		// The direction vector to the light source
uniform vec3 v3InvWavelength;	// 1 / pow(wavelength, 4) for the red, green, and blue channels
uniform float fCameraHeight;	// The camera's current height
uniform float fCameraHeight2;	// fCameraHeight^2
uniform float fOuterRadius;		// The outer (atmosphere) radius
uniform float fOuterRadius2;	// fOuterRadius^2
uniform float fInnerRadius;		// The inner (planetary) radius
uniform float fInnerRadius2;	// fInnerRadius^2
uniform float fKrESun;			// Kr * ESun
uniform float fKmESun;			// Km * ESun
uniform float fKr4PI;			// Kr * 4 * PI
uniform float fKm4PI;			// Km * 4 * PI
uniform float fScale;			// 1 / (fOuterRadius - fInnerRadius)
uniform float fScaleDepth;		// The scale depth (i.e. the altitude at which the atmosphere's average density is found)
uniform float fScaleOverScaleDepth;	// fScale / fScaleDepth

const int nSamples = 2;
const float fSamples = 2.0;

varying vec3 v3Direction;


float scale(float fCos)
{
	float x = 1.0 - fCos;
	return fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
}

void main(void)
{
	// Get the ray from the camera to the vertex, and its length (which is the far point of the ray passing through the atmosphere)
	vec3 v3Pos = gl_Vertex.xyz;
	vec3 v3Ray = v3Pos - v3CameraPos;
	float fFar = length(v3Ray);
	v3Ray /= fFar;

	// Calculate the ray's starting position, then calculate its scattering offset
	vec3 v3Start = v3CameraPos;
	float fHeight = length(v3Start);
	float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fCameraHeight));
	float fStartAngle = dot(v3Ray, v3Start) / fHeight;
	float fStartOffset = fDepth*scale(fStartAngle);

	// Initialize the scattering loop variables
	//gl_FrontColor = vec4(0.0, 0.0, 0.0, 0.0);
	float fSampleLength = fFar / fSamples;
	float fScaledLength = fSampleLength * fScale;
	vec3 v3SampleRay = v3Ray * fSampleLength;
	vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;

	// Now loop through the sample rays
	vec3 v3FrontColor = vec3(0.0, 0.0, 0.0);
	for(int i=0; i<nSamples; i++)
	{
		float fHeight = length(v3SamplePoint);
		float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));
		float fLightAngle = dot(v3LightPos, v3SamplePoint) / fHeight;
		float fCameraAngle = dot(v3Ray, v3SamplePoint) / fHeight;
		float fScatter = (fStartOffset + fDepth*(scale(fLightAngle) - scale(fCameraAngle)));
		vec3 v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI));
		v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
		v3SamplePoint += v3SampleRay;
	}

	// Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader
	gl_FrontSecondaryColor.rgb = v3FrontColor * fKmESun;
	gl_FrontColor.rgb = v3FrontColor * (v3InvWavelength * fKrESun);
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	v3Direction = v3CameraPos - v3Pos;
}

//// FRAGMENT

uniform vec3 v3LightPos;
uniform float g;
uniform float g2;

varying vec3 v3Direction;


void main (void)
{
	float fCos = dot(v3LightPos, v3Direction) / length(v3Direction);
	float fMiePhase = 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + fCos*fCos) / pow(1.0 + g2 - 2.0*g*fCos, 1.5);
	gl_FragColor = (gl_Color + fMiePhase * gl_SecondaryColor)*0.3;
	gl_FragColor.a = gl_FragColor.b;
}



Image Hosted by ImageShack.us

Share this post


Link to post
Share on other sites
Advertisement
Forget the Rayleigh phase function.

gl_FragColor = (gl_Color + fMiePhase * gl_SecondaryColor)*0.3;

->

fRayleighPhase = 0.75 + 0.75 * fCos * fCos;
gl_FragColor = (fRayleighPhase * gl_Color + fMiePhase * gl_SecondaryColor)*0.3;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!