Problem with Textures

Started by
4 comments, last by HugoSoaresBr 15 years, 5 months ago
Hey guys, what's up? I've searched for my problem's solution, but I'm kind stucked. See... I am loading different Textures from different Bitmap files, but it seems that the only avaiable texture is the last loaded... I don't know where i am messing up, if you guys can give me a hand it would be nice... here's the code:

bool LoadGLTexture(char *fileName, GLuint &textBox)
{
	Bitmap *image	=	new Bitmap();
	
	if(image==NULL) {
		return false;
	}
        // Loads the img data to image->data
	if (image->loadBMP(fileName)) {
		glGenTextures(1, &textBox); 
		glBindTexture(GL_TEXTURE_2D, textBox);

		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, image->width, image->height, 0, 
					GL_RGB, GL_UNSIGNED_BYTE, image->data);
		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 
			image->width, 
			image->height, GL_RGB, GL_UNSIGNED_BYTE, image->data);
	}
	else {
		return false;
	}

	if (image) {
		delete image;
	}

	return true;
}
I think this should work like this: opengl would make a unique id and store it textBox, after that, you only have to call glBindTexture(GL_TEXTURE_2D, textureId) to select each texture previously loaded. Where am i messing up?
Advertisement
As far as i can tell that code looks correct. Check the identifiers returned by glGenTextures, also be sure that you have valid GL context at the time you call the function. Other than that, i would look for error somewhere else.
How to create a texture and mipmaps

http://www.opengl.org/wiki/index.php/Common_Mistakes#Creating_a_Texture
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Ok, let's say that everything is ok and that we have 2 uint text1 and text2, each one "pointing" to an unique texture.

would we draw like this:

void renderFrame()
{
glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_QUADS);

glBindTexture(GL_TEXTURE_2D, text1);
glTexCoord3f(-1.0, -1.0, -1.0); glVertex3f(CG_-1.0, -1.0, -1.0);
glTexCoord3f(-1.0, -1.0, 1.0); glVertex3f(-1.0, -1.0, 1.0);
glTexCoord3f(1.0, -1.0, 1.0); glVertex3f(1.0, -1.0, 1.0);
glTexCoord3f(1.0, -1.0, -1.0); glVertex3f(1.0, -1.0, -1.0);

glBindTexture(GL_TEXTURE_2D, text2);

glTexCoord3f(-1.0, 1.0, -1.0); glVertex3f(CG_-1.0, -1.0, -1.0);
glTexCoord3f(-1.0, 1.0, 1.0); glVertex3f(-1.0, -1.0, 1.0);
glTexCoord3f(1.0, 1.0, 1.0); glVertex3f(1.0, -1.0, 1.0);
glTexCoord3f(1.0, 1.0, -1.0); glVertex3f(1.0, -1.0, -1.0);

glEnd();
}

Is that right?
Um yes, except you can't call glBindTexture inside glBegin/glEnd.

This is also why it's good to call glGetError once in a while :)
Man, many thanks!!!!!
Everything is working just fine now!!! Many thanks!

This topic is closed to new replies.

Advertisement