代码之家  ›  专栏  ›  技术社区  ›  Sebastian Gsänger

在Emscripten和Qt之间共享OpenGL代码

  •  1
  • Sebastian Gsänger  · 技术社区  · 7 年前

    两者使用完全相同的GL调用,因此我想将所有调用移到辅助函数,然后从前端调用这些函数。

    问题是为了便于移植,我想从 GLES3/gl3.h QOpenGLFunctions

    guiwrapper.cpp:

    #include <GLES3/gl3.h>
    void drawStuff(){
        glDrawArrays(...); // taken from <GLES3/gl3.h>
    }
    

    emscripten.cpp:

    #include <guiwrapper.h>
    void someDrawFunc(){
        drawStuff(); // glDrawArrays pulled from <GLES3/gl3.h>
        glDrawArrays(...); // also correctly uses <GLES3/gl3.h> version
    }
    

    qt.cpp:

    class guiwidget: public QOpenGLWidget, protected QOpenGLFunctions{
    public:
        void paintGL(void);
    }
    void guiwidget::paintGL(){
         glDrawArrays(...); // correctly calls QOpenGLFunctions::glDrawArrays
         drawStuff(); // still uses <GLES3/gl3.h>
    }
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sebastian Gsänger    7 年前

    我现在找到了一个令人满意的解决方案,它并非完全没有预处理器的魔力,但至少没有宏或代码复制。 也许这对某人有帮助:

    #ifdef __EMSCRIPTEN__
    #include <GLES3/gl3.h>
    class GuiWrapper
    #else
    #include <QOpenGLFunctions>
    class GuiWrapper: protected QOpenGLFunctions
    #endif
    {
        void drawStuff(void); // cpp decides between free and member-functions
    };
    

    #include <guiwrapper.h>
    void someFunc(void){
        GuiWrapper gui;
        gui.drawStuff(); // calls <GLES3/gl3.h>
    }
    

    glwidget.cpp:

    #include <guiwrapper.h>
    #include <QOpenGLWidget>
    class GLWidget: public QOpenGLWidget, private GuiWrapper{
        public:
            void paintGL(void){
                drawStuff(); // calls <QOpenGLFunctions>
            }
    };