-1

I wrote a little program to display a 32bit float texture in a simple quad. When displaying the quad, the texture color is always black. I experimented with a lot of things, but I couldn't make it work. I'm really at loss what the problem with it.

The code of creating the OpenGL texture goes like this

glEnable(GL_TEXTURE_2D);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, textureData);

Using the debugger, there's no error in any of these calls. I also examined the textureData pointer, and got the expected results (in my simplified program, it is just a gradient texture).

This is the vertex shader code in GLSL:

#version 400
in vec4 vertexPosition;
out vec2 uv;
void main() {
    gl_Position = vertexPosition;
    uv.x = (vertexPosition.x + 1.0) / 2;
    uv.y = (vertexPosition.y + 1.0) / 2;
}

It's kind of a simple generation of the UV coordinates without taking them as vertex attributes. The corresponding vertex buffer object is really simple:

GLfloat vertices[4][4] = {
    { -1.0, 1.0, 0.0, 1.0 },
    { -1.0, -1.0, 0.0, 1.0 },
    { 1.0, 1.0, 0.0, 1.0 },
    { 1.0, -1.0, 0.0, 1.0 },
};

I've tested the solution, and it displays the quad covering the entire window as I wanted to. Displaying the UV coordinates in the fragment shader reproduce the gradient that I expected to get. Now here's the fragment shader:

#version 400
uniform sampler2D myTex;
in vec2 uv;
out vec4 fragColor;

void main() {
   fragColor = texture(myTex, uv);
   // fragColor += vec4(uv.x, uv.y, 0, 1);
}

The commented out line displays the UV coordinates as color for debugging purposes. What do I do wrong here? I just can't see why the texture() call returns 0 where the texture seems completely right, and the uv coordinates are also proper. I link here the full code if there's something else I do wrong: gl-view.c

EDIT: This is how I set up the myTex sampler:

glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glUniform1i(glGetUniformLocation(shaderProgram, "myTex"), 0);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

EDIT: Cleared up the vertex shader code.

1 Answers1

0

I've found the issue: I didn't set any MAG or MIN filter on the texture. Setting the MIN filter to GL_NEAREST solved the problem.