Graphics Reference
In-Depth Information
precision highp float ; // Requires highp
vec4 packFloatToVec4 ( const float value ) {
vec4 bitSh = vec4 ( 256.0 ￿ 256.0 ￿ 256.0, 256.0 ￿ 256.0, 256.0, 1.0 );
vec4 bitMsk = vec4 ( 0.0, 1.0/256.0 , 1.0/256.0 , 1.0/256.0 );
vec4 res = fract ( value ￿ bitSh );
res
= res . xxyz ￿ bitMsk ;
return res ;
float unpackFloatFromVec4 ( const vec4 value )
{
const vec4 bitSh = vec4 ( 1.0/( 256.0 ￿ 256.0 ￿ 256.0 ) ,
1.0/( 256.0 ￿ 256.0 ) , 1.0/256.0 , 1.0 );
return dot ( value , bitSh );
}
Listing 2.2. Encoding and decoding a single floating point value into 32 bits of an
RGBA8 texture.
depth only, applications that are memory bandwidth limited should consider
encoding depth instead and reconstruct the world position in the light volume
pass. Reconstructing the world position from depth in both OpenGL ES 2.0
and 3.0 is recommended, as these APIs do not have floating point render target
support as a core feature.
To encode depth to a texture in OpenGL ES 2.0 requires the extension
GL_OES_depth_texture . If this extension is not available, the application can in-
stead encode the depth into 32 bits of a separate RGBA8 texture. An example
of how to encode the depth into 32 bits of the texture is shown in Listing 2.2.
This method may also be used to encode world position.
To reconstruct the position from this depth, the application can use the fol-
lowing code (Listing 2.3). This code applies when rendering the light volumes
and takes the world position of the current view frustum as input uniforms to
the shader.
gl_Position = mvpMatrix ￿ inVertex ;
vec2 textureCoord = gl_Position . xy / gl_Position . w ;
textureCoord =( textureCoord +1.0 ) ￿ 0.5;
// Interpolate the far frustum so that we have a point in world
// space . Multiply by w as gl will do this for us while
// interpolating varyings , noperspective is not available
farWorldPos = mix (
mix ( farFrustumBL , farFrustumTL , textureCoord . y ),
mix ( farFrustumBR , farFrustumTR , textureCoord . y ),
textureCoord . x ) ￿
gl_Position . w ;
nearWorldPos = mix (
mix ( nearFrustumBL , nearFrustumTL , textureCoord . y ),
mix ( nearFrustumBR , nearFrustumTR , textureCoord . y ),
textureCoord . x ) ￿ gl_Position . w ;
Listing 2.3. Reconstructing the world position from a linear depth.
Search WWH ::




Custom Search