GLFW与GLES在简单的c++ Emscripten文件不会构建

opengl es 2.0 - GLFW with GLES in simple C++ Emscripten file wont build

本文关键字:文件 Emscripten 构建 c++ GLES 简单 GLFW      更新时间:2023-10-16

我试图设置一个OpenGL上下文与窗口和viewport使用最小的代码来做到这一点与emscripten。

我已经开始编码了,这是以下代码:

#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() failedn");
        return GL_FALSE;
    }
    if (glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 0, GLFW_WINDOW) != GL_TRUE) {
        printf("glfwOpenWindow() failedn");
        return GL_FALSE;
    }
    return GL_TRUE;
}
void do_frame()
{   
    glfwSwapBuffers();
}
void shutdown_gl()
{
    glfwTerminate();
}

int main() {
    
    printf("hello GL testn");
    if (init_gl() == GL_TRUE) {     
        printf("initGL was true!n");
        emscripten_set_main_loop(do_frame, 0, 1);
    } else {
        printf("could not init GLn");
    }
        
    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_es2之前添加#define GLFW_INCLUDE_ES2;GL/glfw.h>行,使GLFW导入正确的GL文件。

然而,现在我有一个单独的问题;当尝试在Firefox中运行输出时,我得到:'抛出异常:ReferenceError: GL未定义'

有人知道是什么引起的吗?

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