Communications via Textures

Started by
6 comments, last by ic0de 11 years, 4 months ago
I am writing a game using C++ and OpenGL, which includes an automatic world generator which has to draw a floor containing some holes. Initially, I made the generator simply divide the floor into a lot of quads, and leave out the ones where there are holes. But I have some efficiency issues, and I would like to do it a different way.

Basically, my plan is to generate a "template texture", which would only contain the alpha component, and wherever there are holes, I would make the alpha component 0, to make it transparent. Of course, there is also the (smaller, but repeating) floor texture that contains color. I want to make my fragment shader draw the normal texture on, but also look at the template texture to for the alpha channel.

I cannot find any way of sending the two textures. Does anyone know how I could do this?
EDIT: To be more clear, I know that calling glBindTexture() with GL_TEXTURE_2D and the main floor texture and then using sampler2D works, but what about the other texture?

Thanks in advance.
Advertisement
glActiveTexture seems to be what you want.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I don't really understand what that function actually does. What I need to do is for example set a uniform variable to be a certain texture and then sample that texture (the template texture), along with the main color texture. Is this possible?
That function determines which texture unit or slot that glBindTexture will bind to. By default, the active texture unit is 0, and "simple" applications that only use a single texture don't ever have to call glActiveTexture. But if you want to bind more than one texture, then you need to call glActiveTexture. Calling with 0 will make the first (default) slot active, and glBindTexture binds to it. Then for the second texture, you call glActiveTexture with 1. (Note that you should use the GL-defined constants GL_TEXTURE0, GL_TEXTURE1, etc...) Then you bind another texture to that slot. And so forth.
Thanks. So when I do that, how do I access each individual texture from a GLSL fragment shader?

Thanks. So when I do that, how do I access each individual texture from a GLSL fragment shader?


you will have two uniform sampler2Ds in your fragment program and you will access each with the stander texture2D function the only difference is that you will have two uniforms instead of one. You will send textures to your shader like so:

//Sample code used for normal mapping
glActiveTexture(GL_TEXTURE0);
glUniform1i(texture_location, 0);
glBindTexture(GL_TEXTURE_2D, dTex);


glActiveTexture(GL_TEXTURE1);
glUniform1i(normal_texture_location, 1);
glBindTexture(GL_TEXTURE_2D, dNorm);

glActiveTexture(GL_TEXTURE2);
glUniform1i(height_texture_loc, 2);
glBindTexture(GL_TEXTURE_2D, dHeight);


and use them like so


//simple multiplicative blending example
varying vec2 texCoord;

uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D texture3;

void main()
{
vec4 sample1 = texture2D(texture1, texCoord);
vec4 sample2 = texture2D(texture2, texCoord);
vec4 sample3 = texture2D(texture3, texCoord);

gl_FragColor = sample1 * sample2 * sample3;
}
Thanks you very much!

Just to be clear, the texture_location, normal_texture_location and height_texture_loc are the locations of 'sampler2D' uniforms, right?
EDIT: Sorry, posted this before seeing your answer.

Just to be clear, the texture_location, normal_texture_location and height_texture_loc are the locations of 'sampler2D' uniforms, right?


thats it exactly

This topic is closed to new replies.

Advertisement