0

1) Problem summary:

I am following exactly the tutorials on LearnOpenGL dot com.

My program is crashing (with a segmentation fault) whenever glViewport is being called. How can I have it not crash while calling glViewport according to the code shown below (and in the tutorials).

2) What I have tried:

Besides googling this exact thing and correlating with multiple tutorials, I have also tried fiddling with the code. See the code below. Where it calls glViewport() in the main(), I have tried to comment this out and recompile. The program no longer crashes, but if I resize the window, it crashes with a segmentation fault. If I comment out the glViewport call and the glfwSetFramebufferSizeCallback call, then I can resize the window without crashing.

3) Below are the compilation command line, GLXINFO, and the code.

compiling with:

g++ program.cpp  -ldl -lGL -lglfw  -o program

I downloaded GLAD according to instructions and information from glxinfo (excerpted here)

OpenGL core profile version string: 4.6.0 NVIDIA 460.80 OpenGL core profile shading language version string: 4.60 NVIDIA OpenGL core profile context flags: (none) OpenGL core profile profile mask: core profile

OpenGL version string: 4.6.0 NVIDIA 460.80 OpenGL shading language version string: 4.60 NVIDIA OpenGL context flags: (none) OpenGL profile mask: (none)

OpenGL ES profile version string: OpenGL ES 3.2 NVIDIA 460.80 OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20

Code

#include <iostream>
#include "../glad/glad.c"
//#include "glad.c"
#include </usr/include/GLFW/glfw3.h>

// FUNCTION PROTOTYPES 

// register callback function to resize viewport if user resizes window
void framebuffer_size_callback(GLFWwindow* window, int width, int height); 

using namespace std;
int main()
{
    //initializes GLFW
    if (!glfwInit())
        return -1; 
    
    // initializes what version to use
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    //glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    
    // create window object
    GLFWwindow* window; 
    window = glfwCreateWindow(1024, 768, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        cout << "Failed to create GLFW window" << endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    
    // register viewport resize callback function to window (auto resize viewport)
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    
    /* THIS IS CRASHING WITH SEG FAULT ** */
    // viewport
    glViewport(0, 0, 800, 600);
    
    
    //glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); 
    
    // window or application loop
    while(!glfwWindowShouldClose(window))
    {
        glfwSwapBuffers(window);
        glfwPollEvents();    
    }
    
    
    // properly clean up and exit
    glfwTerminate(); 
    return 0;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    /* THIS IS CRASHING WITH SEG FAULT ** */
    glViewport(0, 0, width, height); 
}

1 Answers1

0

After continuing to have this problem with other gl functions and with further research, the problem can be solved by putting the following code after the glfwMakeContextCurrent(window). This has solved all segmentation faults with the glViewport calls listed in the problem.

gladLoadGL(); 
  • I am actually following this from the book. I think that this problem has been addressed in the latest website version (although with a slightly different command). The command that I posted appears to do the same thing as what the author has on the website. I was able to finish the exercise with the command I posted in my answer. – tx_writer_codes Jun 23 '21 at 00:15