Pro OpenGL ES for iOS

(singke) #1

CHAPTER 6: Will It Blend? (^193)
Figure 6-14. On the left (left), the face is illuminated, which is not the case on the right (right).
With this in mind, the ‘‘cheat’’ that bump mapping uses is as follows. Take the actual
texture you want to use, and add a special second companion texture to it. This second
texture encodes normal information in place of the RGB colors. So, instead of using
floats that are 4 bytes each, it uses 1-byte values for the xyz of normal vectors that
conveniently fit inside a single 4-byte pixel. Since the vectors usually don’t have to be
super accurate, the 8-bit resolution is just fine and is very memory efficient. So, these
normals are generated in a way to map directly to the vertical features you want
highlighted.
Because normals can have negative values as well as positive (negative when pointing
away from the sun), the xyz values are centered in the range of 0 to 1. That is, -127 to
+127 must be mapped to anywhere between 0 and 1. So, the ‘‘red’’ component, which
is typically the x part of the vector, would be calculated as follows:
red=(x+ 1 )/ 2. 0
And of course this is similar for the green and blue bits.
Now look at the formula expressed in the GL_DOT3_RGB entry of Table 6-3. This takes the
RGB triplet as the vector and returns its length. N is the normal vector, and L is the light
vector, so the length is solved as follows:
length= 4 ×((Rn−. 5 )×(Rl−. 5 )+(Gn−. 5 )×(Gl−. 5 )+(Bn−. 5 )×(Bl−. 5 ))
So if the face is aimed directly toward the light along the x-axis, the normal’s red would
be 1.0, and the light’s red or x value would also be 1.0. The green and blue bits would
be .5, which is the encoded form of 0. Plugging that into the earlier equation would look
like this:
length= 4 ×(( (^1) n−. 5 )×( (^1) l−. 5 )+(. (^5) n−. 5 )×(. (^5) l−. 5 )+(. (^5) n−. 5 )×(. (^5) l−. 5 ))
length= 4 ×(. 25 + 0 + 0 )= 1. (^0)
This is exactly what we’d expect. And if the normal is pointing up and away from the
surface in the z direction, encoded in the blue byte, the answer should be 0 because the
normals are largely aimed up away from the texture’s X and Y planes. Figure 6-15 (left)

Free download pdf