I am new to OpenGL, and trying to learn ES 2.0.
To start with, I am working on a card game, where I need to render multiple card images. I followed this http://www.l
Your code mixes up two concepts: texture ids (or, as they are called in the official OpenGL documentation, texture names), and texture units:
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, ...)
. The guaranteed minimum for compliant ES 2.0 implementations is 8.You're using texture ids correctly while creating your textures, by generating an id with glGenTextures()
, binding it with glBindTexture()
, and then setting up the texture.
The problem is where you set up the textures for drawing:
GLES20.glUniform1i(u_texture, ms.getTextureId());
The value of the sampler uniform is not a texture id, it is the index of a texture unit. You then need to bind the texture you want to use to the texture unit you specify.
Using texture unit 0, the correct code looks like this:
GLES20.glUniform1i(u_texture, 0);
GLES20.glActiveTexture(GL_TEXTURE0);
GLES20.glBindTexture(ms.getTextureId());
A few remarks on this code sequence:
0
), while the argument of glActiveTexture()
is the corresponding enum (GL_TEXTURE0
). That's because... it was defined that way. Unfortunate API design, IMHO, but you just need to be aware of it.glBindTexture()
binds the texture to the currently active texture unit, so it needs to come after glActiveTexture()
.glActiveTexture()
call is not really needed if you only ever use one texture. GL_TEXTURE0
is the default value. I put it there to illustrate how the connection between texture unit and texture id is established.Multiple texture units are used if you want to sample multiple textures in the same shader.