Original Post
Hi guys and girls, I'm trying to use Atmospheric Scattering (O'Neill 2004) but only get black sphere. The shaders being used are 99% copy/pasted from GPU gems 2, but with comments removed (comments won't compile for some reason).
I've tried changing gl_FrontColor to see if the faces are in the wrong direction, but I can set a color and see it pass through the fragment shader just fine. However, using the actual scattering algorithm is a no go right now and I can't figure out what's causing it to zero out. Am I missing something?
For reference, here they are:
Vertex Shader:
Fragment Shader:
And here is how I feed the parameters into the shader:
Rendering:
[Edited by - DanoruX on January 3, 2011 2:08:02 PM]
I've tried changing gl_FrontColor to see if the faces are in the wrong direction, but I can set a color and see it pass through the fragment shader just fine. However, using the actual scattering algorithm is a no go right now and I can't figure out what's causing it to zero out. Am I missing something?
For reference, here they are:
Vertex Shader:
uniform vec3 v3CameraPos;uniform vec3 v3LightPos; uniform vec3 v3InvWavelength; uniform float fCameraHeight; uniform float fCameraHeight2; uniform float fOuterRadius; uniform float fOuterRadius2; uniform float fInnerRadius; uniform float fInnerRadius2; uniform float fKrESun; uniform float fKmESun; uniform float fKr4PI; uniform float fKm4PI; uniform float fScale; uniform float fScaleDepth; uniform float fScaleOverScaleDepth;const int nSamples = 4;const float fSamples = 4.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){ vec3 v3Pos = gl_Vertex.xyz; vec3 v3Ray = v3Pos - v3CameraPos; float fFar = length(v3Ray); v3Ray /= fFar; vec3 v3Start = v3CameraPos; float fHeight = length(v3Start); float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fCameraHeight)); float fStartAngle = dot(v3Ray, v3Start) / fHeight; float fStartOffset = fDepth*scale(fStartAngle); float fSampleLength = fFar / fSamples; float fScaledLength = fSampleLength * fScale; vec3 v3SampleRay = v3Ray * fSampleLength; vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5; 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; } gl_FrontSecondaryColor.rgb = v3FrontColor * fKmESun; gl_FrontColor.rgb = v3FrontColor * (v3InvWavelength * fKrESun); gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; v3Direction = v3CameraPos - v3Pos;}Fragment Shader:
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; gl_FragColor.a = gl_FragColor.b;}And here is how I feed the parameters into the shader:
glUseProgram(sky_shader); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); GLint v3CameraPos, v3LightPos, v3InvWavelength, fCameraHeight, fCameraHeight2, fInnerRadius, fInnerRadius2, fOuterRadius, fOuterRadius2, fKrESun, fKmESun, fKr4PI, fKm4PI, fScale, fScaleDepth, fScaleOverScaleDepth, g, g2; glGetUniformLocation(v3CameraPos, "v3CameraPos"); glUniform3f(v3CameraPos, 0, 1, 0); glGetUniformLocation(v3LightPos, "v3LightPos"); glUniform3f(v3LightPos, sun_x / sqrt(sun_x * sun_x + sun_y * sun_y), sun_y / sqrt(sun_x * sun_x + sun_y * sun_y), 0); glGetUniformLocation(v3InvWavelength, "v3InvWavelength"); glUniform3f(v3InvWavelength, 1.0 / pow(0.650, 4.0), 1.0 / pow(0.570, 4.0), 1.0 / pow(0.475, 4.0)); glGetUniformLocation(fCameraHeight, "fCameraHeight"); glUniform1fARB(fCameraHeight, 1); glGetUniformLocation(fCameraHeight2, "fCameraHeight2"); glUniform1fARB(fCameraHeight2, 1); glGetUniformLocation(fInnerRadius, "fInnerRadius"); glUniform1fARB(fInnerRadius, 6356.7523142); glGetUniformLocation(fInnerRadius2, "fInnerRadius2"); glUniform1fARB(fInnerRadius2, 6356.7523142 * 6356.7523142); glGetUniformLocation(fOuterRadius, "fOuterRadius"); glUniform1fARB(fOuterRadius, 6452.103598913); glGetUniformLocation(fOuterRadius2, "fOuterRadius2"); glUniform1fARB(fOuterRadius2, 6452.103598913 * 6452.103598913); glGetUniformLocation(fKrESun, "fKrESun"); glUniform1fARB(fKrESun, 0.0025 * 20.0); glGetUniformLocation(fKmESun, "fKmESun"); glUniform1fARB(fKmESun, 0.0015 * 20.0); glGetUniformLocation(fKr4PI, "fKr4PI"); glUniform1fARB(fKr4PI, 0.0025 * 4.0 * 3.141592653); glGetUniformLocation(fKm4PI, "fKm4PI"); glUniform1fARB(fKm4PI, 0.0015 * 4.0 * 3.141592653); glGetUniformLocation(fScale, "fScale"); glUniform1fARB(fScale, 1.0 / (6452.103598913 - 6356.7523142)); glGetUniformLocation(fScaleDepth, "fScaleDepth"); glUniform1fARB(fScaleDepth, 0.25); glGetUniformLocation(fScaleOverScaleDepth, "fScaleOverScaleDepth"); glUniform1fARB(fScaleOverScaleDepth, 4.0 / (6452.103598913 - 6356.7523142)); glGetUniformLocation(g, "g"); glUniform1fARB(g, -0.85); glGetUniformLocation(g2, "g2"); glUniform1f(g2, -0.85 * -0.85);Rendering:
glRotated(CAMERA->rotation->x, 1, 0, 0); glRotated(CAMERA->rotation->y, 0, 1, 0); glTranslated(0,-EARTH_RADIUS -CAMERA->position->y / 1000,0); gluSphere(sky_sphere, EARTH_ATMOSPHERE_RADIUS, 128, 128);[Edited by - DanoruX on January 3, 2011 2:08:02 PM]
