Search code examples
c++copenglglfw

Why can't I see my red triangle on window with OpenGL core profile?


The following code should displays a red triangle on the window with OpenGL compatibility profile, but I can see nothing but a blank window core profile:

#include <GL/glew.h>
#include <GLFW/glfw3.h>

unsigned int shader_create(const char* src_vertex, const char* src_fragment) {
    ...
}

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit()) return -1;

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

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

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

    if (glewInit() != GLEW_OK) return -1;

    float coordinates[6] = {
        -0.5f, -0.5f,
        +0.0f, +0.5f,
        +0.5f, -0.5f
    };

    unsigned int buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(coordinates), coordinates, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);

    const char* vertex_shader   = ... ;
    const char* fragment_shader = ... ;

    unsigned int shader = shader_create(vertex_shader, fragment_shader);
    glUseProgram(shader);

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

        glDrawArrays(GL_TRIANGLES, 0, 3);

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

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

    glfwTerminate();
    return 0;
}

Vertex shader:

#version 330 core
layout(location = 0) in vec4 position;
void main() {
    gl_Position = position;
}

Fragment shader:

#version 330 core
layout(location = 0) out vec4 color;
void main() {
    color = vec4(1.0, 0.0, 0.0, 1.0);
}

When I use no shaders (not a good idea), I can perfectly see the triangle in the default white color (at least on my GPU). Also if use compatibility profile instead of core profile, it works.

I tried to compile as both C and C++ code but both fails to show the triangle.

What am I doing wrong here? Is the source code for shader incorrect?


Solution

  • When you use a core profile OpenGL Context, you've to generate and named Vertex Array Object, because the default Vertex Array Object (0) is not valid.

    Either switch to a compatibility profile:

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
    

    or create a named VAO for the vertex specification:

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);