javaopengltextureslwjglmultitexturing

LWJGL Multi texturing not working


I'm currently learning some OpenGL and I want to have 2 different textures (diffuse and specular) applied to a cube. The problem is that only the first texture (of texture unit 0) is accessible; both samplers seem to use it, instead of using two different ones.

Even if i assign both sampler uniforms to 1, the first (diffuse texture) is used. I presume I must have overlooked something.

The relevant part of my init() looks like this:

texture = Texture.loadTextureFromFile("res/texture/stone_07_diffuse.jpg");
textureSpecular = Texture.loadTextureFromFile("res/texture/stone_07_specular.jpg");

shaderProgram = new ShaderProgram();
shaderProgram.attachVertexShader("res/shader/LightTest.vsh");
shaderProgram.attachFragmentShader("res/shader/LightTest.fsh");
shaderProgram.link()

ShaderProgram.bind(shaderProgram);
{
    shaderProgram.setUniform("materialDiffuseTexture", 0);
    shaderProgram.setUniform("materialSpecularTexture", 1);
    ...
}
ShaderProgram.unbind();

Texture.setActiveTextureUnit(0);
Texture.bind(texture);

Texture.setActiveTextureUnit(1);
Texture.bind(textureSpecular);

The relevant part of the render() method looks like the following:

ShaderProgram.bind(shaderProgram);
{
    //set other uniforms as projectionMatrix, viewMatrix, etc.
    ...

    glBindVertexArray(vaoID);
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, 0);
    glBindVertexArray(0);
}
ShaderProgram.unbind();

texture, textureSpecular, shaderProgram and vaoID are global variables. The uniforms declared in the fragment shader:

uniform sampler2D materialDiffuseTexture;
uniform sampler2D materialSpecularTexture;

What do I do wrong?


Solution

  • Oh my god, I feel so stupid right now. The problem was that I was using the setUniform(String, float...) method of my ShaderProgram class which called glUniform1f(int, float).

    But since the sampler2D uniform holds an integer I have to use glUniform1i(int, int) to assign the texture unit. Now it works!