开发者

How to tell the size of font in pixels when rendered with openGL

I'm working on the editor for Bitfighter, where we use the default OpenGL stroked font. We generally render the text with a linewidth of 2, but this makes smaller fonts less readable. What I'd like to do is detect when the fontsize will fall below some threshold, and drop the linewidth to 1. The problem is, after all the transforms and such are applied, I don't know how to tell how tall (in pixels) a font of size <fontsize> will be rendered.

This is the actual inner rendering function:

if(---something--- < thresholdSizeInPixels)
   glLineWidth(1);

float scalefactor = fontsize / 120;

glPushMatrix();
   glTranslatef(x, y + (fix ? 0 : size), 0);
   glRotatef(angle * radiansToDegreesConversion, 0, 0, 1);
   glScalef(scaleFactor, -scaleFactor, 1);
   for(S32 i = 0; string[i]; i++)
      OpenglUtils::drawCharacter(string[i]);
glPopMatrix();

Just before calling this, I want to check the height of the font, then drop the linewidth if necessary. What goes in the ---something--- spot?

Bitfighter is a pure old-school 2D game, so there are no fancy 3D transforms going on. All code is in C++.


My solution was to combine the first part Christian Rau's solution with a fragment of the second. Basically, I can get the current scaling factor with this:

static float modelview[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelview);    // Fills modelview[]

float scalefact = modelview[0];

Then, I multiply scalefact by the fontsize in pixels, and multiply that by the ratio of windowHeight / canvasHeight to get the hei开发者_运维百科ght in pixels that my text will be rendered.

That is...

textheight = scalefact * fontsize * widndowHeight / canvasHeight

And I liked also the idea of scaling the line thickness rather than stepping from 2 to 1 when a threshold is crossed. It all works very nicely now.


where we use the default OpenGL stroked font

OpenGL doesn't do fonts. There is no default OpenGL stroked font.

Maybe you are referring to GLUT and its glutStrokeCharacter function. Then please take note that GLUT is not part of OpenGL. It's an independent library, focused on providing a simplicistic framework for small OpenGL demos and tutorials.

To answer your question: GLUT Stroke Fonts are defined in terms of vertices, so the usual transformations apply. Since usually all transformations are linear, you can simply transform the vector (0, base_height, 0) through modelview and projection finally doing the perspective divide (gluProject does all this for you – GLU is not part OpenGL, too), the resulting vector is what you're looking for; take the vector length for scaling the width.


This should be determinable rather easily. The font's size in pixels just depends on the modelview transformation (actually only the scaling part), the projection transformation (which is a simple orthographic projection, I suppose) and the viewport settings, and of course on the size of an individual character of the font in untransformed form (what goes into the glVertex calls).

So you just take the font's basic size (lets consider the height only and call it height) and first do the modelview transformation (assuming the scaling shown in the code is the only one):

height *= scaleFactor;

Next we do the projection transformation:

height /= (top-bottom);

with top and bottom being the values you used when specifying the orthographic transformation (e.g. using glOrtho). And last but not least we do the viewport transformation:

height *= viewportHeight;

with viewportHeight being, you guessed it, the height of the viewport specified in the glViewport call. The resulting height should be the height of your font in pixels. You can use this to somehow scale the line width (without an if), as the line width parameter is in floats anyway, let OpenGL do the discretization.


If your transformation pipeline is more complicated, you could use a more general approach using the complete transformation matrices, perhaps with the help of gluProject to transform an object-space point to a screen-space point:

double x0, x1, y0, y1, z;
double modelview[16], projection[16];
int viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
gluProject(0.0, 0.0, 0.0, modelview, projection, viewport, &x0, &y0, &z);
gluProject(fontWidth, fontHeight, 0.0, modelview, projection, viewport, &x1, &y1, &z);
x1 -= x0;
y1 -= y0;
fontScreenSize = sqrt(x1*x1 + y1*y1);

Here I took the diagonal of the character and not only the height, to better ignore rotations and we used the origin as reference value to ignore translations.

You might also find the answers to this question interesting, which give some more insight into OpenGL's transformation pipeline.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜