0
votes

I want to load 2 texture at the same time into my fragment shader:

in vec2 TexCoord;
layout (location = 0) out vec4 FragColor;

uniform sampler2D tex1;
uniform sampler2D tex2;

void main() {
    FragColor = mix(texture(tex1, TexCoord),texture(tex2, TexCoord), 0.3);
}

I'm using SOIL library to do this:

GLuint tex1 = SOIL_load_OGL_texture("rock.bmp", SOIL_LOAD_AUTO,
                                SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
GLuint tex2 = SOIL_load_OGL_texture("lena512.bmp", SOIL_LOAD_AUTO,
                                SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);

However only the second texture is showing up, not the first one. I realize that I need to perform glUniform1i somewhere attaching sampler2D tex1 to the first one and vice versa. How do I fix the problem?

1

1 Answers

1
votes

This is nearly impossible to explain because of the mix operation. If you want to debug this properly, remove that and try with FragColor = texture (tex1, TexCoord); and FragColor = texture (tex2, TexCoord);. Whichever of those two variations produces a solid color, that's the texture unit that doesn't have anything bound to it.

My guess is that the uniform tex1 (0 by default) has the second texture you loaded.

SOIL will bind the texture to the currently active texture unit, so consider something like this instead:

glActiveTexture (GL_TEXTURE0);
GLuint tex1 = SOIL_load_OGL_texture("rock.bmp", SOIL_LOAD_AUTO,
                                    SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);

glActiveTexture (GL_TEXTURE1);
GLuint tex2 = SOIL_load_OGL_texture("lena512.bmp", SOIL_LOAD_AUTO,
                                    SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);

Now tex1 will be bound to 0 and tex2 will be bound to 1. Also consider using different names for textures and samplers, this whole thing is confusing because it's never clear whether you're talking about tex1 in the C code or tex1 in the GLSL code ;)