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; -*- // -*- mode: C; -*-
// Licence: GPL v2 // Licence: GPL v2
// Authors: Original code by Thorsten Jordan, Luis Barrancos and others (dangerdeep.sf.net) // Author: Frederic Bouvier
// 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 varying vec4 ecPosition;
uniform sampler2D tex_normal; // normal map, RGB + A Specular varying vec3 VNormal;
varying vec3 VTangent;
varying vec3 VBinormal;
varying vec4 constantColor;
uniform sampler2D tex_color;
uniform sampler2D tex_normal;
varying vec4 ecPosition; void main (void)
varying vec3 lightdir, halfangle;
void main()
{ {
// get and normalize vector to light source vec4 ns = texture2D(tex_normal, gl_TexCoord[0].st);
vec3 L = normalize(lightdir); 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 float nDotVP = max(0.0, dot(N, normalize(gl_LightSource[0].position.xyz)));
vec4 v = texture2D(tex_normal, gl_TexCoord[0].st); float nDotHV = max(0.0, dot(N, gl_LightSource[0].halfVector.xyz));
vec3 N = normalize(vec3(v.xyz * 2.0 - 1.0));
N.y = -N.y;
if (!gl_FrontFacing)
N = -N;
// compute specular color float pf;
// get and normalize half angle vector if (nDotVP == 0.0)
vec3 H = normalize(halfangle); pf = 0.0;
else
pf = pow(nDotHV, gl_FrontMaterial.shininess);
// compute resulting specular color vec4 Diffuse = gl_LightSource[0].diffuse * nDotVP;
vec3 specular_color = vec3(gl_FrontMaterial.specular) * vec4 Specular = gl_LightSource[0].specular * pf;
pow(max(dot(H, N), 0.0), gl_FrontMaterial.shininess);
// compute diffuse color vec4 color = constantColor + Diffuse * gl_FrontMaterial.diffuse;
vec3 diffuse_color = vec3(texture2D(tex_color, gl_TexCoord[0].st)); color *= texture2D(tex_color, gl_TexCoord[0].xy);
// handle ambient color += Specular * gl_FrontMaterial.specular * ns.a;
diffuse_color = diffuse_color * mix(max(dot(L, N), 0.0), 0.6, gl_LightSource[0].ambient.r); color = clamp( color, 0.0, 1.0 );
specular_color = specular_color * v.a;
// final color of fragment float fogFactor;
vec3 final_color = clamp((diffuse_color + specular_color) * vec3(gl_LightSource[0].diffuse /*light_color*/), 0.0, 1.0); float fogCoord = ecPosition.z;
const float LOG2 = 1.442695;
fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2);
fogFactor = clamp(fogFactor, 0.0, 1.0);
float fog_factor;
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);
// output color is a mix between fog and final color gl_FragColor = mix(gl_Fog.color, color, fogFactor);
gl_FragColor = vec4(mix(vec3(gl_Fog.color), final_color, fog_factor), 1.0);
} }

View file

@ -1,53 +1,24 @@
// -*- mode: C; -*- // -*- mode: C; -*-
// Licence: GPL v2 // Licence: GPL v2
// Authors: Original code by Thorsten Jordan, Luis Barrancos and others (dangerdeep.sf.net) // Author: Frederic Bouvier
// Modified by Emilian Huminiuc (emilianh@gmail.com) (i4dnf on the flightgear forums).
/* input: varying vec4 ecPosition;
gl_Vertex varying vec3 VNormal;
gl_Normal (tangentz) varying vec3 VTangent;
gl_MultiTexCoord0 (texcoord) varying vec3 VBinormal;
tangentx_righthanded (tangentx,righthanded-factor) varying vec4 constantColor;
*/
/* can we give names to default attributes? or do we need to use named attributes for that? attribute vec3 tangent;
how to give named attributes with vertex buffer objects? attribute vec3 binormal;
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.
*/
varying vec4 ecPosition; void main (void)
varying vec3 lightdir, halfangle;
attribute vec4 tangent, binormal;
void main()
{ {
ecPosition = gl_ModelViewMatrix * gl_Vertex; ecPosition = gl_ModelViewMatrix * gl_Vertex;
VNormal = normalize(gl_NormalMatrix * gl_Normal);
// compute direction to light in object space (L) VTangent = normalize(gl_NormalMatrix * tangent);
vec3 lightpos_obj = vec3(gl_ModelViewMatrixInverse * gl_LightSource[0].position); VBinormal = normalize(gl_NormalMatrix * binormal);
vec3 lightdir_obj = normalize(lightpos_obj); constantColor = gl_FrontLightModelProduct.sceneColor + gl_LightSource[0].ambient * gl_FrontMaterial.ambient;
gl_FrontColor = constantColor;
// direction to viewer (E) gl_TexCoord[0] = gl_MultiTexCoord0;
// compute direction to viewer (E) in object space (mvinv*(0,0,0,1) - inputpos) gl_Position = ftransform();
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
gl_Position = ftransform();
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
} }