So i was wondering if this is recreatable?
While i was debugging a java opengl project i found a shader :
#version 420 core
uniform sampler2D texture1;
uniform sampler2D texture2;
in vec2 uv;
out vec4 fragColor;
void main(){
//fragColor = texture(texture1, uv);
fragColor = texture(texture2, uv);
}
looks simple right but now when I uncommand the //fragColor = texture(texture1, uv) and keep the rest I get the texture1 rendered to the screen. WHY ? my brains says that that's not right, shouldn't it just render texture2 because I override fragColor? IDK can somebody explain this?
UPDATE 1: I believe its a problem with glsl compilation.
Is it possible to bind a texture to sampler1 when there is not texture bound to sampler0
UPDATE 2: creating the texture: in my case its just a texture with 1 sample so TEXTURE_2D and its format is .png so 4 channels and there is no interpolation applied
texType = samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
int format;
if (channels == 3) {
format = GL_RGB;
} else if (channels == 4) {
format = GL_RGBA;
} else {
throw new AspectGraphicsException("textures can't be initialized with " + channels + " channels");
}
ID = glGenTextures();
glBindTexture(texType, ID);
glTexParameteri(texType,
GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(texType,
GL_TEXTURE_MAG_FILTER, interpolation ? GL_LINEAR : GL_NEAREST);
glTexParameteri(texType, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(texType, GL_TEXTURE_WRAP_T, GL_REPEAT);
if (samples > 1) {
if (pixels == null) {
glTexImage2DMultisample(texType, samples, format,
width, height, true);
} else {
throw new AspectGraphicsException("textures with defined with pixels can't be multisampled");
}
} else {
if (pixels == null) {
glTexImage2D(texType, 0, format, width, height,
0, format, GL_UNSIGNED_BYTE, NULL);
} else {
glTexImage2D(texType, 0, format,
width, height, 0, format,
GL_UNSIGNED_BYTE, pixels);
}
}
glBindTexture(texType, 0);
binding the texture: texType is just GL_TEXTURE_2D and samplerName is "texture1" or "texture2" (see in the glsl shader) and the sampler is just for "texture1":0 and for "texture2":1
glActiveTexture(GL_TEXTURE0 + sampler);
glBindTexture(texType, ID);
shader.uniform1i(samplerName, sampler);
It's most likely that you didn't assign a texture unit to your sampler uniforms, so they were both set to point to GL_TEXTURE0
. You can specify it in the shader like so:
#version 420 core
layout(binding=0) uniform sampler2D texture1;
layout(binding=1) uniform sampler2D texture2;
// ...
Then you bind the textures with:
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, your_texture);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, other_texture);
glDrawArrays(...);
If done this way, you'll get the right result irrespectively of what uniforms are left out.