#include <iostream>
//Bitmap Class
class bmp
{
public:
//Functions
BYTE &pixel(int,int,int);
GLuint loadBitmap(const char*);
void generateTexture(GLuint*);
//Variables
BYTE *data;
int iHeight,iWidth;
};
BYTE &bmp::pixel(int a,int b,int c)
{
return data[(b*iWidth+a)*3+c];
}
GLuint bmp::loadBitmap(const char *fileName)
{
BYTE colorTable[1024],tempData;
char cHeader[54];
FILE *file = fopen(fileName,"rb");
GLuint glTexture = 0;
int a,b,c,d,iBits,iOffset;
if(!file)
{
MessageBox(0,"Could not load bitmap: file does not exist.","Error",MB_OK);
fclose(file);
return 0;
}
fread(cHeader,54,1,file);
if(cHeader[0] != 'B' || cHeader[1] != 'M')
{
MessageBox(0,"Could not load bitmap: incorrect file type.","Error",MB_OK);
fclose(file);
return 0;
}
iHeight = *(int*)(cHeader+22);
iOffset = *(unsigned int*)(cHeader+10);
iWidth = *(int*)(cHeader+18);
delete[] data;
data = new BYTE[iHeight*iWidth*3];
iBits = int(cHeader[28]);
switch(iBits)
{
case 1:
fread(colorTable,8,1,file);
fseek(file,iOffset,SEEK_SET);
for(a = 0;a < iHeight;++a)
{
for(b = 0;b < iWidth;++b)
{
fread(&tempData,1,1,file);
for(c = 0;c < 8;++c)
{
for(d = 0;d < 3;++d)
{
pixel(b+c,a,d) = colorTable[((tempData >> (7-c))&1)*4+2-d];
}
}
}
}
break;
case 4:
fread(colorTable,64,1,file);
fseek(file,iOffset,SEEK_SET);
for(a = 0;a < 256;++a)
{
for(b = 0;b < 256;++b)
{
fread(&tempData,1,1,file);
for(c = 0;c < 3;++c)
{
pixel(b,a,c) = colorTable[tempData/16*4+2-c];
}
for(c = 0;c < 3;++c)
{
pixel(b+1,a,c) = colorTable[tempData%16*4+2-c];
}
}
}
break;
case 8:
fread(colorTable,1024,1,file);
fseek(file,iOffset,SEEK_SET);
for(a = 0;a < iHeight;++a)
{
for(b = 0;b < iWidth;++b)
{
fread(&tempData,1,1,file);
for(c = 0;c < 3;++c)
{
pixel(b,a,c) = colorTable[tempData*4+2-c];
}
}
}
break;
case 24:
fseek(file,iOffset,SEEK_SET);
fread(data,iHeight*iWidth*3,1,file);
for(a = 0;a < iHeight*iWidth*3;a += 3)
{
tempData = data[a];
data[a] = data[a+2];
data[a+2] = tempData;
}
break;
default:
fclose(file);
MessageBox(0,"Could not load bitmap: unknown file format.","Error",MB_OK);
}
generateTexture(&glTexture);
fclose(file);
return glTexture;
}
void bmp::generateTexture(GLuint *glTexture)
{
glGenTextures(1,glTexture);
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glBindTexture(GL_TEXTURE_2D,*glTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,3,iWidth,iHeight,0,GL_RGB,GL_UNSIGNED_BYTE,data);
glBindTexture(GL_TEXTURE_2D,0);
}
I just can't figure out why it's being rotated like that. The image does display, but as I said, it looks like it's being rotated 90 degrees clockwise and then flipped horizontally. Oh and, the image I'm trying to load is an 8 bit bitmap.
Any help is greatly appreciated!

New Topic/Question
Reply




MultiQuote




|