CS184 Section 8
GPU Fun
Brandon Wang
October 17, 2012
Material adapted from Fu-Chung Huang and the internet.
Brandon Wang
October 17, 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:
2D arrays!
Other effects than just material color
Specular material term (specular maps)
Normal perturbing (bump maps)
Shadows (shadow maps)
GPGPU
And it's all wicked fast
...So what?
We can change the geometry using this information (displacement mapping)
(I don't actually know if this was done using GPUs, but I would hope so)
You can actually do most of this now!
wat
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
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?)
Rather than perturbing vertices, perturb normals
Why?
It's cheaper!
How does it work?
Imagine if you actually displaced the vertices, how would the normals change?
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
if (bumpMapEnabled) { normal = (look up from texture) } else { normal = vec3(0,0,1); } normal = mat3(geo_tangent,geo_bitangent,geo_normal) * normal;
But there is an issue...
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
(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)