PDA

View Full Version : 2d text



Itch
02-17-2001, 06:15 AM
help! I'm getting very distressed :(

I've been trying for a while to overlay some text onto a rendered scene with no success. So in a fit of desperation, I tried to get hold of the code for defining a complete font, example 8_2 of the OpenGL Programming Guide, unfortunatley it wasn't where it said it should be in the book, so I keyed it in.

In order to get it to compile ( using VC++ ) I had to uninclude gl.h and glu.h - why I don't know. However perhaps as a result, when I run this program, nothing appears on my screen!
I hoped this would be a simple library that I could steal and use to overlay text on my rendered scene, so if anyone can shed any light on why it's not working, I would be most grateful.

Below is the code as I have keyed it in, I can't see any significant differences ( aside from the fact that all my char's are A ) between this and the book.

thanks in advance folks..

james

CODE:
OpenGL Programming Guide example 8_2 : Defining a complete font

//#include <Gl/gl.h>
//#include <GL/glu.h>
#include <GL/glut.h>

#include <stdlib.h>
#include <string.h>

GLubyte space[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

GLubyte letters[][13] =
{
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 },
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 },
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 },
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 },
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 },
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 },
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 },
{ 0x00, 0x00, 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xc3, 0xc3, 0x66, 0x3c, 0x18 },
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 },
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 },
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 },
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 }
};

GLuint fontOffset;

void makeRasterFont(void )
{
GLuint i,j;

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

fontOffset = glGenLists(128);

for ( i = 0, j = 'A'; i < 26; i++, j++ )
{
glNewList(fontOffset + j , GL_COMPILE);
glBitmap(8,13,0.0,2.0,10.0,0.0,letters[i]);
glEndList();
}
glNewList(fontOffset + ' ' , GL_COMPILE);
glBitmap(8,13,0.0,2.0,10.0,0.0,space);
glEndList();

}

void init (void)
{

glShadeModel(GL_FLAT);
makeRasterFont();

}

void printString( char *s )
{

glPushAttrib(GL_LIST_BIT);
glListBase(fontOffset);
glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *) s );
glPopAttrib();

}

void display(void)
{

GLfloat white[3] = { 1.0,1.0,1.0 };

glClear(GL_COLOR_BUFFER_BIT);

glColor3fv(white);

glRasterPos2i(20,60);
printString("the quick brown fox jumps");

glRasterPos2i(20,40);
printString("over the lazy dog");

glFlush();

}

void reshape(int w, int h )
{

glViewport(0,0,(GLsizei) w, (GLsizei) h );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (0.0, w, 0.0, h , -1.0, 1.0 );
glMatrixMode(GL_MODELVIEW);

}

void keyboard(unsigned char key, int x, int y )
{
switch( key ) {
case (27):
exit(0);
}
}

int main (int argc, char ** argv )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
glutInitWindowSize(300,100);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();

glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

mango
02-17-2001, 04:50 PM
If you are using GLUT should you not include gl.h or glu.h yourself. If you do that in windows will you get an error if you have not included windows.h first.

GLUT already has support for bitmap and stroke fonts.

One problem with your program is that the strings should be in uppercase "THE QUICK BROWN...

Itch
02-18-2001, 01:24 AM
Well don't I feel stupid now http://www.opengl.org/discussion_boards/ubb/smile.gif

thanks chap.

james

Itch
02-18-2001, 04:52 AM
Unfortunatley my problem still remains. What I am trying to do is put text as above in front of a rendered scene, however I still get no text on my game scene.
Any suggestions?
The game is based on win32, however this does not appear to be the problem, since I have a glut version of the same code, and it has the same issue.

thanks in advance

james

Deiussum
02-18-2001, 08:48 AM
Glut.h includes gl.h and glu.h for you, and also defines all the things from windows that need to be defined before including gl.h. So technically, if you are using glut, you only need glut.h.

As to your problem, it appears you are taking this right from the example in the Red book with one exception... The strings you are trying to print are lower case and the strings in the red book are upper case.

This does make a difference. 'a' is not equal to 'A'. This example takes advantage of the fact that characters can be converted to the ASCII value of the character they repersent. Try changing your text to upper case or in your makeRasterFont function change 'A' to 'a'.

Itch
02-18-2001, 10:59 AM
thanks, yes, I changed it to upper case and it worked right away, which was great... this is no longer the problem, perhaps I should make a new post?
The problem is now that I can't port that code into another piece of code that renders a scene! What I really want is to put some text in front of that screen... vis the console on quake?

Any ideas pls?

thanks again

james

Michael Steinberg
02-18-2001, 11:36 AM
Disable depth testing and draw the console after you've drawn the world. You can use an orthogonal projection for drawing the console as well.

Itch
02-18-2001, 02:20 PM
yes, that makes good sense to me! However it still does not work, would you mind taking a look at this code for me please.

void RenderScene(void)
{
...
Render the scene
...

if ( CONSOLE )
{

glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);

glColor3f(1.0,1.0,1.0);

glPushMatrix();

glColor3f(0.4,0.0,0.4);
glTranslatef(0.0,0.0,31.0);

glBegin(GL_QUADS);

glVertex3f( 13.0,-10.0, 0.0 );
glVertex3f( 13.0, 10.0, 0.0 );
glVertex3f( -13.0, 10.0, 0.0 );
glVertex3f( -13.0,-10.0, 0.0 );

glEnd();

glPopMatrix();

printString("HELP");

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

}

}

using the printString function from my initial post.

thanks

james

Michael Steinberg
02-18-2001, 02:39 PM
You still have the transformation matrix of your scene applied while drawing your text.
Try to add a glLoadIdentity() after glPushMatrix(). I don't understand that glTranslatef as well.

At the end I would switch to textured quads for drawing text...

Michael Steinberg
02-18-2001, 02:45 PM
After all I think the transformation matrices affect the raster positions. I'm not too used in using bitmaps...

mango
02-18-2001, 04:58 PM
Here is something about GLUT and among other things fonts: http://www.fatech.com/tech/opengl/glut/