glMakeContextCurrent / glViewport 致命错误
我正在网上关注一本关于如何开始使用GLFW的书。作为参考,我正在使用Java。
我已经通过跟随这本书编写了一些代码,但是我遇到了此错误:本机方法中的致命错误:线程[Main,5,Main]:没有上下文是当前的,或者是在此处无法使用的函数当前上下文称为
。我的代码下面:
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryUtil;
public class Window {
private long window;
public Window()
{
}
public void init()
{
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 3);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
if(!GLFW.glfwInit())
{
throw new IllegalStateException("Could not initialize glfw");
}
window = GLFW.glfwCreateWindow(800, 600, "window", MemoryUtil.NULL, MemoryUtil.NULL);
if(window == MemoryUtil.NULL)
{
throw new IllegalStateException("Could not initialize window");
}
GLFW.glfwMakeContextCurrent(window);
GL11.glViewport(0, 0, 800, 600);
GLFW.glfwSetFramebufferSizeCallback(window, (window, width, height) -> {
GL11.glViewport(0, 0, width, height);
});
}
public void gameLoop()
{
while(!GLFW.glfwWindowShouldClose(window))
{
GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents();
}
GLFW.glfwTerminate();
}
public void cleanup() {
GLFW.glfwDestroyWindow(window);
}
}
我应该如何解决此错误?另外,如果我的代码结构不佳,请告诉我。
谢谢!
I am following a book online on how to get started with GLFW. For reference, I am using Java.
I have written some code by following along with the book, but I am getting this 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
. My code is below:
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryUtil;
public class Window {
private long window;
public Window()
{
}
public void init()
{
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 3);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
if(!GLFW.glfwInit())
{
throw new IllegalStateException("Could not initialize glfw");
}
window = GLFW.glfwCreateWindow(800, 600, "window", MemoryUtil.NULL, MemoryUtil.NULL);
if(window == MemoryUtil.NULL)
{
throw new IllegalStateException("Could not initialize window");
}
GLFW.glfwMakeContextCurrent(window);
GL11.glViewport(0, 0, 800, 600);
GLFW.glfwSetFramebufferSizeCallback(window, (window, width, height) -> {
GL11.glViewport(0, 0, width, height);
});
}
public void gameLoop()
{
while(!GLFW.glfwWindowShouldClose(window))
{
GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents();
}
GLFW.glfwTerminate();
}
public void cleanup() {
GLFW.glfwDestroyWindow(window);
}
}
How should I fix this error? Also, if my code is not structured well, please let me know.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
GLFW.glfwMakeContextCurrent()
之后需要GL.createCapability()
。官方示例代码中提到了这一点(https://www.lwjgl.org/guide)。
You need
GL.createCapabilities()
afterGLFW.glfwMakeContextCurrent()
.This is mentioned in the official sample code (https://www.lwjgl.org/guide).