PDA

View Full Version : problems with glBitMap()



jdaniel
03-10-2004, 06:07 AM
Hello,

I am having trouble with the glBitmap() function. I have no problem when drawing bitmaps as outlined in the redbook chapter8. What I have problem with is rendering 3D meshes *and* drawing bitmaps. The seem to disappear permanently after certain glut calls. Here is an example:

----------------
GLubyte rasters[24] = {
0xc0, 0x00,
0xc0, 0x00,
0xc0, 0x00,
0xc0, 0x00,
0xc0, 0x00,
0xff, 0x00,
0xff, 0x00,
0xc0, 0x00,
0xc0, 0x00,
0xc0, 0x00,
0xff, 0xc0,
0xff, 0xc0};

void init(void) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glClearColor(0.0, 0.0, 0.0, 0.0);
}

void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1.0, 1.0, 1.0, 0.5);
glRasterPos2i(40, 40);
glBitmap(10, 12, 0.0, 0.0, 11.0, 0.0, rasters);
glBitmap(10, 12, 0.0, 0.0, 11.0, 0.0, rasters);
glBitmap(10, 12, 0.0, 0.0, 11.0, 0.0, rasters);
glFlush();
}

void reshape(int w, int h) {
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}


void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 27: exit(0);
}
}



int main(int argc, char** argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(100, 100);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
---------------------


This code will draw 3 bitmaps (F's) onto the screen fine. The problem I am having however, is the gl state is in no condition to render 3d objects at this point. So after modifying the code slightly...

--------------------


GLubyte rasters[24] = {
0xc0, 0x00,
0xc0, 0x00,
0xc0, 0x00,
0xc0, 0x00,
0xc0, 0x00,
0xff, 0x00,
0xff, 0x00,
0xc0, 0x00,
0xc0, 0x00,
0xc0, 0x00,
0xff, 0xc0,
0xff, 0xc0};

void init(void) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glClearColor(0.0, 0.0, 0.0, 0.0);
}


void display(void) {

glViewport(0, 0, (GLsizei) 100, (GLsizei) 100);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 100, 0, 100);
glMatrixMode(GL_MODELVIEW);

glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1.0, 1.0, 1.0, 0.5);
glRasterPos2i(40, 40);
glBitmap(10, 12, 0.0, 0.0, 11.0, 0.0, rasters);
glBitmap(10, 12, 0.0, 0.0, 11.0, 0.0, rasters);
glBitmap(10, 12, 0.0, 0.0, 11.0, 0.0, rasters);
glFlush();

}

void reshape(int w, int h) {
if(h == 0) h = 1;


// Set Viewport to window dimensions
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(15.0f, (GLfloat)w / (GLfloat)h, 0.01f, 9999999.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

}

void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 27: exit(0);
}
}



int main(int argc, char** argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(100, 100);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
----------------------


Now the bitmaps will not show up. The only thing I am really changing is the init() function to set things up for a mesh render, and then I am trying to change the state back in the display function so I can rasterize the bitmaps.

If anyone can help me solve this problem you would be curing a major headache for a stranger.

Thanks!

Relic
03-10-2004, 08:44 AM
Put a glLoadIdentity() after the glMatrixMode(GL_MODELVIEW) in the display function.


"gluPerspective(15.0f, (GLfloat)w / (GLfloat)h, 0.01f, 9999999.0f);"

This is especially bad for depth buffering precision. You only have 24 bits depth precision normally. Make sure you set the zNear and zFar to more reasonable values which enclose your scene as tightly as possible, that is make your zFar/zNear ration as small as possible,
For starters, most geometry lies in the unit cube in demos so a
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0, 1.0, 5.0);
and a modelview setup with

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -3.0f);
should work.

Try this in the reshape, remove the matrices from the display functions and use e.g. a glRasterpos3f(0.0f, 0.0f, 0.0f);
This printf your text at a 3D position at the origin, which is the middle of the screen with the above setup.

jdaniel
03-10-2004, 09:20 AM
Great advice Relic, and thank you for taking the time to reply. Your suggestions are valuable.

jdaniel