View Full Version : Text in opengl

04-26-2004, 08:19 AM
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.

04-26-2004, 08:54 AM
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

char buffer[BUFFERSIZE];
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)
int l,i;

l=strlen( st );
glRasterPos3i( x, y, -1);
for( i=0; i < l; i++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]);

Sprint( 0,0, "Some text");

To print a float you need to convert it to ASCII format.

char text[32]; // space in which to store converted float.
float x;

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);
Sprint(0,0, text);

On screen would be printed "Score: XXX.XXX"

04-26-2004, 04:53 PM
i need it ,too

04-28-2004, 10:32 PM

In stead of the sprintf being outside the Sprint function, you can also choose to give Sprint multiple arguments:

/* Add this line to include */
#include <stdarg.h>

/* The fourth argument are literally three dots */
Sprint(int x, int y, char * msg, ...)
char buf[1024]; /* Or any other buffer */
va_list args;
va_start(args, msg);
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.