绑定 OpenGL 上下文时错误 GLFW_VISIBILE 提示的目的
Purpose of False GLFW_VISIBILE Hint While Binding OpenGL Context
我目前正在学习一个教程,其中包含创建 window
的初始化代码
private void init() {
// Setup an error callback. The default implementation
// will print the error message in System.err.
errorCallback = GLFWErrorCallback.createPrint(System.err);
glfwSetErrorCallback(errorCallback);
// Initialize GLFW. Most GLFW functions will not work before doing this.
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
// Configure our window
glfwDefaultWindowHints(); // optional, the current window hints are already the default
glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable
int WIDTH = 300;
int HEIGHT = 300;
// Create the window
window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL);
if (window == NULL) {
throw new RuntimeException("Failed to create the GLFW window");
}
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
glfwSetWindowShouldClose(window, true); // We will detect this in our rendering loop
}
}
});
// Get the resolution of the primary monitor
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// Center our window
glfwSetWindowPos(
window,
(vidmode.width() - WIDTH) / 2,
(vidmode.height() - HEIGHT) / 2
);
// Make the OpenGL context current
glfwMakeContextCurrent(window);
// Enable v-sync
glfwSwapInterval(1);
// Make the window visible
glfwShowWindow(window);
}
注意 glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
提示被禁用,然后在创建 window、设置键回调、绑定 opengl 上下文后,它再次启用 glfwShowWindow(window);
文档不建议做这样的事情,删除这两行似乎也没有改变任何东西。 为什么首先禁用提示?
教程:https://lwjglgamedev.gitbooks.io/3d-game-development-with-lwjgl/content/chapter1/chapter1.html
它禁用可见性,因此它可以设置大小、更改位置和初始化上下文,而无需客户端见证。来自 GLFW docs 中的 Window 可见性部分:
Windowed mode windows can be created initially hidden with the
GLFW_VISIBLE window hint. Windows created hidden are completely
invisible to the user until shown. This can be useful if you need to
set up your window further before showing it, for example moving it to
a specific location.
你的程序通过调用
来遵循这个确切的例子
glfwSetWindowPos(
window,
(vidmode.width() - WIDTH) / 2,
(vidmode.height() - HEIGHT) / 2
);
我目前正在学习一个教程,其中包含创建 window
的初始化代码private void init() {
// Setup an error callback. The default implementation
// will print the error message in System.err.
errorCallback = GLFWErrorCallback.createPrint(System.err);
glfwSetErrorCallback(errorCallback);
// Initialize GLFW. Most GLFW functions will not work before doing this.
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
// Configure our window
glfwDefaultWindowHints(); // optional, the current window hints are already the default
glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable
int WIDTH = 300;
int HEIGHT = 300;
// Create the window
window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL);
if (window == NULL) {
throw new RuntimeException("Failed to create the GLFW window");
}
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
glfwSetWindowShouldClose(window, true); // We will detect this in our rendering loop
}
}
});
// Get the resolution of the primary monitor
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// Center our window
glfwSetWindowPos(
window,
(vidmode.width() - WIDTH) / 2,
(vidmode.height() - HEIGHT) / 2
);
// Make the OpenGL context current
glfwMakeContextCurrent(window);
// Enable v-sync
glfwSwapInterval(1);
// Make the window visible
glfwShowWindow(window);
}
注意 glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
提示被禁用,然后在创建 window、设置键回调、绑定 opengl 上下文后,它再次启用 glfwShowWindow(window);
文档不建议做这样的事情,删除这两行似乎也没有改变任何东西。 为什么首先禁用提示?
教程:https://lwjglgamedev.gitbooks.io/3d-game-development-with-lwjgl/content/chapter1/chapter1.html
它禁用可见性,因此它可以设置大小、更改位置和初始化上下文,而无需客户端见证。来自 GLFW docs 中的 Window 可见性部分:
Windowed mode windows can be created initially hidden with the GLFW_VISIBLE window hint. Windows created hidden are completely invisible to the user until shown. This can be useful if you need to set up your window further before showing it, for example moving it to a specific location.
你的程序通过调用
来遵循这个确切的例子glfwSetWindowPos(
window,
(vidmode.width() - WIDTH) / 2,
(vidmode.height() - HEIGHT) / 2
);