Code: Select all
float rY = acos(norm.y+1.0)/1.570796;Code: Select all
float rY = acos(norm.y+1.0)/1.570796;
Code: Select all
s32 matScattering = loadShaders("data/shaders/std.vert", "data/shaders/scatter.frag");
s32 matLookUp = loadShaders("data/shaders/std.vert", "data/shaders/lookup.frag");
screenQuad = new CScreenQuad(smgr->getRootSceneNode(),smgr,10);
screenQuad->getMaterial(0).MaterialType = (video::E_MATERIAL_TYPE)matScattering ;
rt = driver->addRenderTargetTexture(core::dimension2d<u32>(512,512), "RTT1", video::ECF_A8R8G8B8);
skyDome = smgr->addSkyDomeSceneNode(rt,32,32,1.0f,2.0f, 20000.0f);
skyDome->setMaterialFlag ( video::EMF_LIGHTING , false );
skyDome->setMaterialFlag(video::EMF_TRILINEAR_FILTER, true);
skyDome->setMaterialFlag(video::EMF_ANISOTROPIC_FILTER, true);
skyDome->setMaterialTexture(0, rt);
skyDome->setMaterialTexture(1, driver->getTexture("data/stars.jpg"));
skyDome->setMaterialType ( (video::E_MATERIAL_TYPE)matLookUp );Code: Select all
irr::f32 TexVar = -0.921f;
services->setPixelShaderConstant("g", &TexVar, 1);
TexVar = 0.946f;
services->setPixelShaderConstant("gSQ", &TexVar, 1);
TexVar = 10.0f;
services->setPixelShaderConstant("altitudeMetres", &TexVar, 1);
TexVar = 120.0f;
services->setPixelShaderConstant("atmoAltInKm", &TexVar, 1);
TexVar = 150.0f;
services->setPixelShaderConstant("sunIntensity", &TexVar, 1);
TexVar = 1.6f;
services->setPixelShaderConstant("Exposure", &TexVar, 1);
irr::u32 TexVar2 = 9;
services->setPixelShaderConstant("nSamples", (irr::f32*)(&TexVar2), 1);
core::vector3df pos = core::vector3df(0.640,0.560,0.455);
services->setPixelShaderConstant ( "WaveLen" , reinterpret_cast<f32*>( &pos ) , 3 );
core::vector3df np = smgr->getActiveCamera()->getAbsolutePosition();
services->setPixelShaderConstant ( "sun" , reinterpret_cast<f32*>( &np ) , 3 );
irr::u32 TexVar3 = 1;
services->setPixelShaderConstant("tex1", (irr::f32*)(&TexVar3), 1);
TexVar3 = 0;
services->setPixelShaderConstant("tex0", (irr::f32*)(&TexVar3), 1);Code: Select all
driver->beginScene(true, true, video::SColor(255, 50, 50, 200));
driver->setRenderTarget(rt, true, true, video::SColor(255,255,255,255));
screenQuad->render();
driver->setRenderTarget(0, true, true, 0);
smgr->drawAll();
env->drawAll();
driver->endScene();Code: Select all
/*
--------------------------------------------------------------------------------
This source file is part of SkyX.
Visit ---
Copyright (C) 2009 Xavier Verguín González <xavierverguin@hotmail.com>
<xavyiy@gmail.com>
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.
--------------------------------------------------------------------------------
*/
// --------------------- SkyX ground material ------------------------
float scale(float cos, float uScaleDepth)
{
float x = 1.0 - cos;
return uScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
}
void main_vp(
// IN
float4 iPosition : POSITION,
// OUT
out float4 oPosition : POSITION,
out float3 oRayleighColor : TEXCOORD0,
// UNIFORM
uniform float4x4 uWorldViewProj,
uniform float4x4 uWorld,
// Global information
uniform float3 uLightDir,
// Position information
uniform float3 uCameraPos,
uniform float3 uCameraPos_,
uniform float3 uInvWaveLength,
uniform float uInnerRadius,
// Scattering parameters
uniform float uKrESun, // Kr * ESun
uniform float uKr4PI, // Kr * 4 * PI
uniform float uKm4PI, // Km * 4 * PI
// Atmosphere properties
uniform float uScale, // 1 / (outerRadius - innerRadius)
uniform float uScaleDepth, // Where the average atmosphere density is found
uniform float uScaleOverScaleDepth, // Scale / ScaleDepth
uniform float uSkydomeRadius,
// Number of samples
uniform int uNumberOfSamples,
uniform float uSamples)
{
// Clip space position
oPosition = mul(uWorldViewProj, iPosition);
// Calculate vertex world position
float3 vertexWorldPos = mul(uWorld, iPosition);
// 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 v3Pos;
v3Pos.xz = (vertexWorldPos.xz-uCameraPos_.xz) / uSkydomeRadius;
v3Pos.y = uCameraPos.y + vertexWorldPos.y / uSkydomeRadius;
float3 v3Ray = v3Pos - uCameraPos;
float fFar = length(v3Ray);
v3Ray /= fFar;
// Calculate the ray's starting position, then calculate its scattering offset
float3 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;
float3 v3SampleRay = v3Ray * fSampleLength;
float3 v3SamplePoint = v3Start + v3SampleRay * 0.5f;
// Loop the ray
float3 color = float3(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;
float3 v3Attenuate = exp(-fScatter * (uInvWaveLength * uKr4PI + uKm4PI)); // <<< TODO
color += v3Attenuate * (fDepth * fScaledLength);
v3SamplePoint += v3SampleRay;
}
// Outputs
oRayleighColor = color * (uInvWaveLength * uKrESun); // TODO <--- parameter
}
void main_fp(
// IN
float3 iRayleighColor : TEXCOORD0,
// OUT
out float4 oColor : COLOR
// UNIFORM
#ifdef LDR
,uniform float uExposure
#endif // LDR
)
{
oColor = float4(iRayleighColor,1);
#ifdef LDR
oColor.xyz = 1 - exp(-uExposure * oColor);
#endif // LDR
}