PDA

View Full Version : Image Library



soconne
08-17-2003, 03:30 PM
Does anybody know of an OpenGL BMP Loading Library that works with C++ Builder?

soconne
08-17-2003, 03:42 PM
Let me clarify on my problem

I'm having huge problems trying to load a simple texture map into C++ Builder. Here's my code for loading the BMP file.

BITMAPINFOHEADER biHeader;
BITMAPFILEHEADER header;
unsigned char *data;
unsigned char color;

FILE *pfile;

pfile = fopen(filename, "rb");
if(pfile == NULL) return 0;

fread(&header, sizeof(BITMAPFILEHEADER), 1, pfile);
if(header.bfType != BITMAP_ID)
{
fclose(pfile);
return 0;
}

fread(&biHeader, sizeof(BITMAPINFOHEADER), 1, pfile);
fseek(pfile, header.bfOffBits, SEEK_SET);

data = new unsigned char[biHeader.biSizeImage];
fread(data, 1, biHeader.biSizeImage, pfile);

int index;
for(index=0; index < (int)biHeader.biSizeImage; index+=3)
{
color = data[index];
data[index] = data[index+2];
data[index+2] = color;
}
fclose(pfile);

This works perfectly fine in Visual C++, but in Borland C++ Builder if I check the values of biHeader.biWidth, biHeader.biHeight, and/or biHeader.biSizeImage they all have either 0 or really "high" values, like 88083838 or something along those lines. I have the images in the current directory of the project so that's not the issue, and I check to make sure it loads, but for some reason this code doesn't grab the information correctly. I even tried outputing the contents of data to a file and it turned up blank!

I know texture mapping is working because I constructed my own texture map manually and loaded it and it displayed fine, but with this code all I get is a white triangle. Any ideas?

pkaler
08-17-2003, 05:27 PM
Check your compilers documentation about how it packs and aligns memory in structures.

soconne
08-17-2003, 05:34 PM
Well I figured out a different way to do it, so for anyone out there who has had the same problem here's the code:

Graphics::TBitmap *bitmap = new Graphics::TBitmap;
bitmap->LoadFromFile("leaf.bmp");
unsigned char *data = new unsigned char[bitmap->Width*bitmap->Height*3];

for(int i=0; i < bitmap->Height; i++)
for(int j=0; j < bitmap->Width; j++)
{
data[(i*bitmap->Width+j)*3] = (GLbyte)GetRValue(bitmap->Canvas->Pixels[i][j]);
data[(i*bitmap->Width+j)*3+1] = (GLbyte)GetGValue(bitmap->Canvas->Pixels[i][j]);
data[(i*bitmap->Width+j)*3+2] = (GLbyte)GetBValue(bitmap->Canvas->Pixels[i][j]);
}

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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_RGB, bitmap->Width, bitmap->Height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);


Hope it helps someone else like it did me.

knackered
08-18-2003, 12:15 AM
There's a number of major assumptions in that code. Nasty, very nasty.

V-man
08-18-2003, 08:01 AM
Personally, I think that BMP (uncompressed) is worthless.

Try imagelib.org (formerly DevIL). It can handle quite a few things.

soconne
08-18-2003, 08:27 AM
yeah, i've been looking into devil but I'm having a hard time getting it to compile under BCB, but I'm new to BCB so i'll keep trying. Visual C++ is such a pain in the @ss when you need to use a gui

MZ
08-18-2003, 10:49 AM
Then don't compile it. Link to VC-compiled dll (use implib)