0
\$\begingroup\$

I've written a shader that uses three different textures, tex0, tex1, and tex2. I can load it into Slick and successfully display it, but I can't figure out how to set the three different textures. Whichever texture I last load or set is the one used for all three.

My code:

if (sp == null) {
    try {
        sp = ShaderProgram.loadProgram("passthrough.vert", "planet.frag");
        planet = TextureLoader.getTexture("png", new FileInputStream("earth.png"));
        clouds = TextureLoader.getTexture("png", new FileInputStream("clouds.png"));
        lights = TextureLoader.getTexture("png", new FileInputStream("lights.png"));
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}

sp.bind();

// Attempt to set tex0-tex2. This fails and just sets everything to tex2.
glUniform1i(sp.getUniform1i("tex0"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, planet.getTextureID());

glUniform1i(sp.getUniform1i("tex1"), 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, clouds.getTextureID());

glUniform1i(sp.getUniform1i("tex2"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, lights.getTextureID());

sp.setUniform1f("time", tick * 0.03f);
sp.setUniform2f("resolution", 400f, 400f);

// Render a quad. This works, except that the textures are all the same.
glBegin(GL_QUADS);
glVertex2i(0, 0);
glVertex2i(0, 400);
glVertex2i(400, 400);
glVertex2i(400, 0);
glEnd();
\$\endgroup\$
1
\$\begingroup\$

OK, it turns out that instead of

glUniform1i(sp.getUniform1i("tex0"), 0);

I should be using eg

sp.setUniform1i("tex0", 0);
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.