How to generate a float texture?

Hi, now I want to generate a float texture and the code likes the following:

glGenTextures(1, &_iTexture); 
glBindTexture(GL_TEXTURE_2D, _iTexture); 
//read data from a file through the function
//: read_img
float* pData = read_img(cPath, _iWidth, _iHeight); 
//especiall this one 
glTexImage2D(GL_TEXTURE_2D, 0, 1, _iWidth, _iHeight, 0, GL_RED, GL_FLOAT, pData); 

But I cann’t get a correct result. Someone pointed out that there is something wrong with this statement "glTexImage2D(GL_TEXTURE_2D, 0, 1, _iWidth, _iHeight, 0, GL_RED, GL_FLOAT, pData);
". He said that "The third parameter is “internal format”, which can’t be set to “1”. That means you have a GL error. Because you didn’t specify a float internal format, this won’t be a float texture, and therefore you have 8-bit fixed-point values in the texture. "
But I don’t understand why the third parameter cann’t be set to 1.
Any suggestions are appreciated, thank you in advance!

Because if you’d read the spec you’d see that specifying a value of 1, 2, 3 or 4 for that parameter is for backwards compatibility with OpenGL 1.0.


That being said, what are you trying to do?

I still don’t understand the parameter components’s meaning in GlTexImage2D.

void glTexImage2D(GLenum target, GLint level, GLint components, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void* pixel);

If I want to generate a float texture image that each pixel is composed of one component. Then I set the parameter ‘component’ to 1 and parameter ‘type’ to GL_RED(because I only use one component), so I get the following code:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, _iWidth, _iHeight, 0, GL_RED, GL_FLOAT, pData);

are there something wrong with above statement?Thank you!

it’s not GL_RED. if you want just one component - use GL_LUMINANCE!
glTexImage2D(GL_TEXTURE_2D, 0, 1, width, height, 0, GL_LUMINANCE, GL_FLOAT, data);
something like this will generate simple(grayscale) texture with float values :slight_smile:

After setting up such a float texture(all the data are equal to 0.1), I warp it onto a square. If I use the following code to retrieve data from the framebuffer:

glReadPixels(0, 0, _iWidth, _iHeight, GL_LUMINANCE, GL_FLOAT, pImage);

Why the data that I have retrieved are all equal to 0?

can anyone help me to solve this problem?

hm… as I understood the texture is working and set on simple quad, but when you try to readpixel to buffer it’s data equal to 0. (maybe not correctly)
where did you render this quad?
i’m not shure, but reading goes from bottom-left corner of framebuffer, maybe you redered it in upperleft? anyway how do you render it?

I just use the following code to wrap the texture image onto the quad:

glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f);
glTexCoord2f(1, 1); glVertex3f( 1,  1, -0.5f);
glTexCoord2f(0, 1); glVertex3f(-1,  1, -0.5f);

But I cann’t retrieve the correct data :confused:

void TexImage2D( enum target, int level,
int internalformat, sizei width, sizei height,
int border, enum format, enum type, void *data );

type refers to the data representation per color component in memory(not in texture)
format refers to the data structure, rgba, rgba, luminance… in memory
internalformat refers to the representation of the texture components in the resulting texture

So calling TexImage2D with type=GL_FLOAT , format=GL_RGB and internalformat=GL_RGB8, will still result in a 8-bit per component texture.

Try setting your internalformat to GL_RGB16 or GL_LUMINANCE16. It is however not guaranteed that 16 bits will be assigned per component (section 3.8.1 opengl spec)
If you want 32 bit components, take a look at the GL_ATI_texture_float and/or GL_NV_float_buffer extensions at
They provide additional internal format representations.