Jump to content
  • Advertisement
Sign in to follow this  

[SOLVED] Atmospheric Scattering Issues

This topic is 3451 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

Hi, I am having some trouble getting my atmospheric scattering to work properly. I am implementing Sean O'Neils model from GPU Gems 2 but there seems to be some issues. The scattering is the one for looking up at the sky from the atmosphere. I am aware that there any many posts in the forums about similar issues but I have looked through them all and I seem to be doing everything correctly, fix's that have worked for similar problems have not had any effect on my implementation. There are two issues, but I am mainly concerned with the first one 1) The sun does not render, however there is a noticeable white ring in the scene that looks like an outline of the sun... the white ring also moves correctly during sunset and rise. 2) The sky is quite dark at the top... I have heard this is a general problem with the algorithm however and am more concerned about getting the sun to light up properly. HLSL Shader
uniform extern float4x4 ViewProjMatrix : VIEWPROJECTION;
uniform extern float4x4 WorldMatrix    : WORLD;

// @!brief Atmospheric Scattering Constants
uniform extern float3 cameraPos;         // current camera position
uniform extern float3 lightVec;          // camera - > light vector
uniform extern float3 inverseWaveLength; // 1 / pow(wavelength, 4) for the red, green, and blue channels
uniform extern float  cameraHeight;	     // The camera's current height
uniform extern float  cameraHeightSq;	 // camera height squared
uniform extern float  outerRadius;		 // The outer (atmosphere) radius
uniform extern float  outerRadiusSq;	 // fOuterRadius^2
uniform extern float  innerRadius;		 // The inner (planetary) radius
uniform extern float  innerRadiusSq;	 // fInnerRadius^2
uniform extern float  cRayleighESun;	 // Rayleigh Constant * ESun
uniform extern float  cMieESun;			 // Mie Constant * ESun
uniform extern float  cRayleigh4Pi;		 // Rayleigh Constant * 4 * PI
uniform extern float  cMie4Pi;			 // Mie Constant * 4 * PI
uniform extern float  scale;			 // 1 / (outerRadius - innerRadius)
uniform extern float  scaleDepth;		 // The scale depth (i.e. the altitude at which the atmosphere's average density is found)
uniform extern float  scaleOverScaleDepth;	// scale / scaleDepth
uniform extern float3 lightPos;
uniform extern float  g;
uniform extern float  g2;

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

// @!brief scale helper function
float scaleF(float fCos)
	float x = 1.0 - fCos;
	return scaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));

struct VS_INPUT
	float3 Position  : POSITION;  // The position of the vertex in model-space

struct VS_OUTPUT
	float4 Position       : POSITION;
	float3 FrontColour    : TEXCOORD0;
	float3 SecondryColour : TEXCOORD1;
	float3 Direction      : TEXCOORD2;

struct PS_OUTPUT
	float4 Colour : COLOR;

	// Get the ray from the camera to the vertex, and its length (which is the far point of the ray passing through the atmosphere)
	float3 cameraToVert = IN.Position.xyz - cameraPos;
	float fFar = length(cameraToVert);
	cameraToVert /= fFar;

	// Calculate the ray's starting position, then calculate its scattering offset
	float fHeight = length(cameraPos);
	float fDepth = exp(scaleOverScaleDepth * (innerRadius - cameraHeight));
	float fStartAngle = dot(cameraToVert, cameraPos) / fHeight;
	float fStartOffset = fDepth*scaleF(fStartAngle);

	// Initialize the scattering loop variables
	float fSampleLength = fFar / fSamples;
	float fScaledLength = fSampleLength * scale;
	float3 sampleRay = cameraToVert * fSampleLength;
	float3 samplePoint = cameraPos + sampleRay * 0.5;

	// Now loop through the sample rays
	float3 frontColor = float3(0.0f, 0.0f, 0.0f);
	for(int i=0; i < nSamples; i++)
		float fHeight = length(samplePoint);
		float fDepth = exp(scaleOverScaleDepth * (innerRadius - fHeight));
		float fLightAngle = dot(lightVec, samplePoint) / fHeight;
		float fCameraAngle = dot(cameraToVert, samplePoint) / fHeight;
		float fScatter = (fStartOffset + fDepth*(scaleF(fLightAngle) - scaleF(fCameraAngle)));
		float3 attenuate = exp(-fScatter * (inverseWaveLength * cRayleigh4Pi + cMie4Pi));
		frontColor += attenuate * (fDepth * fScaledLength);
		samplePoint += sampleRay;

	//Finally, scale the Mie and Rayleigh colors and set up the output for the pixel shader
	float3 worldPosition = mul( float4(IN.Position, 1.0f), WorldMatrix );        
    OUT.Position = mul( float4(worldPosition, 1.0f), ViewProjMatrix );
	OUT.FrontColour = frontColor * inverseWaveLength * cRayleighESun;
	OUT.SecondryColour = frontColor * cMieESun;
	OUT.Direction = cameraPos - IN.Position;  
    return OUT;

	float fCos = dot(lightPos, IN.Direction) / length(IN.Direction);
	float fMiePhase = 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + fCos*fCos) / pow(1.0 + g2 - 2.0*g*fCos, 1.5);

	OUT.Colour.rgb = IN.FrontColour + fMiePhase * IN.SecondryColour;
	OUT.Colour.a = OUT.Colour.b;
	return OUT;

technique SkySphere
    pass P0
		CullMode = None;
		VertexShader = compile vs_2_0 SkyVS();
		PixelShader  = compile ps_2_0 SkyPS();

And the setup/params:
m_radi(radius),  //Currently 500.0f
//Build our world matrix
CMatrix4x4 worldMatrix(CMatrix4x4::kIdentity);

//Get world view projection
D3DXMATRIX worldViewProj = camera.viewProj();

ID3DXEffect* effect = fxManager.getEffect(effectFile);

	HRESULT hr = 0;
	hr = effect->SetTechnique("SkySphere");
	//Convert from D3D Type to CMatrix4x4
	D3DXMATRIX world = *(D3DXMATRIX*)&worldMatrix;
	hr = effect->SetMatrix("ViewProjMatrix", &worldViewProj);
	hr = effect->SetMatrix("WorldMatrix", &world);

	D3DXVECTOR3 lightPos = lightManager.getLightPosition("Sun");
	D3DXVECTOR3 cameraPos = D3DXVECTOR3(0.0f, 497.399f, 0.0f ); //FIX CAMERA POS in atmosphere for debuging //camera.pos();
	effect->SetValue("cameraPos", &cameraPos, sizeof(D3DXVECTOR3)); // current camera position

	D3DXVECTOR3 lightVector = (lightPos - cameraPos);
	D3DXVec3Normalize(&lightVector, &lightVector);
	effect->SetValue("lightVec", &lightVector, sizeof(D3DXVECTOR3));

	D3DXVECTOR3 waveLength(0.650f, 0.570f, 0.475f); // 650 nm for red, 570 nm for green, 475 nm for blue
	D3DXVECTOR3 waveLength4(powf(waveLength.x, 4.0f), powf(waveLength.y, 4.0f), powf(waveLength.z,4.0f));
	D3DXVECTOR3 inverseWaveLength(1.0f/waveLength4.x, 1.0f/waveLength4.y, 1.0f/waveLength4.z);
	effect->SetValue("inverseWaveLength", &inverseWaveLength, sizeof(D3DXVECTOR3));// 1 / pow(wavelength, 4) for the red, green, and blue channels
	RAfloat cameraHeight = cameraPos.x*cameraPos.x + cameraPos.y*cameraPos.y + cameraPos.z*cameraPos.z;
	cameraHeight = sqrtf(cameraHeight);
	editVarsTemp(cameraHeight, updateTime);	
	effect->SetFloat("cameraHeight", cameraHeight);
	effect->SetFloat("cameraHeightSq", cameraHeight * cameraHeight);

	effect->SetFloat("outerRadius", m_radi);
	effect->SetFloat("outerRadiusSq", m_radi * m_radi);
	effect->SetFloat("innerRadius", m_innerRadius);
	effect->SetFloat("innerRadiusSq", m_innerRadius * m_innerRadius);

	effect->SetFloat("cRayleighESun", m_cRayleigh * m_sunBrightness);
	effect->SetFloat("cMieESun", m_cMie * m_sunBrightness);
	effect->SetFloat("cRayleigh4Pi", m_cRayleigh * 4 * RA_PI);
	effect->SetFloat("cMie4Pi", m_cMie * 4 * RA_PI);

	effect->SetFloat("scale", m_scale);			
	effect->SetFloat("scaleDepth", m_scaleDepth);
	effect->SetFloat("scaleOverScaleDepth", m_scale/m_scaleDepth); 

	effect->SetValue("lightPos", &lightPos, sizeof(D3DXVECTOR3));
	effect->SetFloat("g", m_g);
	effect->SetFloat("g2", m_g*m_g);

I have tried exposing all the vars and tweaking but non seem to help, any help or suggestions to try would be appreciated. The screens are taken from outside with the camera position set inside the atmosphere of the sphere with backface culling turned off, just easier to see whats goings on from that angle) The first screen shows the white circle I mentioned above, I am preety sure thats supposed to be the sun, why its not filling the centre I have no idea. [Edited by - reaper93 on January 12, 2009 11:28:43 AM]

Share this post

Link to post
Share on other sites

I have made a little bit of progress, in the pixel shader I was using the light position, rather than the light vector... I now have a sun, although it is very small when at noon...

I am going to have a tweak with the parameters and see what I can get. Regarding the issues with the darkness I was thinking of just adding a little ambient light to lighten up the scene, does anybody have any ideas on what I can do with the algorihtm to brighten up the top of the sky a little.

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!