Lwjgl 3,如何在当前线程中获取OpenGL上下文当前信息? [英] Lwjgl 3, How to get OpenGL context current in the current thread?
问题描述
我在LWJGL 3中使用OpenGL,但出现以下错误;
I am using OpenGL in LWJGL 3 and I get the following error;
Exception in thread "main" java.lang.IllegalStateException: There is no OpenGL context current in the current thread.
at org.lwjgl.opengl.GL.getCapabilities(GL.java:157)
at org.lwjgl.opengl.GL11.getInstance(GL11.java:1390)
at org.lwjgl.opengl.GL11.glClearColor(GL11.java:1842)
at com.base.engine.RenderUtil.initGraphics(RenderUtil.java:13)
at com.base.engine.Main.<init>(Main.java:14)
at com.base.engine.Main.main(Main.java:24)
Exception in thread "main" java.lang.IllegalStateException: There is no OpenGL context current in the current thread.
at org.lwjgl.opengl.GL.getCapabilities(GL.java:157)
at org.lwjgl.opengl.GL11.getInstance(GL11.java:1390)
at org.lwjgl.opengl.GL11.glClearColor(GL11.java:1842)
at com.base.engine.RenderUtil.initGraphics(RenderUtil.java:13)
at com.base.engine.Main.<init>(Main.java:14)
at com.base.engine.Main.main(Main.java:24)
这是RenderUtil类,从我的主类的构造函数中调用initGraphics.在使用GLFW创建窗口之后,我也曾尝试调用initGraphics,该窗口也生成了类似的错误消息.
This is the RenderUtil class where initGraphics is called from the constructor of my main class. I have also tried to call initGraphics after creating a window with GLFW which has also generated a similar error message.
package com.base.engine;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL30.*;
public class RenderUtil {
public static void clearScreen() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
public static void initGraphics() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_FRAMEBUFFER_SRGB);
}
}
此外,我没有使用多线程.要创建一个窗口,我从主方法中调用方法Window.createWindow(1366, 768, "Test");
.
Also, I am not using multithreading. To create a window I call the method Window.createWindow(1366, 768, "Test");
from my main method.
private static Long window;
public static String createWindow(int width, int height, String title) {
if (GLFW.glfwInit() == 0) {
return "GLFW failed to initialise.";
}
GLFW.glfwWindowHint(GLFW.GLFW_SAMPLES, 4);
window = GLFW.glfwCreateWindow(width, height, title,
GLFW.glfwGetPrimaryMonitor(), 0);
if (window == null) {
GLFW.glfwTerminate();
return "Failed to create window.";
}
GLFW.glfwMakeContextCurrent(window);
return "GLFW has established a window.";
}
我尝试在主方法中将RenderUtil.initGraphics();
放置在两个不同的位置,都导致错误.
I have tried putting RenderUtil.initGraphics();
two different position in my main method, both resulting in errors.
private boolean isRunning = false;
private Game game;
// This is the constructor
public Main() {
// Pos 1 - RenderUtil.initGraphics();
isRunning = false;
game = new Game();
}
public static void main(String[] args) {
System.out.println(Window.createWindow(1366, 768, "Test"));
// Pos 2 - RenderUtil.initGraphics();
Main game = new Main();
game.start();
}
推荐答案
在createWindow
方法的末尾添加对GLContext.createFromCurrent()
的调用.
Add a call to GLContext.createFromCurrent()
at the end of the createWindow
method.
需要这种方法来设置LWJGL GL **类在幕后使用的上下文.
This method is needed to set the context used by the LWJGL GL** classes under the hood.
自最近的夜间版本(3.0.0b#11)起,此功能不再起作用,因为GLContext
类不再存在.而是在createWindow
方法的末尾添加GL.createCapabilities()
.
Since the latest nightly (3.0.0b #11) this no longer works, as the GLContext
class no more exists. Instead, add GL.createCapabilities()
at the end of the createWindow
method.
这篇关于Lwjgl 3,如何在当前线程中获取OpenGL上下文当前信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!