【问题标题】:GLFW, odd offset from the top left of the screen with glClearGLFW,使用 glClear 从屏幕左上角的奇数偏移
【发布时间】: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 上使用第二个程序进行复制。

标签: c++ opengl glfw


【解决方案1】:

与其他一些窗口库不同,GLFW 不会在 glfwCreateWindow() 成功后保留它为您创建的 GL 上下文。

如果没有当前的 GL 上下文,GL 命令将无法正常工作,因此您应该在 glfwCreateWindow() 之后立即调用 glfwMakeContextCurrent(),而不是在退出事件循环之后。

现在我怀疑您只是看到操作系统/驱动程序没有费心清除的剩余垃圾像素。

此外,当您使用 GLEW 时,您将希望在 glfwMakeContextCurrent() 之后调用 glewInit(),因此它声明的所有 GL 函数指针都不会指向任何地方。

【讨论】:

  • 感谢您的回答,您对上下文的看法是绝对正确的,因为我给了颜色 ´glClearColor(0.5f, 0.5f, 0.5f, 1.0f),所以我期望白色背景是错误的);´ 当我像你说的那样移动上下文时,我得到了正确的灰色来显示而不是黑色。但是,我从未真正使用过 glew 并将其从项目中删除。问题仍然存在,我更新了我的帖子。
【解决方案2】:

作为一种快速破解,如果您调整窗口大小,它会自行解决。如果宽度或高度与 Windows 当前尺寸不同,则使用 glfwSetWindowSize(window, width, height) 可以解决问题。我不知道为什么。同样,这是一个 hack,不是解决方案。

#include <iostream>
#include <GLFW/glfw3.h>

GLFWwindow* window;
const int width = 400;
const int height = 400;
int main(int argc, char **argv){
    if(!glfwInit()) std::cout << "glfw failed to init" << std::endl;
    
    window = glfwCreateWindow(width, height-1, "firstWindow", NULL, NULL);
    if(!window) std::cout << "window creation failed" << std::endl;
    glfwMakeContextCurrent(window);
    glfwSetWindowSize(window, width, height);
    
    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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 2021-09-26
    • 1970-01-01
    • 2015-09-28
    • 2022-01-06
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    相关资源
    最近更新 更多