8

Just wondering if someone can help me track down my issue with the following code where the text color is not being set correctly (its just rendering whatever color is in the background)

void RenderText(int x, int y, const char *string)
{
int i, len;

glUseProgram(0);

glLoadIdentity();
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, -5.0f);
glRasterPos2i(x, y);

glDisable(GL_TEXTURE_2D);
for (i = 0, len = strlen(string); i < len; i++)
{
    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int)string[i]);
}
glEnable(GL_TEXTURE_2D);
}

I've checked all the usual things (I think), disabling texturing, setting color before rasterPos'ing, etc Ive disabled shaders but Im still having issues

colordot
  • 231
  • 1
  • 3
  • 4
  • Please post a picture of what you observe. – Bahbar Apr 16 '10 at 21:14
  • When you say that it's "rendering whatever color is in the background", do you mean the color set with `glClearColor`, or something different? – bcat May 06 '10 at 03:26

3 Answers3

10

Looks like you've forgotten to glDisable(GL_LIGHTING) before drawing your string.

Martin Hennig
  • 551
  • 1
  • 6
  • 23
  • 1
    this comment really was a life saver, was not able to figure out why my text kept appearing in black. Can you please explain as to why do we have to turn off lighting before writing to the screen ? – geekoraul Nov 02 '12 at 16:22
  • 1
    The lighting in OpenGL only specifies, how the colours at a specific point in the scene are calculated. If youre not using lighting, all that OpenGL has to calculate the colour of a point is your GLColor command (and maybe some shading interpolation, but the colour is coming from glColor). if you turn glLighting on, there are several circumstances that influence the color of a point: the position and colour of the light source, the material of the point, the material colour of the point, and the texture. so turn it off to tell openGL to draw the next thing with your glColor, nothing more. – Martin Hennig Nov 19 '12 at 08:42
  • You could also have specified all lighting arguments correctly before drawing the characters so they would have been illuminated by a light source. that would have been much more extensive though. – Martin Hennig Nov 19 '12 at 08:44
3

No color is stored with any OpenGL bitmap (which is what glutBitmapCharacter created. The bitmap is monochrome and stores only shape.

When the bitmap is drawn (e.g. glBitmap or maybe glDrawLists), the current raster color is used. The raster color is not always the same as the active color, see http://www.opengl.org/wiki/Coloring_a_bitmap.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Color is usually controlled with the glColor3f function, thus if the text is white and shouldn't be then the following change should help:

glLoadIdentity();
glColor3f(0.5f, 0.5f, 0.5f);     //<-- this line controls the color (now text is gray)
glTranslatef(0.0f, 0.0f, -5.0f);
glRasterPos2i(x, y);

Also, calling glDisable(GL_TEXTURE_2D) and glEnable(GL_TEXTURE_2D) is unnecessary. Instead you can just call glBindTexture(GL_TEXTURE_2D,0) to disable textures and then use the same function to set the active texture. Just make sure to call glEnable(GL_TEXTURE_2D) in your initialization function.

fintelia
  • 1,201
  • 6
  • 17