user868935
user868935

Reputation:

Cant initialize GLew with GLFW3

I'm trying to initialize GLew 1.10 along with the latest version of GLFW3. I've managed to get GLFW3 to work without a problem, but GLew is being quite difficult. I followed the example from the GLew website on how to initialize, but glewInit() for some reason is undefined and glewGetContext identifier not found / undefined (errors at bottom):

#include <GLew110\glew.h>
#define GLFW_INCLUDE_GLU
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>

static void error_callback(int error, const char* description)
{
    fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
    glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{   
    GLFWwindow* window;
    glfwSetErrorCallback(error_callback);
    if (!glfwInit())
        exit(EXIT_FAILURE);
    window = glfwCreateWindow(800, 600, "Simple example", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    GLenum err = glewInit(); // <---- "glewGetContext" is undefined (line 29)
    if (GLEW_OK != err)
    {
        /* Problem: glewInit failed, something is seriously wrong. */
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));

    }

    glfwMakeContextCurrent(window);
    glfwSetKeyCallback(window, key_callback);
    while (!glfwWindowShouldClose(window))
    {
        float ratio;
        int width, height;
        glfwGetFramebufferSize(window, &width, &height);
        ratio = width / (float) height;
        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
        glBegin(GL_TRIANGLES);
        glColor3f(1.f, 0.f, 0.f);
        glVertex3f(-0.6f, -0.4f, 0.f);
        glColor3f(0.f, 1.f, 0.f);
        glVertex3f(0.6f, -0.4f, 0.f);
        glColor3f(0.f, 0.f, 1.f);
        glVertex3f(0.f, 0.6f, 0.f);
        glEnd();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}

TWO ERRORS LISTED:

error C3861: 'glewGetContext': identifier not found (line 29 pointing to glewInit())
IntelliSense: identifier "glewGetContext" is undefined (line 29)

Upvotes: 0

Views: 2617

Answers (2)

Karol
Karol

Reputation: 11

Just in case someone has similar issue. Please try to call glewInit after calling glfwMakeContextCurrent :)

/* Make the window's context current */
glfwMakeContextCurrent(window);
int glewErr = glewInit();

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340516

Something is defining the macro GLEW_MX which is used to configure the glew library for multiple rendering contexts. Building with such a configuration requires some special preparation. The following is from http://glew.sourceforge.net/advanced.html (emphasis added):

Multiple Rendering Contexts (GLEW MX)

Starting with release 1.2.0, thread-safe support for multiple rendering contexts, possibly with different capabilities, is available. Since this is not required by most users, it is not added to the binary releases to maintain compatibility between different versions. To include multi-context support, you have to do the following:

  • Compile and use GLEW with the GLEW_MX preprocessor token defined.
  • For each rendering context, create a GLEWContext object that will be available as long as the rendering context exists.
  • Define a macro or function called glewGetContext() that returns a pointer to the GLEWContext object associated with the rendering context from which OpenGL/WGL/GLX calls are issued. This dispatch mechanism is primitive, but generic.
  • Make sure that you call glewInit() after creating the GLEWContext object in each rendering context. Note, that the GLEWContext pointer returned by glewGetContext() has to reside in global or thread-local memory.

If you don't need multiple rendering contexts, the best thing might be to find out where GLEW_MX is being set and fix it.

Upvotes: 1

Related Questions