0
\$\begingroup\$

I'm just starting out using GLEW and GLFW in a C++ project for my rendering code, but I'm having trouble actually getting anything to render.

I know OpenGL is working in some capacity, because I am able to change the background colour, but nothing else is working beyond that.

I'm setting up the OpenGL context with this code:

    //Attempts to initiate GLFW. If it doesn't, it returns immediately
    if (!glfwInit()) {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return;
    }

    //glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL

    window = GLFWwindowPtr();
    window.reset(glfwCreateWindow(width, height, windowTitle, NULL, NULL), DeleteWindow);

    if (window.get() == nullptr) {
        fprintf(stderr, "Failed to open GLFW window\n");
        glfwTerminate();
        return;
    }

    glfwMakeContextCurrent(window.get());

    glewExperimental = GL_TRUE; // Needed in core profile
    int errorCode = glewInit();
    if (errorCode) {
        printf("%i", errorCode);
        fprintf(stderr, "Failed to initialize GLEW\n");
        return;
    }

    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glfwSwapBuffers(window.get());

A little while after that in my main loop, I call this code every frame:

float ratio;
int width, height;

glfwGetFramebufferSize(WindowHandler::getMainWindowGLPointer(), &width, &height);
ratio = width / (float)height;
glViewport(0, 0, width, height);
glClearColor(red / 255, green / 255, blue / 255, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

Now this code works. I'm incrementing the red, green and blue variables every frame so that I can check that OpenGL is working as a whole and I see the background colour change.

However, when I put the following code after it, I'm seeing nothing different appear.

glBegin(GL_QUADS);              // Each set of 4 vertices form a quad
glColor3f(red/255, green/255, blue/255); // Red
glVertex2f(-0.5f, -0.5f);    // x, y
glVertex2f(0.5f, -0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();

I used the code from an existing example found on https://www3.ntu.edu.sg/home/ehchua/programming/opengl/CG_Introduction.html so I had a better chance of it being something in my implementation and not me messing up trying to write OpenGL code.

\$\endgroup\$
1
  • \$\begingroup\$ Because thats legacy OpenGL, you asked specifically for a 3.3 core profile, which is not backwards compatible. \$\endgroup\$
    – tkausl
    Commented Jun 3, 2016 at 21:36

1 Answer 1

2
\$\begingroup\$

Thats because glBegin, glVertex2f and glColor3f are legacy functions which are deprecated. You may still use them if you request a backwards-compatible context, however I'd recommend you stick to your core-context (which is not backwards compatible) and learn the more powerful modern OpenGL.

There is a pretty good modern OpenGL tutorial series on opengl-tutorial.org, since you've already created the window and set up your context you can start by drawing your first triangle.

\$\endgroup\$
3
  • \$\begingroup\$ Ah right, thanks for letting me know. I honestly had no idea there was an entire deprecated API, I just kinda assumed all of the tutorials and stuff would work. \$\endgroup\$ Commented Jun 3, 2016 at 21:53
  • \$\begingroup\$ @JamEngulfer The tutorials and stuff will confuse you. A lot. It's quite a jungle in there ;) \$\endgroup\$
    – Vaillancourt
    Commented Jun 3, 2016 at 22:16
  • \$\begingroup\$ Yeah, learning all of the OpenGL code I'm going to need is really quite daunting. Well, at least it'll be useful once I know it \$\endgroup\$ Commented Jun 3, 2016 at 23:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .