Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 22 additions & 27 deletions main/resources/DepthComposer.material
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,37 @@
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution.

fragment_program Caelum/DepthComposerFP_Dummy cg
fragment_program Caelum/DepthComposerFP_Dummy glsl hlsl
{
source DepthComposer.cg
entry_point MainFP
source DepthComposerMainFP.glsl
profiles ps_3_0 arbfp1

default_params
{
}
}

fragment_program Caelum/DepthComposerFP_DebugDepthRender cg
fragment_program Caelum/DepthComposerFP_DebugDepthRender glsl hlsl
{
source DepthComposer.cg
entry_point MainFP
source DepthComposerMainFP.glsl
profiles ps_3_0 arbfp1
compile_arguments -DDEBUG_DEPTH_RENDER=1
preprocessor_defines DEBUG_DEPTH_RENDER=1

default_params
{
param_named invViewProjMatrix float4x4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
param_named invViewProjMatrix matrix4x4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
}
}

fragment_program Caelum/DepthComposerFP_ExpGroundFog cg
fragment_program Caelum/DepthComposerFP_ExpGroundFog glsl hlsl
{
source DepthComposer.cg
entry_point MainFP
source DepthComposerMainFP.glsl
profiles ps_3_0 arbfp1
compile_arguments -DEXP_GROUND_FOG=1
preprocessor_defines EXP_GROUND_FOG=1

default_params
{
param_named invViewProjMatrix float4x4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
param_named invViewProjMatrix matrix4x4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

param_named worldCameraPos float4 0 0 0 0

Expand All @@ -46,39 +43,37 @@ fragment_program Caelum/DepthComposerFP_ExpGroundFog cg
}
}

fragment_program Caelum/DepthComposerFP_SkyDomeHaze cg
fragment_program Caelum/DepthComposerFP_SkyDomeHaze glsl hlsl
{
source DepthComposer.cg
entry_point MainFP
source DepthComposerMainFP.glsl
profiles ps_3_0 arbfp1
compile_arguments -DSKY_DOME_HAZE=1 -DHAZE_DEPTH_TEXTURE=s2
preprocessor_defines SKY_DOME_HAZE=1

default_params
{
param_named invViewProjMatrix float4x4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
param_named invViewProjMatrix matrix4x4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

param_named worldCameraPos float4 0 0 0 0

param_named sunDirection float3 0 1 0
param_named hazeColour float3 0.1 0.2 0.6
param_named sunDirection vec3 0 1 0
param_named hazeColour vec3 0.1 0.2 0.6
}
}

fragment_program Caelum/DepthComposerFP_SkyDomeHaze_ExpGroundFog cg
fragment_program Caelum/DepthComposerFP_SkyDomeHaze_ExpGroundFog glsl hlsl
{
source DepthComposer.cg
entry_point MainFP
source DepthComposerMainFP.glsl
profiles ps_3_0 arbfp1
compile_arguments -DEXP_GROUND_FOG=1 -DSKY_DOME_HAZE=1 -DHAZE_DEPTH_TEXTURE=s2
preprocessor_defines EXP_GROUND_FOG=1,SKY_DOME_HAZE=1

default_params
{
param_named invViewProjMatrix float4x4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
param_named invViewProjMatrix matrix4x4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

param_named worldCameraPos float4 0 0 0 0

param_named sunDirection float3 0 1 0
param_named hazeColour float3 0.1 0.2 0.6
param_named sunDirection vec3 0 1 0
param_named hazeColour vec3 0.1 0.2 0.6

param_named groundFogDensity float 0.1
param_named groundFogVerticalDecay float 0.2
Expand Down
178 changes: 178 additions & 0 deletions main/resources/DepthComposerMainFP.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// This file is part of the Caelum project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution.

OGRE_NATIVE_GLSL_VERSION_DIRECTIVE
#include <OgreUnifiedShader.h>

OGRE_UNIFORMS(
uniform SAMPLER2D(screenTexture, 0);
uniform SAMPLER2D(depthTexture, 1);
uniform SAMPLER2D(atmRelativeDepth, 2); // ~ changed from sampler1D

uniform mat4 invViewProjMatrix;
uniform vec4 worldCameraPos;

#if EXP_GROUND_FOG
uniform float groundFogDensity;
uniform float groundFogVerticalDecay;
uniform float groundFogBaseLevel;
uniform vec4 groundFogColour;
#endif // EXP_GROUND_FOG

#if SKY_DOME_HAZE
uniform vec3 hazeColour;
uniform vec3 sunDirection;
#endif // SKY_DOME_HAZE
)

#ifdef EXP_GROUND_FOG

// Returns (exp(x) - 1) / x; avoiding division by 0.
// lim when x -> 0 is 1.
float expdiv(float x) {
if (abs(x) < 0.0001) {
return 1;
} else {
return (exp(x) - 1) / x;
}
}

// Return fogging through a layer of fog which drops exponentially by height.
//
// Standard exp fog with constant density would return (1 - exp(-density * dist)).
// This function assumes a variable density vd = exp(-verticalDecay * h - baseLevel)
// Full computation is exp(density * dist / (h2 - h1) * int(h1, h2, exp(-verticalDecay * (h2 - h1)))).
//
// This will gracefully degrade to standard exp fog in verticalDecay is 0; without throwing NaNs.
float ExpGroundFog (
float dist, float h1, float h2,
float density, float verticalDecay, float baseLevel)
{
float deltaH = (h2 - h1);
return 1 - exp (-density * dist * exp(verticalDecay * (baseLevel - h1)) * expdiv(-verticalDecay * deltaH));
}

#endif // EXP_GROUND_FOG

#ifdef SKY_DOME_HAZE

float bias (float b, float x)
{
return pow (x, log (b) / log (0.5));
}

vec4 sunlightInscatter
(
vec4 sunColour,
float absorption,
float incidenceAngleCos,
float sunlightScatteringFactor
)
{
float scatteredSunlight = bias (sunlightScatteringFactor * 0.5, incidenceAngleCos);

sunColour = sunColour * (1 - absorption) * vec4 (0.9, 0.5, 0.09, 1);

return sunColour * scatteredSunlight;
}

float fogExp (float z, float density) {
return 1 - clamp (pow (2.71828, -z * density), 0, 1);
}

vec4 CalcHaze
(
vec3 worldPos,
vec3 worldCamPos,
vec3 hazeColour,
vec3 sunDirection
)
{
float haze = length (worldCamPos - worldPos);
float incidenceAngleCos = dot (-sunDirection, normalize (worldPos - worldCamPos));
float y = -sunDirection.y;

vec4 sunColour = vec4 (3, 2.5, 1, 1);

// Factor determining the amount of light lost due to absorption
float atmLightAbsorptionFactor = 0.1;
float fogDensity = 15;

haze = fogExp (haze * 0.005, atmLightAbsorptionFactor);

// Haze amount calculation
float invHazeHeight = 100;
float hazeAbsorption = fogExp (pow (1 - y, invHazeHeight), fogDensity);

if (incidenceAngleCos > 0) {
// Factor determining the amount of scattering for the sun light
float sunlightScatteringFactor = 0.1;
// Factor determining the amount of sun light intensity lost due to scattering
float sunlightScatteringLossFactor = 0.3;

vec4 sunlightInscatterColour = sunlightInscatter (
sunColour,
clamp ((1 - tex2D (atmRelativeDepth, y).r) * hazeAbsorption, 0, 1),
clamp (incidenceAngleCos, 0, 1),
sunlightScatteringFactor) * (1 - sunlightScatteringLossFactor);
hazeColour =
hazeColour * (1 - sunlightInscatterColour.a) +
sunlightInscatterColour.rgb * sunlightInscatterColour.a * haze;
}

return vec4(hazeColour.rgb, haze);
}

#endif // SKY_DOME_HAZE

MAIN_PARAMETERS
IN(vec2 screenPos, TEXCOORD0)
MAIN_DECLARATION
{
vec4 inColor = tex2D(screenTexture, screenPos);
float inDepth = tex2D(depthTexture, screenPos).r;

// Build normalized device coords; after the perspective divide.
//vec4 devicePos = vec4(1 - screenPos.x * 2, screenPos.y * 2 - 1, inDepth, 1);
//vec4 devicePos = vec4(screenPos.x * 2 - 1, 1 - screenPos.y * 2, 2 * inDepth - 1, 1);
vec4 devicePos = vec4(screenPos.x * 2 - 1, 1 - screenPos.y * 2, inDepth, 1);

// Go back from device to world coordinates.
vec4 worldPos = mul(invViewProjMatrix, devicePos);

// Now undo the perspective divide and go back to "normal" space.
worldPos /= worldPos.w;

vec4 color = inColor;

#if DEBUG_DEPTH_RENDER
//color = abs(vec4(inDepth, inDepth, inDepth, 1));
color = worldPos * vec4(0.001, 0.01, 0.001, 1);
#endif // DEBUG_DEPTH_RENDER

#if EXP_GROUND_FOG
// Ye olde ground fog.
float h1 = worldCameraPos.y;
float h2 = worldPos.y;
float dist = length(worldCameraPos - worldPos);
float fogFactor = ExpGroundFog(
dist, h1, h2,
groundFogDensity, groundFogVerticalDecay, groundFogBaseLevel);
color = lerp(color, groundFogColour, fogFactor);
#endif // EXP_GROUND_FOG

#if SKY_DOME_HAZE
vec4 hazeValue = CalcHaze (
worldPos.xyz,
worldCameraPos.xyz,
hazeColour,
sunDirection);
color.rgb = lerp(color.rgb, hazeValue.rgb, hazeValue.a);
#endif // SKY_DOME_HAZE

gl_FragColor = color;
}



24 changes: 10 additions & 14 deletions main/resources/DepthRender.program
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,36 @@
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution.

vertex_program Caelum/DepthRenderVP cg
vertex_program Caelum/DepthRenderVP glsl hlsl
{
source DepthComposer.cg
entry_point DepthRenderVP
profiles vs_2_0 arbvp1
source DepthRenderVP.glsl
//profiles vs_2_0 arbvp1

default_params
{
param_named_auto wvpMatrix worldviewproj_matrix
}
}

fragment_program Caelum/DepthRenderFP cg
fragment_program Caelum/DepthRenderFP glsl hlsl
{
source DepthComposer.cg
entry_point DepthRenderFP
profiles ps_3_0 fp40 arbfp1
source DepthRenderFP.glsl
//profiles ps_3_0 fp40 arbfp1
}

vertex_program Caelum/DepthRenderAlphaRejectionVP cg
vertex_program Caelum/DepthRenderAlphaRejectionVP glsl hlsl
{
source DepthComposer.cg
entry_point DepthRenderAlphaRejectionVP
profiles vs_2_0 arbvp1
source DepthRenderAlphaRejectionVP.glsl

default_params
{
param_named_auto wvpMatrix worldviewproj_matrix
}
}

fragment_program Caelum/DepthRenderAlphaRejectionFP cg
fragment_program Caelum/DepthRenderAlphaRejectionFP glsl hlsl
{
source DepthComposer.cg
entry_point DepthRenderAlphaRejectionFP
profiles ps_3_0 fp40 arbfp1
source DepthRenderAlphaRejectionFP.glsl
}
20 changes: 20 additions & 0 deletions main/resources/DepthRenderAlphaRejectionFP.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This file is part of the Caelum project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution.

OGRE_NATIVE_GLSL_VERSION_DIRECTIVE
#include <OgreUnifiedShader.h>

OGRE_UNIFORMS(
uniform SAMPLER2D(mainTex, 0);
)

MAIN_PARAMETERS
IN(vec4 texcoord, TEXCOORD0)
IN(vec4 magic, TEXCOORD1)
MAIN_DECLARATION
{
vec4 texvalue = tex2D(mainTex, texcoord.xy);
// texvalue.a = sin(100 * texcoord.x) + sin(100 * texcoord.y);
gl_FragColor = vec4(vec3_splat(magic.z / magic.w), texvalue.a);
}
29 changes: 29 additions & 0 deletions main/resources/DepthRenderAlphaRejectionVP.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// This file is part of the Caelum project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution.

OGRE_NATIVE_GLSL_VERSION_DIRECTIVE
#include <OgreUnifiedShader.h>

OGRE_UNIFORMS(
uniform mat4 wvpMatrix;
)

MAIN_PARAMETERS
IN(vec4 inPos, POSITION)
IN(vec4 inTexcoord, TEXCOORD0)

OUT(vec4 outTexcoord, TEXCOORD0)
OUT(vec4 magic, TEXCOORD1)
MAIN_DECLARATION
{
// Standard transform.
gl_Position = mul(wvpMatrix, inPos);

// Depth buffer is z/w.
// Let the GPU lerp the components of gl_Position.
magic = gl_Position;

outTexcoord = inTexcoord;
}

Loading
Loading