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:


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


// 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?

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.

That was it, thanks. The specific calls I was missing were


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