4
votes

I've tried to follow this tutorial for OpenGL in C but when it comes to the second tutorial, the one that is supposed to draw a triangle on the window, I couldn't see anything. So this is what I did, I took the code that creates the OpenGL context, window and stuff and I tried to make it simpler: instead of using VAO I tried glBegin/glEnd.

I get this error: 1282 "invalid operation". I'm just using the same sentences taking directly from my LWJGL project. The main loop is so simple I can't understand how it does not work and the 1282 error is not giving me any information. Why do I still get an error?

#include <stdio.h>
#include <stdlib.h>

#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "glew32s.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")

// Include GLEW. Always include it before gl.h and glfw.h, since it's a bit magic.
#define GLEW_STATIC
#include <GL/glew.h>

// Include GLFW
#include <GLFW/glfw3.h>

// Include GLM
#include <glm/glm.hpp>
using namespace glm;

void checkErrors() {
    int error = glGetError();
    if (error != GL_NO_ERROR) {
        printf("%s (%d)\n", gluErrorString(error), error);
    }
}

int main(void)
{
    // Initialise GLFW
    if (!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\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);
    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 

    // Open a window and create its OpenGL context 
    GLFWwindow* window; // (In the accompanying source code, this variable is global) 
    window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
    if (window == NULL){
        fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window); // Initialize GLEW 
    glewExperimental = true; // Needed in core profile 
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    do{
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
            glVertex3f(-1.0f, -1.0f, 0.0f);
            glVertex3f( 1.0f, -1.0f, 0.0f);
            glVertex3f( 0.0f,  1.0f, 0.0f);
        glEnd();

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();

        // Check for errors
        checkErrors();

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

    return 0;
}
1
Might want to have glEnable(GL_DEPTH_TEST) aslo. You are missing alot this might help - videotutorialsrock.com/opengl_tutorial/basic_shapes/text.phpIrrational Person
www.opengl-tutorial.org sticks to modern OpenGL (3.1+) for a reason and I suggest you stick to what is presented in the tutorials until you have a better understanding of OpenGL. The modern way will feel more difficult at first but once you get the pattern of how it works you'll be glad you stuck with it.Lionel

1 Answers

10
votes

glBegin/glEnd, along with all other immediate mode drawing functions and some more, have been deprecated, and cannot be used with an OpenGL 3.1 (and up) core and forward compatible contexts.

You can try requesting a 3.0 compatibility context (which includes all of the deprecated functionality). To do this, remove the glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); and glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); lines, and change the minor version hint to 0. Indeed, according to the OpenGL wiki, you should not explicitly request a forward compatible context with 3.1 and newer anyway. However, your best bet is to figure out what's wrong with the VAO code instead of mucking around with deprecated functionality.