1

I'm having an issue whenever I call glGenBuffers on windows. Whenever I call glGenBuffers, or any 3.2 or above functions, OpenGL returns an INVALID_OPERATION error. After reading around the web this is probably being caused by not having the updated function pointer for 3.2 on windows. From everything I have read you must acquire the function pointers at runtime by asking the windows API wglGetProcAddress to return the function pointer for use in your program and your current driver. This in itself isn't hard, but why reinvent the wheel. Instead I choose to include GLEW to handle the function pointers for me.

Here is a small program to demonstrate my issue.

#define GLEW_STATIC
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <iostream>


void PrintError(void)
{
    // Print glGetError()
}

int main()
{


    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr);
    glfwMakeContextCurrent(window);
    PrintError();

    // Initalize GLEW
    glewExperimental = GL_TRUE;
    GLenum error = glewInit();
    if(error!= GLEW_OK)
        std::cerr << glewGetErrorString(error) << std::endl;


    // Glew information
    if(GLEW_VERSION_3_2)
        std::cout << "Supports 3.2.." << std::endl;

    // Try buffer
    GLuint buffer;
    glGenBuffers(1,&buffer); // INVALID_OPERATION
    PrintError(); // Error shows up


    // This is our main loop
    while(!glfwWindowShouldClose(window))
    {
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

When I run the following code I get an OpenGL windows. However, in my console I see that the first call to PrintError returned NO_ERROR while the second call returned INVALID_OPERATION. I have a feeling I'm overlooking some small fact, but I just can't seem to locate it on glfw or GLEWs webpage.

I'm currently running: glfw-3.0.4 (32bit) glew-1.10.0 (32bit)

**** Update ****

In response to glampert post I added the following code after the glewInit() method.

GLenum loop_error = glGetError();
while (loop_error != GL_NO_ERROR)
{
    switch (loop_error)
    {
    case GL_NO_ERROR: std::cout << "GL_NO_ERROR" << std::endl; break;
    case GL_INVALID_ENUM: std::cout << "GL_INVALID_ENUM" << std::endl; break;
    case GL_INVALID_VALUE: std::cout << "GL_INVALID_VALUE" << std::endl; break;
    case GL_INVALID_OPERATION: std::cout << "GL_INVALID_OPERATION" << std::endl; break;
    case GL_INVALID_FRAMEBUFFER_OPERATION: std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION" << std::endl; break;
    case GL_OUT_OF_MEMORY: std::cout << "GL_OUT_OF_MEMORY" << std::endl; break;
    }
    loop_error = glGetError();
}

Which confirms his assumption that the invalid operation is caused by the glewinit code.

* Update *

It looks like this is a known issue with GLEW & 3.2 context. http://www.opengl.org/wiki/OpenGL_Loading_Library

After locating GLEW as the trouble maker I located the following two post. OpenGL: glGetError() returns invalid enum after call to glewInit()

Seems like the suggested solution is to set the experimental flag, which I'm already doing. However, the website mentions that even after doing so their is still the possibility it will cause an invalid operation and fail to grab the function pointers.

I think at this point my best solution is to just grab my own function pointers.

Community
  • 1
  • 1
Freddy
  • 2,249
  • 1
  • 22
  • 31
  • 1
    It could very well be that the error you are getting is a residual error from GLFW/GLEW. Try `while (glGetError() != GL_NO_ERROR) { }` before creating the buffer and see what happens. – glampert Jun 29 '14 at 01:59
  • @glampert You're correct. I checked before the call to initialize glew and there were no errors. However, after initializing glew I noticed I had the invalid operation. I'm guessing its probably best just to grab my own function pointers instead of counting on glew. Thanks glampert, I never thought glew was causing my issues. Please post your response as a answer so I can give you credit. – Freddy Jun 29 '14 at 02:11
  • @Freddy I've run into a similar issue. What was your conclusion? – stephen Apr 10 '19 at 16:07

1 Answers1

2

It could very well be that the error you are getting is a residual error from GLFW or GLEW.

Try adding this after the library init code:

GLenum error;
while ((error = glGetError()) != GL_NO_ERROR) 
{
    // print(error), etc...
}

and before you attempt to call any OpenGL function, to clear the error cache.

If the error is indeed caused by GLEW, it may not necessarily be a dangerous one that stops the library from working. So I wouldn't drop the library just because of this issue, which will eventually be fixed. However, if you do decide to fetch the function pointers yourself, GLFW provides the glfwGetProcAddress function, which will allow you to do so in a portable manner.

glampert
  • 4,371
  • 2
  • 23
  • 49