Internal texture values

Three easy questions…

I have a 3D sampler that I use for random values in my fragment shader. I setup the texture with

  glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 
               64, 64, 64, 0, GL_RGBA,
               GL_UNSIGNED_BYTE, m_pBytes);

and access it with

uniform sampler3D Random_3D;

vec4 random(const in vec3 pos3v)
    vec4 texel4v = texture3D(Random_3D, pos3v);
    return texel4v;

Everything works fine, but I just realised something I don’t understand.

Why are the values floats between 0.0 and 1.0 and when is the conversion done? I thought the glTexImage3D meant the internal structure was 4 bytes 0 to 255.

Then I wondered how I could have a sampler to look up floats and since glTexImage doesn’t have an internal format of GL_FLOAT I couldn’t see how.

glTexImage can take GL_FLOAT as the input type. Does GL_FLOAT have a defined portable layout so I could create a file to load the data? Or would I have to use some other format and then load and convert the data?



The textures are usually stored as RGBA color values, and each color component is defined as value in range 0 to 1. When using bytes, this floating point value is mapped to byte range, so 0 corresponds to 0.0 and 255 to 1.0 If you use RGBA as internal format, the texture data will be converted to match it. You can supply texture data as floats, but this will be clamped to [0.0…1.0] range. It is possible, however, to use floating-point textures with newer hardware, see

But if you only want to have 0…255 values in the shader, you could just multiply the texel value with 255.

The texture is really stored as RGBA8 when that’s is your internal format, but the GPU fetches those and immediatly converts to float (0.0 to 1.0)

That’s normal operation!

Also, it’s not pleseant to work with bytes. It’s easier to do math (lighting equations and such) with floats.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.