1
0
Fork 0

Rewrote the bumpspec effect to fix light parameters

This commit is contained in:
fredb 2010-03-20 18:00:26 +00:00
parent 026ff680d4
commit 8556d3c1ea
2 changed files with 48 additions and 83 deletions

View file

@ -1,53 +1,47 @@
// -*- mode: C; -*-
// Licence: GPL v2
// Authors: Original code by Thorsten Jordan, Luis Barrancos and others (dangerdeep.sf.net)
// Modified by Emilian Huminiuc (emilianh@gmail.com) (i4dnf on the flightgear forums).
// fixme: maybe lookup texmap is faster than pow(). Quick tests showed that this is not the case...
uniform sampler2D tex_color; // (diffuse) color map, RGB
uniform sampler2D tex_normal; // normal map, RGB + A Specular
// Author: Frederic Bouvier
varying vec4 ecPosition;
varying vec3 lightdir, halfangle;
varying vec3 VNormal;
varying vec3 VTangent;
varying vec3 VBinormal;
varying vec4 constantColor;
void main()
uniform sampler2D tex_color;
uniform sampler2D tex_normal;
void main (void)
{
// get and normalize vector to light source
vec3 L = normalize(lightdir);
vec4 ns = texture2D(tex_normal, gl_TexCoord[0].st);
vec3 N = ns.rgb * 2.0 - 1.0;
N = normalize(N.x * VTangent + N.y * VBinormal + N.z * VNormal);
// get and normalize normal vector from texmap
vec4 v = texture2D(tex_normal, gl_TexCoord[0].st);
vec3 N = normalize(vec3(v.xyz * 2.0 - 1.0));
N.y = -N.y;
if (!gl_FrontFacing)
N = -N;
float nDotVP = max(0.0, dot(N, normalize(gl_LightSource[0].position.xyz)));
float nDotHV = max(0.0, dot(N, gl_LightSource[0].halfVector.xyz));
// compute specular color
// get and normalize half angle vector
vec3 H = normalize(halfangle);
float pf;
if (nDotVP == 0.0)
pf = 0.0;
else
pf = pow(nDotHV, gl_FrontMaterial.shininess);
// compute resulting specular color
vec3 specular_color = vec3(gl_FrontMaterial.specular) *
pow(max(dot(H, N), 0.0), gl_FrontMaterial.shininess);
vec4 Diffuse = gl_LightSource[0].diffuse * nDotVP;
vec4 Specular = gl_LightSource[0].specular * pf;
// compute diffuse color
vec3 diffuse_color = vec3(texture2D(tex_color, gl_TexCoord[0].st));
vec4 color = constantColor + Diffuse * gl_FrontMaterial.diffuse;
color *= texture2D(tex_color, gl_TexCoord[0].xy);
// handle ambient
diffuse_color = diffuse_color * mix(max(dot(L, N), 0.0), 0.6, gl_LightSource[0].ambient.r);
color += Specular * gl_FrontMaterial.specular * ns.a;
color = clamp( color, 0.0, 1.0 );
specular_color = specular_color * v.a;
// final color of fragment
vec3 final_color = clamp((diffuse_color + specular_color) * vec3(gl_LightSource[0].diffuse /*light_color*/), 0.0, 1.0);
float fog_factor;
float fogFactor;
float fogCoord = ecPosition.z;
const float LOG2 = 1.442695;
fog_factor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2);
fog_factor = clamp(fog_factor, 0.0, 1.0);
fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2);
fogFactor = clamp(fogFactor, 0.0, 1.0);
// output color is a mix between fog and final color
gl_FragColor = vec4(mix(vec3(gl_Fog.color), final_color, fog_factor), 1.0);
gl_FragColor = mix(gl_Fog.color, color, fogFactor);
}

View file

@ -1,53 +1,24 @@
// -*- mode: C; -*-
// Licence: GPL v2
// Authors: Original code by Thorsten Jordan, Luis Barrancos and others (dangerdeep.sf.net)
// Modified by Emilian Huminiuc (emilianh@gmail.com) (i4dnf on the flightgear forums).
/* input:
gl_Vertex
gl_Normal (tangentz)
gl_MultiTexCoord0 (texcoord)
tangentx_righthanded (tangentx,righthanded-factor)
*/
/* can we give names to default attributes? or do we need to use named attributes for that?
how to give named attributes with vertex buffer objects?
we could assign them to a variable, but would that be efficient? an unnecessary copy.
but the shader compiler should be able to optimize that...
the way should be to use vertex attributes (together with vertex arrays or VBOs),
that have a name and use that name here.
until then, access sources directly or via a special variable.
*/
// Author: Frederic Bouvier
varying vec4 ecPosition;
varying vec3 lightdir, halfangle;
attribute vec4 tangent, binormal;
varying vec3 VNormal;
varying vec3 VTangent;
varying vec3 VBinormal;
varying vec4 constantColor;
void main()
attribute vec3 tangent;
attribute vec3 binormal;
void main (void)
{
ecPosition = gl_ModelViewMatrix * gl_Vertex;
// compute direction to light in object space (L)
vec3 lightpos_obj = vec3(gl_ModelViewMatrixInverse * gl_LightSource[0].position);
vec3 lightdir_obj = normalize(lightpos_obj);
// direction to viewer (E)
// compute direction to viewer (E) in object space (mvinv*(0,0,0,1) - inputpos)
vec3 viewerdir_obj = normalize(vec3(gl_ModelViewMatrixInverse[3]) - vec3(gl_Vertex));
// compute halfangle vector (H = ||L+E||)
vec3 halfangle_obj = normalize(viewerdir_obj + lightdir_obj);
// transform light direction to tangent space
lightdir.x = dot(tangent, lightdir_obj);
lightdir.y = dot(binormal, lightdir_obj);
lightdir.z = dot(gl_Normal, lightdir_obj);
halfangle.x = dot(tangent, halfangle_obj);
halfangle.y = dot(binormal, halfangle_obj);
halfangle.z = dot(gl_Normal, halfangle_obj);
// finally compute position
VNormal = normalize(gl_NormalMatrix * gl_Normal);
VTangent = normalize(gl_NormalMatrix * tangent);
VBinormal = normalize(gl_NormalMatrix * binormal);
constantColor = gl_FrontLightModelProduct.sceneColor + gl_LightSource[0].ambient * gl_FrontMaterial.ambient;
gl_FrontColor = constantColor;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
}