PDA

View Full Version : Input/Output Console for OpenGL



Sunset
09-14-2004, 08:14 AM
I am looking for a sourcecode which can display the buffer of glutKeyboardFunc(keyboard) on the screen.

Or just a code which displays a variable (text). Optimal would be some kind of console.

Thanks for help.

geohoffman49431
09-14-2004, 12:43 PM
If you are looking for a simple way to debug your code while it is running by displaying text messages within the programs window then this is what I use (this only works with windows and only if you have a handle to the windows device context):

////Begin code
#ifndef __TEXT_H
#define __TEXT_H

#include <String>

class Text
{
public:

void initialize(HDC hDC)
{
//Set up a font to use
HFONT hFont;
GLYPHMETRICSFLOAT agmf[128];
LOGFONT logfont;

logfont.lfHeight = -10;
logfont.lfWidth = 0;
logfont.lfEscapement = 0;
logfont.lfOrientation = 0;
logfont.lfWeight = FW_BOLD;
logfont.lfItalic = FALSE;
logfont.lfUnderline = FALSE;
logfont.lfStrikeOut = FALSE;
logfont.lfCharSet = ANSI_CHARSET;
logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
logfont.lfQuality = DEFAULT_QUALITY;
logfont.lfPitchAndFamily= DEFAULT_PITCH;
strcpy(logfont.lfFaceName,"Arial");
hFont = CreateFontIndirect(&logfont);

SelectObject(hDC, hFont);

nFontBase = glGenLists(128);

wglUseFontOutlines(hDC, 0, 128, nFontBase ,0.0f, 0.0001f, WGL_FONT_POLYGONS, agmf);

output = "";
}

void drawText()
{
glPushMatrix();
glLoadIdentity();
glTranslatef(-.77,.56,-1);
glScaled(.025, .025, .025) ;

glListBase(nFontBase);
glCallLists(output.length(),GL_UNSIGNED_BYTE,outpu t.c_str());
glPopMatrix();
}

void setText(string changeTo)
{
output = changeTo;
}

void addText(char stringToAdd)
{
output += stringToAdd;
}

void addText(string stringToAdd)
{
output += stringToAdd;
}

void addText(int stringToAdd)
{
char buffer [33];
itoa (stringToAdd,buffer,10);
output = output + buffer;
}

void addText(float stringToAdd)
{
char buffer[33];
int temp = (int)stringToAdd;
itoa(temp,buffer,10);
output = output + buffer;
}

string output;
GLuint nFontBase;

};

#endif

I create a global Text object. I then call the initialize function (this must be done after opengl is set up so that is why I did not put it in the constructor). Then I use the helper functions (addText,setText) to change the message. Then I make sure to call drawText every frame. In the drawText function are some calls to glScale and glTranslate. These you might need to tweek depending on how big you want your text and where you want it positioned. This is a very crude method for displaying text but it serves its purpose. You can add other helper functions to display things like float point integers but I never bothered to. You could also use some operator like << to add successive strings or different data types but I got lazy when I wrote the class. Hope this helps.

But wait, if you are writing a standard c++ console program then you should already have a console. Why can't you just use cout?

The code is mine except for the init function and I forget where I got it. Sorry.