Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows scaling causes blank spaces in JOGL

Tags:

java

opengl

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

like image 353
StreakerOne Avatar asked Dec 22 '25 21:12

StreakerOne


1 Answers

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));
like image 64
StreakerOne Avatar answered Dec 24 '25 11:12

StreakerOne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!