Graphics Reference
In-Depth Information
views or faces that capture reflected environment, assuming a single
viewpoint in the middle of the shiny object). The fixed-function OpenGL
specification describes the texture coordinate generation modes as
GL_SPHERE_MAP and GL_REFLECTION_MAP , respectively. The GL_SPHERE_MAP
mode generates a texture coordinate that uses a reflection vector to
compute a 2D texture coordinate for lookup into a 2D texture map.
The GL_REFLECTION_MAP mode generates a texture coordinate that is a
reflection vector, which can then can be used as a 3D texture coordinate
for lookup into a cubemap. Examples 8-4 and 8-5 show the vertex shader
code that generates the texture coordinates that will be used by the
appropriate fragment shader to calculate the reflected image on the shiny
object.
Example 8-4
Sphere Map Texture Coordinate Generation
// position is the normalized position coordinate in eye space.
// normal is the normalized normal coordinate in eye space.
// This function returns a vec2 texture coordinate.
vec2 sphere_map ( vec3 position, vec3 normal )
{
reflection = reflect ( position, normal );
m = 2.0 * sqrt ( reflection.x * reflection.x +
reflection.y * reflection.y +
( reflection.z + 1.0 ) * ( reflection.z + 1.0 ) );
return vec2(( reflection.x / m + 0.5 ),
( reflection.y / m + 0.5 ) );
}
Example 8-5
Cubemap Texture Coordinate Generation
// position is the normalized position coordinate in eye space.
// normal is the normalized normal coordinate in eye space.
// This function returns the reflection vector as a vec3 texture
// coordinate.
vec3 cube_map ( vec3 position, vec3 normal )
{
return reflect ( position, normal );
}
The reflection vector will then be used inside a fragment shader as the
texture coordinate to the appropriate cubemap.
 
 
Search WWH ::




Custom Search