1
0
Fork 0

Unified lightmap, bumpmap, and reflect shader

Signed-off-by: Vivian Meazza <vivian.meazza@lineone.net>
This commit is contained in:
Vivian Meazza 2011-12-01 20:52:05 +00:00
parent cc329668dc
commit 0adb61f2a7
2 changed files with 269 additions and 0 deletions

161
Shaders/ubershader.frag Normal file
View file

@ -0,0 +1,161 @@
// -*- mode: C; -*-
// Licence: GPL v2
// Authors: Frederic Bouvier and Gijs de Rooy
// with major additions and revisions by
// Emilian Huminiuc and Vivian Meazza 2011
#version 120
varying vec3 rawpos;
varying vec3 VNormal;
varying vec3 VTangent;
varying vec3 VBinormal;
varying vec3 vViewVec;
varying vec3 reflVec;
varying float alpha;
uniform samplerCube Environment;
uniform sampler2D BaseTex;
uniform sampler2D NormalTex;
uniform sampler2D LightMapTex;
uniform sampler2D ReflMapTex;
uniform sampler2D ReflFresnelTex;
uniform sampler2D ReflRainbowTex;
uniform sampler3D ReflNoiseTex;
uniform int nmap_enabled;
uniform int nmap_dds;
uniform int refl_enabled;
uniform int refl_map;
uniform int lightmap_enabled;
uniform int lightmap_multi;
uniform int shader_qual;
uniform float lightmap_r_factor;
uniform float lightmap_g_factor;
uniform float lightmap_b_factor;
uniform float lightmap_a_factor;
uniform float refl_correction;
uniform float refl_fresnel;
uniform float refl_rainbow;
uniform float refl_noise;
uniform float amb_correction;
uniform vec3 lightmap_r_color;
uniform vec3 lightmap_g_color;
uniform vec3 lightmap_b_color;
uniform vec3 lightmap_a_color;
///fog include//////////////////////
uniform int fogType;
vec3 fog_Func(vec3 color, int type);
////////////////////////////////////
void main (void)
{
vec4 texel = texture2D(BaseTex, gl_TexCoord[0].st);
vec4 nmap = texture2D(NormalTex, gl_TexCoord[0].st);
vec4 reflmap = texture2D(ReflMapTex, gl_TexCoord[0].st);
vec4 noisevec = texture3D(ReflNoiseTex, rawpos.xyz);
vec4 reflection = textureCube(Environment, reflVec);
vec4 lightmapTexel = texture2D(LightMapTex, gl_TexCoord[0].st);
vec3 mixedcolor;
vec3 N;
float pf;
///BEGIN bump
if (nmap_enabled > 0 && shader_qual > 1){
N = nmap.rgb * 2.0 - 1.0;
N = normalize(N.x * VTangent + N.y * VBinormal + N.z * VNormal);
if (nmap_dds > 0)
N = -N;
} else {
N = normalize(VNormal);
}
///END bump
vec3 viewVec = normalize(vViewVec);
float v = dot(viewVec, normalize(VNormal));// Map a rainbowish color
vec4 fresnel = texture2D(ReflFresnelTex, vec2(v, 0.0));
vec4 rainbow = texture2D(ReflRainbowTex, vec2(v, 0.0));
float nDotVP = max(0.0, dot(N, normalize(gl_LightSource[0].position.xyz)));
float nDotHV = max(0.0, dot(N, normalize(gl_LightSource[0].halfVector.xyz)));
if (nDotVP == 0.0)
pf = 0.0;
else
pf = pow(nDotHV, gl_FrontMaterial.shininess);
vec4 Diffuse = gl_LightSource[0].diffuse * nDotVP;
vec4 Specular = gl_FrontMaterial.specular * gl_LightSource[0].specular * pf;
vec4 color = gl_Color + Diffuse * gl_FrontMaterial.diffuse;
color += Specular * gl_FrontMaterial.specular * nmap.a;
color = clamp( color, 0.0, 1.0 );
//////////////////////////////////////////////////////////////////////
//BEGIN reflect
//////////////////////////////////////////////////////////////////////
if (refl_enabled > 0 && shader_qual > 0){
float reflFactor;
float transparency_offset = clamp(refl_correction, -1.0, 1.0);// set the user shininess offset
if(refl_map > 0){
// map the shininess of the object with user input
//float pam = (map.a * -2) + 1; //reverse map
reflFactor = reflmap.a + transparency_offset;
} else if (nmap_enabled > 0 && shader_qual > 1) {
// set the reflectivity proportional to shininess with user input
reflFactor = (gl_FrontMaterial.shininess / 128.0) * nmap.a + transparency_offset;
} else {
reflFactor = gl_FrontMaterial.shininess/128.0 + transparency_offset;
}
reflFactor = clamp(reflFactor, 0.0, 1.0);
// add fringing fresnel and rainbow effects and modulate by reflection
vec4 reflcolor = mix(reflection, rainbow, refl_rainbow * v);
reflcolor += Specular * nmap.a;
vec4 reflfrescolor = mix(reflcolor, fresnel, refl_fresnel * v);
vec4 noisecolor = mix(reflfrescolor, noisevec, refl_noise);
vec4 raincolor = vec4(noisecolor.rgb * reflFactor, 1.0);
raincolor += Specular * nmap.a;
mixedcolor = mix(texel, raincolor, reflFactor).rgb;
} else {
mixedcolor = texel.rgb;
}
///////////////////////////////////////////////////////////////////////
//END reflect
///////////////////////////////////////////////////////////////////////
// set ambient adjustment to remove bluiness with user input
float ambient_offset = clamp(amb_correction, -1.0, 1.0);
vec4 ambient_Correction = vec4(gl_LightSource[0].ambient.rg, gl_LightSource[0].ambient.b * 0.6, 0.5) * ambient_offset ;
ambient_Correction = clamp(ambient_Correction, -1.0, 1.0);
color.a = texel.a * alpha;
vec4 fragColor = vec4(color.rgb * mixedcolor + ambient_Correction.rgb, color.a);
fragColor += Specular * nmap.a;
//////////////////////////////////////////////////////////////////////
// BEGIN lightmap
//////////////////////////////////////////////////////////////////////
if ( lightmap_enabled >= 1 ) {
vec3 lightmapcolor;
if (lightmap_multi >0 ){
lightmapcolor = lightmap_r_color * lightmap_r_factor * lightmapTexel.r +
lightmap_g_color * lightmap_g_factor * lightmapTexel.g +
lightmap_b_color * lightmap_b_factor * lightmapTexel.b +
lightmap_a_color * lightmap_a_factor * lightmapTexel.a ;
} else {
lightmapcolor = lightmapTexel.rgb * lightmap_r_color * lightmap_r_factor;
}
fragColor.rgb = max(fragColor.rgb, lightmapcolor * gl_FrontMaterial.diffuse.rgb * mixedcolor);
}
//////////////////////////////////////////////////////////////////////
// END lightmap
/////////////////////////////////////////////////////////////////////
fragColor.rgb = fog_Func(fragColor.rgb, fogType);
gl_FragColor = fragColor;
}

108
Shaders/ubershader.vert Normal file
View file

@ -0,0 +1,108 @@
// -*- mode: C; -*-
// Licence: GPL v2
// © Emilian Huminiuc and Vivian Meazza 2011
#version 120
varying vec3 rawpos;
varying vec3 VNormal;
varying vec3 VTangent;
varying vec3 VBinormal;
varying vec3 vViewVec;
varying vec3 reflVec;
varying float alpha;
attribute vec3 tangent;
attribute vec3 binormal;
uniform float pitch;
uniform float roll;
uniform float hdg;
uniform int refl_dynamic;
uniform int nmap_enabled;
uniform int shader_qual;
//////Fog Include///////////
uniform int fogType;
void fog_Func(int type);
////////////////////////////
void rotationMatrixPR(in float sinRx, in float cosRx, in float sinRy, in float cosRy, out mat4 rotmat)
{
rotmat = mat4( cosRy , sinRx * sinRy, cosRx * sinRy , 0.0,
0.0 , cosRx , -sinRx * cosRx, 0.0,
-sinRy, sinRx * cosRy, cosRx * cosRy , 0.0,
0.0 , 0.0 , 0.0 , 1.0 );
}
void rotationMatrixH(in float sinRz, in float cosRz, out mat4 rotmat)
{
rotmat = mat4( cosRz, -sinRz, 0.0, 0.0,
sinRz, cosRz , 0.0, 0.0,
0.0 , 0.0 , 1.0, 0.0,
0.0 , 0.0 , 0.0, 1.0 );
}
void main(void)
{
rawpos = gl_Vertex.xyz;
vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
fog_Func(fogType);
VNormal = normalize(gl_NormalMatrix * gl_Normal);
if (nmap_enabled > 0 && shader_qual > 1){
VTangent = normalize(gl_NormalMatrix * tangent);
VBinormal = normalize(gl_NormalMatrix * binormal);
} else {
VTangent = vec3(0.0);
VBinormal = vec3 (0.0);
}
vec3 n = normalize(gl_Normal);
vec3 t = cross(n, vec3(1.0,0.0,0.0));
vec3 b = cross(n,t);
// Super hack: if diffuse material alpha is less than 1, assume a
// transparency animation is at work
if (gl_FrontMaterial.diffuse.a < 1.0)
alpha = gl_FrontMaterial.diffuse.a;
else
alpha = gl_Color.a;
// Vertex in eye coordinates
vec3 vertVec = ecPosition.xyz;
vViewVec.x = dot(t, vertVec);
vViewVec.y = dot(b, vertVec);
vViewVec.z = dot(n, vertVec);
// calculate the reflection vector
vec4 reflect_eye = vec4(reflect(vertVec, VNormal), 0.0);
vec3 reflVec_stat = normalize(gl_ModelViewMatrixInverse * reflect_eye).xyz;
if (refl_dynamic > 0){
//prepare rotation matrix
mat4 RotMatPR;
mat4 RotMatH;
float _roll = roll;
if (_roll>90.0 || _roll < -90.0)
{
_roll = -_roll;
}
float cosRx = cos(radians(_roll));
float sinRx = sin(radians(_roll));
float cosRy = cos(radians(-pitch));
float sinRy = sin(radians(-pitch));
float cosRz = cos(radians(hdg));
float sinRz = sin(radians(hdg));
rotationMatrixPR(sinRx, cosRx, sinRy, cosRy, RotMatPR);
rotationMatrixH(sinRz, cosRz, RotMatH);
vec3 reflVec_dyn = (RotMatH * (RotMatPR * normalize(gl_ModelViewMatrixInverse * reflect_eye))).xyz;
reflVec = reflVec_dyn;
} else {
reflVec = reflVec_stat;
}
gl_FrontColor = gl_FrontMaterial.emission + gl_Color * (gl_LightModel.ambient + gl_LightSource[0].ambient);
gl_Position = ftransform();
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
}