0

I am developing a program in OpenGl. My curent setup includes just one quad and a texture I want to render on it. The problem is that the texture doesn't render on to the quad. If I change the fragment shader to render a color it renders correctly.

Here is the render code:

glfwSwapBuffers(window);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glUseProgram(programID);

glBindVertexArray(vaoID);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glDrawElements(GL_TRIANGLES, indicies.length, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindVertexArray(0);

glUseProgram(0);

Vertex shader:

#version 400 core

layout(location = 0) in vec3 position;
layout(location = 1) in vec2 textureCoords;

out vec2 pass_TextureCoords;

void main() {
    gl_Position = vec4(position, 1.0);
    pass_TextureCoords = textureCoords;
}

Fragmen shader:

#version 400 core

in vec2 pass_textureCoords;

out vec4 out_Color;

uniform sampler2D textureSampler;

void main() {
    out_Color = texture(textureSampler, pass_textureCoords);
}
1
  • Where's the code for setting up the vertex buffer? Commented Aug 20, 2015 at 9:44

1 Answer 1

0

You need to specify the uniform for textureSampler. Ie,

glUniform1i(textureSamplerHandle, 0);

Which you have retrieved from

textureSamplerHandle = glGetUniformLocation(shaderProgram, "textureSampler");
Sign up to request clarification or add additional context in comments.

2 Comments

While generally a good idea, this is not necessary for the simple example. Sampler uniforms have a default value of 0, and the texture is bound to texture unit 0. So the default value is correct.
My answer is incorrect, even, as I state "1" should be used when the example is using TEXTURE0. Corrected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.