简体   繁体   中英

GLSL - Two textures (Not mixed)

I'm working with textures with GLSL. How can I handle two textures with GLSL? One guy recommended me to do two samplers2D in my GLSL... But how can GLSL knows which samplers2D should use? (I am not talking about mixing the textures...)

I heard that I should use glBindTexture. How Can I do this? With glBindTexture? Anyone has an example of this?

openGL 3.3


Edit:

I have this:

uniform Sampler2D texture1;
uniform Sampler2D texture2;

I need to draw two objects, using textures, so how can GLSL know if he should use texture1 or texture2 according to the object I want to draw. That's my question.

You need to bind each texture to a different texture unit, and then use multitexture coords. It would look something like this (assuming you already have the textures) :

glActiveTexture (GL_TEXTURE0);  // First texture is going into texture unit 0
glBindTexture (GL_TEXTURE_2D, tex0);
glEnable (GL_TEXTURE_2D);

glActiveTexture (GL_TEXTURE1);  // Second texture is going into texture unit 1
glBindTexture (GL_TEXTURE2D, tex1);
glEnable (GL_TEXTURE_2D);

glUseProgram (yourGLSLProgramID);
glUniform1i (sampler1Location, 0); // tell glsl that sampler 1 is sampling the texture in texture unit 0
glUniform1i (sampler2Location, 1); // tell glsl that sampler 2 is sampling the texture in texture unit 1
///... set the rest of your uniforms...

glBegin (GL_QUADS);
    glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0);
    glVertexCoord2f(0.0, 0.0);

    glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0, 0.0);
    glVertexCoord2f(width, 0.0);

    glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0, 1.0);
    glVertexCoord2f(width, height);

    glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0);
    glVertexCoord2f(0.0, height);
glEnd();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM