Original Post
Heres the deal, everytime I see something on loading .bmp''s, it''s written in c. I wanted to write one using C++ style file i/o, and it''s not working. I wrote this using the equivalent c++ functions for the c code used in my book (opengl game programming). I just like c++ stuff I guess cause I''m more familiar with it. Here''s the code
that is the first file. Then there''s this one
and then here''s the main file
Does anyone see what might be keeping this from working? It''s never putting anything into my BITMAPINFOHEADER or BITMAPFILEHEADER struct, and returnint 0, and I can''t seem to get it.
#ifndef ImageLoad_h
#define ImageLoad_h
namespace ImageLoad
{
#define BITMAPID 0x4D42
struct BITMAPINFOHEADER
{
unsigned int Size;
int Width;
int Height;
unsigned short int Planes;
unsigned short int BitCount;
unsigned int Compression;
unsigned int SizeImage;
int XPelsPerMeter;
int YPelsPerMeter;
unsigned int ColorsUsed;
unsigned int ColorsImportant;
BITMAPINFOHEADER();
};
struct BITMAPFILEHEADER
{
unsigned short int Type;
unsigned int Size;
unsigned short int Reserved1;
unsigned short int Reserved2;
unsigned int OffBits;
BITMAPFILEHEADER();
};
class ImageLoader
{
private:
public:
ImageLoader() { };
~ImageLoader() { };
unsigned char *LoadBitmapFile(char *, BITMAPINFOHEADER *);
};
}
#endif
#include "ImageLoad.h"
#include <fstream>
using std::fstream;
using std::ios;
namespace ImageLoad
{
BITMAPINFOHEADER::BITMAPINFOHEADER()
{
Size=Compression=SizeImage=ColorsUsed=ColorsImportant=0;
Width=Height=XPelsPerMeter=YPelsPerMeter=0;
Planes=BitCount=0;
}
BITMAPFILEHEADER::BITMAPFILEHEADER()
{
Type=Reserved1=Reserved2=0;
Size=OffBits=0;
}
unsigned char * ImageLoader::LoadBitmapFile(char *FileName, BITMAPINFOHEADER *BitMapInfoHeader)
{
fstream InputFile;
BITMAPFILEHEADER BitMapFileHeader;
unsigned char *BitMapImage;
int ImageIndex = 0;
unsigned char TempRGB;
InputFile.open(FileName, ios::in|ios::binary);
if(!InputFile)
return NULL;
InputFile >> BitMapFileHeader.Type >> BitMapFileHeader.Size
>> BitMapFileHeader.Reserved1 >> BitMapFileHeader.Reserved2
>> BitMapFileHeader.OffBits;
if(BitMapFileHeader.Type != BITMAPID)
{
InputFile.close();
return NULL;
}
InputFile >> BitMapInfoHeader->Size >> BitMapInfoHeader->Width
>> BitMapInfoHeader->Height >> BitMapInfoHeader->Planes
>> BitMapInfoHeader->BitCount >> BitMapInfoHeader->Compression
>> BitMapInfoHeader->SizeImage >> BitMapInfoHeader->XPelsPerMeter
>> BitMapInfoHeader->YPelsPerMeter >> BitMapInfoHeader->ColorsUsed
>> BitMapInfoHeader->ColorsImportant;
InputFile.seekg(BitMapFileHeader.OffBits);
BitMapImage = new unsigned char[BitMapInfoHeader->SizeImage];
if(!BitMapImage)
{
delete BitMapImage;
InputFile.close();
return NULL;
}
for(unsigned long i = 0; i < BitMapInfoHeader->SizeImage; i++)
InputFile >> BitMapImage[i];
if(BitMapImage == NULL)
{
InputFile.close();
return NULL;
}
for(ImageIndex = 0; ImageIndex < BitMapInfoHeader->SizeImage; ImageIndex += 3)
{
TempRGB = BitMapImage[ImageIndex];
BitMapImage[ImageIndex] = BitMapImage[ImageIndex + 2];
BitMapImage[ImageIndex + 2] = TempRGB;
}
InputFile.close();
return BitMapImage;
}
}
#include <iostream>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include "ImageLoad.h"
using namespace std;
using namespace ImageLoad;
ImageLoader MyLoader;
ImageLoad::BITMAPINFOHEADER BMInfoHeader;
unsigned char *data;
unsigned int texture;
void Init(void)
{
data = MyLoader.LoadBitmapFile("MyBitmap.bmp", &BMInfoHeader);
if(!data)
exit(0);
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, BMInfoHeader.Width,
BMInfoHeader.Height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glShadeModel(GL_SMOOTH);
glHint(GL_NICEST, GL_POLYGON_SMOOTH_HINT);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.2f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100, 100, -75, 75, -10, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void MyKeyboard(unsigned char key, int x, int y)
{
if(key)
exit(0);
}
void MyDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-25, -25, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 25, -25, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 25, 25, 0);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-25, 25, 0);
glEnd();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow("ImageLoader");
glutFullScreen();
Init();
glutKeyboardFunc(MyKeyboard);
glutDisplayFunc(MyDisplay);
glutMainLoop();
return 0;
}