Sign in to follow this  
EmrldDrgn

OpenGL Texturing Problem

Recommended Posts

OK, I have a function LoadImage in my Image class, as follows:
bool awImage::LoadImage(std::string filename)
{
	ILuint ImgName;
	ilGenImages(1, &ImgName);
	ilBindImage(ImgName);
	if(!ilLoadImage(filename.c_str()))
	{
		ilDeleteImages(1, &ImgName);
		GetILErrors();
		return true;
	}

	std::cout << "Image successfully loaded from file " << filename << "!\n";

	if(ilGetInteger(IL_IMAGE_BPP) == 4)
	{
		if(!ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE))
		{
			ilDeleteImages(1, &ImgName);
			GetILErrors();
			return true;
		}
	}
	else if (ilGetInteger(IL_IMAGE_BPP) == 3)
	{
		if(!ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE))
		{
			ilDeleteImages(1, &ImgName);
			GetILErrors();
			return true;
		}
	}
	else
	{
		std::cout << "Image has wrong number of bytes per pixel (too many channels)!";
		ilDeleteImages(1, &ImgName);
		GetILErrors();
		return true;
	}

	glEnable(GL_TEXTURE_2D);
	glEnable(GL_COLOR_MATERIAL);

	glGenTextures(1, &pTexture);
	glBindTexture(GL_TEXTURE_2D, pTexture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP),
		ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 
		0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());

	glDisable(GL_TEXTURE_2D);
	glDisable(GL_COLOR_MATERIAL);

	//pTexture = ilutGLBindTexImage();

	ilDeleteImages(1, &ImgName);

	std::cout << "Image " << pTexture << " sent to OpenGL!\n";

	////pTexture = ilutGLLoadImage("C:/Users/Andrew/Documents/Visual Studio 2008/Projects/OpenGLProgram/Debug/tex.bmp");

	//std::cout << ilGetError();
	return false;
}

It's called during the constructor of the parent class, like so: tex.LoadImage("C:/Users/Andrew/Documents/Visual Studio 2008/Projects/OpenGLProgram/Debug/tex.bmp"); I know this runs, because I checked in the debugger. Then I have a Draw method:
bool awImage::DrawImage(unsigned int xpix, unsigned int ypix, unsigned int sidelength)
{
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_COLOR_MATERIAL);

	glBindTexture(GL_TEXTURE_2D, pTexture);

	glBegin(GL_QUADS);
		glTexCoord2i(0, 1);
		glVertex2i(xpix, ypix);
		glTexCoord2i(1, 1);
		glVertex2i(xpix + sidelength, ypix);
		glTexCoord2i(1, 0);
		glVertex2i(xpix + sidelength, ypix + sidelength);
		glTexCoord2i(0, 0);
		glVertex2i(xpix, ypix + sidelength);
	glEnd();

	glDisable(GL_TEXTURE_2D);
	glDisable(GL_COLOR_MATERIAL);

	return false;
}

This code draws a white square. Checking in the debugger, I realized that pTexture was still 0 (which doesn't make sense, as LoadImage was called). Then I discovered I could fix the problem by adding this to the top of DrawImage:
if (pTexture < 1)
        {
		LoadImage("C:/Users/Andrew/Documents/Visual Studio 2008/Projects/OpenGLProgram/Debug/tex.bmp");
	}
This works even if LoadImage is NOT called by the parent constructor. Now my question is... why? I know LoadImage is called beforehand, but pTexture is still 0 after the call to glGenImage... but only the one called from the parent class' constructor (NOT necessarily just the first call). How is this possible? Can anyone explain it to me? I've been looking around Google, and my code seems correct. Ideas?

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