PDA

View Full Version : Texture not appearing



foniks
04-30-2003, 03:15 AM
Hi all,

I am having a real issue with texture mapping that I just cannot work out.

I am getting nothing more than a spinning white cube in space where I was hoping to have a textured cube. The texture is a 256x256 bmp, which I have located in my debug directory (and project directory to be sure). The debugging I have done seems to sugest that the image is being loaded, but is not being displayed - almost as though I haven't bound the texture.

Any input would be appreciated.

Cheers.

PS: I am using MSVC 7 - Win32 API.



AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle

if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}

File=fopen(Filename,"r"); // Check To See If The File Exists

if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}

return NULL; // If Load Failed Return NULL
}

int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator

AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("image.bmp"))
{
Status=TRUE; // Set The Status To TRUE

glGenTextures(1, &texture[0]); // Create The Texture

// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}

if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}

free(TextureImage[0]); // Free The Image Structure
}

return Status; // Return The Status
}

int initGL(void)
{
if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
{
return FALSE; // If Texture Didn't Load Return FALSE
}

glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return TRUE;
}

int display()
{
/* rotate a triangle around */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rotatex, 1.0f, 0.0f, 0.0f);
glRotatef(rotatey, 0.0f, 1.0f, 0.0f);
glRotatef(rotatez, 0.0f, 0.0f, 1.0f);

glBindTexture(GL_TEXTURE_2D, texture[0]);

glBegin(GL_QUADS);
// Front face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glEnd();


rotatex += 5;
rotatey += 5;
rotatez += 5;

return TRUE;
}

void reshape(int height, int width)
{
glViewport( 0, 0, (GLsizei) width, (GLsizei) height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}


[This message has been edited by foniks (edited 04-30-2003).]

M/\dm/\n
04-30-2003, 03:28 AM
Check with gluBuildMipmaps! Bmp, should be in same dir where dsw is located or use "Debug/file.bmp". If your debug shows file is there it could heve been created as emty file somwhere I guess in dsw dir http://www.opengl.org/discussion_boards/ubb/biggrin.gif
BTW, you'll have Gimbal lock with that glRotate

foniks
04-30-2003, 04:08 AM
Thanks http://www.opengl.org/discussion_boards/ubb/smile.gif

Well, I have the texture in the correct directory. I have tried to load the texture using gluBuild2DMipmaps - but no joy. The texture is a 256 x 256 image, so it is a power of 2.. I'm confused!

Gavin
04-30-2003, 04:17 AM
to check you have loaded your texture properly use drawpixels to display it!

M/\dm/\n
04-30-2003, 04:30 AM
Check your array, is it empty or not.

foniks
04-30-2003, 05:10 AM
The bmp has been read - I can get the (correct) dimensions of the file from the AUX_RGBImageRec struct.

M/\dm/\n
04-30-2003, 05:38 AM
Dimensions yes, but bitmap data? You are reading dimensions in struct like bitmapinfoheader, but data reading involves mallock'ing etc. BTW, you should move to BMP loading without aux, it's obsolete lib. Take a look at www.gametutorials.com, (http://www.gametutorials.com,) the second BMP loading routine is clean one http://www.opengl.org/discussion_boards/ubb/biggrin.gif

Vlasko
04-30-2003, 07:03 AM
is your init function called after creating window ?

oliii
04-30-2003, 09:27 AM
yes, that seems to be the problem. Make sure you init OpenGL/glut before playing with textures. Caught me once. Also, try to check for errors returned by OpenGL, it can help. Also, texture width / height(?) must be a power of 2 as far as I remember.

Vlasko
04-30-2003, 12:03 PM
It seems to that this is lesson 6 from nehe which contains this little bug :-)

foniks
04-30-2003, 03:15 PM
It is an ugly godchild of two different tutorials which is probably where my problem is. http://www.opengl.org/discussion_boards/ubb/smile.gif

I am at work at the moment, but will try everyones suggestions out later.

Agreed that the aux library is obsolete - just using it for simplicity at this point as a proof of concept. Once I have written a couple of reusable functions I will move away from that.

Just a quick question - this is going to seem really stupid, but checking the size of texture[0], I can see that it is only 4 bytes, which would be the size of the pointer I presume and not the actual data it is pointing to. Am I going about this the right way?

sizeof(texture[0]);

I have proven that the BMP was read, but I haven't yet proven that a texture was generated from this data, which I will do when I get home tonight. Bleah..

Thanks for all your input. No doubt I will be back for a second round this evening. http://www.opengl.org/discussion_boards/ubb/smile.gif

foniks
05-01-2003, 12:15 AM
Thanks for all those that answered and gave valuable input. The answer was in fact that I was initialising OpenGL prior to creating the window and thus the texture generation was failing.. quite obvious really. Damn!

Thanks again all.