I'm trying to create a tonemap fragment shader, to map values from [0, someint]
to RGB values, but when I try to use GLints, the texture that gets rendered is all 0s.
Here's the relevant texture creation and binding code:
std::array<GLint , 25 > pixels;
int i=0;
float di = 255.0f / 25;
std::generate(pixels.begin(), pixels.end(), [&]{return di * i++;});
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, 5,5 ,0,
GL_RED_INTEGER, GL_INT, pixels.data());
and a fragment shader of:
#version 330
in vec2 Texcoord;
out vec4 outColor;
uniform isampler2D tex;
void main()
{
ivec4 val = texture(tex, Texcoord);
float ret = 0;
if(val.r == 0)
{
ret = 1;
}
outColor = vec4(ret,0,0, 1.0);
}
Which is applied to a basic 2 triangles UV textured from [0,1]
The expected result would be one red corner and the rest black, but the entire square is red, signifying the entire texture is 0.
When I try the experiment with
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 5, 5 ,0, GL_RED, GL_FLOAT, pixels.data());
and a sampler2D
, the shader behaves as expected, but my values are clamped to [0,1]
. Are the enums mismatched? glGetError
returns no error, but it might be silently failing in some other way?
I'm using GLFW and OpenGL 3.3 on Ubuntu, not sure if GL_R32I
is supported with that version?
Are you setting GL_TEXTURE_MIN_FILTER
to be anything but GL_*_MIPMAP_*
? The initial value is GL_NEAREST_MIPMAP_LINEAR
.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
If not then the texture won't work because you only have a single mipmap level defined. Thus the texture being incomplete.
By default the highest defined mipmap level is 1000
.
You can set it to 0
and that would equally work.
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)
The max level is a zero-based index.