PDA

View Full Version : Texture Problem



ankur2000
05-20-2005, 08:18 AM
Hi..I have a box moving in a 3D space...now i want to have a floor and walls for the box in the x-y , x-z, and the y-z planes. I want to load the image which i want have in the wall from a file.

I am trying to use textures for that but its not working. Does anyone have any idea ?

-Thanks
Ankur

Ehsan Kamrani
05-20-2005, 09:23 AM
Hi
Note:Have you enabled the texture mapping in your program:
glEnable( GL_TEXTURE_2D );

OpenGL Game Programming has written some functions to load the pictures.Here's a function to load a bitmap picture in RGB mode:

// LoadBitmapFile
// desc: Returns a pointer to the bitmap image of the bitmap specified
// by filename. Also returns the bitmap header information.
// No support for 8-bit bitmaps.
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr; // the file pointer
BITMAPFILEHEADER bitmapFileHeader; // bitmap file header
unsigned char *bitmapImage; // bitmap image data
int imageIdx = 0; // image index counter
unsigned char tempRGB; // swap variable

// open filename in "read binary" mode
filePtr = fopen(filename, "rb");
if (filePtr == NULL)
return NULL;

// 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 NULL;
}

// 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
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

// verify memory allocation
if (!bitmapImage)
{
free(bitmapImage);
fclose(filePtr);
return NULL;
}

ow can you load a bitmap?First of all, you need to create a bmp file that its width and height is a power of 2. then use the following code to load the file:

BITMAPINFOHEADER bitmapInfoHeader;
unsigned char* bitmapData;

bitmapData = LoadBitmapFile( "yourFileName.bmp", &bitmapInfoHeader );

Then in this example you can use from the bitmapInfoHeader and bitmapData in the function glTexImage2D:

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData );

-Ehsan-

ankur2000
05-20-2005, 11:21 AM
thanks man...its working now :-D

Ehsan Kamrani
05-20-2005, 12:13 PM
You'r welcome.I'm so happy. Good Luck with your OpenGL projects:)
-Ehsan-