开发者

Function to draw text on screen using C++/OpenGL/Glut faster than glutBitmapCharacter

I need something muuuch faster than glutBitmapCharacter(font, text[i]). It's decreasing performacne few times ! I need to display fps, xyz position etc. so not in 3D just displaying HUD.

Currently I'm using :

glRasterPos2f(x,y);

for (int i=0; i<text.size(); i++)
{
   glutBitmapCharacter(font, text[i]);
}

I'm using this function :

void glutPrint(float x, float y, LPVOID font, string text) 
{ 
    glRasterPos2f(x,y); 

    for (int i=0; i<text.size(); i++)
    {开发者_JAVA技巧
        glutBitmapCharacter(font, text[i]);
    }
}

Every frame in DisplayFunction in drawing HUD section (calling DrawHUD()) :

void DrawHUD (void)
{
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0.0, windowWidth, windowHeight, 0.0, -1.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClear(GL_DEPTH_BUFFER_BIT);
    glColor3f(0.2f,0.2f,0.2f);

    glutPrint(2,10,glutFonts[4],its(sfps)+" fps; "+its(todrawquads)+" quads drawing; ");

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);

}

Also using int to string function :

string its(int i)
{
stringstream out;
out << i;
return out.str();
}

some fact about performance (measured in FPS)

Without calling DrawHUD function ~ 3500

With calling DrawHUD function ~ 3500 (maybe few less fps)

With DrawHUD + 1 x GlutPrint ~ 3300

With DrawHUD + 2 x GlutPrint ~ 2400

With DrawHUD + 3 x GlutPrint ~ 1700

(eg. when I mean 3 x GlutPrint I meant in DrawHUD :

{
[...]

glutPrint(...);
glutPrint(...);
glutPrint(...);

[...]
}

)

That's not nice ... I know measuring using frame rate isn't good.

also

when I commented :

glutBitmapCharacter(font, text[i]);

in loop in glutPrint there was ~3500 fps ... so I'm SURE that glutBitmapCharacter is problem. So what use instead it :) ?

Then what to do ?


When in doubt, go back to the basics: create your own (better) drawing function. Make a rectangular texture of characters and draw contiguous quads (triangle strips) on screen with the font texture selected, one strip per string of characters.

The above still holds fine with VBO's if you choose to go that route. The only important thing is to buffer the writes as much as possible, maybe by adding stuff to print to the screen to an array of sorts and writing them out at the end of the frame draw at the same time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜