首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络游戏 >

OpenGL bit地图 fonts显示文字显示不出来

2012-07-23 
OpenGL bitmap fonts显示文字显示不出来我在学习OpenGL NeHe教程的时候,看到第13课bitmap fonts显示文字到

OpenGL bitmap fonts显示文字显示不出来
我在学习OpenGL NeHe教程的时候,看到第13课bitmap fonts显示文字到屏幕上的时候,用glut库写代码,但是程序运行出来不报错,就是窗口上一片漆黑,没有显示出文字。运行环境是VS2008.系统是Win7.有哪位好心的大侠能够帮我看看吗?感激不尽!

我的代码:

C/C++ 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 createGLfloat cnt1 = 3.f,cnt2 = 0.6f;//move on the screen or set colorGLvoid 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    printf("build font successfully!\n");}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    printf("text = %s\n",text);    printf("strlen(text) = %d\n",strlen(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)));    //glColor3f(1.0f,0.0f,0.0f);    // Position The Text On The Screen    glRasterPos2f(-0.45f+0.05f*float(cos(cnt1)), 0.32f*float(sin(cnt2)));    /*glBegin(GL_TRIANGLES);    glColor3f(1.0f,0.0f,0.0f);    glVertex3f(0.0f,1.0f,0.0f);    glColor3f(0.0f,1.0f,0.0f);    glVertex3f(-1.0f,-1.0f,0.0f);    glColor3f(0.0f,0.0f,1.0f);    glVertex3f(1.0f,-1.0f,0.0f);    glEnd();*/    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(45.0,(GLfloat)w / (GLfloat)h,0.1f,100.0f);    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;} 




感激不尽~~

[解决办法]
你这个程序不是画不出来,而是画在屏幕外边了,使用glRasterPos()的时候,该位置会进行矩阵变换,所以修改一些参数,比如glTranslatef(0.0f,0.0f,-100.0f);它绝对可以显现出来的。
如果你不想使用OpenGL的矩阵变换,你可以使用glWindowPos()函数,它是使用屏幕左下角为原点,x轴向右,y轴向上,单位为像素,很好用的。不过你的显卡得支持GL_ARB_window_pos扩展才行。

热点排行