我将使用 freeglut 函数 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();
}
它应该在屏幕左上角显示 FPS
提供的位置glRasterPos
被视为顶点,并由当前模型视图和投影矩阵进行转换。在您的示例中,您指定文本的位置为 (20,20),我猜它应该是屏幕(实际上是视口)坐标。
如果您正在渲染 3D 几何体,尤其是使用透视投影,则您的文本可能会被剪掉。但是,(至少)有两个简单的解决方案(按代码简单性顺序呈现):
使用其中之一glWindowPos
函数而不是glRasterPos
。该函数绕过模型视图和投影转换。
-
use glMatrixMode
, glPushMatrix
, and 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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)