0

I am having troubles using OpenGL functions in my render loop.
Here is my code :

// This works, I have glad in an include folder with glad headers in it
#include "include/glad/glad.h" 
#include <GLFW/glfw3.h>
#include <iostream>


int main(int argc, char *argv[])
{
    if (!glfwInit())
    {
        std::cerr << "Failed to initialise GLFW !" << std::endl;
        return -1;
    }
    glfwSetErrorCallback(callback_error);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
    GLFWwindow *window = glfwCreateWindow(640, 480, "Block++", NULL, NULL);

    if (!window)
    {
        std::cerr << "Failed to open window" << std::endl;
        return -1;
    }
    glfwMakeContextCurrent(window);

    gladLoadGL();

    // This line works well so I guess the issue is not related to OpenGL imports
    std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;

    glfwSwapInterval(1);

    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

    glClearColor(0.22f, 0.83f, 0.86f, 0.0f);
    camera.setWindowSize(640, 480);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glClearDepth(1.0f);

    //Some initialization stuff here, removing does not impact the result


    // This is the main render loop
    while (!glfwWindowShouldClose(window))
    {
        
        // The error happens here (I saw that using gdb with Visual Studio Code)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // And it happens here if I remove the glClear line
        glLoadIdentity(); 

        // The actual rendering stuff is never executed because of the error

        glFlush();
        glfwSwapBuffers(window);

        glfwWaitEvents();
        //glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
}

And the error message is not very helpful and specific :

OpenGL version: 4.6 (Core Profile) Mesa 21.1.1 // OpenGL seems to be working
[1]    19991 segmentation fault (core dumped)  ./main

The answers I found on StackOverflow and on other websites didn't help me fix my problem as they often refer to GLUT which I was told is out-of-date.

genpfault
  • 51,148
  • 11
  • 85
  • 139
LiteApplication
  • 177
  • 2
  • 13
  • 4
    `glLoadIdentity()` will not work with a 4.6 Core Profile context. – Rabbid76 May 31 '21 at 18:49
  • @Rabbid76 I don't understand exactly what your answer implies : should I bee using something else instead of `glLoadIdentity()` or should I use another version of OpenGL ? – LiteApplication May 31 '21 at 18:52
  • 2
    You cannot use `glLoadIdentity` in a core profile context, because `glLoadIdentity` is obsolete and can only be used with a compatibility profile. See [OpenGL Context](https://www.khronos.org/opengl/wiki/OpenGL_Context) and [Legacy OpenGL](https://www.khronos.org/opengl/wiki/Legacy_OpenGL). – Rabbid76 May 31 '21 at 18:53
  • 4
    Also, you should _generate_ a GL loader with glad which matches the actual GL version and profile you are using. A proper core profile loader will not expose `glLoadIdentity` to begin with. – derhass May 31 '21 at 18:55
  • Thank you for your answers, I am going to do some more research about that – LiteApplication May 31 '21 at 18:56
  • @Rabbid76 Is [this](https://open.gl/drawing) up-to-date ? – LiteApplication Jun 01 '21 at 07:36

1 Answers1

0

I found two ways to "fix" the issue :

  1. Using the compatibility profile (not recommended but very easy):
// Replace this
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// by this
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);

This is not really a fix but it allows the program to run deprecated and removed instructions.

  1. Properly loading GLAD and use modern OpenGL (that's the good way of doing it)
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
    std::cout << "Failed to initialize GLAD" << std::endl;
    return -1;
}

This loads GLAD properly, but the rest of the code has to be refactored to fit modern OpenGL requirements (shaders, etc.).
I learned a lot about shaders and vertices arrays on this website.

LiteApplication
  • 177
  • 2
  • 13