Site Stability Read more... ×

# OpenGL Compute Normal.z from Normal.x and Normal.y

This topic is 4138 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have problem with Normal in Deferred Shading. Because in OpenGL FBO MRTs I can't use different internal format, I can't use RGBA16F for Normal, bacause I will must use RGBA16F for all textures, so FPS will be go down. I have to use RGBA8 for all textures, so I have to pack Normal.x and Normal.y for it and compute Normal.z in shader. I try: Normal.z = sqrt(1 - Normal.x*Normal.x - Normal.y*Normal.y); // Normal in object space, but in View space etc. also not work properly How can I compute this Normal.z value? I currently don't test it with normal map from texture, I get Normals from standard calculation it in application.

##### Share on other sites
Notice that mathematically a square root results in a positive and a negative value, since a square supresses the sign of a number. So the result you are looking for is either
Normal.z = +sqrt(1 - Normal.x*Normal.x - Normal.y*Normal.y);
or else
Normal.z = -sqrt(1 - Normal.x*Normal.x - Normal.y*Normal.y);
You have to encode this case distinction, too, if you can't predict the sign otherwise.

Next, you have to consider that normals are of unit length, so that all of its components show an absolute value less than or equal to 1. Encoding this into an integer needs an up-scaling to take advantage of the full range, not to say to avoid to suffer from the value being clipped to 0. Later, in the shader program, the up-scaling has to be undone before or just after squaring the values.

##### Share on other sites
Thanks for answer, I solved my problem:)

[Edited by - carew on June 22, 2008 5:51:17 PM]

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 15
• 21
• 26
• 11