Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.

Shane I Am

My Texture Class Wount Work! Please Help.

This topic is 5801 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay my texture class seems to not be working because after i attempt to use the data i load the texture seems to not exist. Here is the class... class TEXTURE { public: unsigned char *data; // Stored the bitmap data unsigned int ID; // the ID of the texture for binding unsigned int bbc; // color by bit count unsigned int height; // width of image unsigned int width; // height of image /////////////////////////////////////////////// // Prototypes /////////////////////////////////////////////// bool LoadTexture(char* fileName); //////////////////////////////////////////////// // Constructor/Deconstructor //////////////////////////////////////////////// TEXTURE() { data = NULL; ID = 1; bbc = 0; height = 0; width = 0; } ~TEXTURE() { // Lets free up the memory on texture destruction! Memory leaks suck! if(data != NULL) { glDeleteTextures(1, &ID); delete data; data = NULL; } } }; /////////////////////////////////////////////////////// // Function definitions /////////////////////////////////////////////////////// bool TEXTURE:: LoadTexture(char* fileName) { BITMAPFILEHEADER bitmapFileHeader; // bitmap file header int imageIdx = 0; // image index counter unsigned char tempRGB; // swap variable BITMAPINFOHEADER bitmapInfoHeader; // bitmap info header data = NULL; ///////////////////////////////////////////////////////////// // Load the bitmap ///////////////////////////////////////////////////////////// // open filename in "read binary" mode FILE *filePtr = fopen(fileName, "rb"); if (filePtr == NULL) return false; // read the bitmap file header fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr); // verify that this is a bitmap by checking for the universal bitmap id if (bitmapFileHeader.bfType != BITMAP_ID) { fclose(filePtr); return false; } // read the bitmap information header fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr); // move file pointer to beginning of bitmap data fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET); // allocate enough memory for the bitmap image data data = new unsigned char [bitmapInfoHeader.biSizeImage]; // verify memory allocation if (!data) { free(data); fclose(filePtr); return false; } // read in the bitmap image data fread(data, 1, bitmapInfoHeader.biSizeImage, filePtr); // make sure bitmap image data was read if (data == NULL) { fclose(filePtr); return false; } // swap the R and B values to get RGB since the bitmap color format is in BGR for (imageIdx = 0; imageIdx < bitmapInfoHeader.biSizeImage; imageIdx+=3) { tempRGB = data[imageIdx]; data[imageIdx] = data[imageIdx + 2]; data[imageIdx + 2] = tempRGB; } // Get the width height and bbc width = bitmapInfoHeader.biWidth; height= bitmapInfoHeader.biWidth; bbc = bitmapInfoHeader.biBitCount; if(bbc==32) { bbc = GL_RGBA; } if(bbc==24) { bbc = GL_RGB; } ///////////////////////////////////////////////////////////// // Load the texture ///////////////////////////////////////////////////////////// glEnable(GL_TEXTURE_2D); // enable 2D texturing glGenTextures(1, &ID); // generate texture object glBindTexture(GL_TEXTURE_2D, ID); // enable our texture object glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // generate the texture image glTexImage2D(GL_TEXTURE_2D, 0, bbc, width, height, 0, bbc, GL_UNSIGNED_BYTE, data); //gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, GL_RGB, GL_UNSIGNED_BYTE, data); // close the file and return the bitmap image data fclose(filePtr); return true; } ////////////////////////////////////////////////////////////// // End //////////////////////////////////////////////////////////////// So does anyone have any suggestions? I''ve been stuck on this for days! It always seems to not work when i put my code into a class for use

Share this post

Link to post
Share on other sites
Oh yeah here is a sample of how i use it....



glBindTexture(GL_TEXTURE_2D, shane.ID);
glTexCoord2f(0,0); glVertex3f(-1,-1,-5);
glTexCoord2f(1,0); glVertex3f(1,-1,-5);
glTexCoord2f(1,1); glVertex3f(1,1,-5);
glTexCoord2f(0,1); glVertex3f(-1,1,-5);


Share this post

Link to post
Share on other sites
Hello there,

First of all I need to say, I''m REALLY new to OpenGL and 3D programming! I have programming experience, but I only started studying these technologies 2 days ago.

Anyway, while I was looking at your code, I was wondering.... Didn''t you switch the width and height properties in your TEXTURE class definition?

unsigned int height; // width of image
unsigned int width; // height of image

And how about this piece of your loading code:

width = bitmapInfoHeader.biWidth;
height= bitmapInfoHeader.biWidth;

It assumed that the texture has equal sizes for width and height. Are they really?


Excuse me if I''m talking nonsense here. It just looks odd to me. I just wanted to point it out. I might be wrong.

Anyhow.. I hope I''m right (newby-luck hehe). And I hope you will find your solution to the problem..


We are only alive because we dream %*

Share this post

Link to post
Share on other sites
I read somewhere that with RGB bitmaps, the biSizeImage field can be zero. You might want to check if it is zero, and if so, store width * height * 3 in it.

Also, in your glTexImage2D call, you use bbc twice, containing the value GL_RGB or GL_RGBA. In the first instance, that field is the number of bytes per pixel, so 3 or 4. But in my implementation, GL_RGB is 0x1907 and GL_RGBA is 0x1908, so that could definitely be a problem. Try placing 3 in that field for an RGB image.

Share this post

Link to post
Share on other sites
Actually i did find those problems with my width/height but they didn''t help at all, thanks anyhow and that was a good observation.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!