/////////////////////////////////////////////////////////////////////////////// // // ## ###### // ###### ### // ## ############### Shark 3D Engine (www.shark3d.com) // ########## # # # // ######## Copyright (c) 1996-2006 Spinor GmbH. // ######### # # # All rights reserved. // ## ########## // ## // /////////////////////////////////////////////////////////////////////////////// @include "levelutil/shader/prog/ogl_glsl/" \ "include_shmap_filter_ogl_glsl_vs1x0.s3d_shadercode_run" /////////////////////////////////////////////////////////////////////////////// @define S3D_NORMALIZE(v) (v) /////////////////////////////////////////////////////////////////////////////// uniform sampler2D tex0; uniform sampler2D normalSamp; /////////////////////////////////////////////////////////////////////////////// @ifdef S3D_LIGHT_BRIGHT uniform sampler2D lightBrightSamp; @endif @ifdef S3D_LIGHT_PROJ uniform sampler2D lightProjSamp; @endif @ifdef S3D_LIGHT_SHMAP uniform sampler2D lightShmapSamp; @endif /////////////////////////////////////////////////////////////////////////////// uniform vec4 lightAmbient; uniform vec4 lightDiffuse; uniform vec4 lightSpecular; @ifdef S3D_LIGHT_SHMAP uniform vec4 lightShmapSize; uniform vec4 lightShmapRcpSize; @endif @ifdef S3D_LIGHT_BRIGHT uniform vec4 lightBrightRcpSize; @endif /////////////////////////////////////////////////////////////////////////////// @define S3D_INTENS_DISCARD_TRESHOLD 0.001 @define S3D_BRIGHT_EPS 0.01 /////////////////////////////////////////////////////////////////////////////// void main(void) { @ifdef S3D_GEOMETRIC_SHADOW // Geometric shadow if(gl_TexCoord[1].z < 0.0) discard; @endif @ifdef S3D_LIGHT_SHMAP vec3 clipVal = gl_TexCoord[2].www - abs(2.0 * gl_TexCoord[2].xyz - gl_TexCoord[2].www); if(clipVal.x < 0.0 || clipVal.y < 0.0 || clipVal.z < 0.0) discard; @endif @ifdef S3D_LIGHT_BRIGHT @ifdef S3D_LIGHT_BRIGHT_CARE vec4 brightVecMain = texture2Dproj(lightBrightSamp, input.lightBrightCoord); float brightIdent = frac(100 * input.secPosScr.z / input.secPosScr.w); float brightDeltaMain = abs(brightVecMain.w - brightIdent); float brightVal = brightVecMain.x; if(brightDeltaMain > S3D_BRIGHT_EPS) { float2 brightOffs = lightBrightRcpSize.xy * input.lightBrightCoord.w; vec4 brightCoordX = vec4( input.lightBrightCoord.xy + brightOffs, input.lightBrightCoord.zw); vec4 brightCoordY = vec4( input.lightBrightCoord.xy - brightOffs, input.lightBrightCoord.zw); vec4 brightCoordZ = brightCoordX; brightCoordZ.y = brightCoordY.y; vec4 brightCoordW = brightCoordY; brightCoordW.y = brightCoordX.y; vec4 brightVecX = texture2Dproj(lightBrightSamp, brightCoordX); vec4 brightVecY = texture2Dproj(lightBrightSamp, brightCoordY); vec4 brightVecZ = texture2Dproj(lightBrightSamp, brightCoordZ); vec4 brightVecW = texture2Dproj(lightBrightSamp, brightCoordW); brightVal = min(brightVecMain.x, min(min(brightVecX.x, brightVecY.x), min(brightVecZ.x, brightVecW.x))); } @else // Lookup brightness: float brightVal = texture2DProj(lightBrightSamp, gl_TexCoord[5]).x; @endif // Exit if no light: if(brightVal <= S3D_INTENS_DISCARD_TRESHOLD) discard; @endif @ifdef S3D_LIGHT_SHMAP // Shadow map: float shmapVal = s3d_shmapFilter( lightShmapSamp, gl_TexCoord[6], lightShmapSize, lightShmapRcpSize); if(shmapVal <= S3D_INTENS_DISCARD_TRESHOLD) discard; @endif // Texture coordinates: vec2 mainTexCoord = gl_TexCoord[0].xy; @ifdef S3D_PARALLAX_MAPPING vec3 camToVertSurfNrm = normalize(gl_TexCoord[4].xyz); float height = texture2D(normalSamp, mainTexCoord).a; vec2 offset = S3D_PARALLAX_SCALE * (S3D_PARALLAX_BIAS - height) * camToVertSurfNrm.xy; mainTexCoord = mainTexCoord + offset; @endif // Texture lookups: vec4 texCol = texture2D(tex0, mainTexCoord.xy); vec4 rawNormalView = texture2D(normalSamp, mainTexCoord.xy); vec3 normalView = (rawNormalView.xyz - 0.5) * 2.0; // Calculate range-factor from scaled surface-to-light vector: float rangeFac = max(1.0 - dot(gl_TexCoord[3].xyz, gl_TexCoord[3].xyz), 0.0); // Diffuse direction: vec3 diffuseDirSurfNrm = S3D_NORMALIZE(gl_TexCoord[1].xyz); // Calculate diffuse: vec4 diffuse = vec4(lightDiffuse.xyz * rangeFac, lightDiffuse.w); diffuse += lightAmbient; float diffDot = dot(normalView, diffuseDirSurfNrm); #ifdef S3D_MILD float diffFac = clamp(lerp(diffDot, 1, S3D_MILD), 0.0, 1.0); #else float diffFac = clamp(diffDot, 0.0, 1.0); #endif diffuse *= diffFac; // Calculate specular: vec4 specular = vec4(lightSpecular.xyz * rangeFac, lightSpecular.w); // ATI workaround: Don't use x component of gl_TexCoord[2]: vec3 specularDirSurfNrm = S3D_NORMALIZE(gl_TexCoord[2].yzw); float specDot = dot(normalView, specularDirSurfNrm); float specFac = clamp(pow(clamp(specDot, 0.0, 1.0), S3D_SPECULAR_EXP, 0.0, 1.0); specular *= specFac; vec4 outCol; outCol.rgb = clamp(diffuse.rgb * texCol.rgb + specular.rgb * texCol.a, 0.0, 1.0); // Preserve original opacity from texture. outCol.a = texCol.a; @ifdef S3D_GEOMETRIC_SHADOW // Geometric shadow float geomShadow = clamp(diffuseDirSurfNrm.z * 4.0, 0.0, 1.0); outCol.rgb = clamp(outCol.rgb * geomShadow, 0.0, 1.0); @endif @ifdef S3D_LIGHT_BRIGHT // Correct brightness: // The smoothing algorithm generates higher brightness gradients // near 0 and 1 due to sticking to 0 and 1. // This is approximately compensated by using a quadratic function // near 0 and 1. brightVal = 2.0 * brightVal * brightVal * (1.5 - brightVal); // Apply brightness: outCol.rgb = outCol.rgb * brightVal; @endif @ifdef S3D_LIGHT_SHMAP outCol *= shmapVal; @endif gl_FragColor = outCol; } ///////////////////////////////////////////////////////////////////////////////