   multitexturing... order matters?
  Hi all! This is my first post!I have this problem. Actually, I it's solved but I don't really like the solution. I must be doing something wrong so I'll be glad if you can enlight me.Here we go. It's simple. I'm using GLSL. I have two textures and I want to sort of blend them in a rectangle. Texture1 is fixed in the background while Texture2 moves with the mouse.This is my program:// begin rendering    glEnable(GL_TEXTURE_2D);    glDisable(GL_LIGHTING);    // ...    // calculate texture #1 offsets (X0, Y0) is the lower left corner    // and (X1, Y1) is the upper right.    float X0 = ...    float Y0 = ...    float X1 = ...    float Y1 = ...    // calculate texture #2 offset based on mouse position    float U0 = ... mouseXY_x... blah blah...    float V0 = ... mouseXY_y... blah blah...    float U1 = ... mouseXY_x... blah blah...    float V1 = ... mouseXY_y... blah blah...    // activate textures    glActiveTexture(GL_TEXTURE0);    glBindTexture(GL_TEXTURE_2D, texture1_id);    glActiveTexture(GL_TEXTURE1);    glBindTexture(GL_TEXTURE_2D, texture2_id);    // activate shader    activate_shader();    // initialize the necessary uniform variables    texLoc = glGetUniformLocation(programObj, "texture1");    glUniform1i(texLoc, 0);    texLoc = glGetUniformLocation(programObj, "texture2");    glUniform1i(texLoc, 1);    // draw the rectangle    glColor3f(1.0, 1.0, 1.0);    glBegin(GL_POLYGON);   glMultiTexCoord2f(GL_TEXTURE0, X0, Y0);   glMultiTexCoord2f(GL_TEXTURE1, U0, V0);   glVertex2f(-0.5, -0.5);   glMultiTexCoord2f(GL_TEXTURE0, X0, Y1);   glMultiTexCoord2f(GL_TEXTURE1, U0, V1);   glVertex2f(-0.5, +0.5);   glMultiTexCoord2f(GL_TEXTURE0, X1, Y1);   glMultiTexCoord2f(GL_TEXTURE1, U1, V1);   glVertex2f(+0.5, +0.5);   glMultiTexCoord2f(GL_TEXTURE0, X1, Y0);   glMultiTexCoord2f(GL_TEXTURE1, U1, V0);   glVertex2f(+0.5, -0.5);    glEnd();    // deactivate shader    deactivate_shader();    glEnable(GL_LIGHTING);    glDisable(GL_TEXTURE_2D);    glutSwapBuffers();// end rendering procMy fragment shader is like this:// begin fragment shaderuniform sampler2D texture1;uniform sampler2D texture2;varying vec2 texCoord1;varying vec2 texCoord2;void main(void){   vec3 color = texture2D(texture2, texCoord2).rgb;   gl_FragColor = vec4(color, 1.0);}// end fragment shaderYeah, you are right... I'm just rendering texture2 and that's the problem. When I move the mouse the texture seems to blend magically with some colors. I suspect that those colors come from texture1. Very weird!!!I told you that I've solved the problem. How? I changed the order of texture activation, like this:    glActiveTexture(GL_TEXTURE1);    glBindTexture(GL_TEXTURE_2D, texture2_id);    glActiveTexture(GL_TEXTURE0);    glBindTexture(GL_TEXTURE_2D, texture1_id);And now it works fine!!! ZOMG WHY???   
  #1 There are a few comments I can make. I don't know if you are already using a vertex shader or not, but always write a vertex shader as well.#2glEnable(GL_TEXTURE_2D);glDisable(GL_LIGHTING);have no effect since you are using shaders.#3texLoc = glGetUniformLocation(programObj, "texture1");glUniform1i(texLoc, 0);texLoc might be -1 since you aren't using texture1, in other words, it is invalid.#4perhaps there is a bug in the driver. I don't know.   