PDA

View Full Version : Passing Multiple Textures from OpenGL to GLSL shader



Brooksy
05-22-2006, 11:08 AM
I am quite a newby when it comes to GLSL. I am currently trying to write a shader to render a water surface. This involves multi-texturing: the water texture, reflection environment map, and a refracted texture. I have written this shader in RenderMonkey and the shader works.

In my fragment shader I initialise the following variables:

uniform sampler2D BaseMap;
uniform sampler2D ReflectMap;
uniform sampler2D RefractMap;

So therefore my OpenGL program needs to pass the location of these textures to the shader.

My textures are stored in a texture class, which definitely works.

When i run this code, it displays the first texture three times, mapped in the three different ways.

glBindTexture(GL_TEXTURE_2D, Base);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, BaseData);

glBindTexture(GL_TEXTURE_2D, Reflection);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, ReflectionData);

glBindTexture(GL_TEXTURE_2D, Refraction);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, RefractionData);

glUseProgram(ShaderID);

GLint texLoc;
texLoc = glGetUniformLocation(ShaderID, "BaseMap");
glUniform1i(texLoc, Base);

texLoc = glGetUniformLocation(ShaderID, "ReflectMap");
glUniform1i(texLoc, Reflection);

texLoc = glGetUniformLocation(ShaderID, "RefractMap");
glUniform1i(texLoc, Refraction);

Then in further down in my draw() function:

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Base);

glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Reflection);

glActiveTexture(GL_TEXTURE2);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Refraction);


If anyone can help me out here I'd be very grateful :)

Humus
05-22-2006, 12:00 PM
glUniform1i() doesn't accept texture IDs. It takes a texture unit. Further, glEnable(GL_TEXTURE_2D) is not needed for shaders. Your code should look like:

texLoc = glGetUniformLocation(ShaderID, "BaseMap");
glUniform1i(texLoc, 0);

texLoc = glGetUniformLocation(ShaderID, "ReflectMap");
glUniform1i(texLoc, 1);

texLoc = glGetUniformLocation(ShaderID, "RefractMap");
glUniform1i(texLoc, 2);


glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Base);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, Reflection);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, Refraction);

Brooksy
05-22-2006, 12:51 PM
Yes, that works!

Thank you very much for your help :)