Jump to content
  • Advertisement
Sign in to follow this  
Endar

OpenGL Bitmap font colors

This topic is 4503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is a little code snippet from me. Let me paint you a picture: I have a scene that consists of nothing but a rotating cube and some bitmap font text on the window. Lighting is turned on. Texturing is on. When I turn texturing off, because of the lighting the cube turns white instead of whatever color it would normally turn. The text also turns white and I don't know why. I thought that maybe the color was set to white because of the cube and then I didn't set the color for the text before drawing it. See below.
/**
 * Draw some text on the window
 * The majority of code from this function is from the NeHe tutorial 13.
 * \param fhandle The handle for the bitmap font
 * \param text A char pointer to the string
 * \param x The x opengl coord to draw the text at
 * \param y The y opengl coord to draw the text at
 * \param len The length of the string (if set to -1, it will be counted inside the function using 'strlen')
 */
void COpenGLDriver::drawText(int fhandle, const char* text, float x, float y, int len = -1)
{
	// no string == don't print anything
	if( text == NULL )
		return;

	// store the length of the string (either supplied or we count it using 'strlen')
	int length = (len == -1) ? strlen(text) : len;
	
	// move the window position to the position to draw the text
	pglWindowPos2f(x, y);

	// get the color and set the ogl color
	video::CColor text_color = ((COpenGLBitmapFont*)m_fontList[fhandle])->getColor();
	float oglcolor[4];
	glGetFloatv(GL_CURRENT_COLOR, oglcolor);	// get the current ogl color

	// set the ogl color
	glColor4f(	text_color.getRedFloat(), 
				text_color.getGreenFloat(), 
				text_color.getBlueFloat(), 
				text_color.getAlphaFloat() );


	glPushAttrib(GL_LIST_BIT);						// Pushes the display list bits
	glListBase( ((COpenGLBitmapFont*)m_fontList[fhandle])->getID() - 32);	// Provides offset for characters

	glCallLists(length, GL_UNSIGNED_BYTE, text);	// Draws the display list text
	glPopAttrib();		// Pops the display list bits

	// set the previous ogl color
	glColor4f( oglcolor[0], oglcolor[1], oglcolor[2], oglcolor[3] );
}
I can't really think of any other reason why the text would turn white. Has this happened to anyone before?

Share this post


Link to post
Share on other sites
Advertisement
The raster color is a different state from the primary color. The raster color is copied from the primary color when the raster position is set. Changes to the primary color does not change the raster color. Therefore, to set raster color, you must set the corresponding primary color before setting the raster position.

Share this post


Link to post
Share on other sites
That fixed the white problem, but now my text is blue when the texturing is on and black when the texturing is off. The text color is supposed to be black all the time.

This will take some thought and code searching. [smile]

I was going to ask another question as well: when calling glEnable, glDisable, are those costly calls to make? I mean, if I turn off lighting, does OGL have to wait until the pipeline is completely empty and then do the change? Is that a costly thing, time wise?

Share this post


Link to post
Share on other sites
If you leave texturing on it will affect the output. Fragments produced by pixel transfers are no different that those from rasterization of primitives. If texturing is enabled, texturing is applied as usual. There is a state for texture coordinates associated with the raster position, just like there is a raster color. The current texture coordinate when the raster position is set will be used for texturing the fragments. Result will depend on how the texture combiners are set up.

How an implementation chooses to treat enabling/disabling of states is completely up to the developer of the implementation. Some states may be very expensive, some not. Some may require a complete flush, some not. Some states being enabled/disabled in some situations may be more/less expensive than being enabled/disabled in other situations. But assume they do cost and don't do any unnecessary state changes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!