glutBitmapString什么都不显示 [英] glutBitmapString shows nothing

查看:94
本文介绍了glutBitmapString什么都不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用freeglut函数glutBitmapString在屏幕上显示FPS,但它什么也没有显示.这是我的代码.有谁能弄清楚问题出在哪里?

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

它应该在屏幕的左上方显示FPS

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

推荐答案

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

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.

如果要渲染3D几何图形(尤其是透视投影),则文本可能会被剪切掉.但是,(至少)有两个简单的解决方案(以代码简单性的顺序呈现):

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. 使用glWindowPos函数之一代替glRasterPos.此功能绕过了模型视图和投影的转换.

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

使用glMatrixModeglPushMatrixglPopMatrix临时切换到窗口坐标进行渲染:

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

这篇关于glutBitmapString什么都不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆