Correct after blending.

This commit is contained in:
Geenz 2013-06-09 22:48:14 -04:00
parent 5efe323c5e
commit d512b414ee
2 changed files with 108 additions and 106 deletions

View file

@ -1,106 +1,107 @@
#version 120 #version 120
// //
// For licensing information, see http://http.developer.nvidia.com/GPUGems/gpugems_app01.html: // For licensing information, see http://http.developer.nvidia.com/GPUGems/gpugems_app01.html:
// //
// NVIDIA Statement on the Software // NVIDIA Statement on the Software
// //
// The source code provided is freely distributable, so long as the NVIDIA header remains unaltered and user modifications are // The source code provided is freely distributable, so long as the NVIDIA header remains unaltered and user modifications are
// detailed. // detailed.
// //
// No Warranty // No Warranty
// //
// THE SOFTWARE AND ANY OTHER MATERIALS PROVIDED BY NVIDIA ON THE ENCLOSED CD-ROM ARE PROVIDED "AS IS." NVIDIA DISCLAIMS ALL // THE SOFTWARE AND ANY OTHER MATERIALS PROVIDED BY NVIDIA ON THE ENCLOSED CD-ROM ARE PROVIDED "AS IS." NVIDIA DISCLAIMS ALL
// WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, // WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// //
// Limitation of Liability // Limitation of Liability
// //
// NVIDIA SHALL NOT BE LIABLE TO ANY USER, DEVELOPER, DEVELOPER'S CUSTOMERS, OR ANY OTHER PERSON OR ENTITY CLAIMING THROUGH OR // NVIDIA SHALL NOT BE LIABLE TO ANY USER, DEVELOPER, DEVELOPER'S CUSTOMERS, OR ANY OTHER PERSON OR ENTITY CLAIMING THROUGH OR
// UNDER DEVELOPER FOR ANY LOSS OF PROFITS, INCOME, SAVINGS, OR ANY OTHER CONSEQUENTIAL, INCIDENTAL, SPECIAL, PUNITIVE, DIRECT // UNDER DEVELOPER FOR ANY LOSS OF PROFITS, INCOME, SAVINGS, OR ANY OTHER CONSEQUENTIAL, INCIDENTAL, SPECIAL, PUNITIVE, DIRECT
// OR INDIRECT DAMAGES (WHETHER IN AN ACTION IN CONTRACT, TORT OR BASED ON A WARRANTY), EVEN IF NVIDIA HAS BEEN ADVISED OF THE // OR INDIRECT DAMAGES (WHETHER IN AN ACTION IN CONTRACT, TORT OR BASED ON A WARRANTY), EVEN IF NVIDIA HAS BEEN ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF THE ESSENTIAL PURPOSE OF ANY // POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF THE ESSENTIAL PURPOSE OF ANY
// LIMITED REMEDY. IN NO EVENT SHALL NVIDIA'S AGGREGATE LIABILITY TO DEVELOPER OR ANY OTHER PERSON OR ENTITY CLAIMING THROUGH // LIMITED REMEDY. IN NO EVENT SHALL NVIDIA'S AGGREGATE LIABILITY TO DEVELOPER OR ANY OTHER PERSON OR ENTITY CLAIMING THROUGH
// OR UNDER DEVELOPER EXCEED THE AMOUNT OF MONEY ACTUALLY PAID BY DEVELOPER TO NVIDIA FOR THE SOFTWARE OR ANY OTHER MATERIALS. // OR UNDER DEVELOPER EXCEED THE AMOUNT OF MONEY ACTUALLY PAID BY DEVELOPER TO NVIDIA FOR THE SOFTWARE OR ANY OTHER MATERIALS.
// //
// //
// Atmospheric scattering fragment shader // Atmospheric scattering fragment shader
// //
// Author: Sean O'Neil // Author: Sean O'Neil
// //
// Copyright (c) 2004 Sean O'Neil // Copyright (c) 2004 Sean O'Neil
// //
uniform vec3 v3CameraPos; // The camera's current position uniform vec3 v3CameraPos; // The camera's current position
uniform vec3 v3InvWavelength; // 1 / pow(wavelength, 4) for the red, green, and blue channels uniform vec3 v3InvWavelength; // 1 / pow(wavelength, 4) for the red, green, and blue channels
uniform float fInnerRadius; // The inner (planetary) radius uniform float fInnerRadius; // The inner (planetary) radius
uniform float fKrESun; // Kr * ESun uniform float fKrESun; // Kr * ESun
uniform float fKmESun; // Km * ESun uniform float fKmESun; // Km * ESun
uniform float fKr4PI; // Kr * 4 * PI uniform float fKr4PI; // Kr * 4 * PI
uniform float fKm4PI; // Km * 4 * PI uniform float fKm4PI; // Km * 4 * PI
uniform float fScale; // 1 / (fOuterRadius - fInnerRadius) 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 fScaleDepth; // The scale depth (i.e. the altitude at which the atmosphere's average density is found)
uniform float fScaleOverScaleDepth; // fScale / fScaleDepth uniform float fScaleOverScaleDepth; // fScale / fScaleDepth
const int nSamples = 2; const int nSamples = 2;
const float fSamples = 2.0; const float fSamples = 2.0;
uniform vec3 v3LightPos; uniform vec3 v3LightPos;
uniform float g; uniform float g;
uniform float g2; uniform float g2;
varying vec3 position; varying vec3 position;
float scale(float fCos) float scale(float fCos)
{ {
float x = 1.0 - fCos; float x = 1.0 - fCos;
return fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25)))); return fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
} }
void main (void) 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) // 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 = position; vec3 v3Pos = position;
vec3 v3Ray = v3Pos - v3CameraPos; vec3 v3Ray = v3Pos - v3CameraPos;
float fFar = length(v3Ray); float fFar = length(v3Ray);
v3Ray /= fFar; v3Ray /= fFar;
// Calculate the ray's starting position, then calculate its scattering offset // Calculate the ray's starting position, then calculate its scattering offset
vec3 v3Start = v3CameraPos; vec3 v3Start = v3CameraPos;
float fHeight = length(v3Start); float fHeight = length(v3Start);
float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight)); float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));
float fStartAngle = dot(v3Ray, v3Start) / fHeight; float fStartAngle = dot(v3Ray, v3Start) / fHeight;
float fStartOffset = fDepth * scale(fStartAngle); float fStartOffset = fDepth * scale(fStartAngle);
// Initialize the scattering loop variables // Initialize the scattering loop variables
//gl_FrontColor = vec4(0.0, 0.0, 0.0, 0.0); //gl_FrontColor = vec4(0.0, 0.0, 0.0, 0.0);
float fSampleLength = fFar / fSamples; float fSampleLength = fFar / fSamples;
float fScaledLength = fSampleLength * fScale; float fScaledLength = fSampleLength * fScale;
vec3 v3SampleRay = v3Ray * fSampleLength; vec3 v3SampleRay = v3Ray * fSampleLength;
vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5; vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;
// Now loop through the sample rays // Now loop through the sample rays
vec3 v3FrontColor = vec3(0.0, 0.0, 0.0); vec3 v3FrontColor = vec3(0.0, 0.0, 0.0);
for(int i=0; i<nSamples; i++) for(int i=0; i<nSamples; i++)
{ {
float fHeight = length(v3SamplePoint); float fHeight = length(v3SamplePoint);
float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight)); float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));
float fLightAngle = dot(v3LightPos, v3SamplePoint) / fHeight; float fLightAngle = dot(v3LightPos, v3SamplePoint) / fHeight;
float fCameraAngle = dot((v3Ray), v3SamplePoint) / fHeight * 0.99; float fCameraAngle = dot((v3Ray), v3SamplePoint) / fHeight * 0.99;
float fScatter = (fStartOffset + fDepth * (scale(fLightAngle) - scale(fCameraAngle))); float fScatter = (fStartOffset + fDepth * (scale(fLightAngle) - scale(fCameraAngle)));
vec3 v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI)); vec3 v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI));
v3FrontColor += v3Attenuate * (fDepth * fScaledLength); v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
v3SamplePoint += v3SampleRay; v3SamplePoint += v3SampleRay;
} }
// Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader // Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader
vec3 secondaryFrontColor = v3FrontColor * fKmESun; vec3 secondaryFrontColor = v3FrontColor * fKmESun;
vec3 frontColor = v3FrontColor * (v3InvWavelength * fKrESun); vec3 frontColor = v3FrontColor * (v3InvWavelength * fKrESun);
vec3 v3Direction = v3CameraPos - v3Pos; vec3 v3Direction = v3CameraPos - v3Pos;
float fCos = dot(v3LightPos, v3Direction) / length(v3Direction); 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); 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.rgb = frontColor.rgb + fMiePhase * secondaryFrontColor.rgb; gl_FragColor.rgb = frontColor.rgb + fMiePhase * secondaryFrontColor.rgb;
gl_FragColor.a = gl_FragColor.b; gl_FragColor.a = gl_FragColor.b;
} gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(1.0/2.2));
}

View file

@ -110,4 +110,5 @@ void main (void)
vec3 secondaryColor = v3FrontColor * fKmESun; vec3 secondaryColor = v3FrontColor * fKmESun;
gl_FragColor.rgb = color + fMiePhase * secondaryColor; gl_FragColor.rgb = color + fMiePhase * secondaryColor;
gl_FragColor.a = gl_FragColor.b; gl_FragColor.a = gl_FragColor.b;
gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(1.0/2.2));
} }