Normal mapping question

I’ve been reading on-line tutorials about normal mapping. Mostly makes sense except for this piece in the fragment shader:

    vec3 BumpMapNormal = texture(gNormalMap, TexCoord0).xyz;
    BumpMapNormal = 2.0 * BumpMapNormal - vec3(1.0, 1.0, 1.0);

Question: The gNormalMap texture presumably has RGB components of type byte/int (according to glTexImage2D() specs for type) which are extracted by texture().xyz. But the second line seems to treat them as floats to get normal vector xyz components in -1.0 to 1.0. Where does the type conversion happen or am I missing something?

Textures can hold any of the following types of values:

  • Unsigned normalised values in the range 0 to 1
  • Signed normalised values in the range -1 to 1
  • Floating point values
  • Unsigned integers
  • Signed integers

The earliest versions of OpenGL only supported the first option.

The type of data held in the texture isn’t necessarily the same as the type of data used to initialise it (e.g. in glTexImage2D() etc). That’s why glTexImage2D() has both an internalformat parameter and the format and type parameters.

Textures holding normalised or floating-point values are accessed using “sampler” types (sampler2D) etc. The lookup functions (texture() etc) for these types return floats. Signed integer textures use isampler2D etc and the lookup functions return signed integers; unsigned integer textures use usampler2D etc.

The fact that the example you give multiplies the result by two and subtracts one indicates that the texture holds unsigned normalised values. These are often used for normal maps because the conventions were established when that was the only supported texture type.

Thanks, GClements.

I had a follow-up question but my post (with code wraps and fair bit of text) keeps getting denied. No idea why. I’ll try again tomorrow. Hope this 2-line post gets through now.

[QUOTE=GClements;1280815]Textures can hold any of the following types of values:

  • Unsigned normalised values in the range 0 to 1
  • Signed normalised values in the range -1 to 1
  • Floating point values
  • Unsigned integers
  • Signed integers

The earliest versions of OpenGL only supported the first option.

The type of data held in the texture isn’t necessarily the same as the type of data used to initialise it (e.g. in glTexImage2D() etc). That’s why glTexImage2D() has both an internalformat parameter and the format and type parameters.

Textures holding normalised or floating-point values are accessed using “sampler” types (sampler2D) etc. The lookup functions (texture() etc) for these types return floats. Signed integer textures use isampler2D etc and the lookup functions return signed integers; unsigned integer textures use usampler2D etc.

The fact that the example you give multiplies the result by two and subtracts one indicates that the texture holds unsigned normalised values. These are often used for normal maps because the conventions were established when that was the only supported texture type.[/QUOTE]

I am trying to post a follow-up question with about 20 lines of text and some code snippets but the system will not allow it. Tried several times but after each:

“Post denied. New posts are limited by number of URLs it may contain and checked if it doesn’t contain forbidden words.”

Why?? I have no URLs, obviously no x-rated content, … Help please.

I can’t post small images from my computer to this forum.
There are some bugs with the forum software, and this sounds like another one.
Try to find a workaround. Are you putting the code between [ code] and [ /code] tags?
If not, try it (without the spaces after ‘[’). If so, try it without the tags.

[QUOTE=chachacha;1280826]I am trying to post a follow-up question with about 20 lines of text and some code snippets but the system will not allow it. Tried several times but after each:

“Post denied. New posts are limited by number of URLs it may contain and checked if it doesn’t contain forbidden words.”

Why?? I have no URLs, obviously no x-rated content, … Help please.[/QUOTE]

New users are a bit limited to avoid problems with spammers posting spam using new accounts. Sorry for the inconvenience. Please try to post whatever you need in any form that will work. If you need to post URLs, just put spaces in them, and we’ll fix them up. After you’ve posted a few more times, your account won’t have these limitations.