GLFW/GLEW:读取访问冲突时出错(glfwPollEvents 功能)

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

我目前正在学习有关 OpenGL 的教程,但遇到了问题。 在真正详细地告诉您之前,我想告诉您,我知道代码写得不好,但这只是一个学习如何使用 OpenGL 实际实现事情的教程,而不是编写真正好的代码的教程。希望你能理解。

这是我的问题,当我启动程序时,一切正常,但几秒钟后,应用程序崩溃了。它返回退出代码-1073741819并将其转换为十六进制,我发现这是错误代码C0000005:缓冲区溢出。然后我决定调试它,调试器说:

"Exception thrown at 0x00007FFD28144921 (igxelpicd64.dll) in App.exe: 0xC0000005: Access violation writing location 0x0000025170620000."

编译器输出图像

这是完整的代码:

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

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

struct ShaderProgramSource
{
    std::string VertexSource;
    std::string FragmentSource;
};

static ShaderProgramSource parseShader(const std::string& filepath)
{
    std::ifstream stream(filepath);

    enum class ShaderType
    {
        NONE = -1, VERTEX = 0, FRAGMENT = 1
    };

    std::string line;
    std::stringstream ss[2];
    ShaderType type = ShaderType::NONE;
    while (getline(stream, line))
    {
        if (line.find("#shader") != std::string::npos)
        {
            if (line.find("vertex") != std::string::npos)
                type = ShaderType::VERTEX;
            else if (line.find("fragment") != std::string::npos)
                type = ShaderType::FRAGMENT;
        }
        else
        {
            ss[(int)type] << line << '\n';
        }
    }

    return { ss[0].str(), ss[1].str() };
}

typedef unsigned int uint;

static uint compileShader(const std::string& source, uint type)
{
    uint id = glCreateShader(type);
    const char* src = source.c_str();
    glShaderSource(id, 1, &src, nullptr);
    glCompileShader(id);

    int result;
    glGetShaderiv(id, GL_COMPILE_STATUS, &result);
    if (result == GL_FALSE)
    {
        int length;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
        //char* message = (char*)alloca(length * sizeof(char));
        char* message = new char[length];
        glGetShaderInfoLog(id, length, &length, message);
        std::cout << "Failed to compile the " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment")
            << " shader !" << std::endl;
        std::cout << message << std::endl;
        delete[](message);
        glDeleteShader(id);
        return EXIT_FAILURE;
    }

    return id;
}

static uint CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
{
    uint program = glCreateProgram();
    uint vs = compileShader(vertexShader, GL_VERTEX_SHADER);
    uint fs = compileShader(fragmentShader, GL_FRAGMENT_SHADER);

    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    glValidateProgram(program);

    glDeleteShader(vs);
    glDeleteShader(fs);

    return program;
}

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    auto state = glewInit();
    if (state != GLEW_OK)
    {
        std::cerr << "Error initializing GLEW" << std::endl;
        return EXIT_FAILURE;
    }

    std::cout << glGetString(GL_VERSION) << std::endl;

    float positions[6] = {
        -0.5f, -0.5f,
         0.0f,  0.5f,
         0.5f, -0.5f
    };

    unsigned int buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);

    ShaderProgramSource source = parseShader("res/shaders/basic.shader");

    std::cout << "VERTEX" << std::endl;
    std::cout << source.VertexSource << std::endl;
    std::cout << "FRAGMENT" << std::endl;
    std::cout << source.FragmentSource << std::endl;

    //uint shader = CreateShader(source.VertexSource, source.VertexSource);
    //glUseProgram(shader);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES, 0, 3);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    //glDeleteProgram(shader);

    glfwTerminate();
    return 0;
}

我没有找到任何可以添加的内容,如果您需要任何其他信息,请告诉我。

我在谷歌上查找是否有人遇到过这个错误,但我没有找到任何有用的东西。我在 GLFW 文档上搜索,但我没有看到任何报告的错误。

c++ opengl runtime-error glfw
1个回答
0
投票

看来您没有使用着色器,因为您已注释掉

glCompileShader()
glUseProgram(shader)
并且 OpenGL 没有默认着色器。尝试取消注释这两行并将
glCompileShader
留在原处(在主循环之前),但在调用 'glDrawArrays()`
 之前将 
glUseShader(shader)

移动到渲染循环

另外,我没有看到您正在创建需要使用

glBindVertexArray(GLint: VAO)
创建和绑定的 VertexArrayObject,而是使用常规缓冲区作为
VertexArray
,因此用于创建
VertexArray
的代码应该如下所示

unsigned int VAO, VBO;

//this is missing in your code
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), &positions GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glBindVertexArrays(0)

现在,当您拥有顶点数组时,您可以使用以下方法渲染三角形

//while loop

glUseProgram(shaderProgram)
glBindVertexArrays(VAO)
glDrawArray(GL_TRIANGLES, 0, 3);

//----------

假设您的色调已正确编译并且您将所有内容传递给

VertexArrayBuffer
,这应该可以解决您的问题。

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