CS 455 Lecture 20 – Environment Mapping
- what ?s
- reflecting a vector
- environment mapping
As a lab exercise:
- Sync with the class repo to get the
- Run the project to find a scene with three models hovering over a terrain and nestled in a skybox. Use the cursor keys to rotate the models. We want to these models to reflect their environment, like they’re made out of chrome.
- We need a shader that colors a fragment by shooting a ray from the eye to the fragment and then bouncing off to hit the skybox. Whatever pixel that reflected vector hits is the color we want. We can start by using the skybox shader, which doesn’t do any reflection, but it does the texture lookup that we’ll ultimately want. In
shader_programsto draw from
skybox.f.glsl. Run it to see what happens.
- Now let’s tackle the reflection problem. Switch the vertex shader to
environment.v.glsland open it for editing. Because we consider the cube map as a box surrounding our world, it’s easiest to compute the reflection vector using world coordinates. We will need three things:
- The vertex’s position in world space.
- The vertex’s normal in world space.
- The camera’s position in world space.
This shader already receives the vertex’s position in model space. Add an attribute for the normal in model space, a uniform for the camera’s position in world space (which you can get from the
Cameraclass), and a uniform for the transformation matrix that takes us from model space to world space. We’ll use the matrix to turn our model space coordinates to world space coordinates.
- Upload the two uniforms in
OnDraw. The normal attribute has already been uploaded; it just went unused.
- In the vertex shader, transform the vertex’s position and normal to world space.
- Compute a world space vector from the camera to the vertex. Let’s call this the incident vector.
- Reflect the incident vector about the world space normal:
vec3 reflection = reflect(incident, normal_world);
- This vector points to our skybox! Send it along to the fragment shader (which is still
skybox.f.glsl) to do your texture lookup. Run your code and walk close to an object. Do the surfaces facing you reflect what’s behind you? Do the top faces reflect the sky? What do the bottom faces reflect?
- Email me a screenshot of your chrome-y surfaces.