glGenVertexArrays(1, &ID) 仅在使用 openGL 3.3 和核心配置文件时导致段错误

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

我目前正在使用 OpenGL 教程here。根据 VSCode 调试器,函数

glGenVertexArrays(1, &ID);
出现段错误,我认为函数本身就是一个问题,因为仅使用 openGL 3.3 及更高版本会导致问题

我的main.cpp函数如下

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <glad/glad.hpp>


#include <stb/std_image.hpp>
#include <GLFW/glfw3.h>
#include <GL/glu.h>
#include <GL/gl.h>


#include <sugar/shader.hpp>
#include <sugar/ebo.hpp>
#include <sugar/vao.hpp>
#include <sugar/vbo.hpp>
#include <sugar/window.hpp>

GLfloat vertices[] = 
    {
        -0.5f, -0.5f, 0.0f,     1.0f, 0.0f, 0.0f,   0.0f, 0.0f, 
        -0.5f,  0.0f, 0.0f,     0.0f, 1.0f, 0.0f,   0.0f, 1.0f,
         0.5f,  0.5f, 0.0f,     0.0f, 0.0f, 1.0f,   1.0f, 1.0f, 
         0.5f, -0.5f, 0.0f,     1.0f, 1.0f, 1.0f,   1.0f, 1.0f
    };

    GLuint indices[] = 
    {
        0, 2, 1,
        0, 3, 2 
    };

static void glfwError(int id, const char* desc)
{
    fprintf(stderr, "Error #%d: %s", id, desc);
}

int main() 
{
    glfwInit();
    glfwSetErrorCallback(glfwError);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);  
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);


    GLFWwindow* window = glfwCreateWindow(800, 800, "Flight Sim", NULL, NULL);
    if (window == NULL)
    {
        fprintf(stderr, "Failed to create window!\n");
        glfwTerminate();
        exit(-1);
    }
    
    glfwMakeContextCurrent(window);
    gladLoadGL();

    glViewport(0, 0, 800, 600);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    Engine::Shader shaderProgram("res/shaders/default.vert", "res/shaders/default.frag");

    Engine::VAO VAO1;
    VAO1.Bind();

    Engine::VBO VBO1(vertices, sizeof(vertices));
    Engine::EBO EBO1(indices, sizeof(indices));

    VAO1.LinkAttrib(VBO1, 0, 3, GL_FLOAT, 8 * sizeof(float), (void*)0);
    VAO1.LinkAttrib(VBO1, 1, 3, GL_FLOAT, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    VAO1.LinkAttrib(VBO1, 2, 2, GL_FLOAT, 8 * sizeof(float), (void*)(6 * sizeof(float)));

    VAO1.Unbind();
    VBO1.Unbind();
    EBO1.Unbind();

    GLuint uniID = glGetUniformLocation(shaderProgram.ID, "scale");

    int imageWidth;
    int imageHeight;
    int numColChannels;

    unsigned char* bytes = stbi_load("res/images/pop_cat.png", &imageWidth, &imageHeight, &numColChannels, 0);

    GLuint texture;
    glGenTextures(1, &texture);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, bytes);
    glGenerateMipmap(GL_TEXTURE_2D);


    stbi_image_free(bytes);
    glBindTexture(GL_TEXTURE_2D, 0);

    GLuint tex0uni = glGetUniformLocation(shaderProgram.ID, "tex0");
    shaderProgram.Activate();
    glUniform1i(tex0uni, 0);

    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT);

        shaderProgram.Activate();
        glUniform1f(uniID, 0.5f);
        VAO1.Bind();
        glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_INT, 0); 

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    VAO1.Delete();
    VBO1.Delete();
    EBO1.Delete();
    shaderProgram.Delete();

    glDeleteTextures(1, &texture);


    glfwDestroyWindow(window);
    glfwTerminate();

    
    return 0;
}

然而,改变

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);  
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);  

删除段错误,并使代码正确显示

c++ opengl glfw glad
© www.soinside.com 2019 - 2024. All rights reserved.