Aspect ratio not updated when resizing viewport

Hi,

for my game I want the 3D viewport to take up only 80% of the screen. The GUI should take up 100% of the screen as usual.

My trivial approach was to simply do this:

cam.setViewPortBottom(0.2f);

This works, but the aspect ratio of the rendered scene is now wrong; everything is squeezed together.

I tried to resize the camera with no avail:

cam.resize(1024, 768 * 0.8f, true);

Updating the frustum didn’t work, either. What is the correct way to change the main camera’s/viewport’s size?

Additional question: Where would be the right place to put this code. Putting it into simpleInitApp() worked. Putting it into the initialize()-method of an AppState didn’t.

1 Like

Have you tried calling cam.update() after the adjustments?

1 Like

Not enough information provided… these two things are functionally identical so something else must be in play.

Regarding your other, you need to setup the frustum properly. See the setPerspective… related methods on camera. Look at the javadoc. You can specify your own aspect ratio there.

1 Like

Hi tonihele,
thanks for your answer. It didn’t fix the problem, unfortunately. I’ll provide a minimal testcase then. Maybe it’s something in my code…

1 Like

SimpleApplication.simpleInitApp() is not the same as SomeAppState.initialize(), really.

Judging from a little debugging it seems that the altered viewport information is overwritten by some jME fullscreen post effect before it could be actually applied to the viewport. (Disclaimer: I didn’t dive fully into the code, so this first result could be completely wrong/misleaded.)

there is no method “setPerspective()”. Did you mean “setFrustumPerspective()”?

1 Like

Minimal testcase, derived from jME’s HelloLoop example:

public class HelloLoop2 extends SimpleApplication
{
    public static void main(String[] args) {
        new HelloLoop2().start();
    }

    @Override
    public void simpleInitApp() {
        Geometry cube = new Geometry("blue cube", new Box(1, 1, 1));
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        cube.setMaterial(mat);
        
        rootNode.attachChild(cube);
        
        // Trying to update the viewport:
        cam.setViewPortBottom(0.5f);
        cam.resize(640, 480/2, false);
        cam.setFrustumPerspective(40, 640f/(480/2), 0.05f, 500f);
        cam.update();
    }
}

Here, I’m trying to set the viewport to half the window height. (Resolution is 640x480 in this example.) The result is a squeezed box.

What am I doing wrong?
I’m using the most recent trunk revision of jME 3.1.0.

2 Likes

This does work for me:

public class HelloLoop2 extends SimpleApplication
{
    public static void main(String[] args) {
        new HelloLoop2().start();
    }

    @Override
    public void simpleInitApp() {
        Geometry cube = new Geometry("blue cube", new Box(1, 1, 1));
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        cube.setMaterial(mat);
        
        rootNode.attachChild(cube);
        
        // Trying to update the viewport:
        cam.setViewPortBottom(0.5f);
        cam.setFrustumPerspective(40, 640f/(480f/2f), 0.05f, 500f);
        //cam.update();
    }
}
1 Like

Unfortunately on my machine and with the must current trunk version of jME 3.1.0 the cube is still squeezed together to half its original height even with your code.
What version of jME do you use?

Maybe it’s a regression in 3.1.0?!

1 Like

Wait, the cube should have the same absolute size, or only the same aspect ratio?

Not on the pc anymore, so not sure what jme version i used, but i can check tomorrow

1 Like

Everything rendered should look exactly the same, except that the visible viewport area should only cover the upper half of the window.

1 Like

well, eigther you render everything smaller, or you dont see as much since your available space is smaller.

Which one is it?

1 Like

Yeah, sure everything is smaller when the viewport is only half the size. But the aspect ratio should not be affected. There should be no stretching.
Just to make sure; this is what I’m seeing:

What I should be seeing is a (smaller) 1x1x1 cube, not a (seemingly) 2x1x1 cube.

1 Like

In fact I can basically put any value in the ‘aspect’ parameter of setFrustumPerspective() and don’t see any difference:

cam.setFrustumPerspective(40, 100, 0.05f, 500f);

Is this a bug? Can someone of the core devs confirm / dispute?!

1 Like

ah ok, my codr above does show a smaller cube. can verify version and the core code an an hour

1 Like

OK, first review shows that this does indeed not work anymore as it did in 3.0
Not yet sure if its related to Camera.java but a first view showed only 4 commits since the release of 3.0

1 Like

Quick headsup since i have to go back to work.

The FrustumValues (Left and Right are updated correctly) however the ProjectionMatrix is not…

        cam.setFrustumPerspective(40, 10f, 0.05f, 500f);
        System.out.println(cam.getFrustumLeft());
        System.out.println(cam.getProjectionMatrix());
        cam.setFrustumPerspective(40, 0.1f, 0.05f, 500f);
        System.out.println(cam.getFrustumLeft());
        System.out.println(cam.getProjectionMatrix());

Unfortunately i have also not found a code change in those lines… If i find time i will look into it more deeply in the evening. In the meantime you can open an issue on github, and link to this thread…

1 Like

See Updating the frustum does not work as expected · Issue #357 · jMonkeyEngine/jmonkeyengine · GitHub

3 Likes

same problem here, can’t change aspect ratio

1 Like

I’ve submitted a fix for the issue.

2 Likes