CS184 Section 5

Brandon Wang
February 21, 2012
Material adapted from Fu-Chung Huang and the internet.




NeHe is a great site to learn about various OpenGL techniques outside of this class. We're approaching the end of OpenGL-land, so if you would to learn more, this is a great place to start


So far, we've seen textures as repeating patterns, like this:


But what are textures really?

2D arrays!

Other effects than just material color

Specular material term (specular maps)

Normal perturbing (bump maps)

Shadows (shadow maps)


And it's all wicked fast


...So what?

We can change the geometry using this information (displacement mapping)

Post-processing effects, like bloom, or crepuscular rays

Velocity Maps


Cool Effects

(I don't actually know if this was done using GPUs, but I would hope so)

Not just a material property

HW3 Extra Credit

You can actually do most of this now!


You can do this for extra credit in HW3

Displacement Mapping

Do this in...

The vertex shader!

Move the vertices around - the same place you apply the model-view-projection matrix.

Dark = low number, go against normal; White = high number, go along normal

Displacement Mapping

Displacement Mapping

 v = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex
uniform sampler2D heightMap;
dv = texture2D( heightMap,  gl_TexCoord[0].st ) - vec4(0.5);
newVertexPos = vec4(gl_Normal * dv.x * 0.25, 0.0) + gl_Vertex;
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * newVertexPos;

(Were you expecting more?)

Bump Mapping

Rather than perturbing vertices, perturb normals


It's cheaper!

How does it work?

Imagine if you actually displaced the vertices, how would the normals change?

Bump Mapping

More tricky.

Construct a 3D basis at each pixel to have a space to perturb normals in

Usually the tangent, binormal, normal (tbn)

(A little) complicated derivation

But really, just imagine tracing a camera along a surface. Walking around a globe, your "up" will be the normal, Forward will be the tangent, and to your side is the bitangent

Bump Mapping

    if (bumpMapEnabled) {
      normal = (look up from texture)
    } else {
      normal = vec3(0,0,1);
    normal = mat3(geo_tangent,geo_bitangent,geo_normal) * normal;

Displacement vs Bump Mapping

But there is an issue...

Environment Mapping

Environment is infinitely far away

Local translations don't matter - rotations do

Pretend you are on your surface and looking at the direction of your eye vector mirrored across the normal

In fact, all parallel normals will point to the same infitely far away object

Make a function to map from your eye to reflection! (Pure reflection)

The code is a bit complicated, but there are ways to make OpenGL do it for you

Environment Mapping

Shadow Mapping

(I probably won't get to this in discussion, but this is a cool one. It's also quite a bit harder than the other ones so far)

Imagine that you are a light. Everything that you can see will be lit, and the things you can't are in shadow.

Think of it like photons that your eye shoots out - photons can't hit things that are blocked

So, don't store color or anything silly like that, but distance that it took for your photon to get blocked

Now you have a 3D representation of all pixels that should be in light!

The 3D representation can be in world space, and you can map your world space coordinates to this shadow map

The implementation is difficult (rendering to a texture, etc)