Access floating point values from texture?

I want to use one big texture as data container for 3D-vertices, so I can access all the values I need inside a fragment shader (I’m writing some sort of raytracer, that’s why I’m doing some per-vertex calculations in the frag shader).

Does anyone know if the values of the texture will be automatically normalized to 0…1 when accessed from the frag shader? Since I don’t have colors, but vertices, I need common float values!
I know this question probably sounds somewhat stupid and must have been answered somewere, but I googled and used forum search, but haven’t found anything on this topic so far.

Help, please!

If you use a float texture then it’s not normalized to 0…1, the normalization is only for non float textures and colors.

Hi zeoverlord,
thanks for your reply! Great, that means my approch will actually work? :slight_smile:

Just to be sure, with the following code I shoud be able to pass 2 vec3 with full floating point range to a fragment shader via the texture?

	float *texData = new float[2*3];
	texData[0] = 10.0f;	texData[1] = 20.0f;	texData[2] = 30.0f;
	texData[3] = -10.0f;	texData[4] = -20.0f;	texData[5] = -30.0f;
	glBindTexture( GL_TEXTURE_2D, texture[1]);
	glTexImage2D( GL_TEXTURE_2D, 0, 3, 2, 1, 0, GL_RGB, GL_FLOAT, texData);

best regards, Michael

No, in glTexImage2D the parameter currently set to GL_RGB determines the internal format, to get full size floats you need to use GL_RGBA32F_ARB, alternatively GL_RGBA16F_ARB if the former is not available somehow.

The “GL_FLOAT” parameter only determines in what format you are putting into the texture, not what it will end up as.

However if you only need two vec3, might i suggest using texture coords instead, they are really vec4 when you think about it.

Oh, I didn’t realize that I have to use GL_RGBA16F_ARB (I think the precision will be sufficient for my cause), thanks again!

I know I could simply pass the values as tex coords in theory, but I’m already using 8 vec4-varyings in my shader, so I think I don’t have enough varyings left for the additional vertices I need.

A little correction. glTexImage2D takes two format parameters. First one is the internal format. Second is the format of data, that you pass in (texData).
glTexImage2D will perform conversion from your data to internal texture storage.

In many tutorials you’ll find internal format set to 1,2,3 or 4 meaning 1-4 components.
In some tutorials you’ll find GL_RGB, GL_RGBA instead.
The most accurate way is to pass GL_RGB8, GL_RGBA8, GL_RGB32F here, because otherwise, RGB8 texture may be stored internally as RGB4 by the graphics driver - they do that if you’re running in 16bpp desktop mode.

Check out glTexImage2D documentation for more specific information.

If I understand you correct, I have to set the internal format specifier to the intended value. So the following statement should specify a 2D texture with three channels and full floating point range, which would allow me to pass unclamped vec3 to the fragment shader?

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB32F_ARB, <w>, <h>, 0, GL_RGB, GL_FLOAT, <data>);

I read through the OGL 2.1 reference page for glTexImage2D here at, but I didn’t find anything about the appropriate ARB-formats, so I did a forum search and found the statement given above.

I hope I’m not completely on the wrong track now, but if I understood the threads I read correctly, the following should also be correct and working:
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB32F_ARB, <w>, <h>, 0, GL_RGB32F_ARB, GL_FLOAT, <data>);
Is there any difference to my former solution? Does it make any difference how I specify the format of the pixel data?

Yes, this should do.

As for floating-point texture formats, there are 2 extensions defining them:

#define GL_RGB_FLOAT32_ATI 0x8815

#define GL_RGB32F_ARB 0x8815

As you can see, id doesn’t matter if you put GL_RGB_FLOAT32_ATI or GL_RGB32F_ARB in your code - it’s the same and your code will work, no matter which one driver supports :slight_smile:

So, you should actually be looking at these extension specifications. You should find confirmation to whenever floats are clamped by glTexImage2D in these documents.

I use the specific tokens for internal format only. For data I pass in, I only use tokens like GL_RGB, GL_RGBA etc., because there’s one more parameter there, where I pass GL_FLOAT, GL_UNSIGNED_BYTE, GL_HALF_FLOAT and others.

Once again, thank you very much for your answers! :slight_smile:
I hope that tomorrow I will have the time to add this to my code and see if I get it to work.

regards, Michael

It works! :slight_smile:
And the best thing is: Fetching the values from the texture is exactly as fast as using attribute/varying variables (althoug I used only 4 vec3-varyings and now I fetch up to 12 vec3 from the texture).

Thank you all,

(There is no way I can set this query as resolved, isn’t it?)

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