pale_rider
pale_rider

Reputation: 75

Why is my opengl triangle code producing a black screen?

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

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;
    }


    glfwMakeContextCurrent(window);

    glewExperimental = true;
    glewInit();


    while (!glfwWindowShouldClose(window))
    {

        glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        std::cout << glGetError() << std::endl;

        glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(0.5f, -0.5f);
        glVertex2f(0.0f, 0.5f);
        glEnd();


        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

Despite setting glClearColor, only a black screen is shown. Drawing the triangle doesn't work either, and glGetError returns 0 everywhere. What am I doing wrong?

(I know this is a legacy way to render a triangle, I'm just trying to test if opengl is working, and apparently, it isn't.)

Upvotes: 1

Views: 326

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

You cannot use the legacy OpenGL (glBegin/glEnd sequences) with a Core profile OpenGL Context (GLFW_OPENGL_CORE_PROFILE) you must use a Compatibility profile OpenGL context (GLFW_OPENGL_COMPAT_PROFILE):

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);

Upvotes: 1

Related Questions