Jump to content

  • Log In with Google      Sign In   
  • Create Account


Communications via Textures


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Madd   Members   -  Reputation: 166

Like
0Likes
Like

Posted 15 December 2012 - 07:51 AM

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.

Edited by Madd, 15 December 2012 - 08:10 AM.


Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 7867

Like
3Likes
Like

Posted 15 December 2012 - 08:31 AM

glActiveTexture seems to be what you want.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#3 Madd   Members   -  Reputation: 166

Like
0Likes
Like

Posted 15 December 2012 - 08:41 AM

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?

Edited by Madd, 15 December 2012 - 08:44 AM.


#4 FLeBlanc   Crossbones+   -  Reputation: 3101

Like
2Likes
Like

Posted 15 December 2012 - 09:17 AM

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.

#5 Madd   Members   -  Reputation: 166

Like
0Likes
Like

Posted 15 December 2012 - 09:22 AM

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

#6 ic0de   Members   -  Reputation: 839

Like
2Likes
Like

Posted 15 December 2012 - 09:44 AM

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;
}

Edited by ic0de, 15 December 2012 - 09:50 AM.

you know you program too much when you start ending sentences with semicolons;


#7 Madd   Members   -  Reputation: 166

Like
0Likes
Like

Posted 15 December 2012 - 09:55 AM

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.

Edited by Madd, 15 December 2012 - 09:56 AM.


#8 ic0de   Members   -  Reputation: 839

Like
1Likes
Like

Posted 15 December 2012 - 10:47 AM

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


thats it exactly

you know you program too much when you start ending sentences with semicolons;





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS