Sign in to follow this  
Oni Sephiroth

Textures Won't Bind

Recommended Posts

I've been attempting to rewrite my graphics engine recently to speed it up, but I'm running into the issue of my textures not binding. This is my function for loading the image:
unsigned char * HVSTGFX::loadPNGFile(char *fileName, HVSTGFX::PNGFILE *pngfile, GLuint &texture)
{
	FREE_IMAGE_FORMAT fif = FIF_PNG;

	//pointer to the image, once loaded
	FIBITMAP *dib(0);
	//pointer to the image data
	unsigned char* bits(0);
	unsigned char tempRGB;
	GLuint tempTex = 0;
	

	if(FreeImage_FIFSupportsReading(fif))
		dib = FreeImage_Load(fif, fileName);

	if(!dib)
		return NULL;


	bits = FreeImage_GetBits(dib);
	pngfile->width = FreeImage_GetWidth(dib); pngfile->height = FreeImage_GetHeight(dib);
	pngfile->size = sizeof(bits);
	int size = pngfile->width*pngfile->height;//(FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib));

	for (int imageIDx = 0; imageIDx < size * 4; imageIDx += 4)
	{
		tempRGB = bits[imageIDx];
		bits[imageIDx] = bits[imageIDx + 2];
		bits[imageIDx + 2] = tempRGB;
	}
	//FreeImage_Unload(dib);
	glGenTextures(1, &tempTex);
	glBindTexture(GL_TEXTURE_2D, tempTex);
	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, GL_RGBA, pngfile->width, pngfile->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits);
	texture = tempTex;
	
	
	return bits;
}
After watching it in debug mode, I've noticed that the GenTextures function isn't doing anything to tempTex, which is probably where the problem is. But, just to be sure, this is my function for actually drawing the sprite:
void HVSTGFX::createSpritePNGX(float width, float height, float x, float y, CSprite sprite)
{
	glBindTexture(GL_TEXTURE_2D, sprite.texture);
	glColor4f(1.0f,1.0f,1.0f, 1.0f );
		
		glBegin(GL_QUADS);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, 0.0f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y, 0.0f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, 0.0f);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, 0.0f);
		glEnd();
}
I've been digging around my textbooks and everywhere else on the internet, I just can't seem to find an answer as to why this isn't working.

Share this post


Link to post
Share on other sites
If tempTex = 0 after glGenTextures, then glGenTextures must have failed - most likely because OpenGL is not initialized for the current thread. I suspect the problem is that you are reading your image before you initialize OpenGL (or you are doing it in a different thread than the one where you initialized OpenGL).

Share this post


Link to post
Share on other sites
Yes, GL is enabled, it's being done on WM_CREATE, as you can see by this function:

void HVSTGFX::initGL()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnable(GL_ALPHA_TEST);


}


Also, I should add that calling glTexImage2D before drawing a sprite works, but this is what's causing my current engine to be so slow.

Share this post


Link to post
Share on other sites
Calling glTexImage2D loads the data into the current texture memory - so calling it every time you draw will clearly work.

If you do something like

int main() {

loadImages();

initializeWindow();

}

It will not work, because when you call loadImages(), you have not yet created the OpenGL rendering context. I can almost guarantee that this is your problem.

Share this post


Link to post
Share on other sites
I have this being done on WM CREATE

hdc = GetDC(hwnd);
g_hdc = hdc;
HVSTGFX::SetupPixelFormat(hdc);
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
HVSTGFX::initGL();
FreeImage_Initialise(TRUE);


g_hdc is a global device context.

Unless this is considered a different thread from winMain (since it's called in mainWndProc?), then GL should be initializing. Unless I'm wrong and misunderstood something, that is.

Share this post


Link to post
Share on other sites
Yes, that seems OK - but I am curious - how/where is HVSTGFX::loadPNGFile called?

You might try

GLenum errCode;
const GLubyte* errString;
errCode = glGetError();
if (errCode != GL_NO_ERROR)
{
cout << gluErrorString(errCode) << endl;
}

After glGenTextures (which must be failing). Hopefully this will give you useful information.

Share this post


Link to post
Share on other sites
I see no glTexParameteri( .... GL_REPEAT ); stuff

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pngfile->width, pngfile->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits); <-- break point here see if bits contains data

failing that try glintercept, gldebugger which will let u view any oprngl textures

Share this post


Link to post
Share on other sites
I've got an error code from the glGenTextures, specifically GL_INVALID_OPERATION. How I interpret this, I'm not really sure, though.

And bits has to have data, or else glTexImage2D wouldn't work.

EDIT: I take that back, there is no error on glGenTexture, apparantly I had made a call to the function earlier on in the code which was triggering an error. I have it working now, but the texture still isn't working.

[Edited by - Oni Sephiroth on May 30, 2009 12:23:29 AM]

Share this post


Link to post
Share on other sites
Oh, goddamnit. I commented out all the code in my main loop and just drew the sprite using the updated functions. Turns out it works, but now the transparency isn't working. Did I forget something in my loading or drawing functions?

EDIT: Excuse my stupidity. It clicked with me last night after I went to bed that when I commented out the main loop code I also commented out the glAlphaFunc and glBlendFunc functions. Everything's fine now! Thank you all for your help.

[Edited by - Oni Sephiroth on May 30, 2009 10:40:54 AM]

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