Search code examples
c++opengllinkerglfwglew

How to correctly link an opengl program under debian?


To learn how to do opengl, I have a minimalist build system and a toy program. My makefile produces these commands:

g++ -O1 -std=c++17 -Wall -Wextra -pedantic   -c -o main.o main.cpp
g++   main.o  -lglfw -lGLEW -lGL -o main

In that directory I have a lone main.cpp file. Contrary to many questions here on SO, this actually works. It compiles and links without problems. However, when I run the program, it merely displays the background colour I write with glClear but not my test triangle.

I have verified that it's not a problem with my code, because if I compile it with a (quite complex and bloated) build system I found in a tutorial it seems to work. However, I want to understand how to actually build an opengl program myself. I suspect I am missing some library or something, but I would expect the program to crash, not to just do (almost) nothing.

I am running the newest debian.

How can it be that it compiles and links perfectly, but fails to run correctly?


Disclaimer: I am absolutely new to opengl and windowed applications. I am not a 100% certain why I need glew.

Code:

#include <iostream>

#include <GL/glew.h>

#include <GLFW/glfw3.h>

bool install_shader(GLuint where, std::string const& code, GLuint* attach_to) {
  GLuint shader_id = glCreateShader(where);
  {
    auto const p = code.c_str();
    glShaderSource(shader_id,1,&p,nullptr); // nullptr indicates null-termination here
  }
  glCompileShader(shader_id);
  GLint out;
  glGetShaderiv(shader_id, GL_COMPILE_STATUS, &out);
  if (out == GL_FALSE) {
    // something went wrong with the shader compilation
    glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &out);
    if (out == 0) {
      std::cout << "Unknown Error during shader compilation\n";
    } else {
      std::string error;
      error.resize(out-1);
      glGetShaderInfoLog(shader_id, out, nullptr, error.data());
      std::cout << "Shader Compilation failed with error: " << error;
    }
    return false;
  } else {
    std::cout << "shader(" << int(shader_id) << "@" << int(where) << ") compiled fine\n";

    if (attach_to) {
      glAttachShader(*attach_to, shader_id);
    }

    // XXX THE SHADERS SHOULD BE DELETED AFTER LINKING !!!!
    // glDeleteShader(shader_id)

    return true;
  }
}

bool install_program(GLuint program_id) {
  glLinkProgram(program_id);
  GLint out;
  glGetProgramiv(program_id, GL_LINK_STATUS, &out);
  if (out == GL_FALSE) {
    // something went wrong with the program linking
    glGetProgramiv(program_id, GL_INFO_LOG_LENGTH, &out);
    if (out == 0) {
      std::cout << "Unknown link-Error in shader program\n";
    } else {
      std::string error;
      error.resize(out-1);
      glGetProgramInfoLog(program_id, out, nullptr, error.data());
      std::cout << "Program linking failed with error: " << error;
    }
    return false;
  } else {
    std::cout << "program(" << int(program_id) << ") compiled fine\n";
    return true;
  }
}

int main() {
  if (glfwInit()) {
    std::cout <<  "Initialisation fine\n";
  } else {
    std::cout <<  "Initialisation failed\n";
    return 1;
  }

  glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

  if (GLFWwindow* window = glfwCreateWindow(800,600,"testwindow",nullptr,nullptr)) {
    glfwMakeContextCurrent(window);

    if (glewInit() == GLEW_OK) {
      std::cout << "GLEW also fine\n";
    } else {
      std::cout << "GLEW says nope!\n";
      goto die;
      return 2;
    }

    //std::cout << "Ensure we can capture the escape key being pressed below...\n";
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
    /*business*/
    // (1) specify the triangle to draw
    // An array of 3 vectors which represents 3 vertices
    GLfloat g_vertex_buffer_data[] = {
       -0.4f, -0.8f, 0.f,
        0.4f, -0.8f, 0.f,
        0.0f,  0.8f, 0.f,
    };
    GLuint vertexbuffer;
    glGenBuffers(1, &vertexbuffer);

    GLuint program_id = glCreateProgram();

    // (3) vertex shader
    std::string vshader_code = "#version 330 core\n"
      "layout (location = 0) in vec3 aPos;\n"
      "\n"
      "void main()\n"
      "{\n"
      "    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
      "}\n";

    if (!install_shader(GL_VERTEX_SHADER,vshader_code,&program_id)) {
      goto die;
    }

    std::string fshader_code = "#version 330 core\n"
      "out vec3 color;\n"
      "\n"
      "void main()\n"
      "{\n"
      "    color = vec3(0.9,0.8,0.1);\n"
      "}\n";

    if (!install_shader(GL_FRAGMENT_SHADER,fshader_code,&program_id)) {
      goto die;
    }

    if (!install_program(program_id)) {
      goto die;
    }

    glClearColor(0.3, 0.5, 0.5, 1.0);

    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

    do {
      glClear(GL_COLOR_BUFFER_BIT);

      glUseProgram(program_id);


      glEnableVertexAttribArray(0); // this 0 also refes to 'layout (location = 0)' in the vertex shader

      glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
      glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

      glVertexAttribPointer(
        0,        // this refes to 'layout (location = 0)' in the vertex shader
        3,        // each vertex has 3 coordinates (2d data is also possible)
        GL_FLOAT, // coordinates are of type GLfloat (GL_FLOAT)
        GL_FALSE, // we don't need the input data to be nomalised,
        0, //&g_vertex_buffer_data[3] - &g_vertex_buffer_data[0], // stride
        nullptr        // offset ptr
      );

      // Draw the triangle !
      glDrawArrays(GL_TRIANGLES, 0, 3);
      glDisableVertexAttribArray(0);

      glfwSwapBuffers(window);

      glfwPollEvents();
    } // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 );

  } else {
    std::cout << "Could not create window\n";
  }

  die:
  glfwTerminate();
  std::cout <<  "Terminated\n";
}

Solution

  • You need to have a Vertex Array Object, and bind that to store the states the are required to draw the triangle:

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    
    // your remaining code
    GLuint vertexbuffer;
    glGenBuffers(1, &vertexbuffer);
    // ...
    

    The compatibility OpenGL profile makes VAO object 0 a default object. The core OpenGL profile makes VAO object 0 not an object at all. So if VAO 0 is bound in the core profile, you should not call any function that modifies VAO state. This includes binding the GL_ELEMENT_ARRAY_BUFFER with glBindBuffer.

    Depending on the profile and/or driver there can be an active default VAO which would result in the traingle to be rendered. But to have a standard complain setup you are required to create a VAO for the object you want to render.

    This does not really explain why it works with the bundled GLFW and GLEW, but maybe one of them creates a default VAO to mimic the compatibility profile behavior or the on of the NVIDIA driver.

    You can check if you are in core profile or compatibility profile using that code:

    GLint prof;
    glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &prof);
    std::cout << (prof&GL_CONTEXT_CORE_PROFILE_BIT) << " " << (prof&GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) << std::endl;