1

I am trying to show two displays at the same time, using threads and showing them on 2 different canvas.

Stats of software:

  • OS name Linux
  • OS version 3.16.0-34-generic
  • LWJGL version 2.9.3
  • OpenGL version 3.3 (Core Profile) Mesa 10.1.3

Code from my GUI Class that I am trying to run:

public UIMain2() {
    initComponents();

    canvas1 = new Canvas();
    canvas1.setSize(700, 350);
    canvas1.setBackground(Color.WHITE);
    canvas1.isDisplayable();
    canvas1.setVisible(true);      
    jPanel2.add(canvas1);      
    dt1 = new DisplayThread(canvas1);
    dt1.start();

    canvas2 = new Canvas();
    canvas2.setSize(700, 350);
    canvas2.setBackground(Color.WHITE);
    canvas2.isDisplayable();
    canvas2.setVisible(true);      
    jPanel3.add(canvas2);      
    dt2 = new DisplayThread(canvas2);
    dt2.start();
}

Sample code of my thread Class:

public class DisplayThread extends Thread {

    Canvas canvas;
    String modelString = "";
    Player player;
    Camera camera;
    DisplayManager m;

    public DisplayThread(Canvas canvas) {
        this.canvas = canvas;
    }

    public void run() {
        m = new DisplayManager();
        m.createDisplayJFrame(canvas);
        ...
    }

    ...

 }

Sample code of my DisplayManager Class:

public class DisplayManager {

    private static final int WIDTH = 700;
    private static final int HEIGHT = 350;
    private static final int FPS_CAP = 120;

    private static long lastFrameTime;
    private static float delta;

    //Display d = new Display();


    public void createDisplayJFrame(Canvas canvas) {

        ContextAttribs attribs = new ContextAttribs(3, 2)
            .withForwardCompatible(true).withProfileCore(true);

        try {

            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.setParent(canvas);
            Display.create(new PixelFormat(), attribs);
            Display.setTitle("Potato");
        } catch (LWJGLException ex) {
        }
        GL11.glViewport(0, 0, WIDTH, HEIGHT);   
    }

    ...

}

The error that I get is: "Only one LWJGL context may be instantiated at any one time"

Is there a way around this to display the display that each thread makes? At the moment only one canvas shows a display.

Eagle
  • 339
  • 1
  • 3
  • 14

1 Answers1

1

LWJGL2 only allows a single display, which is accessed through static methods in the Display class. If you wanted to use multiple displays, you would have to upgrade to version 3, where GLFW gives you a better control of your windows.

You could however, as a workaround, use the LWJGL Display and a JFrame with a Canvas. Then you could render into a framebuffer, get the pixel data with glGetTexImage and display it in the canvas.

But because this would not have good performance, and because LWJGL3 is already stable, I recommend that you use the new version.

javac
  • 2,431
  • 4
  • 17
  • 26