31
Mar
08

Bump Mapping

Thought I’d have a go using the Normal Map code from the last post to do some bump-mapping.
Turns out it’s quite easy to use the output of the Normal Map CIFilter as normals in a Fragment Shader. All that needs to be done is to transform it into the correct range by multiplying the RGB value of each pixel by 2.0, then subtracting 1.0.

Some examples using a simple Phong lighting model, and normals extracted from first a static image, then the live video from the iSight camera, run through the Normal Map CIFilter:

Bump Mapping 01
Bump Mapping 02

Here’s the GLSL code:

Vertex Shader:

varying vec3 normal, lightDir, eyeVec;

void main()
{
	// Normal from mesh
	normal = gl_NormalMatrix * gl_Normal;

	// Current Vertex coordinates transformed into World space
	vec3 Vertex = vec3(gl_ModelViewMatrix * gl_Vertex);

	// Light Direction
	lightDir = vec3(gl_LightSource[0].position.xyz - Vertex);
	// Eye Vector
	eyeVec = -Vertex;

	gl_Position = ftransform();

	gl_FrontColor = gl_Color;
	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
}

Fragment Shader:

varying vec3 normal, lightDir, eyeVec;
uniform sampler2D texture;
uniform sampler2D normalMap;

void main (void)
{
	// Base color
	vec4 final_color =
	(gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) +
	(gl_LightSource[0].ambient * gl_FrontMaterial.ambient);

	vec3 N = texture2D(normalMap, gl_TexCoord[0].st).rgb * 2.0 - 1.0;
	//vec3 N = normalize(normal);	// Normal
	vec3 L = normalize(lightDir);	// Light Direction (normalized)
	float lambertTerm = dot(N,L);	// Base lighting (no ambient or specular contribution)

	if(lambertTerm > 0.0)
	{
		// Add diffuse contribution
		final_color += gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * lambertTerm;
		// Compute specular contribution
		vec3 E = normalize(eyeVec);
		vec3 R = reflect(-L, N);
		float specular = pow(max(dot(R, E), 0.0), gl_FrontMaterial.shininess);
		// Add specular contribution
		final_color += gl_LightSource[0].specular * gl_FrontMaterial.specular * specular;
	}

	// Output
	gl_FragColor = final_color;// * texture2D(texture, gl_TexCoord[0].xy);
}

Note
You’ll need to stick the GLSL shader inside a Lighting Patch in order to be able to set the properties for the light.
You’ll get an error in the Frag shader about the varying normal not being used.

Advertisements

2 Responses to “Bump Mapping”


  1. 1 Victor
    March 31, 2008 at 2:21 pm

    Hey

    so I have been testing your code, is quite cool, i got the mormal patch cifilter going, but the lates bumpmap stuff i do not what i need to do, i put your code on the glsl shader, it gives the error you are talking about but nothing shows on the viewer, could you post a sample comp? Thanks

    v

  2. 2 toneburst
    March 31, 2008 at 2:55 pm

    Hi Victor,

    sorry, my post is a bit short on detail.
    You’ll probably need to stick the GLSL patch inside a Lighting patch to get it to work correctly. I’ve also found that the lighting model itself needs some work. The way highlights are rendered isn’t that great.

    a|x


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Twitter

March 2008
M T W T F S S
« Feb   Apr »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Links

Blog Stats

  • 470,209 hits

%d bloggers like this: