OpenGL Font scrolling bug

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

Recommended Posts

Hi, im fairly new to openGL and all the 3D stuff has been going fine but ive been trying to render fonts in a 2D perspective by using the matrix stacks. What keeps happening is the text render at the bottom left of the screen and each frame seems to offset to the right really fast until you can't see it. Heres the code i use to do this:
void CBitmapFont::renderText(int xPos, int yPos, char *text, int screenWidth, int screenHeight)
{
if( m_fontBase == 0 || text == NULL )
return;

if( xPos &gt; screenWidth || yPos &gt; screenHeight)
return;

glDisable( GL_DEPTH_TEST );
glDisable( GL_LIGHTING );

setOrthoScreen( screenWidth, screenHeight );
glPushMatrix();
glTranslatef(xPos, yPos, 0);
glListBase( m_fontBase - 32 );
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text );;
glPopMatrix();
resetPerspectiveProjection();

glEnable( GL_DEPTH_TEST );							// Re-enables Depth Testing
glEnable( GL_LIGHTING );
}

void CBitmapFont::setOrthoScreen(int screenWidth, int screenHeight)
{
// switch to projection mode
glMatrixMode(GL_PROJECTION);
glPushMatrix();
// set a 2D orthographic projection
gluOrtho2D(0, screenWidth, 0, screenHeight);
// invert the y axis, down is positive
glScalef(1, -1, 1);
// move the origin from the bottom left corner
// to the upper left corner
glTranslatef(0, -screenHeight, 0);
glMatrixMode(GL_MODELVIEW);
}

void CBitmapFont::resetPerspectiveProjection()
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}

I originally did it the way it was in NeHe's guide and the same thing happened so i tried others and its still happening. Any help would be appreciated. [Edited by - Infinite_Daremo on June 26, 2008 11:00:36 AM]

Share on other sites
My first reaction was to check for pushing and popping the right matrices, which you seem to do in the code presented. Can you give us the rest of the rendering code, as well as where you set/manipulate xPos and yPos. When posting the code, put it in [source] and [/source] tags to make it all pretty-like and scrollable.

-jouley

Share on other sites
Heres the code, i commented out 2 lines incase they were causing the error. This is called every frame:

void CGameEngine::EventRender(){		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and z buffer	glLoadIdentity(); // Reset model view mat	// set the camera position	/*m_pCamera->Animate(0.2f);		glTranslatef(200, -300, 0);			m_pTerrain->doRender();*/	m_font->renderText(100, 500, "OpenGL Text Test", m_iWindowWidth, m_iWindowHeight );	glFlush();	SwapBuffers(m_hDC);}

Share on other sites
Quote:
 Original post by Infinite_DaremoHeres the code, i commented out 2 lines incase they were causing the error. This is called every frame:*** Source Snippet Removed ***

Just to clarify, you've tested without those lines?

I've made a little map of what happens every frame:
glLoadIdentity	(assumed modelview)renderText(100, 500, "Text", width, height)	setOrtho(width, height)		matMode(projection)		pushmatrix	-	proj_stack_depth += 1		loadIdentity		setup		madMode(modelview)	pushmatrix		-	mvw_stack_depth += 1	loadIdentity	translate	drawtext	popmatrix		-	mvw_stack_depth -= 1	resetPerspective		matMode(projection)		popMatrix	-	proj_stack_depth -= 1		matMode(modelview)

Nothing's jumping to mind, and I don't have time right now to test in my own environment, but perhaps this simplified view will give somebody else some insight...

If you can put together a test project that reproduces this with as little code as possible, hordes of people (at least 2, anyway) will download and do some debugging.

I'll keep pondering in the background, but I hope you get this resolved before I have time to dive into it again!

Share on other sites
Yeh i tested with those, i originally thought the terrain may have been setting to modify a different matrix so i commented it out incase, the problem persists either way. This isn't a big deal, its just a personal project during my holidays but its irritating me nonetheless. One thing to note is this error does not happen if i leave it as perspective.

I quickly rar'd the whole project:
http://rapidshare.com/files/125237874/Orth_Error.rar.html ~5mb

The files concerned are GameEngine which calls the render function of the BitmapFont class.

All functions relating to the drawing of the text is inside BitmapFont.

EDIT: Im almost positive its not the matrix stacks because un-commenting the lines i previously commented, they still render fine and if there was an error in the stacks they would not.

[Edited by - Infinite_Daremo on June 26, 2008 3:11:42 PM]

Share on other sites
I fixed this, it was rather simple although i don't know why it wouldnt work the other way. I changed glTranslate to glRaster2i and all is well. If someone can tell my why this happening id be greatful.

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5

• 20
• 12
• 13
• 14
• 80
• Forum Statistics

• Total Topics
632144
• Total Posts
3004399

×