/* -------------------------------------------------------------------------------- This source file is part of SkyX. Visit http://www.paradise-studios.net/products/skyx/ Copyright (C) 2009-2012 Xavier Verguín González This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt. -------------------------------------------------------------------------------- */ #ifdef GL_ES precision highp int; precision highp float; #endif // --------------------- SkyX ground material ------------------------ // ------------------------- GLSL Vertex ----------------------------- #define USE_OGRE_FROM_FUTURE #include OGRE_UNIFORMS( uniform mat4 uWorldViewProj; uniform mat4 uWorld; uniform vec3 uLightDir; uniform vec3 uCameraPos; uniform vec3 uCameraPos_; uniform vec3 uInvWaveLength; uniform float uInnerRadius; uniform float uKrESun; // Kr * ESun uniform float uKr4PI; // Kr * 4 * PI uniform float uKm4PI; // Km * 4 * PI uniform float uScale; // 1 / (outerRadius - innerRadius) uniform float uScaleDepth; // Where the average atmosphere density is found uniform float uScaleOverScaleDepth; // Scale / ScaleDepth uniform float uSkydomeRadius; uniform int uNumberOfSamples; uniform float uSamples; ) float scale(float cos, float scaledepth) { float x = 1.0 - cos; return scaledepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25)))); } MAIN_PARAMETERS IN(vec4 vertex, POSITION) OUT(vec3 vRayleighColor, TEXCOORD0) OUT(vec3 vDirection, TEXCOORD1) MAIN_DECLARATION { // Clip space position gl_Position = mul(uWorldViewProj, vertex); // Calculate vertex world position vec3 vertexWorldPos = mul(uWorld, vertex).xyz; // 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; v3Pos.xz = (vertexWorldPos.xz - uCameraPos_.xz) / vec2_splat(uSkydomeRadius); v3Pos.y = uCameraPos.y + vertexWorldPos.y / uSkydomeRadius; vec3 v3Ray = v3Pos - uCameraPos; float fFar = length(v3Ray); v3Ray /= vec3_splat(fFar); // Calculate the ray's starting position, then calculate its scattering offset vec3 v3Start = uCameraPos; // Creo k ai k ajustar la posicion y del pixel float fDepth = exp((uInnerRadius - uCameraPos.y) / uScaleDepth); float fCameraAngle = dot(v3Ray, uCameraPos) / length(v3Pos); float fLightAngle = dot(normalize(uLightDir), v3Pos) / length(v3Pos); float fCameraScale = scale(fCameraAngle, uScaleDepth); float fLightScale = scale(fLightAngle, uScaleDepth); float fCameraOffset = fDepth * fCameraScale; float fTemp = (fLightScale + fCameraScale); // Init loop variables float fSampleLength = fFar / uSamples; float fScaledLength = fSampleLength * uScale; vec3 v3SampleRay = v3Ray * vec3_splat(fSampleLength); vec3 v3SamplePoint = v3Start + v3SampleRay * vec3_splat(0.5); // Loop the ray vec3 color = vec3(0.0 , 0.0, 0.0); for (int i = 0; i < uNumberOfSamples; i++) { float fHeight = length(v3SamplePoint); float fDepth = exp(uScaleOverScaleDepth * (uInnerRadius-fHeight)); float fScatter = fDepth * fTemp - fCameraOffset; vec3 v3Attenuate = exp(-vec3_splat(fScatter) * (uInvWaveLength * vec3_splat(uKr4PI) + vec3_splat(uKm4PI))); color += v3Attenuate * vec3_splat(fDepth * fScaledLength); v3SamplePoint += v3SampleRay; } // Outputs vRayleighColor = color * (uInvWaveLength * vec3_splat(uKrESun)); vDirection = uCameraPos - v3Pos; }