glMakeContextCurrent / glViewport 致命错误

发布于 2025-01-20 20:19:26 字数 1667 浏览 3 评论 0原文

我正在网上关注一本关于如何开始使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮光沉寂 2025-01-27 20:19:26

GLFW.glfwMakeContextCurrent() 之后需要 GL.createCapability()

官方示例代码中提到了这一点(https://www.lwjgl.org/guide)。

 // 此行对于 LWJGL 与 GLFW 的互操作至关重要
  // OpenGL 上下文,或任何外部管理的上下文。
  // LWJGL 检测当前线程中的当前上下文,
  // 创建 GLCapability 实例并创建 OpenGL
  // 可供使用的绑定。
  GL.createCapability();

You need GL.createCapabilities() after GLFW.glfwMakeContextCurrent().

This is mentioned in the official sample code (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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文