LibGDX FrameBuffer scaling
Asked Answered
M

2

6

I'm working on a painting application using the LibGDX framework, and I am using their FrameBuffer class to merge what the user draws onto a solid texture, which is what they see as their drawing. That aspect is working just fine, however, the area the user can draw on isn't always going to be the same size, and I am having trouble getting it to display properly on resolutions other than that of the entire window.

I have tested this very extensively, and what seems to be happening is the FrameBuffer is creating the texture at the same resolution as the window itself, and then simply stretching or shrinking it to fit the actual area it is meant to be in, which is a very unpleasant effect for any drawing larger or smaller than the window.

I have verified, at every single step of my process, that I am never doing any of this stretching myself, and that everything is being drawn how and where it should, with the right dimensions and locations. I've also looked into the FrameBuffer class itself to try and find the answer, but strangely found nothing in there either, but, given all of the testing I've done, it seems to be the only possible place for this issue to be created somehow.

I am simply completely out of ideas, having spent a considerable amount of time trying to troubleshoot this problem.

Mastat answered 29/12, 2012 at 19:33 Comment(2)
Welcome to StackOverflow. You'll get better response to your post if you have a discrete question and show what you've tried, what happens, etc.Malkin
I would really like to be able to post a more specific question and give more details, but given the nature of the problem, I simply have no idea where I should be focusing and am at a complete loss. I am happy to post anything that may help on request, but at the moment I am not even sure what would be genuinely helpful to add here.Mastat
C
7

Thank you so much Synthetik for finding the core issue. Here is the proper way to fix this situation that you elude to. (I think!)

The way to make frame buffer produce a correct ratio and scale texture regardless of actual device window size is to set the projection matrix to the size required like so :

SpriteBatch batch = new SpriteBatch();
Matrix4 matrix = new Matrix4();
matrix.setToOrtho2D(0, 0, 480,800); // here is the actual size you want
batch.setProjectionMatrix(matrix);
Confiscable answered 16/1, 2013 at 0:19 Comment(0)
M
3

I believe I've solved my problem, and I will give a very brief overview of what the problem is.

Basically, the cause of this issue lies within the SpriteBatch class. Specifically, assuming I am not using an outdated version of the class, the problem lies on line 181, where the projection matrix is set. The line :

        projectionMatrix.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

This is causing everything that is drawn to, essentially, be drawn at the scale of the window/screen and then stretched to fit where it needs to afterwards. I am not sure if there is a more "proper" way to handle this, but I simply created another method within the SpriteBatch class that allows me to call this method again with my own dimensions, and call that when necessary. Note that it isn't required on every draw or anything like that, only once, or any time the dimensions may change.

Mastat answered 2/1, 2013 at 4:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.