PDA

View Full Version : Bump mapping example in "OpenGL Shading Language"



firegun9
02-14-2007, 02:37 PM
Hi,

I follow the bump mapping example from the book "OpenGL Shading Language," but the result looks very different from what it does in the book.

Please see the comparison here:
http://joe.phsiao.googlepages.com/bump.jpg

The left one is what I got in Shader Designer, and it does not bump :( . The right one is the result in the book.

Here is the code:

varying vec3 LightDir;
varying vec3 EyeDir;

attribute vec3 Tangent;

void main()
{
vec3 LightPosition = vec3(0,0,0);
EyeDir = vec3(gl_ModelViewMatrix * gl_Vertex);
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;

vec3 n = normalize(gl_NormalMatrix * gl_Normal);
vec3 t = normalize(gl_NormalMatrix * Tangent);
vec3 b = cross(n, t);

vec3 v;
v.x = dot(vec3(gl_LightSource[0].position), t);
v.y = dot(vec3(gl_LightSource[0].position), b);
v.z = dot(vec3(gl_LightSource[0].position), n);
LightDir = normalize(v);

v.x = dot(EyeDir, t);
v.y = dot(EyeDir, b);
v.z = dot(EyeDir, n);
EyeDir = normalize(v);
}
varying vec3 LightDir;
varying vec3 EyeDir;

void main()
{
vec3 litColor;
vec2 c = 16.0 * gl_TexCoord[0].st;
vec2 p = fract(c) - vec2(0.5);

float d, f;
d = p.x * p.x + p.y * p.y;
f = 1.0 / sqrt(d+1.0);

if(d >= 0.15)
{
p = vec2(0.0); f = 1.0;
}

vec3 normDelta = vec3(p.x, p.y, 1.0)*f;
litColor = vec3(.7, .6, .18) * max(dot(normDelta, LightDir), 0.0);
vec3 reflectDir = reflect(LightDir, normDelta);

float spec = max(dot(EyeDir, reflectDir), 0.0);
spec = pow(spec, 6.0);
spec *= .5;

litColor = min(litColor + spec, vec3(1.0));

gl_FragColor = vec4(litColor, 1.0);
}I just replaced all the uniform variables in the example to make it executable in the program Shader Designer. What is the problem?

Ffelagund
02-15-2007, 03:25 AM
Shader Designer comes with exactly that shader correctly implemented and working without problems. Check out the dimple.gdp shader at Shader Designer's shaders folder.

firegun9
02-15-2007, 09:56 AM
Hi Ffelagund,

Your response is very helpful, and I got the problem.
I compare the vertex shader in both of them. In the book, it retrieves the tangent vector in the tangent space from an attribute variable, "Tangent."

vec3 t = normalize(gl_NormalMatrix * Tangent);In the example of Shader Designer, the tangent vector is computed by cross product a fixed vector and the normal.

vec3 t = normalize(cross(vec3(1.141, 2.78, 3.14), n));After I copied this line from Shader Designer's example to the book's example, everything works great! The result is exactly the same as the picture in the book.

I am wondering is this a bug of the book or the inconsistent results for different video cards? I am using Ati 9600 mobility.

ZbuffeR
02-15-2007, 01:24 PM
No, in this case the custom attribute 'Tangent' is expected to be filled in by your application.
You can do it in Shader Designer too.