   Atmospheric Scattering (Sean O'Neill - GPU Gems2) Problem
  Hi, I'm attempting to implement atmospheric scattering which is in GPU gems 2. I have the book and found this thread that someone has converted to hlsl. Here's my code from my shader and parameters I'm passing to it. I know I'm close but its not displaying the skydome. If i set the output colour to red it does render the skydome so i know its there. I think i have one of the parameters wrong but after spending 2 days trying to find out which i've got no where. Any help would be fantastic! Shader// transformations provided by the app as inputfloat4x4 matWorldViewProj: WORLDVIEWPROJECTION;float3 v3LightDir;// Light directionfloat3 v3CameraPos;// Camera's current positionfloat3 v3InvWavelength;// 1 / pow(wavelength, 4) for RGB channelsfloat fCameraHeight;float fCameraHeight2;float fInnerRadius;float fInnerRadius2;float fOuterRadius;float fOuterRadius2;// Scattering parametersfloat KrESun;// Kr * ESunfloat KmESun;// Km * ESunfloat Kr4PI;// Kr * 4 * PIfloat Km4PI;// Km * 4 * PI// Phase functionfloat g;float g2;float fScale;    // 1 / (outerRadius - innerRadius) = 4 herefloat fScaleDepth;    // Where the average atmosphere density is foundfloat fScaleOverScaleDepth;// scale / scaleDepthfloat fSkydomeRadius;    // Skydome radius (allows us to normalize skydome distances etc)int numSamples;float samples;struct VS_INPUT  { float4 Position   : POSITION;  }; struct VS_OUTPUT{   float4 Position: POSITION;float4 RayleighColor: COLOR0;float4 MieColor: COLOR1;float3 Direction: TEXCOORD0;};float scale(float cos){float x = 1.0 - cos;return fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));}VS_OUTPUT VS(VS_INPUT IN)    {    VS_OUTPUT Out = (VS_OUTPUT)0;  //Transform Position    Out.Position = mul( matWorldViewProj,IN.Position );    // Get the ray from the camera to the vertex, and it's length (far point)float3 v3Pos = IN.Position / fSkydomeRadius; v3Pos.y += fInnerRadius; float3 v3Ray = v3Pos - v3CameraPos;float fFar = length(v3Ray);v3Ray /= fFar;// Calculate the ray's starting position, then calculate its scattering offsetfloat3 v3Start = v3CameraPos;float fHeight = length(v3Start);float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fCameraHeight));float fStartAngle = dot(v3Ray, v3Start) / fHeight;float fStartOffset = fDepth * scale(fStartAngle);// Init loop variablesfloat fSampleLength = fFar / samples;float fScaledLength = fSampleLength * fScale;float3 v3SampleRay = v3Ray * fSampleLength;float3 v3SamplePoint = v3Start + v3SampleRay * 0.5f;// Loop the rayfloat3 color;for (int i = 0; i < numSamples; i++){float fHeight = length(v3SamplePoint);float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius-fHeight));float fLightAngle = dot(v3LightDir, v3SamplePoint) / fHeight;float fCameraAngle = dot(v3Ray, v3SamplePoint) / fHeight;float fScatter = (fStartOffset + fDepth*(scale(fLightAngle) - scale(fCameraAngle)));float3 v3Attenuate = exp(-fScatter * (v3InvWavelength * Kr4PI + Km4PI));// Accumulate colorv3Attenuate *= (fDepth * fScaledLength);color += v3Attenuate;// Next sample pointv3SamplePoint += v3SampleRay;}// Finally, scale the Mie and Rayleigh colorsOut.RayleighColor.xyz = color * (v3InvWavelength * KrESun);Out.RayleighColor.w = 1.0f;Out.MieColor.xyz = color * KmESun;Out.MieColor.w = 1.0f;Out.Direction = v3CameraPos - v3Pos;   return Out;}struct PS_INPUT{float4 RayleighColor: COLOR0;float4 MieColor: COLOR1;float3 Direction: TEXCOORD0;}; float4 PS(PS_INPUT IN) : COLOR   {float cos = dot(v3LightDir, IN.Direction) / length(IN.Direction);float miePhase = 1.5f * ((1.0f - g2) / (2.0f + g2)) * (1.0f + cos*cos) / pow(1.0f + g2 - 2.0f * g * cos, 1.5f); return IN.RayleighColor + miePhase * IN.MieColor;//The skydome is there because you can set colour to red and it works!//float4 colour = float4( 0.5f, 0.2f, 0.2f, 1.0f ); //return colour;}// Effect technique to be usedtechnique Sky{pass P0    {   // shaders   VertexShader = compile vs_2_0 VS();   PixelShader = compile ps_2_0 PS();    //Fillmode = wireframe;} }Parametersvoid setTechniqueVariables(D3DXMATRIX view,D3DXMATRIX matProj,D3DXMATRIX WorldMat,D3DXVECTOR3 eyevector){D3DXMATRIX worldViewProjection = WorldMat* view * matProj;D3DXMatrixTranspose( &worldViewProjection, &worldViewProjection );float m_fPhi = 90.0f; // D3DXToRadian(90);static float time = 0.5f;float m_fTheta = 57.0f; //(time -0.5)*2*D3DXToRadian(90);float y = cos(m_fTheta);float x = sin(m_fTheta) * cos(m_fPhi);float z = sin(m_fTheta) * sin(m_fPhi);float w = 1.0f;D3DXVECTOR4 vSunDir = D3DXVECTOR4(x,y,z,w); vSunDir.w = 1.0;//calculate sun colour and intensity float m_fIntensity = 0.5f;float nTurbidity = 2.0f;float fBeta = 0.04608365822050f * nTurbidity - 0.04586025928522f;float fTauR, fTauA, fTau[3];float m = 1.0/(cos(m_fTheta) + 0.15f*pow(93.885f-m_fTheta/D3DX_PI*180.0f,-1.253f));  // Relative Optical Massint i;float fLambda[3]; fLambda[0] = 0.65f;fLambda[1] = 0.57f;fLambda[2] = 0.475f;for(i = 0; i < 3; i++){fTauR = exp( -m * 0.008735f * pow(fLambda, float(-4.08f)));const float fAlpha = 1.3f;fTauA = exp(-m * fBeta * pow(fLambda, -fAlpha));  fTau = fTauR * fTauA; }D3DXVECTOR4 vAttenuation = D3DXVECTOR4(fTau[0], fTau[1], fTau[2], 1);D3DXVECTOR4 vSunColourIntensity = D3DXVECTOR4(vAttenuation.x,vAttenuation.y,vAttenuation.z,m_fIntensity);  float fMieMult = 0.0025f; float fRayMult = 0.0015f; float g = -0.991f;   D3DXVECTOR4 vInvWavelength;float m_fWavelength[3];float m_fWavelength4[3];m_fWavelength[0] = 0.650f;m_fWavelength[1] = 0.570f;m_fWavelength[2] = 0.475f;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);vInvWavelength.x = 1.0f / m_fWavelength4[0];vInvWavelength.y = 1.0f / m_fWavelength4[1];vInvWavelength.z = 1.0f / m_fWavelength4[2];float fInnerRadius = 10.0f;float fOuterRadius = 10.25f;float fScale = 1 / (fOuterRadius - fInnerRadius);float fScaleDepth = 0.0125f;float fScaleOverScaleDepth = 16.0f;D3DXVECTOR4 vecCamera = eyevector;float fSkydomeRadius = 150000.0f; vecCamera /= fSkydomeRadius;vecCamera.y += fInnerRadius;if (vecCamera.y <= fInnerRadius) vecCamera.y = fInnerRadius + 1.0e-6f;vecCamera.x = 0;vecCamera.z = 0;float fCameraHeight = vecCamera.y;float fkr4PI = fRayMult * 4.0f * D3DX_PI;float fkm4PI = fMieMult * 4.0f * D3DX_PI;float fKrESun = fRayMult * vSunColourIntensity.w;float fKmESun = fMieMult * vSunColourIntensity.w;g_pEffect->SetMatrix( "matWorldViewProj", &worldViewProjection );g_pEffect->SetVector("m_pv3LightDir", &vSunDir);g_pEffect->SetVector("m_pv3CameraPos", &vecCamera);g_pEffect->SetVector("m_pv3InvWavelength", &vInvWavelength);g_pEffect->SetFloat("m_pfCameraHeight",fCameraHeight);g_pEffect->SetFloat("m_pfCameraHeight2",fCameraHeight*fCameraHeight);g_pEffect->SetFloat("m_pfInnerRadius", fInnerRadius);g_pEffect->SetFloat("m_pfInnerRadius2", fInnerRadius * fInnerRadius);g_pEffect->SetFloat("m_pfOuterRadius", fOuterRadius);g_pEffect->SetFloat("m_pfOuterRadius2", fOuterRadius * fOuterRadius);g_pEffect->SetFloat("m_pKrESun",fKrESun);g_pEffect->SetFloat("m_pKmESun",fKmESun);g_pEffect->SetFloat("m_pKr4PI", fkr4PI);g_pEffect->SetFloat("m_pKm4PI", fkm4PI);g_pEffect->SetFloat("m_pg", g);g_pEffect->SetFloat("m_pg2", g*g);g_pEffect->SetFloat("m_pfScale", fScale);g_pEffect->SetFloat("m_pScaleDepth", fScaleDepth);g_pEffect->SetFloat("m_pScaleOverScaleDepth", fScaleOverScaleDepth);g_pEffect->SetFloat("m_pfSkydomeRadius", fSkydomeRadius);int iNumSamples = 2; g_pEffect->SetInt("m_pnumSamples", iNumSamples);g_pEffect->SetInt("m_psamples", iNumSamples);}If anyone has implemented this before and got it working i would most appreciate your input. Thanks   
  Hi Colinolivant,Suggest you check out this thread if you haven't already.Formski   