Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (606)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  libGDX, BitmapFont, and OrthographicCamera  (Read 18382 times)
0 Members and 1 Guest are viewing this topic.
Offline yuma

Junior Devvie


Medals: 2
Projects: 1


Monkeys are listening


« Posted 2012-02-05 11:48:46 »

I think this question must have been asked and answered somewhere, but I can't find it!

I'm using libGDX (mainly because of his Android painless portability). I have setup a OrthographicCamera so my OpenGL coordinates are from -1f to 1f in the Y direction and from -ar to ar in the X direction (ar is a float with the aspect ratio of the screen resolution). Some code:
1  
2  
3  
4  
5  
6  
@Override
public void resize(int width, int height)
{
    float ar = (float) width / (float) height;
    camera = new OrthographicCamera(2f*ar, 2f);
}


In the game create() overridden method I setup a font using BitmapFont.
1  
2  
3  
4  
5  
6  
@Override
public void create()
{
    sb = new SpriteBatch();
    font = new BitmapFont(Gdx.files.internal("menuFont.fnt"), Gdx.files.internal("menuFont.png"), false);
}


Finally, in the render() method I update the camera, update the projection matrix in the SpriteBatch, and draw the font:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
@Override
public void render()
{
    camera.update();
    camera.apply(Gdx.gl10);
    sb.setProjectionMatrix(camera.combined);    // HERE

    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    sb.begin();
        font.draw(sb, "Hola mundo", -100f, 10f);
    sb.end();
}


This way, the font gets huge on the screen! (I could only see part of a letter).

What I want to do is to use OpenGL coordinates to position the text on the screen, but without any annoying scaling. I can disable the font scaling not passing the projection matrix to the SpirteBatch, i.e., commenting line with the "// HERE" comment. But can't use OpenGL coordinates.

This must be a misconception I bear, but can't figure it out!

Thanks in advance for any comment :-)

Offline pitbuller
« Reply #1 - Posted 2012-02-05 12:05:18 »

If you want to use openGl coords why you pass -100,10 coordinates?
Another point is that you don't set the size of the font anywhere.
Offline yuma

Junior Devvie


Medals: 2
Projects: 1


Monkeys are listening


« Reply #2 - Posted 2012-02-05 12:23:21 »

If you want to use openGl coords why you pass -100,10 coordinates?

That's just to see something on screen. If I use -.5f, .5f I see nothing, but that's the coordinate system I want to use.

Another point is that you don't set the size of the font anywhere.

You mean with BitmapFont.setScale(float) method?

Hmm, I guessed that since BitmapFont produces text by packing the font glyphs from a texture, it behaves like a texture. Following the "Projection, Viewport, and Camera" tutorial on libGDX, it seemed to me that you don't need to do scalings on the texture.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2012-02-05 13:33:53 »

Defaul size is the pixel perfect size that is readed from .fnt
So I bet yout font size is something like 32 and its not gonna show with openGL coordibnates properly.

Can I ask why you want to use openGL coordinates for fonts?
Offline yuma

Junior Devvie


Medals: 2
Projects: 1


Monkeys are listening


« Reply #4 - Posted 2012-02-05 14:01:58 »

Thanks for your time!

Why I want to use OpenGL coordinates? Good question.

It all boils down to placing text and sprites in a fixed coordinate system which is easy for me to understand. For example, using the OpenGL coordinate system from -.5f to .5f I can easily calculate the middle (.25f), quarter (.125f), etc. positions without having to access to the screen resolution (ScreenSettings.WIDTH/2f, for example). In addition, my idea is to adjust the viewport to the OpenGL scene, so multiple screen resolutions have the same appearance.

This mustn't be a good way to achieve that, but can't think of a better way to support multiple resolutions without having to hardcode them ony by one.

Do you have any suggestions?

Offline Nate

« JGO Bitwise Duke »


Medals: 159
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #5 - Posted 2012-02-05 15:19:10 »

The glyph positions in the BitmapFont use pixel units. Eg, it is going to draw a glyph 14 units wide and 24 units high. If your camera is setup to map 1:1 with pixels, it will render at the size you expect.

You could try setting the scale on the BitmapFont so the units it uses are appropriate for your camera. You don't want your font scaled at all though, or it will look bad!

You could use a camera that maps 1:1 with screen pixels. Your model could use whatever coordinate system you like, and you would convert to pixels only for drawing.

You could use scene2d and layout stuff using Table, which can give you layouts that work at different resolutions.

Offline pitbuller
« Reply #6 - Posted 2012-02-05 15:28:58 »

Thanks for your time!

Why I want to use OpenGL coordinates? Good question.

It all boils down to placing text and sprites in a fixed coordinate system which is easy for me to understand. For example, using the OpenGL coordinate system from -.5f to .5f I can easily calculate the middle (.25f), quarter (.125f), etc. positions without having to access to the screen resolution (ScreenSettings.WIDTH/2f, for example). In addition, my idea is to adjust the viewport to the OpenGL scene, so multiple screen resolutions have the same appearance.

This mustn't be a good way to achieve that, but can't think of a better way to support multiple resolutions without having to hardcode them ony by one.

Do you have any suggestions?

I allways use meters size with my camera becouse I allways use box2d with my games. So in game my camera size is something like 25x15 but I dont use that camera for my UI or HUD's becouse I want static camera for my text and buttons. I also dont want to scale fonts much becouse then it look crappy. I have two solutions for this either is that I use pixel perfect matrix for UI. This work great if I only want draw simple text on left corner eg fps counter. But for ui where I have to do positioning etc I allways use 800x480 projection. Then I can desing UI using these coordinates but it will work also for every screensize. Font don't look super crappy If I use linear filtering and only scaled 0.5-1.5 its base size.

1  
2  
3  
4  
Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, 800,480);

//setting linear filtering
font.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);

Offline yuma

Junior Devvie


Medals: 2
Projects: 1


Monkeys are listening


« Reply #7 - Posted 2012-02-05 16:22:57 »

I think I was dealing with two problems at the same time:
  • Displaying the same scene in different resolutions with the same aspect ratio.
  • Displaying fonts positioned using openGL space.

I have dealt with the first problem using a fixed camera for a fixed (virtual) resolution, and modifying the viewport for each (physical) resolution. I'll try to make a blog post about this, because I think it's complicated to explain in words, but could be interesting for others (or for me if anyone thinks it's not the best way to accomplish it).

About the second problem, thanks for your insightful comments.

Quote from: Nate
You could use a camera that maps 1:1 with screen pixels. Your model could use whatever coordinate system you like, and you would convert to pixels only for drawing.

That sounds like what I want to do. When you say "you convert to pixels only for drawing" you mean I could set my sprites/fontCaches positions in my coordinates and then, in render() convert them using an custom utility class especially crafted for that? Or is there something more optimized (less custom)?

Quote from: pitbuller
But for ui where I have to do positioning etc I allways use 800x480 projection.

Yes, I'm thinking on working on 480x320 coordinates (that's what's called "pixel perfect" coordinates? I get lost easily...) and then scale using the linear filter for the font. Thanks for the code, it helps me a lot! Sometimes it's way better to use code/mathematics to explain concepts :-)

Offline yuma

Junior Devvie


Medals: 2
Projects: 1


Monkeys are listening


« Reply #8 - Posted 2012-02-05 22:06:12 »

I made a "tutorial" with the "keeping the aspect ratio" idea I had and took the liberty of posting it in the tutorial section (here Pointing visit it!).

All comments are welcomed!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (20 views)
2014-12-28 10:39:27

CopyableCougar4 (21 views)
2014-12-28 02:10:29

BurntPizza (25 views)
2014-12-27 22:38:51

Mr.CodeIt (15 views)
2014-12-27 04:03:04

TheDudeFromCI (20 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (60 views)
2014-12-15 09:26:44

Mr.CodeIt (48 views)
2014-12-14 19:50:38

BurntPizza (98 views)
2014-12-09 22:41:13

BurntPizza (117 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!