I’m trying to create a texture object in OpenGL from a bitmap. (And by bitmap, I mean a true one-bit-per-pixel OpenGL type bitmap, not that Microsoft abomination file type.)
I would think that I could use glTexImage2D like any other texture, but I can’t seem to get this to work. Below is a test program that I’m using. Perhaps you can see what I am doing wrong. The program displays a quad and should show the texture, but it currently only shows a solid red rectangle. Thanks for your help.
The following program uses SDL as the window manager. It should work in both Windows and Linux, but has only been tested in Linux.
#include <stdio.h>
#ifdef WIN32include <windows.h>
#endif
#include <GL/gl.h>
#include <SDL/SDL.h>#if 0
const unsigned char BITMAP_DATA = {
0x41, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00,
0x42, 0x00, 0x00, 0x00,
0x22, 0x00, 0x00, 0x00,
0x22, 0x00, 0x00, 0x00,
0x14, 0x00, 0x00, 0x00,
0x14, 0x00, 0x00, 0x00,
0x1C, 0x00, 0x00, 0x00,
0x0C, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
0x30, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
#endif//#if 0
const unsigned char BITMAP_DATA = {
0x41,
0x41,
0x42,
0x22,
0x22,
0x14,
0x14,
0x1C,
0x0C,
0x08,
0x08,
0x30,
0x00,
0x00,
0x00,
0x00
};
//#endifconst int BITMAP_PITCH = 1;
const int BITMAP_ROWS = 16;
const int BITMAP_COLS = 8* BITMAP_PITCH;
const int NUM_PIXELS = BITMAP_COLS * BITMAP_ROWS;const int SCREEN_WIDTH = 900;
const int SCREEN_HEIGHT = 750;int main () {
// Init SDL
SDL_Init (SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_OPENGL | SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL){ return 0; }
SDL_WM_SetCaption (“Test Bitmap OpenGL Texture Program”, NULL);// Init OpenGL
glViewport (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, -1, 1);
glMatrixMode (GL_MODELVIEW);glClearColor (0.0, 0.0, 0.0, 0.0);
glClearDepth (1.0);
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LEQUAL);
glPolygonMode (GL_FRONT, GL_FILL);
glPolygonMode (GL_BACK, GL_LINE);glEnable (GL_TEXTURE_2D);
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);// Create texture
unsigned int texObj;
glGenTextures (1, &texObj);if (texObj == 0){
fprintf (stderr, "Unable to create an OpenGL texture object
");
SDL_Quit();
return 1;
}glBindTexture (GL_TEXTURE_2D, texObj);
glTexImage2D (
/target/ GL_TEXTURE_2D,
/level/ 0,
/internalFormat/ GL_INTENSITY,
/width/ BITMAP_COLS,
/height/ BITMAP_ROWS,
/border/ 0,
/format/ GL_LUMINANCE,
/type/ GL_BITMAP,
/texels/ BITMAP_DATA
);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);// Show a quad with the texture
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glTranslatef (200.0f, 50.0f, 0.0f);
//glBindTexture (GL_TEXTURE_2D, texObj);
glBegin (GL_QUADS);
glColor3f (1.0, 0.0, 0.0);
glTexCoord2f (0.0, 1.0); glVertex3f ( 0.0f, 0.0f, 0.0f);
glTexCoord2f (1.0, 1.0); glVertex3f (400.0f, 0.0f, 0.0f);
glTexCoord2f (1.0, 0.0); glVertex3f (400.0f, 600.0f, 0.0f);
glTexCoord2f (0.0, 0.0); glVertex3f ( 0.0f, 600.0f, 0.0f);
glEnd ();SDL_GL_SwapBuffers ();
SDL_Event event;
bool done = false;while (!done){
SDL_PollEvent (&event); if (event.type == SDL_QUIT){ done = true; }else if (event.type == SDL_KEYUP){ if (event.key.keysym.sym == SDLK_ESCAPE) done = true; }
}
SDL_Quit ();
glDeleteTextures (1, &texObj);return 0;
}
Thanks for your help.