Why does the window keep disappearing?

Hello I am learning OpenGL and glfw from the book learn OpenGL by packt.

I am doing chapter2 how to draw a triangle.

The code compiles ok but when I execute a.out the window appears for a split second then disappears again.

How can I make it stay there?

I am compiling with g++ -lGLEW -lGL -lglfw

I have:

#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>

const GLuint WIDTH = 800, HEIGHT = 600;

const GLchar * vertexShaderSource = "Version 330 core\n"

"layout (location = 0) in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
"}\0";

const GLchar * fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n";

int main()
{
glfwInit();

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

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow *window = glfwCreateWindow (WIDTH, HEIGHT, "Learn OpenGL", nullptr, nullptr);

int screenWidth, screenHeight;

glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

if (nullptr == window)
{
std::cout<<"Failed to create glfw window!\n";
glfwTerminate();

return EXIT_FAILURE;
}

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;

if (GLEW_OK != glewInit())
{
std::cout<<"Failed to initialize glew!"<<std::endl;

return EXIT_FAILURE;
}

glViewport(0,0,screenWidth,screenHeight);
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);

glCompileShader(vertexShader);
GLint success;
GLchar infoLog(512);

glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);

if(!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout<<"ERROR::SHADER::FRAGMENT::COMPILATION_FAILED"<<infoLog<<std::endl;
}

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
 
if(!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout<<"ERROR::SHADER::FRAGMENT::COMPILATION::FAILED\n"<<infoLog<<std::endl;
}

GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);

glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);

if(!success)
{
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout<<"ERROR::SHADER::PROGRAM::LINKING::FAILED\n"<<infoLog<<std::endl;
}

glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);

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

GLuint VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);

glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *) 0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);

glfwSwapBuffers(window);
}

glDeleteVertexArrays(1,&VAO);
glDeleteBuffers(1,&VBO);

glfwTerminate();

return EXIT_SUCCESS;
}

Hi @Al84 welcome to the GLFW forum,

It looks like your program had an error and exited, possibly because it couldn’t create a window or because it couldn’t initialize.

I would first take a look at trying to run the simple program in the documentation.

If this doesn’t run, you can add some error output information as follows:

#include <GLFW/glfw3.h>

void error_callback(int error, const char* description)
{
    fprintf(stderr, "Error: %s\n", description);
}

int main(void)
{
    GLFWwindow* window;

    /* set error callback */
    glfwSetErrorCallback(error_callback);

    /* Initialize the library */
    if (!glfwInit())
    {
        printf("glfwInit failed\n");
        return -1;
    }

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        printf("glfwCreateWindow failed to create a window\n");
        glfwTerminate();
        return -1;
    }

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

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

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

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

    glfwTerminate();
    return 0;
}

If it does run, then take a look at how the error callback works above and add that to your program. Note that you shouldn’t call glfwGetFramebufferSize until after you check if your window pointer is nullptr.

Hope this helps.