glutBitmapString shows nothing

只愿长相守 提交于 2019-12-19 10:34:12

问题


I'm going to show FPS on the screen with the freeglut function glutBitmapString,but it shows nothing. Here is my code. Is there anyone can figure where the problem is?

void PrintFPS()
{
    frame++;
    time=glutGet(GLUT_ELAPSED_TIME);
    if (time - timebase > 100) {
        cout << "FPS:\t"<<frame*1000.0/(time-timebase)<<endl;
        char* out = new char[30];
        sprintf(out,"FPS:%4.2f",frame*1000.0f/(time-timebase));
        glColor3f(1.0f,1.0f,1.0f);
        glRasterPos2f(20,20);
        glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24,(unsigned char* )out);


        timebase = time;
        frame = 0;
    }
}

void RenderScene(void)
{
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 0.5f };
    GLfloat vYellow[] = {1.0f,1.0f,0.0f,1.0f};
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vYellow);
    //triangleBatch.Draw();
    squareBatch.Draw();
    PrintFPS();
    glutSwapBuffers();
}

it supposed to show the FPS on the top left of the screen


回答1:


The position that's provided by glRasterPos is treated just like a vertex, and transformed by the current model-view and projection matrices. In you example, you specify the text to be position at (20,20), which I'm guessing is supposed to be screen (viewport, really) coordinates.

If it's the case that you're rendering 3D geometry, particularly with a perspective projection, your text may be clipped out. However, there are (at least) two simple solutions (presented in order of code simplicity):

  1. use one of the glWindowPos functions instead of glRasterPos. This function bypasses the model-view and projection transformations.

  2. use glMatrixMode, glPushMatrix, and glPopMatrix to temporarily switch to window coordinates for rendering:

    // Switch to window coordinates to render
    glMatrixMode( GL_MODELVIEW );
    glPushMatrix();
    glLoadIdentity();    
    
    glMatrixMode( GL_PROJECTION );
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D( 0, windowWidth, 0, windowHeight );
    
    glRasterPos2i( 20, 20 );  // or wherever in window coordinates
    glutBitmapString( ... );
    
    glPopMatrix();
    glMatrixMode( GL_MODELVIEW );
    glPopMatrix();
    


来源:https://stackoverflow.com/questions/16873561/glutbitmapstring-shows-nothing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!