在使用LWJGL中的glCreateShader时出现上下文错误。

huangapple go评论72阅读模式
英文:

Context error when using glCreateShader in LWJGL

问题

在尝试创建着色器时,它会抛出致命错误。

以下是代码部分:

glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

handle = glfwCreateWindow(800, 600, "Omlet", 0, 0);
if (handle == 0) {
    System.out.println("无法创建GLFW窗口");
    glfwTerminate();
    System.exit(-1);
}

glfwMakeContextCurrent(handle);
glfwSetFramebufferSizeCallback(handle, (win, w, h) -> glViewport(0, 0, w, h));
glCreateShader(GL_VERTEX_SHADER);

以下是错误信息:

本机方法中的致命错误: 线程[主,5,主]: 当前没有上下文,或者调用了当前上下文中不可用的函数。JVM 将中止执行。
	at org.lwjgl.opengl.GL20C.glCreateShader(Native Method)
	at net.zyrafal.Main.main(Main.java:25)

我正在使用 org.lwjgl.opengl.GL46Corg.lwjgl.glfw.GLFW

英文:

It throws a FATAL ERROR when i try to create a shader.

Here is the code

glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

handle = glfwCreateWindow(800, 600, "Omlet", 0, 0);
if (handle == 0) {
    System.out.println("Failed to create GLFW window");
    glfwTerminate();
    System.exit(-1);
}

glfwMakeContextCurrent(handle);
glfwSetFramebufferSizeCallback(handle, (win, w, h) -> glViewport(0, 0, w, h));
glCreateShader(GL_VERTEX_SHADER);

And here is the error:

FATAL ERROR in native method: Thread[main,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.
	at org.lwjgl.opengl.GL20C.glCreateShader(Native Method)
	at net.zyrafal.Main.main(Main.java:25)

I am using org.lwjgl.opengl.GL46C and org.lwjgl.glfw.GLFW

答案1

得分: 0

你遗漏了调用GL.createCapabilities()函数。这在LWJGL中是必要的,以便从线程本地上下文中获取OpenGL函数的函数指针。请参阅以下代码和解释性注释,网址为:https://www.lwjgl.org/guide :

// 这行代码对于LWJGL与GLFW的OpenGL上下文之间的交互,或者任何外部管理的上下文都是关键的。
// LWJGL会检测当前线程中的上下文,创建GLCapabilities实例,并使OpenGL绑定可供使用。
GL.createCapabilities();
英文:

You are missing the call to GL.createCapabilities(). It is needed for LWJGL to obtain the function pointers for OpenGL functions from a thread-local context.
Please see the code and explaining comments on https://www.lwjgl.org/guide :

// This line is critical for LWJGL's interoperation with GLFW's
// OpenGL context, or any context that is managed externally.
// LWJGL detects the context that is current in the current thread,
// creates the GLCapabilities instance and makes the OpenGL
// bindings available for use.
GL.createCapabilities();

huangapple
  • 本文由 发表于 2020年10月7日 03:39:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64232717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定