Reputation: 1915
I am now learning OpenGL NeHe production.When I come to read Lesson 13 Bitmap Fonts,I encounter a problem.I write my code using glut.And my PC system is Windows7.I run my code on Microsoft Visual Studio 2008 and there is not any error.But nothing appears in the window.I don't know what is wrong.What may cause this problem generally?Did I miss some settings?
Here is my code:
#pragma comment(lib,"GLAUX.LIB")
#include <GL/glut.h>
#include <windows.h>
#include <GL/glaux.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
HDC hDC = NULL;
GLuint base;//the first display list we create
GLfloat cnt1,cnt2;//move on the screen or set color
GLvoid buildFont() // Build Our Bitmap Font
{
HFONT font; // Windows Font ID
HFONT oldfont; // Used For Good House Keeping
base = glGenLists(96); // Storage For 96 Characters
font = CreateFont(
-24, // Height Of Font
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
FW_BOLD, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
ANTIALIASED_QUALITY, // Output Quality
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
"Times New Roman"); // Font Name
oldfont = (HFONT)SelectObject(hDC, font); // Selects The Font We Want
wglUseFontBitmaps(hDC, 32, 96, base); // Builds 96 Characters Starting At Character 32
SelectObject(hDC, oldfont); // Selects The Font We Want
DeleteObject(font); // Delete The Font
}
void killFont()
{
glDeleteLists(base,96);
}
void glPrint(const char *fmt, ...) // Custom GL "Print" Routine
{
char text[256]; // Holds Our String
va_list ap; // Pointer To List Of Arguments
if (fmt == NULL) // If There's No Text
{
printf("the string to print is NULL!\n");
return; // Do Nothing
}
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
}
int init(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
buildFont(); // Build The Font
return TRUE; // Initialization Went OK
}
void display() // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f,0.0f,-1.0f); // Move One Unit Into The Screen
// Pulsing Colors Based On Text Position
glColor3f(1.0f*float(cos(cnt1)),1.0f*float(sin(cnt2)),1.0f-0.5f*float(cos(cnt1+cnt2)));
// Position The Text On The Screen
glRasterPos2f(-0.45f+0.05f*float(cos(cnt1)), 0.32f*float(sin(cnt2)));
glPrint("Active OpenGL Text With NeHe - %7.2f", cnt1); // Print GL Text To The Screen
glutSwapBuffers();// Everything Went OK
}
void spinDisplay()
{
cnt1 += 0.051f;
cnt2 += 0.005f;
printf("cnt1: %f\n",cnt1);
printf("cnt2: %f\n",cnt2);
}
void reshape(int w,int h)
{
if (0 == h)
h = 1;
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f,(GLfloat)w / (GLfloat)h,1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Bitmap Fonts");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(spinDisplay);
//glutKeyboardFunc(keyboard);
glutMainLoop();
killFont();
return 0;
}
This is the result on Visual Studio 2008:
Upvotes: 1
Views: 11702
Reputation: 21
There isn't anything wrong with your code.
I'm taking a class in OpenGL using glut. We have encountered a problem in class where the labs computers correctly display the characters in the correct colors, but a few of the students' laptops will only display the characters in black. All the machines are running windows 7, so we suspect that it has to do with what version of OpenGL is on the machine. Anyway change your background color to white ( or something that will easily show black text). You should see your text if your positioning is correct.
Upvotes: 2
Reputation: 296
you can use glRasterPos and glutBitmapCharacter this is present in glut
glRasterPos3f( 30.0f , 25.0f ,0.0f );
glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18 , 'A');
or use glutBitmapString (supported in freeglut current).
glRasterPos3f(30.0f , 20.0f ,0.0f);
glutBitmapString( GLUT_BITMAP_HELVETICA_18 , "Hello World!" );
if you can't use glutBitmapString to print a string you can use a loop
char *a="Hello World!";
glRasterPos3f( 30.0f , 25.0f ,0.0f );
for(i = 0; a[i] != '\0'; i++)
glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18 , a[i]);
Upvotes: 2
Reputation: 734
GLUT is outdated, and no longer maintained. Maybe this is the reason of problem on Win7. The last GLUT version (3.7) dating back to August 1998.
You can try freeglut, a full compatible alternative to GLUT to get a 100% replacement without changing anything in source.
I've just tried NeHe Lesson 13 project (based on GLUT) on
Both of them works fine! But under Win7 with MS VS2010 the 64bit debug version cannot be built because of some unresolved external. Did you build a 32bit or a 64bit version? Have you already tried other NeHe downloads without GLUT? (http://nehe.gamedev.net/data/lessons/vc/lesson13.zip)
You can try to update your graphic driver and try to switch on/off Windows Aero Theme, it helps often, because of different Pixel Format Descriptor.
I hope that helps.
Upvotes: 1