使用未声明的标识符 GluBuildMipMaps (emscripten)

Use of undeclared identifier GluBuildMipMaps (emscripten)

我使用 SDL 创建了一个 OpenGL 程序。我已经尝试使用 emscripten 将其编译为 HTML5+JS,但出现此错误:

我不明白为什么会出现此错误。 GluBuildMipMaps 在 glu.h 中定义:https://github.com/kripken/emscripten/blob/master/system/include/GL/glu.h

glm 错误也令人困惑,因为我已经将 glm 库复制到 glm 目录,并且它包含在我使用它的地方。


编辑

纯文本错误信息:

D:\proj\e_bead>emcc main.cpp MyApp.cpp ObjParser_OGL3.cpp -s FULL_ES2=1 -O3 --me
mory-init-file 0
In file included from MyApp.cpp:2:
./GLUtils.hpp:148:2: error: use of undeclared identifier 'gluBuild2DMipmaps'
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, loaded_img->w, loaded_i...
        ^
MyApp.cpp:194:25: error: no matching function for call to 'translate'
  ...spaceShipTransform = glm::translate<float>(randnum, randnum, randnum);
                               ^~~~~~~~~~~~~~~~~~~~~
./glm/gtc/matrix_transform.inl:40:35: note: candidate function template not
      viable: requires 2 arguments, but 3 were provided
        GLM_FUNC_QUALIFIER tmat4x4<T, P> translate
                                         ^
./glm/gtx/transform.inl:36:35: note: candidate function template not viable:
      requires single argument 'v', but 3 arguments were provided
        GLM_FUNC_QUALIFIER tmat4x4<T, P> translate(
                                         ^
MyApp.cpp:199:25: error: no matching function for call to 'translate'
  ...spaceShipTransform = glm::translate<float>(randnum, randnum, randnum);
                               ^~~~~~~~~~~~~~~~~~~~~
./glm/gtc/matrix_transform.inl:40:35: note: candidate function template not
      viable: requires 2 arguments, but 3 were provided
        GLM_FUNC_QUALIFIER tmat4x4<T, P> translate
                                         ^
./glm/gtx/transform.inl:36:35: note: candidate function template not viable:
      requires single argument 'v', but 3 arguments were provided
        GLM_FUNC_QUALIFIER tmat4x4<T, P> translate(
                                         ^
MyApp.cpp:234:9: error: no matching function for call to 'translate'
        mvp *= glm::translate(0.0f, 5.0f, 0.0f);
               ^~~~~~~~~~~~~~
./glm/gtc/matrix_transform.inl:40:35: note: candidate function template not
      viable: requires 2 arguments, but 3 were provided
        GLM_FUNC_QUALIFIER tmat4x4<T, P> translate
                                         ^
./glm/gtx/transform.inl:36:35: note: candidate function template not viable:
      requires single argument 'v', but 3 arguments were provided
        GLM_FUNC_QUALIFIER tmat4x4<T, P> translate(
                                         ^
4 errors generated.
ERROR:root:compiler frontend failed to generate LLVM bitcode, halting

编辑2

我已将 glm 库复制到 glm 目录中,并将其包含在以下代码中:

// GLM
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtx/transform2.hpp"

纹理加载代码,其中gluBuild2DMipmaps调用:

GLuint TextureFromFile(const char* filename)
{
    SDL_Surface* loaded_img = IMG_Load(filename);  

    int img_mode = 0;

    if ( loaded_img == 0 )
    {
        std::cout << "[TextureFromFile] Error when loading image: " << filename << std::endl;
        return 0;
    }

    #if SDL_BYTEORDER == SDL_LIL_ENDIAN
        if ( loaded_img->format->BytesPerPixel == 4 )
            img_mode = GL_BGRA;
        else
            img_mode = GL_BGR;
    #else
        if ( loaded_img->format->BytesPerPixel == 4 )
            img_mode = GL_RGBA;
        else
            img_mode = GL_RGB;
    #endif

    GLuint tex;
    glGenTextures(1, &tex);

    glBindTexture(GL_TEXTURE_2D, tex);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, loaded_img->w, loaded_img->h, img_mode, GL_UNSIGNED_BYTE, loaded_img->pixels);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    SDL_FreeSurface( loaded_img );

    return tex;
}

编辑3

我已经根据Robert Rouhani的回答编辑了GLUtils.hpp,现在我在这一行没有得到编译错误:

glBindTexture(GL_TEXTURE_2D, tex);
//gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, loaded_img->w, loaded_img->h, img_mode, GL_UNSIGNED_BYTE, loaded_img->pixels);

glTexImage2D(GL_TEXTURE_2D,
    0,
    GL_RGB,
    loaded_img->w,
    loaded_img->h,
    0,
    img_mode,
    GL_UNSIGNED_BYTE,
    loaded_img->pixels);
glGenerateMipmap(GL_TEXTURE_2D); // /!\ Allocate the mipmaps /!\

我对 emscripten 不太熟悉,但从一些搜索看来,你选择 FULL_ES2=1 意味着你获得了整个 OpenGL ES 2.0 API。我猜这意味着您获得的唯一包含目录是 GLES2 instead of GL,该文件夹包含您链接到的 glu.h

GLU 已有将近 20 年的历史,并且只支持 OpenGL 1.3(2001 年发布)。这意味着您不能将它与 ES 2.0 一起使用。您想要的是 glGenerateMipmap with glHintGL_GENERATE_MIPMAP_HINT 选项。一个例子:

//Create the mipmapped texture
glGenTextures(1, &ColorbufferName);
glBindTexture(ColorbufferName);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 512, 0, GL_UNSIGNED_BYTE, NULL);
glGenerateMipmap(GL_TEXTURE_2D); // /!\ Allocate the mipmaps /!\

摘自 this article.

至于 glm 错误,看起来它正在正确地找到 glm,它只是不喜欢您使用它的方式(并且仅针对 translate 函数)。你能 post 来自 MyApp.cpp 的相关行吗(有错误的行和它上下的几行作为上下文)?

其他人已经通过 glm 使用 emscripten,也许这个邮件列表 post 会对您有所帮助:https://groups.google.com/forum/#!topic/emscripten-discuss/b6DY3GCDlug


编辑:对于 glm 错误

您只是使用了错误的包含。

你要的是glm/gtx/transform.hpp