Advertisement

Lesson 13 prob

Started by November 14, 2007 08:50 PM
7 comments, last by experiment 17 years ago
I just went trough lesson 13: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=13, and came up with a problem.. The code compiles fine, the app. starts, but there's no text to be seen anywhere.. Some info: I didn't use those float cnt1, cnt2; variables to make the colors an position change (I have it just set to "glRasterPos2f(0.0,0.0)" ), and don't think that's the reason NOT-TO-WORK either.. Second, I use glut library, so I didn't use any of the handles (except "HDC hDC = NULL; // Private GDI Device Context", whatever that means.. the code requires it to be able to compile). And what does "glRasterPos2f" do anyway..? I was expecting something like setting the matrix mode to GL_PROJECTION, set the position of the text, put mode back to GL_MODELVIEW and do the rest of drawing.. And how about if I have a light enabled..? Does that affect the drawing of the text..? (My guess is: maybe..) Do I have to add the normals/orientation of the text to make it react to light (or something like that)..? PS: heh :) my last question sounds pretty funny now that I read it.. Is somebody suspecting something..?

Setting the raster position to 0,0 is probably not a good idea - I don't remember which way the coordinate system goes, and it may well be printing off the screen. Try settign the raster position to something like 100,100 and see what happens.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement
OK, to be honest, I've set the glRasterPos2f(myX, myY) to these variables, and then tried changing values with realtime keyboard input..

This morning I've even tried disabling the lighting, and using glColor3f as used in the demo in lesson 13.. Turning off other objects, changing Z position to take a look if it works from the other side.. nothing.. I don't know what else to suspect..

The only thing I can doubt is that I'm not doing something right with handles..
Once again: I'm using glut.lib, and cause of that there is no need for window handles, while the lesson 13 does use them..

[Edited by - experiment on November 15, 2007 8:46:59 AM]
Okay.. I've cut simple the code and this is how it looks:

// CODE: ----------------------------------------------------------

#include <windows.h>
#include <stdio.h>
#include <stdarg.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

HDC hDC = NULL;
float myPosX, myPosY;

// FONT SETUP -----------------------------------------------------

GLuint fontBase;

GLvoid BuildFont(GLvoid)
{
HFONT font;
HFONT oldfont;

fontBase = glGenLists(96);

font = CreateFont(-24, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
FF_DONTCARE|DEFAULT_PITCH, "Courier New");

oldfont = (HFONT)SelectObject(hDC, font);
wglUseFontBitmaps(hDC, 32, 96, fontBase);
SelectObject(hDC, oldfont);
DeleteObject(font);
}

GLvoid KillFont(GLvoid)
{
glDeleteLists(fontBase, 96);
}

GLvoid glPrint(const char *fmt, ...)
{
char text[256];
va_list ap;

if (fmt == NULL) { return; }

va_start(ap, fmt);
vsprintf(text, fmt, ap);
va_end(ap);

glPushAttrib(GL_LIST_BIT);
glListBase(fontBase - 32);

glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
glPopAttrib();
}

// INIT ---------------------------------------------------------

void init()
{
glShadeModel(GL_SMOOTH);

glutSetCursor(GLUT_CURSOR_NONE);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

BuildFont();
}

// DISPLAY FUNC ------------------------------------------------

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

gluLookAt(0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glColor3f(0.9, 0.4, 0.3);
glRasterPos2f(myPosX, myPosY);
glPrint("FPS: ");

glutSwapBuffers();
}

// IDLE FUNC ---------------------------------------------------

void idleState1()
{
if( ::GetAsyncKeyState(VK_UP) & 0x8000f ) { myPosX += 1.0; }
if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f ) { myPosX -= 1.0; }
if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f ) { myPosY += 1.0; }
if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f ) { myPosY -= 1.0; }
// -----------------
glutPostRedisplay();
}

// RESHAPE ----------------------------------------------------

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glFrustum (-1.0, 1.0, 0.0, 1.0, 0.0, 20.0);
glMatrixMode (GL_MODELVIEW);
}

// GL KEYBOARD ------------------------------------------------

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

// MAIN -------------------------------------------------------

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (800, 600);
glutInitWindowPosition (0, 0);
glutCreateWindow ("app. test");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(idleState1);
glutMainLoop();
return 0;
}
Okay.. I've got an answer to my question.. when using glut, there is
a simple routine(with some preset font styles)..

-------------------------------------------------------------------------
void glPrint(GLfloat x, GLfloat y, char *string)
{
int len, i;
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string);
}
}
-------------------------------------------------------------------------
source: http://pyopengl.sourceforge.net/documentation/manual/glutBitmapCharacter.3GLUT.html
-------------------------------------------------------------------------
GLUT_BITMAP_HELVETICA_18 is one of the font presets, there are some
other types inside. I'm not sure tho if you can use your own defined font
inside the call..
Honestly, NeHe tutorials are old and use poor coding methods. He wrote the tutorials as he was learning. I'd recommend a good, up-to-date OpenGL book (with 3.0 coming out soon you might want to hold off though). The Red Book should be a great guide to learning OpenGL and the Blue book should help you with all the different functions etc.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Advertisement
Yeah.. I kinda noticed I was talking to myself inside this ghost thread.. :P

I do have the redbook the 1.1 (the free version).. That's where I learned
anything so far, I don't have any idea how to get a hold on newer versions
since I live in a forgotten land and don't use a credit card..
Hi,

Sorry this is a bit late, but if you are looking to put text on screen it may be worth taking a look at lesson 43. This uses the FreeType library to load fonts so you can specify different font families and sizes.
Member of the NeHe team.
Quote: Original post by Kazade
Hi,

Sorry this is a bit late, but if you are looking to put text on screen it may be worth taking a look at lesson 43. This uses the FreeType library to load fonts so you can specify different font families and sizes.


I've only wanted to display my FPS on the bottom of the screen, and glut has this simple routine to do that by few given preset fonts.. It's not much, but I don't need anything else right now.. But I'm going to take a look on 43 in case it gives me an idea.. :) - Like the half life team.. It seems they use their own font for the HUD weapons display.. :)

tnx..!

This topic is closed to new replies.

Advertisement