EasyBMP C++ Bitmap Library

EasyBMP Extensions: OpenGL

This extension to EasyBMP provides some basic interfaces to OpenGL.

Click here to download the extension. (Contained in the extensions package)

This extension allows you to convert BMP objects to textures that OpenGL can use for texture mapping, etc. Because the width and height of an OpenGL texture must be multiples of 2, the extension will automatically pad the texture with white pixels such that it fits in a 2n x 2n square.

Within the texture, the coordinates (x,y) gives a point x from the left and y from the bottom of the texture. Note that (0,0) gives the bottom left corner and (1,1) gives the top right corner of the image. The data members MaxX and MaxY give the coordinates of the edge of the original image within this padding.

class EasyBMP_Texture
{public:
 GLubyte* Texture; // an OpenGL-compatible array of bytes for the texture
 int Width; // width of the texture
 int Height; // height of the texture
 GLfloat MaxX; // max x coordinate of the texture
 GLfloat MaxY; // max y coordinate of the texture
 
 EasyBMP_Texture(); // creates an empty texture
 ~EasyBMP_Texture(); // leanup (don't use this)
 
 int TellWidth( void ); // tells the width of the texture
 int TellHeight( void ); // tells the height of the texture
 
 void SetSize( int NewWidth, int NewHeight ); // resizes the texture
 void ImportBMP( BMP& InputImage ); // imports and converts a BMP object
};

Here is a sample application:

#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <windows.h>

#include "EasyBMP.h"
#include "EasyBMP_OpenGL.h"

#include <cstdlib>
#include <cstdio>

using namespace std;

static GLuint texName;
GLubyte* Texture;
EasyBMP_Texture BMPtexture;

void display( void )
{
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 glEnable(GL_TEXTURE_2D);
 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
 glBindTexture(GL_TEXTURE_2D,texName);

 GLfloat MaxX = BMPtexture.MaxX;
 GLfloat MaxY = BMPtexture.MaxY;

 GLfloat Width = MaxX*4;
 GLfloat Height = MaxY*4;

 glPushMatrix();
 glTranslatef(-2,-2,0);

 glBegin(GL_QUADS);
  glTexCoord2f(0,0) ; glVertex3f(0,0,0);
  glTexCoord2f(MaxX,0); glVertex3f(Width,0,0);
  glTexCoord2f(MaxX,MaxY);glVertex3f(Width,Height,0);
  glTexCoord2f(0,MaxY); glVertex3f(0,Height,0);
 glEnd();

 glPopMatrix();

 glFlush();
 glDisable(GL_TEXTURE_2D);
}

void reshape(int w,int h)
{
 glViewport(0,0,(GLsizei) w,(GLsizei) h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(60,(GLfloat) w/(GLfloat) h, 1,30);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 glTranslatef(0,0,-3.6);
}

void init( void )
{
 glClearColor(0,0,0,0);
 glShadeModel(GL_FLAT);
 glEnable(GL_DEPTH_TEST);

 BMP Temp;
 Temp.ReadFromFile( "nixon.bmp" );
 BMPtexture.ImportBMP( Temp );

 glPixelStorei(GL_UNPACK_ALIGNMENT,1);

 glGenTextures(1,&texName);
 glBindTexture(GL_TEXTURE_2D, texName );

 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
 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_RGBA,BMPtexture.TellWidth(),BMPtexture.TellHeight(),
 0,GL_RGBA,GL_UNSIGNED_BYTE, BMPtexture.Texture );
}

int main( int argc, char* argv[] )
{
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
 glutInitWindowSize(250,250);
 glutInitWindowPosition(15,15);
 glutCreateWindow(argv[0]);
 init();

 glutReshapeFunc(reshape);
 glutDisplayFunc(display);
 glutMainLoop();

 return 0;
}

Here's how to compile it using mingw in Windows:

g++ -o easybmp_opengl easybmp_opengl.cpp -lopengl32 -lglut32 -lglu32

The result should be the image nixon.bmp on a black background.