Search code examples
javaopengljogl

Windows scaling causes blank spaces in JOGL


Im using Java OpenGL binding (JOGL 2.3.2) and the problem is my canvas is stuck in lower left corner of my form, so no matter what i write, theres black spaces on the top and right of canvas.

I figured out that this is because the scaling in my windows settings is set to 125%, and if i set it to 100%, the problem goes away. But, i feel like this is not the right way to solve such problems. Here is my code for creating a frame:

//FrameMain class just adds listener to frame so it can be closed by pressing cross button
final FrameMain frame = new FrameMain("Form 1");
frame.setSize(720, 720);

final GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(profile);

// The canvas
final GLCanvas glcanvas = new GLCanvas(capabilities);
AppMain b = new AppMain();
glcanvas.addGLEventListener(b);
glcanvas.setSize(720, 720);
frame.add(glcanvas);
frame.setVisible(true);

And my display and reshape methods:

public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glBegin(GL2.GL_QUADS);
    gl.glVertex3f(-1, 1, 0);
    gl.glVertex3f(1, 1, 0);
    gl.glVertex3f(1, -1, 0);
    gl.glVertex3f(-1, -1, 0);
    gl.glEnd();
}

public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    GL2 gl = drawable.getGL().getGL2();

    gl.glLoadIdentity();
    gl.glViewport(0, 0, width, height);
}

The result i am getting:

strange black spaces

How do i make so canvas occupy full form, and not like it is now?

PS: i cant change technologies used, it should be JOGL and something that creates window without external libraries. Yes, i know that swing is outdated and ugly


Solution

  • So, using Rabbid76's advice, I came up with this solution: I added this to my class, using this answer https://stackoverflow.com/a/70119734/20433881 :

        private static JFrame frame = null;
        private static float scalingFactor;
        
        private static float getScalingFactor() {
            return (float) getWindow(frame).getGraphicsConfiguration().getDefaultTransform().getScaleX();
        }
    

    Added this line to main(), after creation of a frame:

    scalingFactor = getScalingFactor();
    

    And also I changed line with viewport like this:

    gl.glViewport(0, 0, (int) (width * scalingFactor), (int) (height * scalingFactor));