Passing two textures to GLSL

Hi!
First of all, I want you to know that I have searched the forum and founh solutions to similar problems, but somehow I couldn’t manage to make my program work.

I am trying to pass 2 textures to GLSL from C++, and there just add their color and display. I really cannot see what I’m doing wrong, so please help :slight_smile:

Here is the code:

 
glGenTextures(1,&texName1);
	glBindTexture(GL_TEXTURE_2D,texName1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,sizeX,sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,fileData1);
	glBindTexture(GL_TEXTURE_2D,0);

	glGenTextures(1,&texName2);
	glBindTexture(GL_TEXTURE_2D,texName2);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,sizeX2,sizeY2,0,GL_RGB,GL_UNSIGNED_BYTE,fileData2);
	glBindTexture(GL_TEXTURE_2D,0);

set shaders function

 void setShaders() {

	char *vs = NULL,*fs = NULL;
	GLint location1,location2;

	v = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
	f = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);


	vs = textFileRead("texture.vert");
	fs = textFileRead("texture.frag");

	const char * ff = fs;
	const char * vv = vs;

	glShaderSourceARB(v, 1, &vv,NULL);
	glShaderSourceARB(f, 1, &ff,NULL);

	free(vs);free(fs);

	glCompileShaderARB(v);
	glCompileShaderARB(f);

	p = glCreateProgramObjectARB();
	glAttachObjectARB(p,f);
	glAttachObjectARB(p,v);

	glLinkProgramARB(p);

	location1=glGetUniformLocationARB(p,"texture1");
	location2=glGetUniformLocationARB(p,"texture2");

	glActiveTextureARB(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, texName1);
	glEnable(GL_TEXTURE_2D);
	glUniform1iARB(location1, 0);

	glActiveTextureARB(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, texName2);
	glEnable(GL_TEXTURE_2D);
	glUniform1iARB(location2, 1);

	glUseProgramObjectARB(p);
}

vertex shader:

 varying vec3 normal;
void main()
{
	normal = gl_NormalMatrix * gl_Normal ;
	gl_Position = ftransform();

	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_TexCoord[1] = gl_MultiTexCoord1;
}

fragment shader:

 
varying vec3 normal;
uniform sampler2D texture1;
uniform sampler2D texture2;

void main()
{
	gl_FragColor = texture2D(texture1,gl_TexCoord[0])+texture2D(texture2,gl_TexCoord[1]);
}

this program displays just the data found under the fileData1 texture, and the fileData2 texture is just black. If I swap the two textures in the glTexImage2D functions, it will display only the second texture, so it’s not a problem related with reading texture data…

I have managed to half - solve the problem: the code


location1=glGetUniformLocationARB(p,"texture1");
	location2=glGetUniformLocationARB(p,"texture2");

	glActiveTextureARB(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, texName1);
	glEnable(GL_TEXTURE_2D);
	glUniform1iARB(location1, 0);

	glActiveTextureARB(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, texName2);
	glEnable(GL_TEXTURE_2D);
	glUniform1iARB(location2, 1);


has to be after glUseProgram

But now the second half-problem:

The program works only if I have


gl_FragColor = texture2D(texture1,gl_TexCoord[0])+texture2D(texture2,gl_TexCoord[0]);
}

So, on both texture lookups I have to have the same gl_TexCoord[0] variable. If I put gl_TexCoord[1] on the second texture it doesn’t work. Can anyone say why?

Strange, your first problem remind me something but I can’t find out. AFAIK, you should not have to put the program in use to get uniforms location…

How do you set textures coordinates in you draw function?

How do you pass the second texture coordinates? Have you tried using something else, just to make sure it’s the texture reading that’s going wrong? Ie use object space coordinates as texture coords?

thanks for your replies

my draw function is:

 
void renderScene(void) {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBegin( GL_QUADS );
		glTexCoord2d(0.0,0.0); glVertex2f(-2.0,2.0);
		glTexCoord2d(1.0,0.0); glVertex2f(2.0,2.0);
		glTexCoord2d(1.0,1.0); glVertex2f(2.0,-2.0);
		glTexCoord2d(0.0,1.0); glVertex2f(-2.0,-2.0);
	glEnd();
	glutSwapBuffers();
}

Well you’re not assigning anything to the second texture coordinates (which you use in the vertex shader through gl_MultiTexCoord1). You’ll need to use glMultiTexCoord2d() instead. It works exactly like glTexCoord except it takes a texture “name” to specify which set to use. So for instance “glMultiTexCoord2d(GL_TEXTURE0, 0.0,0.0);” You’ll need to pass something for GL_TEXTURE1 as well.

See section 2.7 “Vertex Specification” in the specifications for more details.

Tnx, that solved my problem :slight_smile:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.