XiaJun
XiaJun

Reputation: 1915

the internalformat of Texture

Look at the following OpenGL function:

void glTexImage2D(GLenum    target,
                  GLint     level,
                  GLint     internalFormat,
                  GLsizei   width,
                  GLsizei   height,
                  GLint     border,
                  GLenum    format,
                  GLenum    type,
                  const GLvoid * data);

I know the parameter format and type describe the format and type of the image data,but I don't understand the prameter internalFormat.How should I set its value in my application?

For example,I create a texture like this:

glTexImage2D(GL_TEXTURE_2D,0,GL_LUMINANCE8,size,size,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,buffer);

When I aceess the texture in it in my GLSL shader,it seems that the value that I get is between [0,1].WHy?Shouldn't it between [0,255]?

Part of My shader code is :

vec = EntryPoint + delta_dir * texture(noiseTex,EntryPoint.xy * 32).x;

Part of my C++ Code :

for (int i = 0;i < temp;++i)
    {
        buffer[i] = 255.0 * rand() / (float)RAND_MAX;
    }
    glGenTextures(1,&noiseTex);
    glActiveTexture(GL_TEXTURE0 + activeTexUnit);
    glBindTexture(GL_TEXTURE_2D,noiseTex);
    glTexImage2D(GL_TEXTURE_2D,0,GL_LUMINANCE8,size,size,
                            0,GL_LUMINANCE,GL_UNSIGNED_BYTE,buffer);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

Upvotes: 4

Views: 9760

Answers (2)

Nicol Bolas
Nicol Bolas

Reputation: 473322

The format and type parameters describe the data you are passing to OpenGL as part of a pixel transfer operation. The internalformat describes the format of the texture. You're telling OpenGL that you're giving it takes that looks like X, and OpenGL is to store it in a texture where the data is Y. The internalformat is "Y".

The GL_LUMINANCE8 internal format represents a normalized unsigned integer format. This means that the data is conceptually floating-point, but stored in a normalized integer form as a means of compression.

For that matter, the format of GL_LUMINANCE says that you're passing either floating-point data or normalized integer data (the type says that it's normalized integer data). Of course, since there's no GL_LUMINANCE_INTEGER (which is how you say that you're passing integer data, to be used with integer internal formats), you can't really use luminance data like this.

Use GL_RED_INTEGER for the format and GL_R8UI for the internal format if you really want 8-bit unsigned integers in your texture. Note that integer texture support requires OpenGL 3.x-class hardware.

That being said, you cannot use sampler2D with an integer texture. If you are using a texture that uses an unsigned integer texture format, you must use usampler2D.

Upvotes: 20

JDischler
JDischler

Reputation: 301

How the value is stored internally is not necessarily relevant to how you would access it in GLSL. Using normalised colour values (0-1) is much easier in practice. Is there some reason you want to manipulate pixel values in your pixel shaders in the range of (0-255)?

Upvotes: 1

Related Questions