From b803d0e9493467456aabfe573046d934f159496c Mon Sep 17 00:00:00 2001 From: scrawl Date: Mon, 9 Jul 2012 19:46:36 +0200 Subject: [PATCH] converted shadow caster shader --- apps/openmw/mwrender/localmap.cpp | 2 +- apps/openmw/mwrender/shadows.cpp | 2 +- apps/openmw/mwrender/water.cpp | 2 +- components/nifogre/ogre_nif_loader.cpp | 4 +- extern/shiny | 2 +- files/materials/shadowcaster.mat | 35 ++++++++++++ files/materials/shadowcaster.shader | 56 ++++++++++++++++++ files/materials/shadowcaster.shaderset | 15 +++++ files/shadows/depthshadowcaster.cg | 51 ----------------- files/shadows/depthshadowcaster.material | 73 ------------------------ 10 files changed, 112 insertions(+), 130 deletions(-) create mode 100644 files/materials/shadowcaster.mat create mode 100644 files/materials/shadowcaster.shader create mode 100644 files/materials/shadowcaster.shaderset delete mode 100644 files/shadows/depthshadowcaster.cg delete mode 100644 files/shadows/depthshadowcaster.material diff --git a/apps/openmw/mwrender/localmap.cpp b/apps/openmw/mwrender/localmap.cpp index 1a4002b12..663388e94 100644 --- a/apps/openmw/mwrender/localmap.cpp +++ b/apps/openmw/mwrender/localmap.cpp @@ -229,7 +229,7 @@ void LocalMap::render(const float x, const float y, vp->setVisibilityMask(RV_Map); // use fallback techniques without shadows and without mrt - //vp->setMaterialScheme("local_map"); + vp->setMaterialScheme("local_map"); //rtt->update(); diff --git a/apps/openmw/mwrender/shadows.cpp b/apps/openmw/mwrender/shadows.cpp index 1bea14530..93152244b 100644 --- a/apps/openmw/mwrender/shadows.cpp +++ b/apps/openmw/mwrender/shadows.cpp @@ -53,7 +53,7 @@ void Shadows::recreate() mSceneMgr->setShadowTextureSelfShadow(true); mSceneMgr->setShadowCasterRenderBackFaces(true); - // mSceneMgr->setShadowTextureCasterMaterial("openmw_shadowcaster_default"); + mSceneMgr->setShadowTextureCasterMaterial("openmw_shadowcaster_default"); mSceneMgr->setShadowTexturePixelFormat(PF_FLOAT32_R); mSceneMgr->setShadowDirectionalLightExtrusionDistance(1000000); diff --git a/apps/openmw/mwrender/water.cpp b/apps/openmw/mwrender/water.cpp index 311df9169..b8642a754 100644 --- a/apps/openmw/mwrender/water.cpp +++ b/apps/openmw/mwrender/water.cpp @@ -336,7 +336,7 @@ void Water::applyRTT() vp->setBackgroundColour(ColourValue(0.8f, 0.9f, 1.0f)); vp->setShadowsEnabled(false); // use fallback techniques without shadows and without mrt (currently not implemented for sky and terrain) - //vp->setMaterialScheme("water_reflection"); + vp->setMaterialScheme("water_reflection"); rtt->addListener(this); rtt->setActive(true); diff --git a/components/nifogre/ogre_nif_loader.cpp b/components/nifogre/ogre_nif_loader.cpp index f33ac2364..e5b196804 100644 --- a/components/nifogre/ogre_nif_loader.cpp +++ b/components/nifogre/ogre_nif_loader.cpp @@ -276,8 +276,8 @@ void NIFLoader::createMaterial(const String &name, else warn("Unhandled alpha setting for texture " + texName); } - //else - //instance->getMaterial ()->setShadowCasterMaterial ("openmw_shadowcaster_noalpha"); + else + instance->getMaterial ()->setShadowCasterMaterial ("openmw_shadowcaster_noalpha"); // As of yet UNTESTED code from Chris: /*pass->setTextureFiltering(Ogre::TFO_ANISOTROPIC); diff --git a/extern/shiny b/extern/shiny index d25605ee3..a0b3020bd 160000 --- a/extern/shiny +++ b/extern/shiny @@ -1 +1 @@ -Subproject commit d25605ee3a2d67dc0b6367cd150cc6d0c6a3b661 +Subproject commit a0b3020bdba4433c1e3f45a1d5188eab0172bd88 diff --git a/files/materials/shadowcaster.mat b/files/materials/shadowcaster.mat new file mode 100644 index 000000000..25b791f1f --- /dev/null +++ b/files/materials/shadowcaster.mat @@ -0,0 +1,35 @@ + + +material openmw_shadowcaster_default +{ + create_configuration Default + pass + { + fog_override true + + vertex_program openmw_shadowcaster_vertex + fragment_program openmw_shadowcaster_fragment + + shader_properties + { + shadow_transparency true + } + } +} + +material openmw_shadowcaster_noalpha +{ + create_configuration Default + pass + { + fog_override true + + vertex_program openmw_shadowcaster_vertex + fragment_program openmw_shadowcaster_fragment + + shader_properties + { + shadow_transparency false + } + } +} diff --git a/files/materials/shadowcaster.shader b/files/materials/shadowcaster.shader new file mode 100644 index 000000000..f772066a6 --- /dev/null +++ b/files/materials/shadowcaster.shader @@ -0,0 +1,56 @@ +#include "core.h" + +#define ALPHA @shPropertyBool(shadow_transparency) + +#ifdef SH_VERTEX_SHADER + + SH_BEGIN_PROGRAM +#if ALPHA + shInput(float2, uv0) + shOutput(float2, UV) +#endif + shUniform(float4x4 wvp) @shAutoConstant(wvp, worldviewproj_matrix) + shOutput(float2, depth) + SH_START_PROGRAM + { + // this is the view space position + shOutputPosition = shMatrixMult(wvp, shInputPosition); + + // depth info for the fragment. + depth.x = shOutputPosition.z; + depth.y = shOutputPosition.w; + + // clamp z to zero. seem to do the trick. :-/ + shOutputPosition.z = max(shOutputPosition.z, 0); + +#if ALPHA + UV = uv0; +#endif + } + +#else + + SH_BEGIN_PROGRAM +#if ALPHA + shInput(float2, UV) + shSampler2D(texture1) +#endif + shInput(float2, depth) + SH_START_PROGRAM + { + float finalDepth = depth.x / depth.y; + + +#if ALPHA + // use alpha channel of the first texture + float alpha = shSample(texture1, UV).a; + + // discard if alpha is less than 0.5 + if (alpha < 1.0) + discard; +#endif + + shOutputColor(0) = float4(finalDepth, finalDepth, finalDepth, 1); + } + +#endif diff --git a/files/materials/shadowcaster.shaderset b/files/materials/shadowcaster.shaderset new file mode 100644 index 000000000..5f4990ed1 --- /dev/null +++ b/files/materials/shadowcaster.shaderset @@ -0,0 +1,15 @@ +shader_set openmw_shadowcaster_vertex +{ + source shadowcaster.shader + type vertex + profiles_cg vs_2_0 arbvp1 + profiles_hlsl vs_2_0 +} + +shader_set openmw_shadowcaster_fragment +{ + source shadowcaster.shader + type fragment + profiles_cg ps_2_x ps_2_0 ps arbfp1 + profiles_hlsl ps_2_0 +} diff --git a/files/shadows/depthshadowcaster.cg b/files/shadows/depthshadowcaster.cg deleted file mode 100644 index 3457a4f8d..000000000 --- a/files/shadows/depthshadowcaster.cg +++ /dev/null @@ -1,51 +0,0 @@ -void main_vp( - float4 position : POSITION, - float2 uv : TEXCOORD0, - - out float4 oPosition : POSITION, - out float2 oDepth : TEXCOORD0, - out float2 oUv : TEXCOORD1, - - uniform float4x4 wvpMat) -{ - // this is the view space position - oPosition = mul(wvpMat, position); - - // depth info for the fragment. - oDepth.x = oPosition.z; - oDepth.y = oPosition.w; - - // clamp z to zero. seem to do the trick. :-/ - oPosition.z = max(oPosition.z, 0); - - oUv = uv; -} - -void main_fp( - float2 depth : TEXCOORD0, - float2 uv : TEXCOORD1, - uniform sampler2D texture1 : register(s0), - - out float4 oColour : COLOR) -{ - float finalDepth = depth.x / depth.y; - - // use alpha channel of the first texture - float alpha = tex2D(texture1, uv).a; - - // discard if alpha is less than 0.5 - clip((alpha >= 0.5) ? 1 : -1); - - oColour = float4(finalDepth, finalDepth, finalDepth, 1); -} - -void main_fp_noalpha( - float2 depth : TEXCOORD0, - float2 uv : TEXCOORD1, - - out float4 oColour : COLOR) -{ - float finalDepth = depth.x / depth.y; - - oColour = float4(finalDepth, finalDepth, finalDepth, 1); -} diff --git a/files/shadows/depthshadowcaster.material b/files/shadows/depthshadowcaster.material deleted file mode 100644 index f645cad01..000000000 --- a/files/shadows/depthshadowcaster.material +++ /dev/null @@ -1,73 +0,0 @@ -vertex_program depth_shadow_caster_vs cg -{ - source depthshadowcaster.cg - profiles vs_1_1 arbvp1 - entry_point main_vp - - default_params - { - param_named_auto wvpMat worldviewproj_matrix - } -} - -fragment_program depth_shadow_caster_ps cg -{ - source depthshadowcaster.cg - profiles ps_2_0 arbfp1 - entry_point main_fp - - default_params - { - } -} - -fragment_program depth_shadow_caster_ps_noalpha cg -{ - source depthshadowcaster.cg - profiles ps_2_0 arbfp1 - entry_point main_fp_noalpha - - default_params - { - } -} - -material depth_shadow_caster -{ - technique - { - pass - { - // force-disable fog (relevant for DirectX profiles below SM3 that always apply fixed function fog) - fog_override true - - vertex_program_ref depth_shadow_caster_vs - { - } - - fragment_program_ref depth_shadow_caster_ps - { - } - } - } -} - -material depth_shadow_caster_noalpha -{ - technique - { - pass - { - // force-disable fog (relevant for DirectX profiles below SM3 that always apply fixed function fog) - fog_override true - - vertex_program_ref depth_shadow_caster_vs - { - } - - fragment_program_ref depth_shadow_caster_ps_noalpha - { - } - } - } -}