mirror of
				https://github.com/OpenMW/openmw.git
				synced 2025-10-25 08:56:37 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			348 lines
		
	
	
	
		
			10 KiB
		
	
	
	
		
			GLSL
		
	
	
	
	
	
			
		
		
	
	
			348 lines
		
	
	
	
		
			10 KiB
		
	
	
	
		
			GLSL
		
	
	
	
	
	
| #include "core.h"
 | |
| 
 | |
| #define IS_FIRST_PASS (@shPropertyString(pass_index) == 0)
 | |
| 
 | |
| #define FOG (@shGlobalSettingBool(fog) && !@shPropertyBool(render_composite_map))
 | |
| 
 | |
| #define SHADOWS_PSSM @shGlobalSettingBool(shadows_pssm)
 | |
| #define SHADOWS @shGlobalSettingBool(shadows)
 | |
| 
 | |
| #if SHADOWS || SHADOWS_PSSM
 | |
| #include "shadows.h"
 | |
| #endif
 | |
| 
 | |
| #define NUM_LAYERS @shPropertyString(num_layers)
 | |
| 
 | |
| #if FOG || SHADOWS_PSSM
 | |
| #define NEED_DEPTH 1
 | |
| #endif
 | |
| 
 | |
| #define UNDERWATER @shGlobalSettingBool(render_refraction)
 | |
| 
 | |
| #define VIEWPROJ_FIX @shGlobalSettingBool(viewproj_fix)
 | |
| 
 | |
| #define RENDERCMP @shPropertyBool(render_composite_map)
 | |
| 
 | |
| #define LIGHTING !RENDERCMP
 | |
| 
 | |
| #define COMPOSITE_MAP @shPropertyBool(display_composite_map)
 | |
| 
 | |
| 
 | |
| #if NEED_DEPTH
 | |
| @shAllocatePassthrough(1, depth)
 | |
| #endif
 | |
| 
 | |
| @shAllocatePassthrough(2, UV)
 | |
| 
 | |
| @shAllocatePassthrough(3, worldPos)
 | |
| 
 | |
| #if LIGHTING
 | |
| @shAllocatePassthrough(3, lightResult)
 | |
| @shAllocatePassthrough(3, directionalResult)
 | |
| 
 | |
| #if SHADOWS
 | |
| @shAllocatePassthrough(4, lightSpacePos0)
 | |
| #endif
 | |
| #if SHADOWS_PSSM
 | |
| @shForeach(3)
 | |
|     @shAllocatePassthrough(4, lightSpacePos@shIterator)
 | |
| @shEndForeach
 | |
| #endif
 | |
| #endif
 | |
| 
 | |
| #ifdef SH_VERTEX_SHADER
 | |
| 
 | |
|     // ------------------------------------- VERTEX ---------------------------------------
 | |
| 
 | |
|     SH_BEGIN_PROGRAM
 | |
|         shUniform(float4x4, worldMatrix) @shAutoConstant(worldMatrix, world_matrix)
 | |
|         shUniform(float4x4, viewProjMatrix) @shAutoConstant(viewProjMatrix, viewproj_matrix)
 | |
|         
 | |
| #if VIEWPROJ_FIX
 | |
|         shUniform(float4, vpRow2Fix) @shSharedParameter(vpRow2Fix, vpRow2Fix)
 | |
| #endif
 | |
|         
 | |
|         shVertexInput(float2, uv0)
 | |
|         shVertexInput(float2, uv1) // lodDelta, lodThreshold
 | |
| 
 | |
| #if LIGHTING
 | |
|         shNormalInput(float4)
 | |
|         shColourInput(float4)
 | |
| 
 | |
|         shUniform(float, lightCount) @shAutoConstant(lightCount, light_count)
 | |
|         shUniform(float4, lightPosition[@shGlobalSettingString(num_lights)]) @shAutoConstant(lightPosition, light_position_object_space_array, @shGlobalSettingString(num_lights))
 | |
|         shUniform(float4, lightDiffuse[@shGlobalSettingString(num_lights)]) @shAutoConstant(lightDiffuse, light_diffuse_colour_array, @shGlobalSettingString(num_lights))
 | |
|         shUniform(float4, lightAttenuation[@shGlobalSettingString(num_lights)]) @shAutoConstant(lightAttenuation, light_attenuation_array, @shGlobalSettingString(num_lights))
 | |
|         shUniform(float4, lightAmbient)                    @shAutoConstant(lightAmbient, ambient_light_colour)
 | |
|         
 | |
| #if SHADOWS
 | |
|         shUniform(float4x4, texViewProjMatrix0) @shAutoConstant(texViewProjMatrix0, texture_viewproj_matrix)
 | |
| #endif
 | |
| 
 | |
| #if SHADOWS_PSSM
 | |
|     @shForeach(3)
 | |
|         shUniform(float4x4, texViewProjMatrix@shIterator) @shAutoConstant(texViewProjMatrix@shIterator, texture_viewproj_matrix, @shIterator)
 | |
|     @shEndForeach
 | |
| #endif
 | |
| 
 | |
| #endif
 | |
| 
 | |
|         
 | |
|         @shPassthroughVertexOutputs
 | |
| 
 | |
|     SH_START_PROGRAM
 | |
|     {
 | |
|         float4 worldPos = shMatrixMult(worldMatrix, shInputPosition);
 | |
| 
 | |
|         shOutputPosition = shMatrixMult(viewProjMatrix, worldPos);
 | |
|         
 | |
| #if NEED_DEPTH
 | |
| #if VIEWPROJ_FIX
 | |
|         float4x4 vpFixed = viewProjMatrix;
 | |
| #if !SH_GLSL
 | |
|         vpFixed[2] = vpRow2Fix;
 | |
| #else
 | |
|         vpFixed[0][2] = vpRow2Fix.x;
 | |
|         vpFixed[1][2] = vpRow2Fix.y;
 | |
|         vpFixed[2][2] = vpRow2Fix.z;
 | |
|         vpFixed[3][2] = vpRow2Fix.w;
 | |
| #endif
 | |
| 
 | |
|         float4x4 fixedWVP = shMatrixMult(vpFixed, worldMatrix);
 | |
| 
 | |
|         float depth = shMatrixMult(fixedWVP, shInputPosition).z;
 | |
|         @shPassthroughAssign(depth, depth);
 | |
| #else
 | |
|         @shPassthroughAssign(depth, shOutputPosition.z);
 | |
| #endif
 | |
| 
 | |
| #endif
 | |
| 
 | |
|         @shPassthroughAssign(UV, uv0);
 | |
|         
 | |
|         @shPassthroughAssign(worldPos, worldPos.xyz);
 | |
| 
 | |
| #if LIGHTING
 | |
| 
 | |
| #if SHADOWS
 | |
|         float4 lightSpacePos = shMatrixMult(texViewProjMatrix0, shMatrixMult(worldMatrix, shInputPosition));
 | |
|         @shPassthroughAssign(lightSpacePos0, lightSpacePos);
 | |
| #endif
 | |
| #if SHADOWS_PSSM
 | |
|         float4 wPos = shMatrixMult(worldMatrix, shInputPosition);
 | |
|         
 | |
|         float4 lightSpacePos;
 | |
|     @shForeach(3)
 | |
|         lightSpacePos = shMatrixMult(texViewProjMatrix@shIterator, wPos);
 | |
|         @shPassthroughAssign(lightSpacePos@shIterator, lightSpacePos);
 | |
|     @shEndForeach
 | |
| #endif
 | |
| 
 | |
| 
 | |
|         // Lighting
 | |
|         float3 lightDir;
 | |
|         float d;
 | |
|         float3 lightResult = float3(0,0,0);
 | |
|         float3 directionalResult = float3(0,0,0);
 | |
|         @shForeach(@shGlobalSettingString(num_lights))
 | |
|             lightDir = lightPosition[@shIterator].xyz - (shInputPosition.xyz * lightPosition[@shIterator].w);
 | |
|             d = length(lightDir);
 | |
|             lightDir = normalize(lightDir);
 | |
| 
 | |
| 
 | |
|             lightResult.xyz += lightDiffuse[@shIterator].xyz
 | |
|                     * shSaturate(1.0 / ((lightAttenuation[@shIterator].y) + (lightAttenuation[@shIterator].z * d) + (lightAttenuation[@shIterator].w * d * d)))
 | |
|                     * max(dot(normal.xyz, lightDir), 0);
 | |
| 
 | |
| #if @shIterator == 0
 | |
|             directionalResult = lightResult.xyz;
 | |
| #endif
 | |
|         @shEndForeach
 | |
|         lightResult.xyz += lightAmbient.xyz;
 | |
|         lightResult.xyz *= colour.xyz;
 | |
|         directionalResult.xyz *= colour.xyz;
 | |
| 
 | |
|         @shPassthroughAssign(lightResult, lightResult);
 | |
|         @shPassthroughAssign(directionalResult, directionalResult);
 | |
| 
 | |
| #endif
 | |
|     }
 | |
| 
 | |
| #else
 | |
| 
 | |
|     // ----------------------------------- FRAGMENT ------------------------------------------
 | |
| 
 | |
| #if UNDERWATER
 | |
|     #include "underwater.h"
 | |
| #endif
 | |
| 
 | |
|     SH_BEGIN_PROGRAM
 | |
|     
 | |
|     
 | |
| #if COMPOSITE_MAP
 | |
|         shSampler2D(compositeMap)
 | |
| #else
 | |
| 
 | |
| @shForeach(@shPropertyString(num_blendmaps))
 | |
|         shSampler2D(blendMap@shIterator)
 | |
| @shEndForeach
 | |
| 
 | |
| @shForeach(@shPropertyString(num_layers))
 | |
|         shSampler2D(diffuseMap@shIterator)
 | |
| @shEndForeach
 | |
| 
 | |
| #endif
 | |
|     
 | |
| #if FOG
 | |
|         shUniform(float3, fogColour) @shAutoConstant(fogColour, fog_colour)
 | |
|         shUniform(float4, fogParams) @shAutoConstant(fogParams, fog_params)
 | |
| #endif
 | |
|     
 | |
|         @shPassthroughFragmentInputs
 | |
| 
 | |
| #if LIGHTING
 | |
| #if SHADOWS
 | |
|         shSampler2D(shadowMap0)
 | |
|         shUniform(float2, invShadowmapSize0)   @shAutoConstant(invShadowmapSize0, inverse_texture_size, @shPropertyString(shadowtexture_offset))
 | |
| #endif
 | |
| #if SHADOWS_PSSM
 | |
|     @shForeach(3)
 | |
|         shSampler2D(shadowMap@shIterator)
 | |
|         shUniform(float2, invShadowmapSize@shIterator)  @shAutoConstant(invShadowmapSize@shIterator, inverse_texture_size, @shIterator(@shPropertyString(shadowtexture_offset)))
 | |
|     @shEndForeach
 | |
|     shUniform(float3, pssmSplitPoints)  @shSharedParameter(pssmSplitPoints)
 | |
| #endif
 | |
| 
 | |
| #if SHADOWS || SHADOWS_PSSM
 | |
|         shUniform(float4, shadowFar_fadeStart) @shSharedParameter(shadowFar_fadeStart)
 | |
| #endif
 | |
| #endif
 | |
| 
 | |
| #if (UNDERWATER) || (FOG)
 | |
|         shUniform(float4x4, worldMatrix) @shAutoConstant(worldMatrix, world_matrix)
 | |
|         shUniform(float4, cameraPos) @shAutoConstant(cameraPos, camera_position) 
 | |
| #endif
 | |
| 
 | |
| #if UNDERWATER
 | |
|         shUniform(float, waterLevel) @shSharedParameter(waterLevel)
 | |
| #endif
 | |
| 
 | |
|     SH_START_PROGRAM
 | |
|     {
 | |
| 
 | |
| #if NEED_DEPTH
 | |
|         float depth = @shPassthroughReceive(depth);
 | |
| #endif
 | |
| 
 | |
|         float2 UV = @shPassthroughReceive(UV);
 | |
|         
 | |
|         float3 worldPos = @shPassthroughReceive(worldPos);
 | |
|         
 | |
|         
 | |
| #if UNDERWATER
 | |
|         float3 waterEyePos = intercept(worldPos, cameraPos.xyz - worldPos, float3(0,0,1), waterLevel);
 | |
| #endif
 | |
|         
 | |
| 
 | |
| #if !IS_FIRST_PASS
 | |
| // Opacity the previous passes should have, i.e. 1 - (opacity of this pass)
 | |
| float previousAlpha = 1.f;
 | |
| #endif
 | |
| 
 | |
| 
 | |
| shOutputColour(0) = float4(1,1,1,1);
 | |
| 
 | |
| #if COMPOSITE_MAP
 | |
|         shOutputColour(0).xyz = shSample(compositeMap, UV).xyz;
 | |
| #else
 | |
| 
 | |
|         // Layer calculations 
 | |
| // rescale UV to directly map edge vertices to texel centers - this is
 | |
| // important to get correct blending at cell transitions
 | |
| // TODO: parameterize texel size
 | |
| float2 blendUV = (UV - 0.5) * (16.0 / (16.0+1.0)) + 0.5;
 | |
| @shForeach(@shPropertyString(num_blendmaps))
 | |
|         float4 blendValues@shIterator = shSaturate(shSample(blendMap@shIterator, blendUV));
 | |
| @shEndForeach
 | |
| 
 | |
| 
 | |
|         float3 albedo = float3(0,0,0);
 | |
| 
 | |
|         float2 layerUV = UV * 16;
 | |
| 
 | |
| @shForeach(@shPropertyString(num_layers))
 | |
| 
 | |
| 
 | |
| #if IS_FIRST_PASS
 | |
|         #if @shIterator == 0
 | |
|         // first layer of first pass is the base layer and doesn't need a blend map
 | |
|         albedo = shSample(diffuseMap0, layerUV).rgb;
 | |
|         #else
 | |
|         albedo = shLerp(albedo, shSample(diffuseMap@shIterator, layerUV).rgb, blendValues@shPropertyString(blendmap_component_@shIterator));
 | |
|         #endif
 | |
| #else
 | |
|         #if @shIterator == 0
 | |
|         albedo = shSample(diffuseMap@shIterator, layerUV).rgb, blendValues@shPropertyString(blendmap_component_@shIterator);
 | |
|         #else
 | |
|         albedo = shLerp(albedo, shSample(diffuseMap@shIterator, layerUV).rgb, blendValues@shPropertyString(blendmap_component_@shIterator));
 | |
|         #endif
 | |
|         previousAlpha *= 1.f-blendValues@shPropertyString(blendmap_component_@shIterator);
 | |
| #endif
 | |
| @shEndForeach
 | |
|         
 | |
|         shOutputColour(0).rgb *= albedo;
 | |
|         
 | |
| #endif
 | |
| 
 | |
| #if LIGHTING
 | |
|         // Lighting 
 | |
|         float3 lightResult = @shPassthroughReceive(lightResult);
 | |
|         float3 directionalResult = @shPassthroughReceive(directionalResult);
 | |
|         
 | |
|         // shadows only for the first (directional) light
 | |
| #if SHADOWS
 | |
|             float4 lightSpacePos0 = @shPassthroughReceive(lightSpacePos0);
 | |
|             float shadow = depthShadowPCF (shadowMap0, lightSpacePos0, invShadowmapSize0);
 | |
| #endif
 | |
| #if SHADOWS_PSSM
 | |
|         @shForeach(3)
 | |
|             float4 lightSpacePos@shIterator = @shPassthroughReceive(lightSpacePos@shIterator);
 | |
|         @shEndForeach
 | |
| 
 | |
|             float shadow = pssmDepthShadow (lightSpacePos0, invShadowmapSize0, shadowMap0, lightSpacePos1, invShadowmapSize1, shadowMap1, lightSpacePos2, invShadowmapSize2, shadowMap2, depth, pssmSplitPoints);
 | |
| #endif
 | |
| 
 | |
| #if SHADOWS || SHADOWS_PSSM
 | |
|             float fadeRange = shadowFar_fadeStart.x - shadowFar_fadeStart.y;
 | |
|             float fade = 1-((depth - shadowFar_fadeStart.y) / fadeRange);
 | |
|             shadow = (depth > shadowFar_fadeStart.x) ? 1.0 : ((depth > shadowFar_fadeStart.y) ? 1.0-((1.0-shadow)*fade) : shadow);
 | |
| #endif
 | |
| 
 | |
| #if !SHADOWS && !SHADOWS_PSSM
 | |
|             float shadow = 1.0;
 | |
| #endif
 | |
| 
 | |
|         shOutputColour(0).xyz *= (lightResult - directionalResult * (1.0-shadow));
 | |
| #endif
 | |
| 
 | |
| #if FOG
 | |
|         float fogValue = shSaturate((depth - fogParams.y) * fogParams.w);
 | |
|         
 | |
|         #if UNDERWATER
 | |
|         shOutputColour(0).xyz = shLerp (shOutputColour(0).xyz, UNDERWATER_COLOUR, shSaturate(length(waterEyePos-worldPos) / VISIBILITY));
 | |
|         #else
 | |
|         shOutputColour(0).xyz = shLerp (shOutputColour(0).xyz, fogColour, fogValue);
 | |
|         #endif
 | |
| #endif
 | |
| 
 | |
|         // prevent negative colour output (for example with negative lights)
 | |
|         shOutputColour(0).xyz = max(shOutputColour(0).xyz, float3(0,0,0));
 | |
| 
 | |
| #if IS_FIRST_PASS
 | |
|         shOutputColour(0).a = 1;
 | |
| #else
 | |
|         shOutputColour(0).a = 1.f-previousAlpha;
 | |
| #endif
 | |
|     }
 | |
| 
 | |
| #endif
 |