Search code examples
javaopengllwjgl

OpenGL Texture Shader not Working


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);
}

Solution

  • You need to specify the uniform for textureSampler. Ie,

    glUniform1i(textureSamplerHandle, 0);
    

    Which you have retrieved from

    textureSamplerHandle = glGetUniformLocation(shaderProgram, "textureSampler");