Home > other >  glutBitmapString() does not use the color set for it
glutBitmapString() does not use the color set for it

Time:01-18

I call a custom function like this to put a string on the screen:

// Debug: draw a blue line

glColor3f((GLfloat)0, (GLfloat)0, (GLfloat)1);
glBegin(GL_LINES);
glVertex2i(1, 1);
glVertex2i(1, 10);
glEnd();

// Call text renderer.

write_default_bitmap_text((const unsigned char*)"TEST string 1", 100, 100);
write_default_bitmap_text((const unsigned char*)"TEST string 2", 100, 120);

and this is the custom function:

void write_default_bitmap_text(const unsigned char* text,
                            const int x,
                            const int y,
                            const font_x_align x_align)
{
    if (x_align == font_x_align::Left) {
        glRasterPos2i(x, y);
    }
    else if (x_align == font_x_align::Center) {
        glRasterPos2i(x - glutBitmapLength(default_font, text)/2, y);
    }
    else {
        glRasterPos2i(x - glutBitmapLength(default_font, text), y);
    }
    glColor3f((GLfloat)1, (GLfloat)0, (GLfloat)0); //XXX debug
    glDisable(GL_TEXTURE_2D);                      //XXX debug
    glDisable(GL_LIGHTING);                        //XXX debug
    glutBitmapString(default_font, text);
    next_call_font_color = default_font_color;
}

where I hardcoded the text color to be red for debugging. To my surprise I see that the first string uses the blue color for the line just before calling write_default_bitmap_text(), but not red, and only the second string becomes red as intended.

If I place the few lines above for drawing the blue line inside my write_default_bitmap_text() body, then the text does not use the blue color but whatever was used for line drawing before calling write_default_bitmap_text() for the first time.

I have absolutely no idea what happens here.

CodePudding user response:

glRasterPos*() "locks in" the current color/texure-coordinate state (much like glVertex*() does) while setting the raster position, so calling glColor*() after it will only affect the next glRasterPos*() call.

  • Related