【发布时间】:2021-09-20 04:40:15
【问题描述】:
我会直奔主题;我正在用 C++ 编写,我正在使用 glew (openGL)。然而,一个反复出现的问题是,当我第一次打开窗口并清除它时,'glClear' 并没有像预期的那样用白色填充窗口。它从左上角的偏移开始。这个偏移恰好与我的窗口和屏幕左上角之间的距离完全相同,所以我怀疑这有点奇怪。但是,我已经 3 个小时没有在网上找到任何关于它的信息,所以我决定自己问这个问题。附上问题代码和截图。 Screen capture of the window here, the red lines are the same length and added after the screenshot.
#define GLEW_STATIC
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
int main(int argc, char **argv){
if(!glfwInit()) std::cout << "glfw failed to init" << std::endl;
window = glfwCreateWindow(400, 400, "firstWindow", NULL, NULL);
if(!window) std::cout << "window creation failed" << std::endl;
while (!glfwWindowShouldClose(window)){
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwMakeContextCurrent(window);
}
更新: 我对整个 GL 事情真的很陌生,有人告诉我 glfwMakeContextCurrent 应该在 glfwWindowCreate 之后直接调用。一旦我这样做了,整个事情就变得不那么错误了。我现在得到一个灰色(不是白色)背景,至少部分。然而问题依然存在。我从代码中清除了 glew,因为它没有被使用。
#include <iostream>
#include <GLFW/glfw3.h>
GLFWwindow* window;
int main(int argc, char **argv){
if(!glfwInit()) std::cout << "glfw failed to init" << std::endl;
window = glfwCreateWindow(400, 400, "firstWindow", NULL, NULL);
if(!window) std::cout << "window creation failed" << std::endl;
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)){
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
【问题讨论】:
-
无法在 Kubuntu 20.04 上使用第二个程序进行复制。