OpenGL wgl center text - c++

I am drawing text in an OpenGL context under Windows, with the following helper function, I got ir from http://nehe.gamedev.net/tutorial/bitmap_fonts/17002/ :
GLvoid glPrint(HDC hDC, const char *fmt, ...) //Custom GL "Print" Routine
{
char text[256]; // Holds Our String
va_list ap; // Pointer To List Of Arguments
if (fmt == nullptr) // If There's No Text
return; // Do Nothing
va_start(ap, fmt); // Parses The String For Variables
vsprintf_s(text, fmt, ap); // And Converts Symbols To Actual Numbers
va_end(ap); // Results Are Stored In Text
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(base - 32); // Sets The Base Character to 32
glCallLists((GLsizei)strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
}
I position the text by calling
glTranslatef(x, y, z);
glRasterPos2f(0.f, 0.f);
before glPrint, where x, yand zis the point coordinates where I would like to center my text.
This draws my text starting on the bottom-left corner. How can I center this text on its own centroid?
I tried GetTextExtentPoint32 to position my text, as follows
SIZE extent;
GetTextExtentPoint32(hDC, SpinTools::string2wstring(text).c_str(), strlen(text), &extent);
glRasterPos2f(-extent.cx / 2, -extent.cy / 2);
but this is not correct.

I think you idea with GetTextExtentPoint32 was right. But this futnction returns value in screen space, you need to convert it to [-1..1] sapce. You can try this code: glRasterPos2f((float)-extent.cx / (g_width), (float)-extent.cy / (g_height)); g_height and g_width are size of window

Related

glutBitmapString() does not use the color set for it

I call a custom function like this to put a string on the screen:
// Debug: draw a blue line
glColor3f((GLfloat)0, (GLfloat)0, (GLfloat)1);
glBegin(GL_LINES);
glVertex2i(1, 1);
glVertex2i(1, 10);
glEnd();
// Call text renderer.
write_default_bitmap_text((const unsigned char*)"TEST string 1", 100, 100);
write_default_bitmap_text((const unsigned char*)"TEST string 2", 100, 120);
and this is the custom function:
void write_default_bitmap_text(const unsigned char* text,
const int x,
const int y,
const font_x_align x_align)
{
if (x_align == font_x_align::Left) {
glRasterPos2i(x, y);
}
else if (x_align == font_x_align::Center) {
glRasterPos2i(x - glutBitmapLength(default_font, text)/2, y);
}
else {
glRasterPos2i(x - glutBitmapLength(default_font, text), y);
}
glColor3f((GLfloat)1, (GLfloat)0, (GLfloat)0); //XXX debug
glDisable(GL_TEXTURE_2D); //XXX debug
glDisable(GL_LIGHTING); //XXX debug
glutBitmapString(default_font, text);
next_call_font_color = default_font_color;
}
where I hardcoded the text color to be red for debugging. To my surprise I see that the first string uses the blue color for the line just before calling write_default_bitmap_text(), but not red, and only the second string becomes red as intended.
If I place the few lines above for drawing the blue line inside my write_default_bitmap_text() body, then the text does not use the blue color but whatever was used for line drawing before calling write_default_bitmap_text() for the first time.
I have absolutely no idea what happens here.
glRasterPos*() "locks in" the current color/texure-coordinate state (much like glVertex*() does) while setting the raster position, so calling glColor*() after it will only affect the next glRasterPos*() call.

Why drawString method does not seem to always start at the given coordinates?

In my code I cannot draw a String at precise coordinates. Its upper left corner does not start at the given coordinates but somewhere else. However if I draw a rectangle from the same given coordinates it is well placed. How on earth can this behaviour be possible ?
Here is my code I call in the beforeShow() method :
Image photoBase = fetchResourceFile().getImage("Voiture_4_3.jpg");
Image watermark = fetchResourceFile().getImage("Watermark.png");
f.setLayout(new LayeredLayout());
final Label drawing = new Label();
f.addComponent(drawing);
// Image mutable dans laquelle on va dessiner (fond blancpar défaut)
Image mutableImage = Image.createImage(photoBase.getWidth(), photoBase.getHeight());
// Paint all the stuff
paintAll(mutableImage.getGraphics(), photoBase, watermark, photoBase.getWidth(), photoBase.getHeight());
drawing.getUnselectedStyle().setBgImage(mutableImage);
drawing.getUnselectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
// Save the graphics
// Save the image with the ImageIO class
long time = new Date().getTime();
OutputStream os;
try {
os = Storage.getInstance().createOutputStream("screenshot_" + Long.toString(time) + ".png");
ImageIO.getImageIO().save(mutableImage, os, ImageIO.FORMAT_PNG, 1.0f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And the paintAll method
public void paintAll(Graphics g, Image background, Image watermark, int width, int height) {
// Full quality
float saveQuality = 1.0f;
// Create image as buffer
Image imageBuffer = Image.createImage(width, height, 0xffffff);
// Create graphics out of image object
Graphics imageGraphics = imageBuffer.getGraphics();
// Do your drawing operations on the graphics from the image
imageGraphics.drawImage(background, 0, 0);
imageGraphics.drawImage(watermark, 0, 0);
imageGraphics.setColor(0xFF0000);
// Upper left corner
imageGraphics.fillRect(0, 0, 10, 10);
// Lower right corner
imageGraphics.setColor(0x00FF00);
imageGraphics.fillRect(width - 10, height - 10, 10, 10);
imageGraphics.setColor(0xFF0000);
Font f = Font.createTrueTypeFont("Geometos", "Geometos.ttf").derive(220, Font.STYLE_BOLD);
imageGraphics.setFont(f);
// Draw a string right below the M from Mercedes on the car windscreen (measured in Gimp)
int w = 0, h = 0;
imageGraphics.drawString("HelloWorld", w, h);
// Coin haut droit de la string
imageGraphics.setColor(0x0000FF);
imageGraphics.fillRect(w, h, 20, 20);
// Draw the complete image on your Graphics object g (the screen I guess)
g.drawImage(imageBuffer, 0, 0);
}
Result for w = 0, h = 0 (no apparent offset) :
Result for w = 841 , h = 610 (offset appears on both axis : there is an offset between the blue point near Mercedes M on the windscreen and the Hello World String)
EDIT1:
I also read this SO question for Android where it is advised to convert the dpi into pixel. Does it also applies in Codename One ? If so how can I do that ? I tried
Display.getInstance().convertToPixel(measureInMillimeterFromGimp)
without success (I used mm because the javadoc tells that dpi is roughly 1 mm)
Any help would be appreciated,
Cheers
Both g and imageGraphics are the same graphics created twice which might have some implications (not really sure)...
You also set the mutable image to the background of a style before you finished drawing it. I don't know if this will be the reason for the oddities you are seeing but I would suspect that code.
Inspired from Gabriel Hass' answer I finally made it work using another intermediate Image to only write the String at (0 ; 0) and then drawing this image on the the imageBuffer Image now at the right coordinates. It works but to my mind drawString(Image, Coordinates) should directly draw at the given coordinates, shouldn't it #Shai ?
Here is the method paintAll I used to solve my problem (beforeShow code hasn't changed) :
// Full quality
float saveQuality = 1.0f;
String mess = "HelloWorld";
// Create image as buffer
Image imageBuffer = Image.createImage(width, height, 0xffffff);
// Create graphics out of image object
Graphics imageGraphics = imageBuffer.getGraphics();
// Do your drawing operations on the graphics from the image
imageGraphics.drawImage(background, 0, 0);
imageGraphics.drawImage(watermark, 0, 0);
imageGraphics.setColor(0xFF0000);
// Upper left corner
imageGraphics.fillRect(0, 0, 10, 10);
// Lower right corner
imageGraphics.setColor(0x00FF00);
imageGraphics.fillRect(width - 10, height - 10, 10, 10);
// Create an intermediate image just with the message string (will be moved to the right coordinates later)
Font f = Font.createTrueTypeFont("Geometos", "Geometos.ttf").derive(150, Font.STYLE_BOLD);
// Get the message dimensions
int messWidth = f.stringWidth(mess);
int messHeight = f.getHeight();
Image messageImageBuffer = Image.createImage(messWidth, messHeight, 0xffffff);
Graphics messageImageGraphics = messageImageBuffer.getGraphics();
messageImageGraphics.setColor(0xFF0000);
messageImageGraphics.setFont(f);
// Write the string at (0; 0)
messageImageGraphics.drawString(mess, 0, 0);
// Move the string to its final location right below the M from Mercedes on the car windscreen (measured in Gimp)
int w = 841, h = 610;
imageGraphics.drawImage(messageImageBuffer, w, h);
// This "point" is expected to be on the lower left corner of the M letter from Mercedes and on the upper left corner of the message string
imageGraphics.setColor(0x0000FF);
imageGraphics.fillRect(w, h, 20, 20);
// Draw the complete image on your Graphics object g
g.drawImage(imageBuffer, 0, 0);

text in 3d space - how to carriage return? (openGL, C++, OSX)

Here is my if statement that detects a bomb threat (just a game I'm making)...
if (exodus2_bomb == 1)
{
float exodus2_theta_math = (exodus2_theta)/10.0*M_PI;
float exodus2_phi_math = (exodus2_phi)/10.0*2*M_PI;
r_exodus2_x = radius_exodus_pos * sin(exodus2_theta_math) * cos(exodus2_phi_math);
r_exodus2_y = radius_exodus_pos * sin(exodus2_theta_math) * sin(exodus2_phi_math);
r_exodus2_z = radius_exodus_pos * cos(exodus2_theta_math);
glPushMatrix();
glTranslatef(r_exodus2_x,r_exodus2_y,r_exodus2_z);
glColor3f (1.0, 0.0, 0.0);
glRasterPos3i(exodus2_x/10,exodus2_y/10,exodus2_z/10);
string exodus2 = "BOMB!!";
void * fontexodus2 = GLUT_BITMAP_HELVETICA_10;
for (string::iterator i = exodus2.begin(); i != exodus2.end(); ++i)
{
char c = *i;
glutBitmapCharacter(fontexodus2, c);
}
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(r_exodus2_x,r_exodus2_y,r_exodus2_z);
glColor3f (1.0, 0.0, 0.0);
glRasterPos3i(exodus2_x/10,exodus2_y/10,exodus2_z/10);
string exodus2b = "\n THREAT LEVEL 1";
void * fontexodus2b = GLUT_BITMAP_HELVETICA_10;
for (string::iterator i = exodus2b.begin(); i != exodus2b.end(); ++i)
{
char c = *i;
glutBitmapCharacter(fontexodus2b, c);
}
glEnd();
glPopMatrix();
}
What I would like to have is a carriage return between "string exodus2" and "string exodus2b". However, it is always on one line. And I don't have the real estate onscreen for that.
Is it possible to add a carriage return somewhere in my string iterator?
Unfortunately, screwing around with "glTranslatef" and "glRasterPos3i" only results in the distance between the two lines of text NOT being consistent as I move the camera.
You need to render each string separately, and modify the raster position appropriate to reset the string's position. If you use glutBitmapHeight, you can determine how far you need to move the raster position "down" to emulate a new line.
If you want the strings to be positioned in 3D, but then to "act" like they're restricted to the same plane in 2D, you can use the glBitmap command to modify the raster position in screen space.
int lineHeight = glutBitmapHeight( font ); // number of pixels between "lines"
int stringLength = glutBitmapLength( font, s1 ); // returns length of string 1 in pixels
glutBitmap( 0, 0, 0, 0, -stringLength, lineHeight, NULL ); // move raster position
glutBitmapString( font, s2 );
the fifth and sixth parameters of glBitmap control how far the raster position is moved.
Alternatively, if you want these strings to be anchored in a position relative to the window, as compared to a point in your 3D space, check out glWindowPos.
By the way, all of these functions are deprecated with OpenGL 3.0, removed in OpenGL 3.1, unless you use a compatibility context in verisons of OpenGL greater than 3.2.

Defining members of a struct within a namespace

I'm using NeHe's tutorial on FreeType and OpenGL, and I'm having a problem defining members of
struct font_data within namespace freetype. It doesn't recognize font_data as a struct when I define its members in the namespace.
CE_Text.h:
#ifndef CE_TEXT
#define CE_TEXT
#include <Common/Headers.h>
/////////////////// MAJOR CREDIT TO NeHe FOR HIS TUTORIAL ON FREETPYE ///////////////////
///Wrap everything in a namespace, that we can use common
///function names like "print" without worrying about
///overlapping with anyone else's code.
namespace freetype {
//Inside of this namespace, give ourselves the ability
//to write just "vector" instead of "std::vector"
using std::vector;
//Ditto for string.
using std::string;
//This holds all of the information related to any
//freetype font that we want to create.
struct font_data{
float h; ///< Holds the height of the font.
GLuint * textures; ///< Holds the texture id's
GLuint list_base; ///< Holds the first display list id
//The init function will create a font of
//of the height h from the file fname.
void init(const char * fname, unsigned int h);
//Free all the resources assosiated with the font.
void clean();
};
//The flagship function of the library - this thing will print
//out text at window coordinates x,y, using the font ft_font.
//The current modelview matrix will also be applied to the text.
void print(const font_data &ft_font, float x, float y, const char *fmt) ;
}
#endif
CE_Text.cpp (my problem is at void font_data::init):
#include <Common/Headers.h>
using namespace freetype;
namespace freetype {
///This function gets the first power of 2 >= the
///int that we pass it.
inline int next_p2 ( int a )
{
int rval=1;
while(rval<a) rval<<=1;
return rval;
}
///Create a display list coresponding to the give character.
void make_dlist ( FT_Face face, char ch, GLuint list_base, GLuint * tex_base ) {
//The first thing we do is get FreeType to render our character
//into a bitmap. This actually requires a couple of FreeType commands:
//Load the Glyph for our character.
if(FT_Load_Glyph( face, FT_Get_Char_Index( face, ch ), FT_LOAD_DEFAULT ))
throw std::runtime_error("FT_Load_Glyph failed");
//Move the face's glyph into a Glyph object.
FT_Glyph glyph;
if(FT_Get_Glyph( face->glyph, &glyph ))
throw std::runtime_error("FT_Get_Glyph failed");
//Convert the glyph to a bitmap.
FT_Glyph_To_Bitmap( &glyph, ft_render_mode_normal, 0, 1 );
FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
//This reference will make accessing the bitmap easier
FT_Bitmap& bitmap=bitmap_glyph->bitmap;
//Use our helper function to get the widths of
//the bitmap data that we will need in order to create
//our texture.
int width = next_p2( bitmap.width );
int height = next_p2( bitmap.rows );
//Allocate memory for the texture data.
GLubyte* expanded_data = new GLubyte[ 2 * width * height];
//Here we fill in the data for the expanded bitmap.
//Notice that we are using two channel bitmap (one for
//luminocity and one for alpha), but we assign
//both luminocity and alpha to the value that we
//find in the FreeType bitmap.
//We use the ?: operator so that value which we use
//will be 0 if we are in the padding zone, and whatever
//is the the Freetype bitmap otherwise.
for(int j=0; j <height;j++) {
for(int i=0; i < width; i++){
expanded_data[2*(i+j*width)]= expanded_data[2*(i+j*width)+1] =
(i>=bitmap.width || j>=bitmap.rows) ?
0 : bitmap.buffer[i + bitmap.width*j];
}
}
//Now we just setup some texture paramaters.
glBindTexture( GL_TEXTURE_2D, tex_base[ch]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
//Here we actually create the texture itself, notice
//that we are using GL_LUMINANCE_ALPHA to indicate that
//we are using 2 channel data.
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height,
0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data );
//With the texture created, we don't need to expanded data anymore
delete [] expanded_data;
//So now we can create the display list
glNewList(list_base+ch,GL_COMPILE);
glBindTexture(GL_TEXTURE_2D,tex_base[ch]);
glPushMatrix();
//first we need to move over a little so that
//the character has the right amount of space
//between it and the one before it.
glTranslatef(bitmap_glyph->left,0,0);
//Now we move down a little in the case that the
//bitmap extends past the bottom of the line
//(this is only true for characters like 'g' or 'y'.
glTranslatef(0,bitmap_glyph->top-bitmap.rows,0);
//Now we need to account for the fact that many of
//our textures are filled with empty padding space.
//We figure what portion of the texture is used by
//the actual character and store that information in
//the x and y variables, then when we draw the
//quad, we will only reference the parts of the texture
//that we contain the character itself.
float x=(float)bitmap.width / (float)width,
y=(float)bitmap.rows / (float)height;
//Here we draw the texturemaped quads.
//The bitmap that we got from FreeType was not
//oriented quite like we would like it to be,
//so we need to link the texture to the quad
//so that the result will be properly aligned.
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex2f(0,bitmap.rows);
glTexCoord2d(0,y); glVertex2f(0,0);
glTexCoord2d(x,y); glVertex2f(bitmap.width,0);
glTexCoord2d(x,0); glVertex2f(bitmap.width,bitmap.rows);
glEnd();
glPopMatrix();
glTranslatef(face->glyph->advance.x >> 6 ,0,0);
//increment the raster position as if we were a bitmap font.
//(only needed if you want to calculate text length)
//glBitmap(0,0,0,0,face->glyph->advance.x >> 6,0,NULL);
//Finnish the display list
glEndList();
}
void font_data::init(const char * fname, unsigned int h) {
//Allocate some memory to store the texture ids.
textures = new GLuint[128];
this->h=h;
//Create and initilize a freetype font library.
FT_Library library;
if (FT_Init_FreeType( &library ))
throw std::runtime_error("FT_Init_FreeType failed");
//The object in which Freetype holds information on a given
//font is called a "face".
FT_Face face;
//This is where we load in the font information from the file.
//Of all the places where the code might die, this is the most likely,
//as FT_New_Face will die if the font file does not exist or is somehow broken.
if (FT_New_Face( library, fname, 0, &face ))
throw std::runtime_error("FT_New_Face failed (there is probably a problem with your font file)");
//For some twisted reason, Freetype measures font size
//in terms of 1/64ths of pixels. Thus, to make a font
//h pixels high, we need to request a size of h*64.
//(h << 6 is just a prettier way of writting h*64)
FT_Set_Char_Size( face, h << 6, h << 6, 96, 96);
//Here we ask opengl to allocate resources for
//all the textures and displays lists which we
//are about to create.
list_base=glGenLists(128);
glGenTextures( 128, textures );
//This is where we actually create each of the fonts display lists.
for(unsigned char i=0;i<128;i++)
make_dlist(face,i,list_base,textures);
//We don't need the face information now that the display
//lists have been created, so we free the assosiated resources.
FT_Done_Face(face);
//Ditto for the library.
FT_Done_FreeType(library);
}
void font_data::clean() {
glDeleteLists(list_base,128);
glDeleteTextures(128,textures);
delete [] textures;
}
/// A fairly straight forward function that pushes
/// a projection matrix that will make object world
/// coordinates identical to window coordinates.
inline void pushScreenCoordinateMatrix() {
glPushAttrib(GL_TRANSFORM_BIT);
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(viewport[0],viewport[2],viewport[1],viewport[3]);
glPopAttrib();
}
/// Pops the projection matrix without changing the current
/// MatrixMode.
inline void pop_projection_matrix() {
glPushAttrib(GL_TRANSFORM_BIT);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
}
///Much like Nehe's glPrint function, but modified to work
///with freetype fonts.
void print(const font_data &ft_font, float x, float y, const char *fmt, ...) {
// We want a coordinate system where things coresponding to window pixels.
pushScreenCoordinateMatrix();
GLuint font=ft_font.list_base;
float h=ft_font.h/.63f; //We make the height about 1.5* that of
char text[256]; // Holds Our String
va_list ap; // Pointer To List Of Arguments
if (fmt == NULL) // If There's No Text
*text=0; // Do Nothing
else {
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
}
//Here is some code to split the text that we have been
//given into a set of lines.
//This could be made much neater by using
//a regular expression library such as the one avliable from
//boost.org (I've only done it out by hand to avoid complicating
//this tutorial with unnecessary library dependencies).
const char *start_line=text;
vector<string> lines;
for(const char *c=text;*c;c++) {
if(*c=='\n') {
string line;
for(const char *n=start_line;n<c;n++) line.append(1,*n);
lines.push_back(line);
start_line=c+1;
}
}
if(start_line) {
string line;
for(const char *n=start_line;n<c;n++) line.append(1,*n);
lines.push_back(line);
}
glPushAttrib(GL_LIST_BIT | GL_CURRENT_BIT | GL_ENABLE_BIT | GL_TRANSFORM_BIT);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glListBase(font);
float modelview_matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelview_matrix);
//This is where the text display actually happens.
//For each line of text we reset the modelview matrix
//so that the line's text will start in the correct position.
//Notice that we need to reset the matrix, rather than just translating
//down by h. This is because when each character is
//draw it modifies the current matrix so that the next character
//will be drawn immediatly after it.
for(int i=0;i<lines.size();i++) {
glPushMatrix();
glLoadIdentity();
glTranslatef(x,y-h*i,0);
glMultMatrixf(modelview_matrix);
// The commented out raster position stuff can be useful if you need to
// know the length of the text that you are creating.
// If you decide to use it make sure to also uncomment the glBitmap command
// in make_dlist().
// glRasterPos2f(0,0);
glCallLists(lines[i].length(), GL_UNSIGNED_BYTE, lines[i].c_str());
// float rpos[4];
// glGetFloatv(GL_CURRENT_RASTER_POSITION ,rpos);
// float len=x-rpos[0];
glPopMatrix();
}
glPopAttrib();
pop_projection_matrix();
}
}
struct freetype::font_data{
should be
struct font_data{
The fact that font_data is in namespace freetype is already covered by the surrounding namespace freetype { }.
So, in fact, in your code it is true that you never did declare any freetype::font_data type! It's as if you were attempting to declare a freetype::freetype::font_data type instead.
This is analagous to how you do not write:
struct T
{
void T::foo();
};
but instead:
struct T
{
void foo();
};
You have to include CE_Text.h into CE_Text.cpp for that to work. Without seeing the definition for font_data class, the compiler will not allow you to define its members.
That's what it is telling you by "not recognizing font_data as a struct". Of course, it is not recognizing it, since it is completely unknown in CE_Text.cpp.
According to your comments, you included your header files in circular fashion. This is your problem right there. Never include header files circularly. Granted, your include guards make sure that the inclusion cycle gets broken in some way. But that does not in any way help your code to compile, as you can see in your example.
Until you completely remove any inclusion cycles from your code, trying to fix it is a crapshoot.

OpenGL: set glPrint() location relative to screen

I am trying to print some debugging info (FPS, camera location and rotation, etc) in the lower left of the screen. I have set it up so that it works fine using glTranslate() and glRasterPos2f(), but if I try and resize the window, the text is no longer resting perfectly in the corner, and depending on how much I stretch out the window, it may appear in the middle of the screen, or it may start getting cut off. Is there any way to use glPrint() to print text that will be say, 10px from the left, and 10px to the right?
Here is the code I am using:
GLvoid glPrint(const char *fmt, ...){ // Custom GL "Print" Routine
char text[256]; // Holds Our String
va_list ap; // Pointer To List Of Arguments
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
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(base - 32); // Sets The Base Character to 32
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
}
void GLOUT(float xLocation, float yLocation, float colorRed, float colorGreen, float colorBlue){
glDisable(GL_TEXTURE_2D);
glLoadIdentity();
glTranslatef(0,0,-.002);
glColor3f(colorRed,colorGreen,colorBlue);
glRasterPos2f(xLocation/10000, yLocation/10000);
glPopMatrix();
}
The trick with rendering HUDs or other overlay stuff, is to switch to a more convenient combination of viewport and projection for them.
You're not forced to stick to one particular projection with OpenGL. Let me guess: You setup viewport and projection in the window's resizing handler, right? Suggestion: Move that code to the drawing function, and (hopefully) have an epiphany doing so.