Opengl glutBitmapString不显示任何内容

Opengl glutBitmapString不显示任何内容,opengl,freeglut,Opengl,Freeglut,我将用freelut函数glutBitmapString在屏幕上显示FPS,但它什么也不显示。这是我的密码。有没有人能找出问题出在哪里 void PrintFPS() { frame++; time=glutGet(GLUT_ELAPSED_TIME); if (time - timebase > 100) { cout << "FPS:\t"<<frame*1000.0/(time-timebase)<<endl

我将用freelut函数glutBitmapString在屏幕上显示FPS,但它什么也不显示。这是我的密码。有没有人能找出问题出在哪里

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();
}
void PrintFPS()
{
frame++;
时间=glutGet(GLUT\u运行时间);
如果(时间-时基>100){

cout由
glRasterPos
提供的位置被视为一个顶点,并通过当前模型视图和投影矩阵进行变换。在您的示例中,您指定的文本位置为(20,20),我猜它应该是屏幕(视口,实际上)坐标

如果渲染3D几何体,特别是使用透视投影,文本可能会被剪裁掉。但是,有(至少)两种简单的解决方案(按代码简单性顺序排列):

  • 使用一个
    glWindowPos
    函数代替
    glRasterPos
    。此函数绕过模型视图和投影变换

  • 使用
    glMatrixMode
    glPushMatrix
    glPopMatrix
    临时切换到窗口坐标进行渲染:

    // 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();
    

  • 与实际问题不太相关,但
    char*out=new char[30];
    永远不会被删除,所以你会泄漏每一帧。最好在堆栈上分配该数组。谢谢。当我在调试过程中跟踪out的值时,该值完全正确。它只是无法在屏幕上打印。我也尝试使用glutBitmapCharacter,但仍然没有足够的代码来真正说明。发布一个。@davidly虽然我并不反对,但这正是海报所问的。我想如果OpenGL ARB真的想摆脱不推荐的代码,他们就不会在提供核心配置文件的同时提供兼容性配置文件。我的评论并不是要批评你的答案,只是一般的情况。我见过一些人试图进入g花几个月时间打印即时模式代码的图形,然后基本上必须重新开始。我希望API默认为核心配置文件。:)不用担心,不接受批评。我看到了同样的事情,一方面我完全同意你的看法。另一方面,任何有助于某人更快完成工作的东西。可能没有什么好的解决方案比任何留下更多时间喝咖啡的人都要发誓:-)