java - 在 LWJGL 中使用 glCreateShader 时出现上下文错误
问题描述
当我尝试创建着色器时,它会引发致命错误。
这是代码
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);
这是错误:
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)
我正在使用 org.lwjgl.opengl.GL46C 和 org.lwjgl.glfw.GLFW
解决方案
您错过了对 的调用GL.createCapabilities()
。LWJGL 需要从线程本地上下文中获取 OpenGL 函数的函数指针。请参阅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();
推荐阅读
- java - 使用@ManyToOne 和@OneToMany,@Column(name = "QuizID") 给出警告“无法解析列 'QuizID'
- html - HTML:将 Google 地图放入 Twitter-Bootstrap div
- android - 有没有人在他们的 android 应用程序中使用过 android 的默认 FaceID 身份验证功能?
- android - 如何将android中可用的默认字体系列设置为操作栏标题
- javascript - 如何在 ExpressJS 中处理全局数据
- udp - 每次发送数据包后,我添加了Thread.sleep(1),为什么udp的丢包率下降了?
- linux - 如何判断 Openwrt 构建目标是使用大端还是小端?
- aws-lambda - 如何使用无服务器框架在 Lambda 中管理 Aurora Serverless 数据 API 的 typeORM 连接
- mysql - 在 mysql 中搜索类似的 unicode 字符
- ios - Firebase RealtimeDatabase 与第二个数据库的连接