title: How can I pass multiple textures to a single shader?
body: <p>I am using freeglut, GLEW <a href="http://r3dux.org/2011/09/simple-texture-loading-with-devil-revisited/">and DevIL</a> to <a href="http://www.lighthouse3d.com/tutorials/glsl-tutorial/combine-texture-fragment/">render a textured teapot</a> using a vertex and fragment shader. This is all working fine in OpenGL 2.0 and GLSL 1.2 on Ubuntu 14.04.</p>
  
  <p>Now, I want to apply a bump map to the teapot. My lecturer evidently doesn't brew his own tea, and so doesn't know they're supposed to be <a href="https://en.wikipedia.org/wiki/File:Original_Utah_Teapot.jpg">smooth</a>. Anyway, I found <a href="http://content.gpwiki.org/index.php/OpenGL:Tutorials:GLSL_Bump_Mapping">a nice-looking tutorial on old-school bump mapping</a> that includes a fragment shader that begins:</p>
  
  <pre><code>uniform sampler2D DecalTex; //The texture
  uniform sampler2D BumpTex; //The bump-map 
  </code></pre>
  
  <p>What they don't mention is <em>how</em> to pass two textures to the shader in the first place.</p>
  
  <p>Previously I</p>
  
  <pre><code>//OpenGL cpp file
  glBindTexture(GL_TEXTURE_2D, textureHandle);
  
  //Vertex shader
  gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
  
  //Fragment shader
  gl_FragColor = color * texture2D(DecalTex,gl_TexCoord[0].xy);
  </code></pre>
  
  <p>so now I</p>
  
  <pre><code>//OpenGL cpp file
  glBindTexture(GL_TEXTURE_2D, textureHandle);
  glBindTexture(GL_TEXTURE_2D, bumpHandle);
  
  //Vertex shader
  gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
  gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1;
  
  //Fragment shader
  gl_FragColor = color * texture2D(BumpTex,gl_TexCoord[0].xy);
  //no bump logic yet, just testing I can use texture 1 instead of texture 0
  </code></pre>
  
  <p>but this doesn't work. The texture disappears completely (effectively the teapot is white). I've tried GL_TEXTURE_2D_ARRAY, glActiveTexture and few other likely-seeming but fruitless options.</p>
  
  <p>After sifting through the usual mixed bag of references to OpenGL and GLSL new and old, I've come to the conclusion that I probably need <code>glGetUniformLocation</code>. How exactly do I use this <em>in the OpenGL cpp file</em> to pass the already-populated texture handles to the fragment shader?</p>
  
  <ul>
  <li><a href="https://www.gamedev.net/topic/589024-how-to-pass-an-array-of-textures-with-different-sizes-to-glsl/">How to pass an array of textures with different sizes to GLSL?</a></li>
  <li><a href="https://www.opengl.org/discussion_boards/showthread.php/163092-Passing-Multiple-Textures-from-OpenGL-to-GLSL-shader?p=1152122&amp;viewfull=1#post1152122">Passing Multiple Textures from OpenGL to GLSL shader</a></li>
  <li><a href="https://stackoverflow.com/questions/3569261/multiple-textures-in-glsl-only-one-works">Multiple textures in GLSL - only one works</a></li>
  </ul>
  
  <p>(This is homework so please answer with minimal code fragments (if at all). Thanks!)</p>
  
  <p><em>Failing that, does anyone have a tea cosy mesh?</em></p>