Sign in to follow this  

OpenGL multi-texturing problem

Recommended Posts

I'm trying to load these textures into the shader which seems okay as in the shader code I can effect either sampler2D like tex1+tex2 will work but for some reason tex2 doesn't seem to be loading the texture I assigned for texture unit 1 but rather texture unit 0. I found that for some reason tex1 is loaded into texture unit 0 and 1 although I bind a different texture for each texture unit [ useTexture function calls glBindTexture] I'm using the immediate mode to draw the vertices and texcoords and I do use glMultiTexCoord2f 2 time one for GL_TEXTURE0 and the other for GL_TEXTURE1. glMultiTexCoord2f( GL_TEXTURE0, ...); glMultiTexCoord2f( GL_TEXTURE1, ...); ///vertices... I have no clue why when I set the active texture to GL_TEXTURE1 and set the 2nd texture it seems to really set the first. I have checked the # of texture units, and made sure the texIds were different. I even went as far as making sure which texture unit was active and so I'm stumped. On the side when I look up the texture unit with glGetIntegerv is the value supposed to return something like 33394 for GL_TEXTURE0 and 33395 for GL_TEXTURE1 (those numbers are not exact but as I see it increased by one I would think that's just opengl's internal thinking). Basically, the 1st texture seems to be bounded in both texUnit 0 and 1 but it doesn't make sense as I binded a different tex in texUnit 1.

int main(...)
    ///GLUT INIT...

    ///Load Texture
    BilbyTex = new texBlock;

    HorseTex = new texBlock;

        HorseTex->useTexture(); ///doesn't seem to want to use it but uses the above instead


    ///Load Shader
    pointLightShader = new Shader("./Shaders/pLight.vert", "./Shaders/pLight.frag");

    int uLoc=0;
    uLoc = glGetUniformLocation( pointLightShader->getProgramHandler(), "texture" );
    glUniform1i(uLoc, 0);

    uLoc = glGetUniformLocation( pointLightShader->getProgramHandler(), "texture_2" );
    glUniform1i(uLoc, 1);

    uLoc = glGetUniformLocation( pointLightShader->getProgramHandler(), "camera_position" );
    glUniform3fv(uLoc, 3, camera_pos);

    pointLightShader->shaderLog(); ///Shows Log in Console Window




varying vec3 position;
varying vec3 normal;
varying vec2 texC;
varying vec2 texC_2;

void main()
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; ///
	gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; ///
	normal = gl_NormalMatrix * gl_Normal;
	position =;

	texC = gl_TexCoord[0].st; ///
	texC_2 = gl_TexCoord[1].st; ///



uniform sampler2D texture;
uniform sampler2D texture_2;
uniform vec3 camera_position;
varying vec3 position;
varying vec3 normal;
varying vec2 texC;
varying vec2 texC_2;

void main()

	int xL=0; 

        ///Other Stuff...

	vec4 texColor = texture2D(texture, texC);
	vec4 texColor2 = texture2D(texture_2, texC_2);

	texColor.r += 1.0;
	texColor2.g += 1.0;

	vec4 cTColor = texColor * texColor2;

	//Note: Can't use var from varying or uniform for indexs apparently. At
	//Least it seems for any of the special gl_xxxx calls
	gl_FragColor = gl_LightSource[xL].ambient+
		      (diffuse * vec4(cTColor.rgb, 1.0)) +

[Edited by - Asem on June 8, 2009 6:35:29 PM]

Share this post

Link to post
Share on other sites
I give it an array ( camera_pos[3] ) so according to the opengl doc it's glUniformfv(location, size, float * value).

I did the multi texturing with the FFP and it works.

The problem is for some reason the 2nd sampler ,texture_2, doesn't seem to want to grab the correct texture from GL_TEXTURE1 even though you can see that I set it. In the end it just grabs the texture for GL_TEXTURE0. I know its not the texcoords as I did an example and scroll 1 texture but not the other and it worked.

I can't figure out what I'm missing or doing wrong or somethings just weird. I know when I mess up in my glsl code just a little it wants to work but then I can't modify the 2 texture like multiplying them and such.

I didn't have the shader program active before calling gluniform*.

//Then call...
gluniform* ...

[Edited by - Asem on June 10, 2009 4:31:56 PM]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this