有没有办法使用多个LWJGL上下文

时间:2022-09-23 07:55:40

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

我试图同时显示两个显示,使用线程并在2个不同的画布上显示它们。

Stats of software:

软件统计:

  • OS name Linux
  • 操作系统名称Linux

  • OS version 3.16.0-34-generic
  • 操作系统版本3.16.0-34-通用

  • LWJGL version 2.9.3
  • LWJGL版本2.9.3

  • OpenGL version 3.3 (Core Profile) Mesa 10.1.3
  • OpenGL版本3.3(核心配置文件)Mesa 10.1.3

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

我想要运行的GUI类的代码:

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:

我的DisplayManager类的示例代码:

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"

我得到的错误是:“任何时候只能实例化一个LWJGL上下文”

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

有没有办法显示每个线程的显示?目前只有一个画布显示一个显示。

1 个解决方案

#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.

LWJGL2只允许单个显示,可通过Display类中的静态方法访问。如果您想使用多个显示器,则必须升级到版本3,其中GLFW可以更好地控制您的窗口。

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.

但是,作为一种解决方法,您可以使用LWJGL Display和带有Canvas的JFrame。然后你可以渲染到帧缓冲区,用glGetTexImage获取像素数据并在画布中显示它。

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

但是因为这不会有很好的性能,并且由于LWJGL3已经稳定,我建议您使用新版本。

#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.

LWJGL2只允许单个显示,可通过Display类中的静态方法访问。如果您想使用多个显示器,则必须升级到版本3,其中GLFW可以更好地控制您的窗口。

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.

但是,作为一种解决方法,您可以使用LWJGL Display和带有Canvas的JFrame。然后你可以渲染到帧缓冲区,用glGetTexImage获取像素数据并在画布中显示它。

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

但是因为这不会有很好的性能,并且由于LWJGL3已经稳定,我建议您使用新版本。