So I'm using opengl, wgl and c++ to render text. The code makes a call to glCallLists to render the text - so it shouldn't be too slow, right? Wrong.
Has a rotating skybox (6 quads, 6 textures, texture size 1024x1024, linear filter), and 1/2/3 characters of text drawn in colour at the top right (represents fps). At this point, fps is between 70 and 90.
Uh oh. Draw some untextured quads and lines (I tried just the console without text, it has no fps hit) and a whole bunch more text, fps down to a persistent 2. Not good.
Here is the relevant code:
The calls are made from Lua into c++, but I don't think the problem lies there as I'm also making tonnes of other calls (like drawing each line, each quad) that are still being made back when fps is up at 80. Just to put this in perspective, when I render 3x3 segments of terrain at the same time (each segment is 64x64 quads, textured, with lighting), fps is about 20. Surely rendering some simple text can't be more intensive than rendering a full 3D terrain with lighting?
Here is the text code:
// Include header
#include "text.h"
// Define functions
int font_create( char* family, int size, int weight ) {
HFONT font;
HFONT oldfont;
HDC hDC = GetHDC();
int base = glGenLists( 96 );
font = CreateFont( -size, 0, 0, 0, weight, false, false, false,
ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
FF_DONTCARE | DEFAULT_PITCH, family );
oldfont = (HFONT)SelectObject( hDC, font );
wglUseFontBitmaps( hDC, 32, 96, base );
SelectObject( hDC, oldfont );
DeleteObject( font );
return base;
}
void font_destroy( int id ) {
glDeleteLists( id, 96 );
}
void font_render_noraster( char* text, int id ) {
glPushAttrib( GL_LIST_BIT );
glListBase( id-32 );
glCallLists( strlen( text ), GL_UNSIGNED_BYTE, text );
glPopAttrib();
}
void font_render( char* text, int id, int x, int y ) {
glRasterPos2f( float( x ), float( y ) );
font_render_noraster( text, id );
}
int font_getwidth( char* text, int id ) {
GLint oldbuffer;
glGetIntegerv( GL_DRAW_BUFFER, &oldbuffer );
glDrawBuffer( GL_NONE );
GLfloat o_rpos[4];
glGetFloatv( GL_CURRENT_RASTER_POSITION, o_rpos );
font_render_noraster( text, id );
GLfloat n_rpos[4];
glGetFloatv( GL_CURRENT_RASTER_POSITION, n_rpos );
glDrawBuffer( oldbuffer );
return int( n_rpos[0] - o_rpos[0] );
}
Here is the lua binding:
static int lbind_r_rendertext( lua_State* L ) {
char* text = const_cast<char*>( luaL_checkstring( L, 1 ) );
int base = luaL_checkint( L, 2 );
int x = luaL_checkint( L, 3 );
int y = luaL_checkint( L, 4 );
font_render( text, base, x, y );
return 0;
}
Perhaps it doesn't like the const_cast much?
Any ideas?
[Edited by - thomasfn1 on April 1, 2010 2:03:54 PM]