Jump to content
  • Advertisement
Sign in to follow this  
boosts

Passing multiple textures to a shader

This topic is 3004 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,
I have some problems about passing 3 textures to my fragment shader. I've tried to search through the forum, but couldn't find the solution. That 3 textures are
1. position buffer texture
2. normal vector buffer texture
3. random texture (just a simple bmp file)

I generated 1 and 2 by using frame buffer object, and I already verified that they were generated correctly by disabling the shader and binding them to draw other polygons.

Here is my code

Vertex shader

varying vec2 texCoord;

void main(void) {
gl_Position = ftransform();
gl_FrontColor = gl_Color;

gl_TexCoord[0] = gl_MultiTexCoord0;
texCoord = gl_MultiTexCoord0.xy;
}


Fragment shader

uniform sampler2D positionTexture;
uniform sampler2D normalVectorTexture;
uniform sampler2D randomTexture;
varying vec2 texCoord;

void main()
{
gl_FragColor = texture2D(positionTexture, texCoord.xy);
}



main.cpp (binding textures part)

// Send the variable to GLSL
normalTextureLoc = glGetUniformLocation(ssaoP, "positionTexture");
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, positionTextureID);
glUniform1i(normalTextureLoc, 0);

positionTextureLoc = glGetUniformLocation(ssaoP, "normalVectorTexture");
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, normalVectorTextureID);
glUniform1i(positionTextureLoc, 1);

randomTextureLoc = glGetUniformLocation(ssaoP, "randomTexture");
glActiveTexture(GL_TEXTURE2);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, randomTextureID);
glUniform1i(randomTextureLoc, 2);


main.cpp (rendering part)

glUseProgram(ssaoP);
// Actually draw scene
glMatrixMode(GL_MODELVIEW);
glClearColor(0.5, 0.9, 0.5, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -7);
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glNormal3f(0, 0, 1);
glTexCoord2f(1, 0);
glVertex3f(3.5, -3.5, 0.0);

glTexCoord2f(1, 1);
glVertex3f(3.5, 3.5, 0.0);

glTexCoord2f(0, 1);
glVertex3f(-3.5, 3.5, 0.0);

glTexCoord2f(0, 0);
glVertex3f(-3.5, -3.5, 0.0);
glEnd();
glUseProgram(0);
glutSwapBuffers();

The problem occurred when I tried to use the shader to draw normal vector texture or random texture. It didn't show, but it just kept showing position texture even though I didn't access it in the shader. For example, if I change the code in fragment shader to
---------------------------------------------------------------------
gl_FragColor = texture2D(normalVectorTexture, texCoord.xy);
---------------------------------------------------------------------
The program still showed positionTexture instead of normalVectorTexture.

It seems to me that it will show only the texture that attaches to TEXTURE0, because when I tried to fix binding textures part by sending the other texture to bind to TEXTURE0, it can show the texture correctly. For example, if I change the code in binding textures part to this
---------------------------------------------------------------------
normalTextureLoc = glGetUniformLocation(ssaoP, "positionTexture");
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, normalVectorTextureID);
glUniform1i(normalTextureLoc, 0);
---------------------------------------------------------------------
and leave the shader as it is

gl_FragColor = texture2D(positionTexture, texCoord.xy);

It can show normal vector texture on a polygon.

Do you have any idea why the fragment shader keeps showing the texture that attaches to TEXTURE0? Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Looks correct to me; are you binding the program before you try and modify the texture uniforms with glUniform1i? If not it would make sense that the values are not being initialized and are using their initial values of 0.

-Chris

Share this post


Link to post
Share on other sites
Quote:
Original post by cPugh
Looks correct to me; are you binding the program before you try and modify the texture uniforms with glUniform1i? If not it would make sense that the values are not being initialized and are using their initial values of 0.

-Chris


The problem has been resolved. I have to bind the textures after calling glUseProgram(ssaoP);
otherwise only the first texture (TEXTURE0) can be showed.

Thank you for your comment [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!