Sign in to follow this  
tjrr3d

Texturing problem

Recommended Posts

I have been working on this problem literally over 12 hours now, and I can not fuigure it out. The problem is that my qauads aren't being textured like I tell them to. In fact they're not getting texturedagt all!. Here my code:
GLuint base, texture;
AUX_RGBImageRec *TextImg;

Text::LoadTextImg(LPWSTR path)
{
	TextImg = auxDIBImageLoad(path);

	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
        glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
        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_LINEAR);
        glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextImg->sizeX, TextImg->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextImg->data);
	free(TextImg->data);
	free(TextImg);

	glDisable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
}

Text::Render()
{
        glEnable(GL_TEXTURE_2D);

	// Set projection matrix to orthographic
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0, ScreenWidth, 0, ScreenHeight, -1, 1);

	// Move to starting position
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
        glTranslated(x,y);
	glBindTexture(GL_TEXTURE_2D, texture);	// Make sure right texture is selected
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);	glVertex2i(0,0);
		glTexCoord2f(1.0f, 0.0f);	glVertex2i(256,0);
		glTexCoord2f(1.0f, 1.0f);	glVertex2i(256,256);
		glTexCoord2f(0.0f, 1.0f);	glVertex2i(0,256);
	glEnd();

	// Go back to original matricies
	glPopMatrix();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();

	glEnable(GL_DEPTH_TEST);
	glDisable(GL_TEXTURE_2D);
}




I think there is a flag or something that I'm not setting because the only thing that ever comes up is the quad, but its all white. Here's my initgl function:
QGLWidget::initializeGL()
{
  glClearColor(0.0, 0.0, 0.3, 0.0);
  glShadeModel(GL_SMOOTH);
  glEnable(GL_DEPTH_TEST);
  glClearDepth(1.0f);
}




Someone please help. [Edited by - tjrr3d on April 1, 2006 10:08:42 PM]

Share this post


Link to post
Share on other sites
That is probably because you clal glDIsable(GL_TEXTURE_2D) in the render function. So as soon as the render function is called once it disables texturing. Now you don't have a enable texture to set the state - so your quads will no longer render textured.

The easy fix is to enable texturing at that start of rendering

Share this post


Link to post
Share on other sites
Sorry bout that. glEnable(GL_TEXTURE_2D) was originally in there, I guess it got removed while editing. I have made the change. Either way it still doesn't work and I just see the color of the object and not the texture.

Share this post


Link to post
Share on other sites
Don't see a problem with your code. But you might want to check the value of texture if it is 0, negative or something else. If its 0 then the texture never got made. It should be a meaningful value but not 0 or some vaguely large value.

Share this post


Link to post
Share on other sites
Thats why Im so confused. The value is not zero. But I just see nom texture. I wonder if it is a Qt Problem. That is I am using Qt 4.1 (Evaluation) with the .NET 2003 integration. Man this is really getting to me.

Share this post


Link to post
Share on other sites
Yes, 256x256. This is verified when I look at TextImg->sizeX & TextImg->sizeY. I put the exact code in SDL and everything worked fine so it must be a Qt problem. Qt offers some methods for binding textures, but either they also don't work, or I'm doing something wrong, because even with Qt's built in functions, I only see white, no texture.

Share this post


Link to post
Share on other sites
I'm not sure. How can I find this out. I can draw points quads etc. if that helps. I just can't see any texture on the quads that are drawn.

Share this post


Link to post
Share on other sites
Assuming you're using Win32 to create your OpenGL window you should make sure that your calls to wglCreateContext and wglMakeCurrent occur before your call to Text::LoadTextImg. You could try checking this by adding an assert(wglGetCurrentContext()); at the start of Text::LoadTextImg.

Σnigma

Share this post


Link to post
Share on other sites
Hi tjrr3d,

I copied the code you posted into a sample app I had and I couldn't see the texture either, but I also couldn't see the white box til I changed a few things.

first off, the code wouldn't compile with the following line from your Render function.
glTranslated(x,y);

this function takes 3 doubles so I assume you fixed this and it didn't make it into the post. I changed that line to.
glTranslated(0.0, 0.0, -1.0);

But I still couldn't see anything until I changed you last parameter to glOrtho to 2. My width and height are 640x480
glOrtho(0, SCREENWIDTH, 0, SCREENHEIGHT, -1, 2);

I also didn't see you make a glViewport call, but maybe you make it somewhere else and I'm not sure if there is a default condition for it in opengl.

I did get it too work ok with the above fixes, but the way I load bitmaps requires me to use GL_BGR_EXT in my glTexImage2D call like this.
glTexImage2D(GL_TEXTURE_2D, 0, 3, Width, Height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, TextureImage[0]);
but if I do it your way I can still see a texture it just has red and blue reversed.

I hope this helps and I can further explain anything I did if you need me to.

Share this post


Link to post
Share on other sites
Diruntled Gamer:
Youre right I originally had glTranslated(x, y, 0). I tried you fixes, but still see only white. Actually I can view quads without the changing the glOrtho to 2 and the glTranslated to -1; But I changed it to your way and still nothing.
When you tested it, was it with Qt, or a windows app?

Enigma:
I put the assert in and my program crashed. This is what it said:

Debug Error!

Program: blah, blah\debug\GfxGui.exe

This application has requested the Runtime to terminate in an unusual way. Please contatct the applications support team for more information.

I think Qt already created the context. I mean there are not handles to device contexts and stuff like a windows app. So I think that may be why the fuction crashed the program

Share this post


Link to post
Share on other sites
I am using win32, and the function I use to load the texture from file is called LoadDIBitmap. The only way I can reproduce your problem is to disable GL_TEXTURE_2D, and then draw the quad I get a white box. I'm guessing I'd also get a white box if my LoadDIBitmap didn't work. You do make a lot of glEnable and glDisable calls with depth testing and texturing and for what you are trying to do you really only need to enable them once and leave them on. Your initializeGL function would be a good place for that.

Are you sure your auxDIBImageLoad works properly can you display bitmaps other ways with it like SetDIBitsToDevice or glDrawPixels? Check that and make sure your not disabling texturing somewhere by not disabling it at all.

DG

Share this post


Link to post
Share on other sites
Are you sure that wasn't the assert firing? I'm not sure what it would look like for you since it depends on your compiler and probably whether you're running through a debugger or not. An alternative test would be to try something like if (!wglGetCurrentContext()){MessageBox(0, "no opengl context", "Assert Failed", MB_OK);}. I'm not familiar with Qt, but it appears you could also try if (!QGLContext::currentContext()){MessageBox(0, "no opengl context", "Assert Failed", MB_OK);}. If either of those display the message box then Qt is not creating your opengl context until after you try to create your texture and you must move the call to create your texture to a point in your program after the context has been created.

Σnigma

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