Why does array of sampler2d uses first bound texture

Started by
3 comments, last by DJROXZHIA 2 years ago

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);
Advertisement

Test the texture2 (GL_TEXTURE1) hard coding it in the fragment shader (for test purpose only).

void main()
{
	//int t = int(tex);
	//FragColor = texture(u_texture[t], texCoords);
	FragColor = texture(u_texture[1], texCoords);	// testing texture2 (GL_TEXTURE1)
}

If it correctly uses texture2, undo the test and check the value of tex coming from Vertex Shader.

in float tex;

you can hard code it in the Vertex Shader

tex = 1.0;  // Testing texture2 (GL_TEXTURE1)

Just try to narrow where the problem come from.

Side question:
why do you use float values and glUniform1fv, instead of int values and glUniform1iv?

None

You should be calling glGetError() and asserting that it's 0 after every GL call. Wrap it in some macro that you can define out for release builds.

Is it possible that the uniform doesn't like float values, and you should be using glUniform1iv() instead?

Also, the standard debugging applies; Bind texture1 as all-red and texture2 as all-green and see what happens, etc.

enum Bool { True, False, FileNotFound };

@Gotanod , @hplus0603 thanks! it worked when i used GLint values and glUniform1iv

This topic is closed to new replies.

Advertisement