It looks like you're new here. If you want to get involved, click one of these buttons!
...
attribute float TextureIn;
varying float TextureOut;
void main(void)
{
...
TextureOut = TextureIn;
}
I use that value in the fragment shader to select the texture:
...
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);
}
}
Compile shaders:
...
_texture = glGetAttribLocation(programHandle, "TextureIn");
glEnableVertexAttribArray(_texture);
_textureUniform0 = glGetUniformLocation(programHandle, "Texture0");
_textureUniform1 = glGetUniformLocation(programHandle, "Texture1");
Init/Setup:
...
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);
Render:
...
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)));
My hope was to dynamically apply the texture associated with a vertex but it seems to only recognize GL_TEXTURE0.glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _textureUniformX);
glUniform1i(_textureUniformX, 0);
glDrawElements(GL_TRIANGLES, indicesCountA, GL_UNSIGNED_SHORT, (GLvoid*) (sizeof(GLushort) * 0));
...
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.
Replies
It's true that glDrawElements is expensive and you want to minimize the number of calls you make to it. However, if you have 3 textures to render, there's nothing wrong with setting a texture, calling glDrawElements, setting another texture, drawing with that, etc. You just don't want to call glDrawElements dozens of times in a rendering pas if you can avoid it.
I don't think the way you're trying to switch textures will work. You need to bind a texture with a call to glBindTexture, draw with it, then bind the next texture and draw with that.
I could be wrong though. When working with OpenGL I find something that works, make sure it's performance is satisfactory, and then move on. I'm hardly an expert on all the different ways you can use it.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI have 7 textures, and 3 calls to glDrawElements which I could make into 2. I have a different rotation to apply so I need 2 separate calls. Would I need to call glDrawElements for each texture in each element?
(7 textures x 2 elements = 14 calls)
I'll be the first to admit I don't know much, but I thought you could specify multiple textures and draw them all at the same time instead of separately.
GL_TEXTURE0 goes up to GL_TEXTURE31 but I can't seem to figure out how to use more than one at a time.
Also, eventually I would like to select different areas from my 7 textures to have dozens or hundreds of different textures, but everything will come from the same 7 textures. Read about texture atlases and once I got things working with the 7 base textures I would look into atlases.
You've given me good advice in the past, even if it takes me a while to figure that out.
Thanks for your input.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI found the one missing line of code I forgot to put in: Not going to make much progress if I don't actually set up the texture data in my vertex array.
Now on to the next hair-pulling dilema, texture atlases I think.
Thanks
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeBTW, there's no reason to use a float as your flag to indicate textures.
I have a flag in one of my shaders that determines if it draws with textures or solid colors. It's declared as a GLuint in my C code, and as type "bool" in the shader. In my C code, I just set the value to 0 for false and 1 for true.
You could use "bool" type inside your shader and switch between textures, or make it an "int" inside your shader and support a variety of different textures.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeDuncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeLittle did I know that it was actually the cause of a problem that was a MAJOR hassle to track down. Apparently lowp is REALLY low precision with a range of -2 to 2.
Of course my textures ranged outside of that and I just couldn't figure out how hard it could possibly be to handle numbers up to 6.
Upgraded to mediump and now it works wonderfully (for now). Whew!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeDuncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome