In order to be as efficient as possible, we will use the normals calculated previously in screen space. For the 2D textures coordinates, we will simply assume that we have a flat 2D texture mapped onto the circular silhouette of sphere, so we can simply use the normalized x and y coordinates of the normal as the texture coords. For the 3D case, all three components will be used.
We are given the following:
However, because of the fixed-point formats required on both ends, we have some additional difficulties:
TextureX = (NormalX * 24 * 0.5) + (216 * 0.5)
= (NormalX * 24 * 2-1) + 215
= (NormalX * 23) + 215
= (NormalX << 3) + 215
TextureY = (NormalY << 3) + 215
TextureZ = NormalZ * 24
= NormalZ << 4
Where NormalX and NormalY representing the range [-1,1] and NormalZ
representing the range [0,1] in 4.12 fixed-point format:
4.12 NormalX and NormalY = NormalX * 4096
= NormalX * 212
4.12 NormalZ = NormalZ * 212
So the normal components are converted in texture coords by a simple scaling (accomplished through a left shift) and translation (except in the case of z). The bit-shifts are fairly straightforward, but it is important to note two options for the translation (addition of 215). Given the 16-bit normal x and y components after the scaling phase, the addition of 215 can be accomplished by one of the following methods: