Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How can I enable more than one texture in OpenGL ES 2 so that I can sample from all of them in my shader? For example, I'm trying to read from two different textures in my shader for the player's car.

This is how I'm currently dealing with the texture for my car:

glBindTexture(GL_TEXTURE_2D, this->texture2DObj);
glUniform1i(1, 0);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);

int offset = 0;
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, this->vertexBufferSize,(const void *)offset);

offset += 3 * sizeof(GLfloat);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, this->vertexBufferSize, (const void*)offset);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
glDrawElements(GL_TRIANGLES, this->indexBufferSize, GL_UNSIGNED_SHORT, 0);
share|improve this question
It sounds like you are fundamentally asking "how can I sample from multiple textures in OpenGL ES 2?" Is that correct? –  Josh Petrie Mar 26 '14 at 16:13
yeah,you are right! i try to load multi texture but it won't show second primitive just one,how to do that? –  Peyman Tahghighi Mar 26 '14 at 17:01
Are you sure that shader uniform slot 1 is your sampler2D? There is a way to query the shader for this information, and you probably should. –  Brent Mar 26 '14 at 20:55

2 Answers 2

In brief, you want code like this:

glBindTexture(GL_TEXTURE_2D, this->texture2DObj);
glBindTexture(GL_TEXTURE_2D, this->otherTexture2DObj);
GLint samplerArrayLoc = glGetUniformLocation(shaderObject, "texture");
const GLint samplers[2] = {0,1}; // we've bound our textures in textures 0 and 1.
glUniform1iv( samplerArrayLoc, 2, samplers );

You bind your textures into two different texture slots (in this case, GL_TEXTURE0 and GL_TEXTURE1), then set your shader uniforms to tell the shader which texture slots to use for their texture lookups.

In this code, I assume that your shader will receive these samplers as uniform sampler2D texture[2]; You may alternately have them as separate uniform variables rather than a single uniform array, in which case you'd set their values separately, instead of in a single call.

Then your shader can just read from either texture[0] or texture[1] to sample from either of the two textures.

And that's pretty much all there is to it!

share|improve this answer
(in practice, you probably want to call glGetUniformLocation() during setup and store the location somewhere for use in this code. No need to go searching for the shader variable every time you're going to draw; it's not going to move around in between calls to a single shader) –  Trevor Powell Mar 27 '14 at 2:04
Thanks! i got to different function for render two texture,i do everything as you said but still second one shows! i think that second primitive not shown at all apart from texture in it! if you need further information tell me. –  Peyman Tahghighi Mar 27 '14 at 6:55
and one more thing in fragmentshader when i simple draw for example a red triangle it won't draw two triangle only one! –  Peyman Tahghighi Mar 27 '14 at 7:11

You need to fist query the sampler uniform locations with glGetUniformLocation, then set the sampler to the same texture unit that you've enabled with glActiveTexture. Thus, some sample code for this would look like:

mySamplerLoc = glGetUniformLocation(programId, "mySampler");
glUniform1i(mySamplerLoc, 0);
glBindTexture(GL_TEXTURE_2D, textureId);

And repeat that for as may textures as you like, replacing the 0 and GL_TEXTURE0 by the corresponding texture unit you wish to bing the texture to.

And of cource, make sure you add the uniform variable declarations in the shader:

uniform sampler2D mySampler;
void main()
    vec4 color = texture(mySampler, texCoords);

Noting that you should avoid querying the shader uniform locations every time. Ideally, query them once with glGetUniformLocation() and store the result for further use.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.