Hello! I am trying to implement a simple batchrenderer. However, the sampler2d array uses the texture bound to GL_TEXTURE0
. It doesn't use textures bound to other units. I would really appreciate if anyone can help me out. here's my code
main.cpp
shader.use()
GLfloat t[2] = { 0.0f, 1.0f };
GLint l = glGetUniformLocation(shader.getID(), "u_texture");
glUniform1fv(l, 2, t);
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture1); //all sprites use this texture
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture2);
fragment.glsl
#version 400 core
in float tex;
in vec2 texCoords;
in vec4 color;
in vec3 pos;
uniform sampler2D u_texture[2];
out vec4 FragColor;
void main()
{
int t = int(tex);
FragColor = texture(u_texture[t], texCoords);
}
Edit: thanks! it worked when i used int values and glUniform1iv. here's my updated code:
main.cpp
shader.use()
GLint t[2] = { 0, 1 };
GLint l = glGetUniformLocation(shader.getID(), "u_texture");
glUniform1iv(l, 2, t);
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture2);