LWJGL:如何创建两个不同的OpenGL上下文?

时间:2015-11-06 23:55:32

标签: java opengl lwjgl

    GLFW.glfwMakeContextCurrent(window);
    main = GLContext.createFromCurrent();
    other = GLContext.createFromCurrent();

这是我到目前为止所尝试的,在另一个帖子中,我打电话给

    GL.setCurrent(other);

但是我的所有OGL调用都没有效果(调用genVertexArrays()返回0)。 我无法在网上找到有关如何创建两个不同上下文的示例,更不用说在另一个线程中创建一个当前内容。

2 个答案:

答案 0 :(得分:3)

今天花了我12个多小时来解决这个问题,但是嘿,它有效并且很棒。无论如何,我缺少的一件事是实际的例子。所以,对于目前担任我所在职位的所有人来说,你走了:

import java.nio.FloatBuffer;

import nick.hacksoi.Program;

import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.system.MemoryUtil;

public class MultiThreadTest
{
    long mainWindow, otherWindow;

    public void run()
    {
        if (GLFW.glfwInit() != GL11.GL_TRUE)
            throw new IllegalStateException("Unable to initialize GLFW");

        mainWindow = GLFW.glfwCreateWindow(1366, 768, "threading", MemoryUtil.NULL, MemoryUtil.NULL);
        if (mainWindow == MemoryUtil.NULL)
            throw new RuntimeException("Failed to create the GLFW window");

        GLFW.glfwSetWindowPos(mainWindow, 1080 / 2 - 1366 / 4, 30);
        GLFW.glfwShowWindow(mainWindow);

        GLFW.glfwMakeContextCurrent(mainWindow);
        GLContext.createFromCurrent();

        GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GL11.GL_FALSE);
        otherWindow = GLFW.glfwCreateWindow(1, 1, "", MemoryUtil.NULL, mainWindow);
        if (otherWindow == MemoryUtil.NULL)
            throw new RuntimeException("Failed to create the GLFW window");

        Runner runner = new Runner();
        Thread other = new Thread(runner);
        other.start();
        try
        {
            other.join();
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        Program program = new Program("shaders/2d/simple.vs", "shaders/2d/simple.fs");

        int vao = GL30.glGenVertexArrays();

        GL30.glBindVertexArray(vao);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, runner.vbo);
        GL20.glEnableVertexAttribArray(0);
        GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);
        GL30.glBindVertexArray(0);
        GL20.glDisableVertexAttribArray(0);

        GL11.glClearColor(0.5f, 0.5f, 1f, 1);

        while (GLFW.glfwWindowShouldClose(mainWindow) != GL11.GL_TRUE)
        {
            GLFW.glfwPollEvents();
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

            program.use();
            {
                GL30.glBindVertexArray(vao);
                GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 6);
                GL30.glBindVertexArray(0);
            }
            program.unuse();

            GLFW.glfwSwapBuffers(mainWindow);
        }
    }

    private class Runner implements Runnable
    {
        public int vbo;

        @Override
        public void run()
        {
            GLFW.glfwMakeContextCurrent(otherWindow);
            GLContext.createFromCurrent();

            float[] vertices = new float[] { -1, -1, 0, 1, 1, -1 };
            FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(6);
            for (float f : vertices)
                vertexBuffer.put(f);
            vertexBuffer.flip();

            vbo = GL15.glGenBuffers();
            GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
            GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer, GL15.GL_STATIC_DRAW);
            GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        }
    }

    public static void main(String[] args)
    {
        new MultiThreadTest().run();
    }
}

在使用此测试代码之前我没有意识到的主要警告是VertexArrayObjects不在上下文之间共享;所以,你在渲染线程中生成它们。

无论如何,希望这有助于某人。它会为我节省数小时的痛苦和痛苦。

答案 1 :(得分:2)

您需要使用glfwCreateWindow创建另一个窗口(但不需要显示)。请参阅GLFW context guide