我创建了一些代码来使用 lwjgl3,但由于我在 Mac 上工作,因此遇到了 macOS 绑定(bind)问题。
问题是我似乎无法让我的程序创建任何窗口。我确信这一点,因为程序就在那时停止了。没有错误。
代码卡住的行是:
long window = glfwCreateWindow(300, 300, "Hello World!", MemoryUtil.NULL, MemoryUtil.NULL);
由于我在 IntelliJ 上进行开发,每当我断开进程时,它都会崩溃:
Exception Type: EXC_BAD_ACCESS (SIGABRT)
Exception Codes: EXC_I386_GPFLT
Exception Note: EXC_CORPSE_NOTIFY
我觉得这不是问题,因为我尝试运行另一个依赖于 lwjgl3 的代码,并且它也卡在创建窗口的部分。
我知道代码卡在创建窗口的部分,因为我在每一行都放置了打印语句。
我真的希望这不是 macOS Big Sur 或 mac book pro 2015 型号的兼容性问题。
当glfwInit()
返回true
时,当我运行System.out.println(glGetString(GL_VERSION));
时,我得到了这个错误 fatal error :线程[main,5,main]:当前没有上下文或调用了当前上下文中不可用的函数。 JVM 将中止执行。
这是我用于测试的完整代码:
public static void main(String[] args) throws Exception{
GLFWErrorCallback.createPrint(System.err).set();
// Initialize GLFW. Most GLFW functions will not work before doing this.
if ( !glfwInit() )
throw new IllegalStateException("Unable to initialize GLFW");
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
// Configure GLFW
glfwDefaultWindowHints(); // optional, the current window hints are already the default
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Should be true for macOS, according to GLFW docs, to get core profile.
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// According to Apple docs, non-core profiles are limited to version 2.1.
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Create the window
long window = glfwCreateWindow(300, 300, "Hello World!", MemoryUtil.NULL, MemoryUtil.NULL);
//glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
System.out.println('0');
glfwDestroyWindow(window);
}
我已经用谷歌搜索过多次:
似乎无法找到相关答案。有人建议 macOS 有一个隐藏的弹出窗口,必须关闭该弹出窗 Eloquent 能分配窗口,但我尽一切努力禁用它(因为我找不到它),但仍然没有任何反应。我认为 GL_VERSION
表明 openGL 如何绑定(bind)到 Java 存在问题。
[编辑]
通过传入 JVM 选项 -XstartOnFirstThread
确保应用程序在主线程上运行。
[编辑2]
离开项目几个小时后,我回来重新运行它。 我在顶部添加了一行:
System.out.println("Hello LWJGL " + Version.getVersion() + "!");
createWindow(...)
函数开始工作,这很奇怪,因为我不知道为什么。如果我删除这条线,它也会开始工作,所以如果有人知道发生了什么,请告诉我。
我没有回答这个问题,因为我仍然不知道问题的原因,我只是很幸运它自行修复了。
[编辑3]
我在intellij上强制重建了整个项目,它又停止工作了。我真的很困惑为什么它在这之间工作
最佳答案
我在 gradle.properties 上有 -XstartOnFirstThread。尝试在当前运行配置的 jvmargs 中设置它。这为我解决了问题
关于java - lwjgl 3 macOS 创建窗口时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64976975/