#### Archived

This topic is now archived and is closed to further replies.

# glPrint Not Working With TrueType-Derived Font

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

## Recommended Posts

Hey, all! I'm having some problems here. I'm trying to make a glPrint routine that will write a formatted text string onto the screen at specified coordinates using an ortho setup. Here's the subroutine:
GLvoid glPrint(GLint x, GLint y, const char *fmt, ...)				// Custom GL "Print" Routine

{
char		text[256];					// Holds Our String

va_list		ap;							// Pointer To List Of Arguments

if (fmt == NULL)						// If There's No Text

return;								// Do Nothing

va_start(ap, fmt);						// Parses The String For Variables

vsprintf(text, fmt, ap);			// And Converts Symbols To Actual Numbers

va_end(ap);								// Results Are Stored In Text

glDisable(GL_DEPTH_TEST);				// Disables depth testing

/****************************
* NOTE: You can enable Depth Testing, but text blending will not occur, and
* text may end up behind objects.  As this can be undesirable, it's best to
* just leave it off.
****************************/
glMatrixMode(GL_PROJECTION);			// Select the Projection Matrix

glPushMatrix();							// Store the Projection Matrix

// NOTE: If we didn't store the projection matrix, then we would lose the world

// when we change our matrices to display the text properly.

glLoadIdentity();						// Reset the Projection Matrix

glOrtho(0,800,600,0,-1,1);				// Set up an Ortho screen

glMatrixMode(GL_MODELVIEW);				// Select the Modelview Matrix

glPushMatrix();							// Store the Modelview Matrix

glLoadIdentity();						// Reset the Modelview Matrix

glTranslated(x,y,0);					// Position the text (0,0 - Bottom left)

glPushAttrib(GL_LIST_BIT);
glListBase(base - 32);					// Sets The Base Character to 32	( NEW )

glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text	( NEW )

glPopAttrib();
glMatrixMode(GL_PROJECTION);			// Select the Projection Matrix

glPopMatrix();							// Restore the Projection Matrix

glMatrixMode(GL_MODELVIEW);				// Select the Modelview Matrix

glPopMatrix();							// Restore the Modelview Matrix

glEnable(GL_DEPTH_TEST);				// Enables Depth Testing

}

When I attempt to use this function, the text is placed in the wrong location and runs together. When I use glRasterPos2f before calling the function, it ignores the function's translate command entirely. Here're some screenshots of what happens when I try to use this: This is what I want it to look like: MNGoldenEagle ~Webmaster of ZSO What I Do: BASIC, QuickBASIC, Visual C++, PHP, mySQL, javascript, HTML, OpenGL [edited by - mngoldeneagle on December 15, 2003 12:22:21 PM]

##### Share on other sites
One problem off hand (which could be the reason for your coordinates problem) is that x and y are declared as GLints and used with glTranslated(x,y,0) as doubles. The reason why it ignores RasterPos2f if I'm not mistaken is because you declare LoadIdentity() before writing the text. This I'm pretty sure overwrites the rasterpos.

If that doesn't help, could you please post what you're feeding into the function as well as previous necessary functions?

[edited by - strider44 on December 16, 2003 9:02:16 AM]

##### Share on other sites
add an if statement like this
if(c=''\n''){
line++;
}
else{
draw(c);
};

##### Share on other sites
that couldn''t be it - he couldn''t be trying that, as if he were there would be /n''s put throughout the text

##### Share on other sites
Thats not true, if you only build the display lists of charachter 32 to something (which is standard in the tutorial with the glPrint function), there isn''t drawn anything.

##### Share on other sites
hmm, what is that a response to?

##### Share on other sites
Umm... well, I''m still having troubles with the function, so I''ll get into specifics here (and sorry about the long delay, I''ve been super-busy). This is what I have for calling the function:

switch(textstage) {	case 1:		glPrint("Welcome to the world of digital physics.");		glPrint("You may not know it, but that ball rolling below is actually very complex");		glPrint("and involves many different laws of physics, such as friction, force,");		glPrint("momentum, energy, and even heat!");		glRasterPos2f(-10.0f, 5.0f);		glColor3f(0.0f, (glow/255.0f), 0.0f);		glBegin(GL_TRIANGLES);			glVertex3f(-9.7f, 6.8f, 0.0f);			glVertex3f(-9.2f, 6.8f, 0.0f);			glVertex3f(-9.45f, 6.3f, 0.0f);		glEnd();		if(glow < 255)			(ascend==true) ? glow++ : (glow != 0) ? glow-- : ascend=true;		if(glow == 255) { ascend=false; glow--; }		break;...

Okay, and that produces all of the text appearing at the origin onwards (as shown in the first screenshot). The following code arranges the text as it''s supposed to, but does it with floats on a standard perspective instead of ortho, which is difficult and impractical:

switch(textstage) {	case 3:		glRasterPos2f(-10.0f, 8.5f);		glPrint("Welcome to the world of digital physics.");		glRasterPos2f(-10.0f, 8.1f);		glPrint("You may not know it, but that ball rolling below is actually very complex");		glRasterPos2f(-10.0f, 7.7f);		glPrint("and involves many different laws of physics, such as friction, force,");		glRasterPos2f(-10.0f, 7.3f);		glPrint("momentum, energy, and even heat!");		glRasterPos2f(-10.0f, 5.0f);		glColor3f(0.0f, (glow/255.0f), 0.0f);		glBegin(GL_TRIANGLES);			glVertex3f(-9.7f, 6.8f, 0.0f);			glVertex3f(-9.2f, 6.8f, 0.0f);			glVertex3f(-9.45f, 6.3f, 0.0f);		glEnd();		if(glow < 255)			(ascend==true) ? glow++ : (glow != 0) ? glow-- : ascend=true;		if(glow == 255) { ascend=false; glow--; }		break;...

##### Share on other sites
I don't see anything wrong with it... except your print function's arguments and implementation don't match. Maybe your matrixs are mixed?

[edited by - Xiachunyi on December 30, 2003 6:24:39 PM]

##### Share on other sites
You have to retranslate every call of glPrint. Perhaps you had better include in glPrint a routine that automatically translates to the lower line. Perhaps this may work:
for (unsigned int loop=0;loop<(strlen(text));loop++)	// Loop To Find Text Length{	length+=gmf[text[loop]].gmfCellIncX;	//Increase Length By Each Characters Width}glPushAttrib(GL_LIST_BIT);			//Pushes The Display List BitsglListBase(base);				//Sets The Base Character to 0glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	//Draws The Display List TextglPopAttrib();					//Pops The Display List BitsglTranslatef(-length,-0.4f,0.0f);		//Translates back so it doesn't ruin future calculations, and down a bit

You can still use translatef if you want just the writing will be the same size on the screen. (stole it and modified it a bit from one of NeHe's lessons)

I just thought of something that's probably even better:
glPushMatrix();glPrint(  ...  );glPopMatrix();glTranslatef(0.0f,-0.4f,0.0f);

try one of those if you don't want to use RasterPos. One of them will probably work

[edited by - strider44 on December 31, 2003 9:42:50 PM]

[edited by - strider44 on December 31, 2003 9:43:28 PM]

##### Share on other sites
No, the problem isn''t that. I can use universe coordinates fine the way it is. The problem is that I can''t seem to write the text in an ortho environment where the coordinates are 0,0 for the upper-left of the client window and 800,600 for the lower-right of the client window.
(And yes, you''re right, the parameters were incorrect. I had actually removed the coordinates because I thought the glPrint function I had up here hadn''t utilized the x,y parameters. Silly me.)

• ### Forum Statistics

• Total Topics
628715
• Total Posts
2984362

• 24
• 11
• 10
• 13
• 14