SHADOW MAPPING

Problems building or running the engine, queries about how to use features etc.
P1610
Gnoblar
Posts: 1
Joined: Fri Mar 29, 2024 12:00 pm

SHADOW MAPPING

Post by P1610 »

Ogre Version: :?:
Operating System: Windows 11
Render System: OpenGL 3 Plus

Hi, I'm trying to setup shadows based on textured-based and shadow mapping, but I'm struggleing to achieve it. Right this is what I have:

Shadow setup:

Code: Select all

scnMgr->setShadowCameraSetup(Ogre::LiSPSMShadowCameraSetup::create());
scnMgr->setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_ADDITIVE);
scnMgr->setShadowTexturePixelFormat(Ogre::PF_DEPTH32);
scnMgr->setShadowTextureCasterMaterial(casterMat);
scnMgr->setShadowTextureReceiverMaterial(receiverMat);

Shadows materials:

Code: Select all

vertex_program ShadowCasterVS glsl {
    source ShadowCasterVS.glsl
}

fragment_program ShadowCasterFS glsl {
    source ShadowCasterFS.glsl
}

material ShadowCaster
{
	technique
    {
        pass
        {
            vertex_program_ref ShadowCasterVS {}

        fragment_program_ref ShadowCasterFS {}
    }
}
}

//----------------------//

vertex_program ShadowReceiverVS glsl {
    source ShadowReceiverVS.glsl

default_params
{
    param_named_auto modelMat world_matrix
    param_named_auto normalModelMat inverse_transpose_world_matrix
param_named_auto worldViewProjMatrix worldviewproj_matrix
param_named_auto texViewProj texture_viewproj_matrix
param_named_auto lightPosition light_position 0
param_named_auto lightColour light_diffuse_colour 0
}
}

fragment_program ShadowReceiverFS glsl {
    source ShadowReceiverFS.glsl

default_params
{
param_named shadowMap int 0

    param_named inverseShadowmapSize float 0.0009765625
}
}

material ShadowReceiver
{
    technique
    {
        pass
        {
            vertex_program_ref ShadowReceiverVS {}

        fragment_program_ref ShadowReceiverFS {}

        texture_unit ShadowMap
        {
            tex_address_mode clamp
            filtering none
        }

        depth_bias 0.0005
    }
}
}

Shadow caster shader

Code: Select all

// VS

#version 330 core

uniform mat4 worldViewProjMatrix;
in vec4 vertex;

void main(void){
    gl_Position = worldViewProjMatrix * vertex;
}

// FS

#version 330 core

out vec4 fFragColor;

void main(void){
    fFragColor = vec4(vec3(gl_FragCoord.z), 1.0);
}

Shadow receiver shader:

Code: Select all

// VS

#version 330 core

uniform mat4 modelMat;
uniform mat4 normalModelMat;
uniform mat4 worldViewProjMatrix;
uniform mat4 texViewProj;
uniform vec4 lightPosition;
uniform vec4 lightColour;
 
in vec4 vertex;
in vec3 normal;
 
out vec4 oUv;
out vec4 outColor;
 
void main()
{
    gl_Position = worldViewProjMatrix * vertex;
    
vec4 worldPos = modelMat * vertex; vec3 worldNorm = (normalModelMat * vec4(normal, 1.0)).xyz; vec3 lightDir = normalize(lightPosition.xyz - (worldPos.xyz * lightPosition.w)); outColor = lightColour * max(dot(lightDir, worldNorm), 0.0); oUv = texViewProj * worldPos; } // FS #define PCF uniform float inverseShadowmapSize; uniform sampler2D shadowMap; uniform float fixedDepthBias; uniform float gradientClamp; uniform float gradientScaleBias; in vec4 oUv; in vec4 outColor; out vec4 fFragColor; void main() { vec4 shadowUV = oUv; shadowUV = shadowUV / shadowUV.w; float centerdepth = texture2D(shadowMap, shadowUV.xy).x; #ifndef OGRE_REVERSED_Z shadowUV.z = shadowUV.z * 0.5 + 0.5; #endif float pixeloffset = inverseShadowmapSize; vec4 depths = vec4( texture2D(shadowMap, shadowUV.xy + vec2(-pixeloffset, 0.0)).x, texture2D(shadowMap, shadowUV.xy + vec2(+pixeloffset, 0.0)).x, texture2D(shadowMap, shadowUV.xy + vec2(0.0, -pixeloffset)).x, texture2D(shadowMap, shadowUV.xy + vec2(0.0, +pixeloffset)).x); #ifdef PCF float final = (centerdepth > shadowUV.z) ? 1.0 : 0.0; final += (depths.x > shadowUV.z) ? 1.0 : 0.0; final += (depths.y > shadowUV.z) ? 1.0 : 0.0; final += (depths.z > shadowUV.z) ? 1.0 : 0.0; final += (depths.w > shadowUV.z) ? 1.0 : 0.0;
final *= 0.2; fFragColor = vec4(outColor.xyz * final, 1.0); #else fFragColor = (centerdepth > shadowUV.z) ? vec4(outColor.xyz, 1) : vec4(0,0,0,1); #endif }

I have already tried to use TEXTURE_ADDITIVE_INTEGRATED and set the receiver material to the object, but it doesn't work either.

Thanks in advance :)

paroj
OGRE Team Member
OGRE Team Member
Posts: 2106
Joined: Sun Mar 30, 2014 2:51 pm
x 1132

Re: SHADOW MAPPING

Post by paroj »

take a look at this demo in sample browser: https://ogrecave.github.io/ogre/wasm/