Opengl glfw仅在我的计算机上不更新窗口

Opengl glfw仅在我的计算机上不更新窗口,opengl,glfw,Opengl,Glfw,我已经编写了一个小应用程序,它使用opengl进行图形处理,使用glfw进行窗口处理。但是,当我在屏幕上绘制不同的内容时,窗口不会更新,除非我取消聚焦,然后通过alt tab返回,或者调整大小。然而,我在另一台计算机上测试了完全相同的程序,它工作得很好,所以这一定是我的计算机有问题或者glfw有缺陷,我如何修复/调试这个问题 以下是我调试时使用的代码,除非使用alt tab,否则不会发生任何更改: #include <iostream> // GLEW #define GLEW_S

我已经编写了一个小应用程序,它使用opengl进行图形处理,使用glfw进行窗口处理。但是,当我在屏幕上绘制不同的内容时,窗口不会更新,除非我取消聚焦,然后通过alt tab返回,或者调整大小。然而,我在另一台计算机上测试了完全相同的程序,它工作得很好,所以这一定是我的计算机有问题或者glfw有缺陷,我如何修复/调试这个问题

以下是我调试时使用的代码,除非使用alt tab,否则不会发生任何更改:

#include <iostream>

// GLEW
#define GLEW_STATIC
#include <GL/glew.h>

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


// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);

// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 800;

// Shaders
const GLchar* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
"}\0";
const GLchar* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";

int main()
{
    // Init GLFW
    glfwInit();
    // Set all the required options for GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    // Create a GLFWwindow object that we can use for GLFW's functions
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Assignment1", nullptr, nullptr);
    glfwMakeContextCurrent(window);

    // Set the required callback functions, this is for input from the keyboard
    glfwSetKeyCallback(window, key_callback);

    // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
    glewExperimental = GL_TRUE;
    // Initialize GLEW to setup the OpenGL Function pointers
    glewInit();

    // Define the viewport dimensions
    glViewport(0, 0, WIDTH, HEIGHT);


    // Build and compile our shader program
    // Vertex shader
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);
    // Check for compile time errors
    GLint success;
    GLchar infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // Fragment shader
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);
    // Check for compile time errors
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // Link shaders
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    // Check for linking errors
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);


    // Set up vertex data (and buffer(s)) and attribute pointers
    GLfloat vertices[] = {
        -0.5f, -0.5f, 0.0f, // Left  
        0.5f, -0.5f, 0.0f, // Right 
        0.0f, 0.5f, 0.0f  // Top   
    };
    GLuint VBO, VAO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO); // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind

    glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)

    // Game loop
    int mo = 0;
    auto mode = GL_LINE;
    while (!glfwWindowShouldClose(window))
    {
        // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
        glfwPollEvents();

        glBindVertexArray(VAO);
        glUseProgram(shaderProgram);
        glfwSwapInterval(3);
        if (mo++ % 2)
        {
            glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            mode = GL_LINE;
            glfwMakeContextCurrent(window);
        }
        else
        {
            glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
            glfwMakeContextCurrent(window);
            mode = GL_FILL;
        }

        // Render
        // Clear the colorbuffer
        glClear(GL_COLOR_BUFFER_BIT);

        // Draw our first triangle
            glPolygonMode(GL_FRONT_AND_BACK, mode);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwSwapBuffers(window);
        glBindVertexArray(0);

        // Swap the screen buffers
    }
    // Properly de-allocate all resources once they've outlived their purpose
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    // Terminate GLFW, clearing any resources allocated by GLFW.
    glfwTerminate();
    return 0;
}

// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}
#包括
//格洛
#定义GLEW_静态
#包括
//GLFW
#包括
//功能原型
void key_回调(GLFWwindow*window,int key,int scancode,int action,int mode);
//窗口尺寸
常数胶接宽度=800,高度=800;
//着色器
const GLchar*vertexShaderSource=“#版本330核心\n”
“布局(位置=0)在vec3位置;\n”
“void main()\n”
“{\n”
gl_位置=vec4(位置.x,位置.y,位置.z,1.0);\n
"}\0";
const GLchar*fragmentShaderSource=“#版本330核心\n”
“输出vec4颜色;\n”
“void main()\n”
“{\n”
color=vec4(1.0f,0.5f,0.2f,1.0f);\n
“}\n\0”;
int main()
{
//初始GLFW
glfwInit();
//为GLFW设置所有必需的选项
glfwWindowHint(GLFW_上下文_版本_专业,3);
glfwWindowHint(GLFW_上下文_版本_小调,3);
glfwWindowHint(GLFW_OPENGL_配置文件、GLFW_OPENGL_核心配置文件);
glfwWindowHint(GLFW_可调整大小,GL_为FALSE);
//创建可用于GLFW函数的GLFWwindow对象
GLFWwindow*window=glfwCreateWindow(宽度、高度、“赋值1”、nullptr、nullptr);
glfwMakeContextCurrent(窗口);
//设置所需的回调函数,这用于从键盘输入
glfwSetKeyCallback(窗口、键回调);
//将此设置为true,以便GLEW知道如何使用现代方法检索函数指针和扩展
glewExperimental=GL_TRUE;
//初始化GLEW以设置OpenGL函数指针
glewInit();
//定义视口尺寸
glViewport(0,0,宽度,高度);
//构建并编译我们的着色器程序
//顶点着色器
GLuint vertexShader=glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader,1和vertexShaderSource,NULL);
glCompileShader(顶点着色器);
//检查编译时错误
辉煌的成功;
GLchar信息日志[512];
glGetShaderiv(顶点着色器、GL\u编译状态和成功);
如果(!成功)
{
glGetShaderInfoLog(vertexShader,512,NULL,infoLog);

std::让我把这一点转到你身上:“我在另一台计算机上测试了GLFW,所以它一定是你程序中的一个bug。”你知道这种逻辑是怎么不起作用的吗?在一台计算机上测试你的程序并不意味着它是正确的。它可能是你程序中的一个bug,但可能是GLFW中的一个bug。你正在调用
glfwSwapBuffers()
,是吗?是的,我也在认真学习learnopengl.com上的在线教程,但每当我试图通过键盘更改变量值来绘制不同的形状时,新形状就不存在了drawn@fYre:在没有看到您的代码的情况下,我们怎么可能知道这是如何发生的或为什么发生的呢?您越确定某个bug不在某个位置n、 它越有可能隐藏在那里。为什么要在循环中调用
glfwMakeContextCurrent
?只需调用一次(在获得窗口后)除非您要渲染到多个窗口。此外,您也不需要在循环中调用
glfwSwapInterval
。调试时,我尝试了所有可能的方法…我几乎尝试了我能想到的所有方法,包括在不同位置和不同值的交换间隔
#include <iostream>

// GLEW
#define GLEW_STATIC
#include <GL/glew.h>

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


// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);

// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 800;

// Shaders
const GLchar* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
"}\0";
const GLchar* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";

    auto type = GL_LINE;
int main()
{
    // Init GLFW
    glfwInit();
    // Set all the required options for GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    // Create a GLFWwindow object that we can use for GLFW's functions
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Assignment1", nullptr, nullptr);
    glfwMakeContextCurrent(window);

    // Set the required callback functions, this is for input from the keyboard
    glfwSetKeyCallback(window, key_callback);

    // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
    glewExperimental = GL_TRUE;
    // Initialize GLEW to setup the OpenGL Function pointers
    glewInit();

    // Define the viewport dimensions
    glViewport(0, 0, WIDTH, HEIGHT);


    // Build and compile our shader program
    // Vertex shader
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);
    // Check for compile time errors
    GLint success;
    GLchar infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // Fragment shader
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);
    // Check for compile time errors
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // Link shaders
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    // Check for linking errors
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);


    // Set up vertex data (and buffer(s)) and attribute pointers
    GLfloat vertices[] = {
        -0.5f, -0.5f, 0.0f, // Left  
        0.5f, -0.5f, 0.0f, // Right 
        0.0f, 0.5f, 0.0f  // Top   
    };
    GLuint VBO, VAO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO); // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind

    glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)

    // Game loop
    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();
        std::cout << type << "\n";

        glUseProgram(shaderProgram);
            glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

        glClear(GL_COLOR_BUFFER_BIT);

        // Draw our first triangle
            glPolygonMode(GL_FRONT_AND_BACK, type);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glBindVertexArray(0);
        glfwSwapBuffers(window);

        // Swap the screen buffers
    }
    // Properly de-allocate all resources once they've outlived their purpose
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    // Terminate GLFW, clearing any resources allocated by GLFW.
    glfwTerminate();
    return 0;
}

// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
    else    if (key == GLFW_KEY_F)
    {
        type = GL_FILL;
    }
    else if (key == GLFW_KEY_L)
    {
        type = GL_LINE;
    }
}