Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I display multiple separate textures (not multi-texturing) with OpenGL ES 2.0?
    primarykey
    data
    text
    <p>My iOS 4 app uses OpenGL ES 2.0 and renders elements with a single texture. I would like to draw elements using multiple different textures and am having problems getting things to work.</p> <p>I added a variable to my vertex shader to indicate which texture to apply:</p> <pre><code>... attribute float TextureIn; varying float TextureOut; void main(void) { ... TextureOut = TextureIn; } </code></pre> <p>I use that value in the fragment shader to select the texture:</p> <pre><code>... varying lowp float TextureOut; uniform sampler2D Texture0; uniform sampler2D Texture1; void main(void) { if (TextureOut == 1.0) { gl_FragColor = texture2D(Texture1, TexCoordOut); } else // 0 { gl_FragColor = texture2D(Texture0, TexCoordOut); } } </code></pre> <p>Compile shaders:</p> <pre><code>... _texture = glGetAttribLocation(programHandle, "TextureIn"); glEnableVertexAttribArray(_texture); _textureUniform0 = glGetUniformLocation(programHandle, "Texture0"); _textureUniform1 = glGetUniformLocation(programHandle, "Texture1"); </code></pre> <p>Init/Setup:</p> <pre><code>... GLuint _texture; GLuint _textureUniform0; GLuint _textureUniform1; ... glActiveTexture(GL_TEXTURE0); glEnable(GL_TEXTURE_2D); // ? glBindTexture(GL_TEXTURE_2D, _textureUniform0); glUniform1i(_textureUniform0, 0); glActiveTexture(GL_TEXTURE1); glEnable(GL_TEXTURE_2D); // ? glBindTexture(GL_TEXTURE_2D, _textureUniform1); glUniform1i(_textureUniform1, 1); glActiveTexture(GL_TEXTURE0); </code></pre> <p>Render:</p> <pre><code>... glVertexAttribPointer(_texture, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 13)); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, _textureUniform0); glUniform1i(_textureUniform0, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, _textureUniform1); glUniform1i(_textureUniform1, 1); glActiveTexture(GL_TEXTURE0); glDrawElements(GL_TRIANGLES, indicesCountA, GL_UNSIGNED_SHORT, (GLvoid*) (sizeof(GLushort) * 0)); glDrawElements(GL_TRIANGLES, indicesCountB, GL_UNSIGNED_SHORT, (GLvoid*) (sizeof(GLushort) * indicesCountA)); glDrawElements(GL_TRIANGLES, indicesCountC, GL_UNSIGNED_SHORT, (GLvoid*) (sizeof(GLushort) * (indicesCountA + indicesCountB))); </code></pre> <p>My hope was to dynamically apply the texture associated with a vertex but it seems to only recognize GL_TEXTURE0.</p> <p>The only way I have been able to change textures is to associated each texture with GL_TEXTURE0 and then draw:</p> <pre><code>glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, _textureUniformX); glUniform1i(_textureUniformX, 0); glDrawElements(GL_TRIANGLES, indicesCountA, GL_UNSIGNED_SHORT, (GLvoid*) (sizeof(GLushort) * 0)); ... </code></pre> <p>In order to render all the textures, I would need a separate glDrawElements() call for each texture, and I have read that glDrawElements() calls are a big hit to performance and the number of calls should be minimized. Thats why I was trying to dynamically specifiy which texture to use for each vertex.</p> <p>It's entirely possible that my understanding is wrong or I am missing something important. I'm still new to OpenGL and the more I learn the more I feel I have more to learn.</p> <p>It must be possible to use textures other than just GL_TEXTURE0 but I have yet to figure out how.</p> <p>Any guidance or direction would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload