Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Help with glActiveTexture / Multitexturing

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    25

    Help with glActiveTexture / Multitexturing

    Hi All,

    I'm going through the orange book, but I got caught early on with a multitexturing example. For starters, I'm running OpenGL 3.0 core, glsl 1.2, starting my context with SDL2.

    Basically I've got a sphere with a normal map and a texture map, both of which are 24 bit RGB images. My general initTexture function looks like this:

    Code :
    uint32_t InitTexture(void * PXA, int w, int h)
    {
    	uint32_t tex;
     
    	glGenTextures(1, &tex);
    	glBindTexture(GL_TEXTURE_2D, tex);
     
    	//Upload host texture to device
    	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, PXA);
     
    	// Set filtering   
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     
    	//Unbind, Delete the host texture, return handle to device texture
    	glBindTexture(GL_TEXTURE_2D, 0);
     
    	return tex;
    }

    My draw calls go something like this

    Code :
    // Bind shader, upload P, MV, N, etc.
     
    // Bind both the texture map and the normal map to their samplers
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE2D, u_TextureSamplerHandle);
     
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE2D, u_NormalSamplerHandle);
     
    // Bind VAO, draw with glDrawElements

    The above code gives me a blank screen. When I remove the second call to glActiveTexture, what happens is that both the Texture Sampler and the Normal Sampler get their data from my normal map. That makes sense, since leaving GL_TEXTURE0 active will bind the normal map in the second glBindTexture call, and what I end up with is the blue/purple from the normal map texturing my sphere (the actual texture is a globe.)

    What I'm wondering is if I have to set GL_TEXTURE0 active when I generate and upload the Texture Map, then do the same for GL_TEXTURE1 with the normal map. I've tried this with no luck, but I could be making a mistake. I'm also wondering if there's some setting I have to enable, but I couldn't find anything in any of the examples I've been looking at.

    This SO post seems to have a pretty good multitexturing example, but they don't use glsl 1.2 (attributes, varying and the like.) I can't seem to find an example that holds my hand through the whole process. I can post more code and details if necessary, but is there something obvious I'm missing?
    Last edited by mynameisjohn; 07-26-2015 at 05:14 PM.

  2. #2
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    5,928
    What I'm wondering is if I have to set GL_TEXTURE0 active when I generate and upload the Texture Map, then do the same for GL_TEXTURE1 with the normal map.
    No, you do not.

    Your problem is most likely that you have not told the shader which texture image unit the texture for a sampler comes from. The OpenGL Wiki has an explanation for this process, including example code.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    25
    That was it, thanks. The specific calls I was missing were

    Code :
    glUniform1i(u_TextureSamplerHandle, 0); // associate with GL_TEXTURE0
    glUniform1i(u_NormalSamplerHandle, 1); // associate with GL_TEXTURE1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •