glGenVertexArrays(1, &ID) causes segfault only when using openGL 3.3 and core profile

52 Views Asked by At

Im currently using the OpenGL tutorial here. The function glGenVertexArrays(1, &ID); segfaults according to the VSCode debugger, and I believe that the function itself is an issue, as only using openGL 3.3 and above cause an issue

My main.cpp function is the following

#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;
}

However, changing

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

to

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

removes the segfault, and causes the code to be displayed correctly

0

There are 0 best solutions below