SourceForge.net Logo
Home
Documentation
Tutorials
    Beginning
    Gears
    Noise
    Bumpmapping
    Point sprites
    Hello World
    Armature
    Geometryshader
    Toolscripting
    Keyframe animation
    Instancing
    Bezier Surface
    Deferred shading
    Depth peeling
    Particle System
Links
Support This Project
OpenGL
This tutorial demonstrate simple dot3 bump-mapping
Basic dot3 shader
For a bump-mapping setup we need a node with some items: Script, Fragmentshader,
Vertexshader, two textures ("Colormap" and "Normalmap") and a Torus as test object.

For this tutorial are also a color map and a normal map texture file needed. A specular
map could be used to prevent the shaded object against the clear lacquer look.

Lets start with a basic script:

shader = gl.Shader(Vertexshader,Fragmentshader);

shader.Uniformi("Colormap",0);
shader.Uniformi("Normalmap",1);

tan = shader.Loc("Tangent");

function render(){
Colormap.Bind(0);
Normalmap.Bind(1);

gl.Rotate(60 * World.getTime(), 0,1,0);
gl.Rotate(60 * World.getTime(), 1,0,0);

shader.Bind();

Torus.UvCoords.Bind();
Torus.Tangent.Bind(tan);
Torus.Vertex.Bind();
Torus.Normal.Bind();

Torus.Index.Draw();
}


A simple vertexshader transforms the data from the stream. It's important to transform
the "Tangent" like the normal vector. Both vectors have to been projected into the
worldspace. For the bitangent vector, it saves some instruction and memory if it's
calculated by a crossproduct.

attribute vec3 Tangent;

varying vec3 T,B,N;

void main(void){
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0 * vec4(3,1,1,1);
N = normalize(gl_NormalMatrix * gl_Normal);
T = normalize(gl_NormalMatrix * Tangent);
B = cross (T,N);
}


Now a simple bummapping shader is needed. The first line in main fetch the value from
the normalmap, that will be transformed into the worldspace by the second line. The
folowing two lines calculate the diffuse and specular lightning. The last line compose
that two values.

uniform sampler2D Colormap;

uniform sampler2D Normalmap;
varying vec3 T,B,N;

vec3 lvec= vec3(0.0, 0.707, 0.707);

void main(void){
vec3 nmap = texture2D(Normalmap, gl_TexCoord[0].xy).rgb - 0.5;
nmap = normalize(T * nmap.x + B * nmap.y + N * nmap.z);
float l = dot(nmap,lvec);
float r = pow(max(dot(reflect (lvec,nmap),vec3(0.0,0.0,-1.0)), 0.0),4.0);
gl_FragColor = texture2D(Colormap,gl_TexCoord[0].xy) * l + r;
}


EDIT