View Full Version : Text in opengl
Just a quick question I wondered if anyone could help me with. I am in the process creating an open gl program and I want to display some variables to the screen. The variables are all floats.
I have been able to display arrays of characters to the screen using the glutBitmapCharacter() command and looping through the string array drawing individual characters. However I cant seem to draw the variable values to screen at all. Is there any way I can display the contents of float variables to screen using this method? Or is there a simpler method avliable?
Any help would be greatly appreciated.
You should sprintf the variables. This means you turn the variables into a string first. I think that sprintf is in the stdio header. After I suppose you can handle it. Here's how:
#define BUFFERSIZE 100
float f1 = 0.234;
float f2 = -2.3954;
sprintf(buffer, "First float is: %f. The second one is: %f", f1, f2);Then print the buffer as usual.
I hope you get the picture. Post again if you have a problem.
04-26-2004, 09:00 AM
Here is the code for the text print routine I use.
void Sprint( int x, int y, char *st)
l=strlen( st );
glRasterPos3i( x, y, -1);
for( i=0; i < l; i++)
Sprint( 0,0, "Some text");
To print a float you need to convert it to ASCII format.
char text; // space in which to store converted float.
sprintf( text, "%f", x); // You can also format the precision, %3.3f would give a max of three digits before decimal and three digits after.
Sprint( 0,0, text); // Print float value.
also could do something like this:
sprintf( text, "Score: %3.3f", x);
On screen would be printed "Score: XXX.XXX"
In stead of the sprintf being outside the Sprint function, you can also choose to give Sprint multiple arguments:
/* Add this line to include */
/* The fourth argument are literally three dots */
Sprint(int x, int y, char * msg, ...)
char buf; /* Or any other buffer */
vsprintf(buf, msg, args);
/* And then print the string buf in openGL */
This mechanism is called variable argument lists. You can google for more information, keywords: variable argument list.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.