openclonk/planet/Graphics.ocg/ObjectShader.glsl

110 lines
2.8 KiB
GLSL

uniform mat3x2 lightTransform;
#ifdef HAVE_NORMALMAP
uniform sampler2D normalTex;
#endif
#ifdef MESH
varying vec3 normalDir;
#endif
uniform vec4 clrMod;
#ifdef HAVE_BASE
uniform sampler2D baseTex;
#endif
#ifdef HAVE_OVERLAY
uniform vec4 overlayClr;
uniform sampler2D overlayTex;
#endif
slice(material)
{
// Default material properties. TODO: Populate them?
vec3 matEmit = vec3(0.0,0.0,0.0);
vec3 matSpot = vec3(1.0,1.0,1.0);
float matAngle = 1.0;
}
slice(texture)
{
#define color gl_FragColor
#ifdef MESH
// TODO: Add emission part of the material. Note we cannot just
// add this to the color, but it would need to be handled separately,
// such that it is independent from the incident light direction.
color = gl_FrontMaterial.diffuse;
#else
vec4 baseColor = gl_Color;
color = baseColor;
#endif
#ifdef HAVE_BASE
color = baseColor * texture2D(baseTex, texcoord.xy);
#endif
#ifdef HAVE_OVERLAY
// Get overlay color from overlay texture
vec4 overlay = baseColor * overlayClr * texture2D(overlayTex, texcoord.xy);
// Mix overlay with texture
float alpha0 = 1.0 - (1.0 - color.a) * (1.0 - overlay.a);
color = vec4(mix(color.rgb, overlay.rgb, overlay.a / alpha0), alpha0);
#endif
}
slice(texture+4)
{
#ifdef HAVE_LIGHT
// prepare texture coordinate for light lookup in LightShader.c
// Extra .xy since some old intel drivers return a vec3
vec2 lightCoord = (lightTransform * vec3(gl_FragCoord.xy, 1.0)).xy;
#endif
}
slice(normal)
{
#ifdef HAVE_NORMALMAP
vec4 normalPx = texture2D(normalTex, texcoord.xy);
vec3 normalPxDir = 2.0 * (normalPx.xyz - vec3(0.5, 0.5, 0.5));
#ifdef MESH
// For meshes, the normal matrix is typically provided in Clonk
// coordinates, but the normal matrix incorporates a component that
// transforms from Ogre to Clonk coordinates. Therefore, we need to
// reverse that transformation for meshes.
// TODO: This could be optimized since the matrix is so simple that
// we don't need to do a full matrix multiplication.
mat3 c2o = mat3(0.0, 1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0);
vec3 normal = normalize(c2o * gl_NormalMatrix * normalPxDir);
#else
vec3 normal = normalize(gl_NormalMatrix * normalPxDir);
#endif
#else
#ifdef MESH
vec3 normal = normalDir; // Normal matrix is already applied in vertex shader
#else
vec3 normal = vec3(0.0, 0.0, 1.0);
#endif
#endif
}
slice(color)
{
// TODO: Instead of a conditional, we could compute the color as
// color = A + B*color + C*clrMod + D*color*clrMod;
// Then, mod2 would be A=-0.5, B=1, C=1, D=0
// and default would be A=0,B=0,C=0,D=1
// Would allow to avoid conditionsals and #ifdefs, but need 4 uniforms...
// Could also try some sort of 3x3 matrix:
// out = (color, clrmod, 1) * (A,B,C,D,E,F,0,0,G) * (color, clrmod, 1)
#ifdef HAVE_MOD2
color = clamp(color + clrMod - 0.5, 0.0, 1.0);
#else
color = color * clrMod;
#endif
}