c++opengl-es-2.0glfwemscripten

GLFW with GLES in simple C++ Emscripten file wont build


I'm trying to setup an OpenGL Context with a window and viewport using the minimum code to do this with emscripten.

I've started coding, this with the following code:

#include<stdio.h>
#include<stdlib.h>
#include<GLES2/gl2.h>
#include<GL/glfw.h>
#include<emscripten/emscripten.h>

int init_gl()
{
    const int width = 480,
                height = 800;

    if (glfwInit() != GL_TRUE) {
        printf("glfwInit() failed\n");
        return GL_FALSE;
    }

    if (glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 0, GLFW_WINDOW) != GL_TRUE) {
        printf("glfwOpenWindow() failed\n");
        return GL_FALSE;
    }

    return GL_TRUE;
}

void do_frame()
{   
    glfwSwapBuffers();
}

void shutdown_gl()
{
    glfwTerminate();
}



int main() {
    
    printf("hello GL test\n");

    if (init_gl() == GL_TRUE) {     
        printf("initGL was true!\n");
        emscripten_set_main_loop(do_frame, 0, 1);
    } else {
        printf("could not init GL\n");
    }
        
    shutdown_gl();
    return 0;
}

When I try to build this using emscripten 1.30.0 I get the following errors:

Someones-MacBook:1.30.0 igloomedialtd$ ./emcc ~/Desktop/myGLTest.cpp -o hello.html
In file included from /Users/igloomedialtd/Desktop/myGLTest.cpp:4:
In file included from /Users/igloomedialtd/emsdk_portable/emscripten/1.30.0/system/include/GL/glfw.h:176:
In file included from /Users/igloomedialtd/emsdk_portable/emscripten/1.30.0/system/include/GL/gl.h:2091:
/Users/igloomedialtd/emsdk_portable/emscripten/1.30.0/system/include/GL/glext.h:5072:19: error: 
      typedef redefinition with different types ('ptrdiff_t' (aka 'int') vs
      'khronos_intptr_t' (aka 'long'))
typedef ptrdiff_t GLintptr;
                  ^
/Users/igloomedialtd/emsdk_portable/emscripten/1.30.0/system/include/GLES2/gl2.h:38:26: note: 
      previous definition is here
typedef khronos_intptr_t GLintptr;
                         ^
In file included from /Users/igloomedialtd/Desktop/myGLTest.cpp:4:
In file included from /Users/igloomedialtd/emsdk_portable/emscripten/1.30.0/system/include/GL/glfw.h:176:
In file included from /Users/igloomedialtd/emsdk_portable/emscripten/1.30.0/system/include/GL/gl.h:2091:
/Users/igloomedialtd/emsdk_portable/emscripten/1.30.0/system/include/GL/glext.h:5073:19: error: 
      typedef redefinition with different types ('ptrdiff_t' (aka 'int') vs
      'khronos_ssize_t' (aka 'long'))
typedef ptrdiff_t GLsizeiptr;
                  ^
/Users/igloomedialtd/emsdk_portable/emscripten/1.30.0/system/include/GLES2/gl2.h:39:26: note: 
      previous definition is here
typedef khronos_ssize_t  GLsizeiptr;
                         ^
2 errors generated.
ERROR    root: compiler frontend failed to generate LLVM bitcode, halting
Someones-MacBook:1.30.0 igloomedialtd$ 

It looks like the GLFW library is redefining some of the definitions in GLES2, what can I do about this?

EDIT: 7th June 2015

I fixed the issue by removing the #include<GLES2/gl2.h> line and adding #define GLFW_INCLUDE_ES2 before the #include<GL/glfw.h> line which causes the GLFW to import the correct GL files.

However, now I have a separate issue; when trying to run the output in Firefox I get: 'exception thrown: ReferenceError: GL is not defined'

Does anyone know what might be causing it?


Solution

  • This is apparently due to a missing dependency for GL when only using GLFW commands. If you add a gl* call, it will compile fine: https://github.com/kripken/emscripten/issues/3530