2

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

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • You probably get the scaled size in the `reshape` callback. So the viewport is set with the wrong size (`gl.glViewport(0, 0, width, height);`) – Rabbid76 Nov 06 '22 at 19:32
  • 1
    Hm, you are right, adding 25% to width and height in viewport fixes the problem, but only with case when it scaled 125%. I might think about how to make it universal for every scaling percentage and post the answer with that – StreakerOne Nov 06 '22 at 20:53

1 Answers1

1

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));
gouessej
  • 3,640
  • 3
  • 33
  • 67
  • Your workaround is correct but if you want to give us a chance to fix it within JOGL, rather post on our official forum and indicate at least which version of Java you use. – gouessej Nov 07 '22 at 19:20
  • I use this workaround: https://jogamp.org/cgit/ardor3d.git/tree/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglAwtCanvas.java#n67 – gouessej Nov 07 '22 at 19:24