GLEW initalization error: Missing GL version

Discussion in 'Visualization' started by Ethan Brooks, Oct 11, 2017 at 7:11 PM.

  1. I have the following code
    Code:
    #include "mujoco.h"
    #include "glfw3.h"
    
    
    mjModel* m;
    mjrContext c;
    char error[300];
    
    int main(int argc, const char** argv) {
      mjr_defaultContext(&c);
      GLFWwindow* window = glfwCreateWindow(800, 800, "Invisible window", NULL, NULL);
      glfwMakeContextCurrent(window);
      m = mj_loadXML(argv[1], 0, error, 1000);
      mjr_makeContext(m, &c, mjFONTSCALE_150);
      mjr_freeContext(&c);
    }
    
    For some reason, when I run this, I get
    Code:
    ❯ ./glfw-bug 
    ERROR: GLEW initalization error: Missing GL version
    
    Press Enter to exit ...
    
    I have looked at a few similar problems on other forums but the comments were either vague, or the solutions didn't work. I appreciate your help with this.

    Versions:
    Using mjPro150 on Ubuntu 16.04.

    Code:
    ❯ apt show libglfw3             
    Package: libglfw3
    Version: 3.1.2-3
    Priority: optional
    Section: universe/libs
    Source: glfw3
    Origin: Ubuntu
    Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
    Original-Maintainer: Debian Games Team <pkg-games-devel@lists.alioth.debian.org>
    Bugs: https://bugs.launchpad.net/ubuntu/+filebug
    Installed-Size: 113 kB
    Depends: libc6 (>= 2.15), libx11-6 (>= 2:1.2.99.901), libxcursor1 (>> 1.1.2), libxi6 (>= 2:1.2.99.4), libxinerama1, libxrandr2 (>= 2:1.2.99.3), libxxf86vm1
    Homepage: http://www.glfw.org/
    Download-Size: 38.5 kB
    APT-Manual-Installed: yes
    APT-Sources: http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 Packages
    Description: portable library for OpenGL, window and input (libraries)
    GLFW is an Open Source, multi-platform library for creating
    windows with OpenGL contexts and managing input and events.
    It is easy to integrate into existing applications and does
    not lay claim to the main loop.
    .
    This package provides shared libraries.
    
     
  2. Emo Todorov

    Emo Todorov Administrator Staff Member

    You need to initalize GLFW first, using the function glfwInit(). The error you are getting means that GLEW could not initialize, which is because OpenGL itself is not initialized.
     
  3. Actually, the original code was
    Code:
    #include "mujoco.h"
    #include "glfw3.h"
    
    
    mjModel* m;
    mjrContext c;
    char error[300];
    
    int main(int argc, const char** argv) {
      mjr_defaultContext(&c);
      glfwInit();
      GLFWwindow* window = glfwCreateWindow(800, 800, "Invisible window", NULL, NULL);
      glfwMakeContextCurrent(window);
      m = mj_loadXML(argv[1], 0, error, 1000);
      mjr_makeContext(m, &c, mjFONTSCALE_150);
      mjr_freeContext(&c);
    }
    
    I took out glfwInit() to make make the example look more like the code in http://www.mujoco.org/book/programming.html#Rendering. Adding glfwInit() makes no difference except it creates an empty window. I still get

    Code:
    ERROR: GLEW initalization error: Missing GL version
    
    Press Enter to exit ...
    
     
  4. Emo Todorov

    Emo Todorov Administrator Staff Member

    You are not activating the MuJoCo license, in which case nothing works... Is m==NULL and is there a message in the string error?
     
  5. Ok I added the initialization code:

    Code:
    #include "mujoco.h"
    #include "glfw3.h"
    
    
    mjModel* m;
    mjrContext c;
    char error[300];
    
    int main(int argc, const char** argv) {
      mj_activate("mjkey.txt");
      mjr_defaultContext(&c);
      glfwInit();
      GLFWwindow* window = glfwCreateWindow(800, 800, "Invisible window", NULL, NULL);
      glfwMakeContextCurrent(window);
      m = mj_loadXML(argv[1], 0, error, 1000);
      mjr_makeContext(m, &c, mjFONTSCALE_150);
      mjr_freeContext(&c);
    }
    
    
    Same error. To give some context to this bug, this is an issue that several mujoco-py users are experiencing (https://github.com/openai/mujoco-py/issues/75, https://github.com/openai/mujoco-py/issues/44). As best as I was able to tell, this is essentially the series of mujoco functions called by mujoco-py, although there is a lot of python wrapped around them.

    Print statements indicate that the problem happens in mjr_makeContext.
     
  6. Emo Todorov

    Emo Todorov Administrator Staff Member

    Ok, so I am guessing this is a typical Linux mess with mixing libraries that were not compiled/tested to work together. This error shows up when GLEW cannot initialize itself for some reason, but it is hard to guess what that reason is. Unfortunately the name of the error is not very revealing.

    Here is the debugging sequence I would suggest:

    1. can you run the precompiled samples that come with mujoco, using the libglew and libglfw provided in the bin directory?
    2. can you recompile the code samples with the provided make file and still run them?
    3. can you replace the code sample with your code, but compile and link in the same way as shown in the makefile, still using the provided libglew and libglfw?
    ...

    Ideally I would link both glew and glfw statically. But people want multiple off-screen rendering options (and sometimes no rendering at all) so it has to be modular, which in turn invites all kinds of trouble.
     
  7. Yes to all three. So the culprit, it seems was that mujoco-py was compiling with libglewosmesa and osmesa instead of libglew, which works.

    This is definitely a big help, although it looks like mujoco-py uses some code that is specifically dependent on these osmesa libraries.

    Can you offer some advice on switching that code out with libglew code?
     
  8. Emo Todorov

    Emo Todorov Administrator Staff Member

    I have never actually used mujoco-py so I don't know. GLEW has the same API for all implementations (osmesa vs. egl vs. X) and which one you use depends on the underlying OpenGL implementation. What are the function calls in mujoco-py that specifically require osmesa? These must be some non-OpenGL functions, otherwise you could simply switch... if so, they are probably related to context creation and you should be able to replace them with corresponding GLFW functions.
     
  9. Well, I appreciate your willingness to go into the weeds a little with me on this. Currently, mujoco-py depends on a file called 'osmesashim.c' (https://github.com/openai/mujoco-py/blob/master/mujoco_py/gl/osmesashim.c). This file looks like it adapts certain osmesa functions to a common interface used by mujoco-py (https://github.com/openai/mujoco-py/blob/master/mujoco_py/gl/glshim.h). The other shim file in the mujoco-py repository is eglshim.c (https://github.com/openai/mujoco-py/blob/master/mujoco_py/gl/eglshim.c). I tried swapping in this file, but go the same 'GLEW initalization error.' My guess is that I need to implement something like 'glewshim.c.'

    In the future, I will also want to get this to work with rendering on the GPU. My limited understanding is that the GPU would need to use EGL, which is not working right now unfortunately. Please feel free to offer any suggestions on this point if you have them.
     
  10. Actually, none of the above proved necessary. I was actually able to fix the issue on my machine by changing the build instructions to use glew, gl, and OSMesa. Now it works :)
     
  11. Emo Todorov

    Emo Todorov Administrator Staff Member

    Good to hear it worked. Re GPU rendering, you need EGL if you don't have the X-server installed, which only happens in the cloud. On a local machine you can use the regular OpenGL drivers.