OpenGL 窗口无响应,全白,几秒钟后自行关闭

问题描述 投票:0回答:1

我已经尝试解决这个问题好几天了,我问过我的朋友,谷歌搜索,问过人工智能,但我无法解决这个问题。

我在 Visual Studio 2022 和 Win32 (x86) 平台上运行此程序。不过,在 x64 上也会出现同样的错误,我已经测试过了。

我在代码中没有发现任何错误,但我仍然尝试使用 std::cout 添加编译错误检查和打印错误到控制台,但上面没有弹出任何内容(控制台确实可以与 cout 配合使用)

当我删除从

// compile shaders
// create buffers n stuff
的所有内容并删除与 VBO/VAO 相关的所有内容时,它工作得很好!但这显然是一个问题,因为我很确定没有它们就无法渲染三角形。

如果有人帮助我弄清楚为什么会发生这种情况并解决它,我会真诚地兴奋、喜悦和狂喜地尖叫,提前非常感谢。

main.cpp:

#include <windows.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

using std::cout;


const char* vshSrc = 
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";

const char* fshSrc = 
"#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"   FragColor = vec4(1.0f, 0.8f, 0.4f, 1.0f);\n"
"}\0";


static void debugMode()
{
    AllocConsole();
    FILE* file;
    freopen_s(&file, "CONOUT$", "w", stdout);
    freopen_s(&file, "CONOUT$", "w", stderr);
}

static void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GLFW_TRUE);
}

static void framebufferSize(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}

_Use_decl_annotations_ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nShowCmd)
{
    if (!glfwInit())
        return -1;

    // window size and position
    const int vWidth = 1280;
    const int vHeight = 720;
    const int vX = (GetSystemMetrics(SM_CXSCREEN) / 2) - (vWidth / 2);
    const int vY = (GetSystemMetrics(SM_CYSCREEN) / 2) - (vHeight / 2);

    glfwWindowHint(GLFW_POSITION_X, vX);
    glfwWindowHint(GLFW_POSITION_Y, vY);

    // create window
    GLFWwindow* window = glfwCreateWindow(vWidth, vHeight, "Game", nullptr, nullptr);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    // buncha settings
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebufferSize);

    glfwSwapInterval(1);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    // comment below to remove console obv
    debugMode();

    // compile shaders
    unsigned int vsh = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vsh, 1, &vshSrc, NULL);
    glCompileShader(vsh);

    unsigned int fsh = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fsh, 1, &fshSrc, NULL);
    glCompileShader(fsh);

    // create shader program
    unsigned int sProgram = glCreateProgram();
    glAttachShader(sProgram, vsh);
    glAttachShader(sProgram, fsh);
    glLinkProgram(sProgram);

    glDeleteShader(vsh);
    glDeleteShader(fsh);

    // get verts
    float verts[] = 
    {
         -0.5f, -0.5f, 0.0f,
         0.5f, -0.5f, 0.0f,
         0.0f,  0.5f, 0.0f
    };

    // create buffers n stuff
    unsigned int VBO, VAO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);

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

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    // the loop ig
    while (!glfwWindowShouldClose(window))
    {
        processInput(window);

        // render
        glClearColor(0.1f, 0.35f, 0.35f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(sProgram);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        // swap buffers and poll events
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // kill everyone and leave without elaborating
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteProgram(sProgram);
    glfwTerminate();
    FreeConsole();

    return 0;
}
c++ glew opengl-compat
1个回答
0
投票

问题是GLEW从未初始化过。因此,未设置指向 OpenGL >1.0 函数的函数指针。在这种情况下,程序尝试调用

glCreateShader
(一个未初始化的函数指针)并崩溃。

解决方案是在

glewInit()
之后调用
glfwMakeContextCurrent(window);

程序现在应该渲染背景颜色,而不是三角形。这是由于启用了深度测试,但深度缓冲区从未被清除。要清除深度缓冲区和颜色缓冲区,请使用以下调用:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

或者在这种情况下,您也可以完全禁用深度测试。现在三角形也渲染出来了:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.