AWTGLCanvas multisampling with JFrame

Started by dzolo, April 24, 2017, 13:08:42

Previous topic - Next topic

dzolo

Hi,

I am trying to enable multisampling for a AWTGLCanvas that is places inside a Java Swing JFrame.

Until now I have no success to do so. I found an old thread that solved this within an Applet (http://forum.lwjgl.org/index.php?topic=1889.msg11119). But setting the PixelFormat as described in that post has no effect.

Do you please have any other tips to get the multisampling working?

Thanks
Ondrej

Update: Added code sample.

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluOrtho2D;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.AWTGLCanvas;
import org.lwjgl.opengl.PixelFormat;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class CanvasTest {

  public static class CustomAWTGLCanvas extends AWTGLCanvas {

    public CustomAWTGLCanvas() throws LWJGLException {
      super(new PixelFormat(8, 8, 0, 4));
    }

    @Override
    public void paintGL() {
      try {
        glViewport(0, 0, getWidth(), getHeight());
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0.0f, (float) getWidth(), 0.0f, (float) getHeight());
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glColor3f(1f, 1f, 0f);
        glTranslatef(getWidth() / 2.0f, getHeight() / 2.0f, 0.0f);
        glRotatef(51f, 0f, 0f, 1.0f);
        glRectf(-50.0f, -50.0f, 50.0f, 50.0f);
        glPopMatrix();
        swapBuffers();
      } catch (LWJGLException ex) {
        ex.printStackTrace();
      }
    }
  }

  public static void main(String[] args) throws LWJGLException {

    CustomAWTGLCanvas canvas = new CustomAWTGLCanvas();

    JPanel panel = new JPanel(new BorderLayout());
    panel.setMinimumSize(new Dimension(100, 100));
    panel.add(canvas);

    JFrame frame = new JFrame("AWTGLCanvas - multisampling");
    frame.setPreferredSize(new Dimension(640, 480));
    frame.add(panel);
    frame.pack();

    frame.setVisible(true);
  }
}

Kai

Please note that LWJGL 2 is not maintained anymore for well over two years now.
You are very encouraged to upgrade to the current version 3, if you can. GLFW supports multiple windows. So if this was your reason to choose AWT, you don't need that anymore.
See: https://www.lwjgl.org/download
And I also cannot reproduce any bug with lacking multisampling on Windows 7 x64 with the LWJGL 2.9.3 release.

Taking your exact example code with sample count 8 produces:


Using sample count 0 produces:


You can clearly see the jagged edges in the last image.

A few questions:
- which OS are you using? (if some Linux variant, maybe with some custom compositor/window manager?)
- which exact version of LWJGL 2 are you using?

dzolo

Hi Kai,

unfortunately I am using libgdx which do not provide LWJGL3 AWT backend yet.

You are right it works. Previously I was testing it on Ubuntu 16.04 with Unity7 window manager when the example does not work.
When I tried the example in Windows 7 it has worked as expected.

Thank You for your suggestion!