在简单的 C++ Emscripten 文件中使用 GLES 的 GLFW 不会构建

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

我正在尝试使用 emscripten 使用最少的代码设置具有 window 和视口的 OpenGL 上下文。

我已经开始编码了,代码如下:

#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;
}

当我尝试使用 emscripten 1.30.0 构建它时,出现以下错误:

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$ 

看起来 GLFW 库正在重新定义 GLES2 中的一些定义,我该怎么办?

编辑:2015 年 6 月 7 日

我通过删除 #include 行并在 #include 行之前添加 #define GLFW_INCLUDE_ES2 来解决问题,这会导致 GLFW 导入正确的 GL 文件。

但是,现在我有一个单独的问题;当尝试 运行 Firefox 中的输出时,我得到:'exception thrown: ReferenceError: GL is not defined'

有谁知道可能是什么原因造成的?

这显然是由于仅使用 GLFW 命令时缺少对 GL 的依赖。如果你添加一个 gl* 调用,它会编译正常:https://github.com/kripken/emscripten/issues/3530