Skip to main content
GameDev.net gamedev.net
🔒 Locked

Bitmap font text positioning

Started by Ivan Mandic Jan 20, 2010 at 7:21 AM 16 replies 2k views
Original Post
Ivan Mandic
Ivan Mandic
Hello. I am drawing the text in the orthogonal projection but I am troubled by the fact that the text is positioned by its lower left corner, how can I make positioning by the upper left corner of the string itself?
SuperVGA
SuperVGA
You should check the algorithm that draws text if it inverts y.
What do you use to draw text? The function you take advantage of was
probably designed to draw where it does...

-Otherwise you could just subtract the y-offset from the screen height.
Ivan Mandic
Ivan Mandic
This is in the section OpenGL so what do you think that I'm using? -_-
Title says : "Bitmap font text". (I'm creating font just like NeHe in this tutorial http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=13)

The problem is that when I set a position Y (with glTranslatef) text sets just above it, not underneath. Does someone knows how to fix this? Thanks.
SuperVGA
SuperVGA
Quote:
Original post by Ivan Mandic
This is in the section OpenGL so what do you think that I'm using? -_-


Well, it's something that can be done a lot of ways, so how would i know for sure what you're using?
I saw height = 24 in there somewhere, subtract/add the half of this to the y-component.
Ivan Mandic
Ivan Mandic
The problem is only that the font height does not apply to pixels.

p.s.
I'm not stupid,and I do know math...
SuperVGA
SuperVGA
Sorry, didn't realize that.

What happens if you add 0.5f or some other number to the offset (in the translation)?
Try experimenting with the values. If it's a typical orthagonal projection,
you should refer to your resolution. If the font was drawn on a texture,
you should try with values 0.0 to 1.0. In the case of perspective projection,
it's difficult to say, as the values are somewhat arbitary.
nerdtron
nerdtron
I ran into the same issue recently as we're converting our 2D rendering from using Windows GDI (origin is at upper-left) to OpenGL (origin is at lower-left). So, all our rendering code uses y relative to the top of the screen. I wrote some helper functions which do the conversion for me then call the GL rendering calls.

Its just a matter of:

y = canvasHeight - y - 1;

This converts from y=0 at the top of the canvas to y=0 at the bottom of the canvas. Make sure you include the "- 1" as well or its not actually correct since its a zero-based position.
nerdtron
nerdtron
Note also, that my original attempt was to just flip the top and bottom values in the call to glOrtho2D. This effectively gave me a GL coordinate system with y=0 at the top of the canvas.

This worked fine for drawing geometry, but when I tried drawing text, the text was upside down. I couldn't get it to work through scaling so I finally gave up. If you could figure out the "text upside down" thing, this might be another option to try.

I've found it pretty easy, though, to just do the conversion of y in the helper function.
Ivan Mandic
Ivan Mandic
@SuperVGA
I'm working on a Game engine. And my goal is to make things work in any cases,doing things that way isn't so smart.
szecs
szecs
I suggest you to setup the 2D part, that it matches the screen resolution, so that the coordinates will be the pixels.
And I don't see the problem with the thing, everybody suggest.
Please tell, why do you think it is a bad solution, or give us more details what you want.
SuperVGA
SuperVGA
Quote:
Original post by Ivan Mandic
@SuperVGA
I'm working on a Game engine. And my goal is to make things work in any cases,doing things that way isn't so smart.


Which of the mentioned ways are you referring to? It's common practice to try different sorts of input while the behavior of a specific function is unclear.
It won't hurt, and in the long run, you can make things work in any case either way.
Ivan Mandic
Ivan Mandic
I did setup glOrtho on the right way, so if I translate 2D object on the position (0,0) he will be renderd in the upper left corner of the screen. That isn't the problem.

The problem is that when I set a position Y (with glTranslatef) text sets just above that position,and not underneath.
I could make offset but the problem is that I do not know how to calculate the hight of letters in pixels.
SuperVGA
SuperVGA
If you locate the text above the position in the upper-left corner, it wouldn't be visible.
Sorry for sounding obvious. I might not have understood your question.
glTranslatef won't change where the text is drawn relatively to your text-function it will just translate whatever the function draws.
So if you go with (0, -24.0f, 0), it will be placed where you request.
I believe the text is 24 units high.
szecs
szecs
Use a constant value. It's a very bad practice, to make it depend on the text, because if the text changes, it will be rendered in an other y position, so the whole text will "shake". Or you can measure the same letter every time.

with:

SIZE size;
GetTextExtentPoint32(hDC,string_to_measure,number_of_letters_to_measure,(LPSIZE)&size); //will be in pixels
//size.cx, and size.cy


But you have to make the font current with

SelectObject(win_state.hDC, Your_Font_Created_with_CreateFont);

I hope that helps.

And maybe I misunderstood something, but this text uses glRasterPos, so translation don't have effect (or does it?, I'm not sure)
Ivan Mandic
Ivan Mandic
Quote:
Original post by SuperVGA
If you locate the text above the position in the upper-left corner, it wouldn't be visible.


Exactly. So one way to make it work is to calculete offset(font high in pixels).
A problem is in the fact that I do not know how to calculate right font high in pixels.
Ivan Mandic
Ivan Mandic
That is the function that I need. Thanks.
(MSDN ftw ;))

Quote:
Original post by szecs
And maybe I misunderstood something, but this text uses glRasterPos, so translation don't have effect (or does it?, I'm not sure)

It's not working on the same way but the effect is the same :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.