Scaleform GFx 与 OpenGL 集成

Scaleform GFx integration with OpenGL

我在将 Scaleform GFx 解决方案集成到简单应用程序中时遇到问题。

问题是在显示的场景中,本应为白色的模型显示为紫色。

据我所知,颜色效果出现在我开始渲染 GFx 组件之前;调用 FxTest::Initialize 足以引起问题。

下面的代码。

GFx 包装器:



    FxTest::FxTest() {
        is_initialized_ = false;
        last_play_time_ = 0;
    }

    FxTest::~FxTest() {
        GMemory::DetectMemoryLeaks();
    }

    void FxTest::Initialize(const char* movie_file) {
        // setup logger
        loader_.SetLog(GPtr(*new GFxPlayerLog()));

        // setup file opener
        GPtr file_opener = *new GFxFileOpener;
        loader_.SetFileOpener(file_opener);

        // setup renderer
        renderer_ = *GRendererOGL::CreateRenderer();
        if(!renderer_ || !renderer_->SetDependentVideoMode()) {
            return;
        }
        render_config_ = *new GFxRenderConfig(renderer_, GFxRenderConfig::RF_EdgeAA);
        if(!render_config_) {
            return;
        }
        loader_.SetRenderConfig(render_config_);

        // setup movie
        ui_movie_def_ = *(loader_.CreateMovie(movie_file, GFxLoader::LoadKeepBindData | GFxLoader::LoadWaitFrame1));
        if(!ui_movie_def_) {
            return;
        }
        ui_movie_ = *ui_movie_def_->CreateInstance(GFxMovieDef::MemoryParams(), true);
        if(!ui_movie_) {
            return;
        }

        ui_movie_->Advance(0.0f, 0);
        ui_movie_->SetBackgroundAlpha(0.0f);
        ui_movie_->SetViewport(1024, 768, 0, 0, 1024, 768);
        ui_movie_->SetViewScaleMode(GFxMovieView::ScaleModeType::SM_NoScale);

        is_initialized_ = true;
        last_play_time_ = timeGetTime();
    }

    void FxTest::UpdateViewport(int width, int height) {
        if (!ui_movie_) {
            return;
        }
        ui_movie_->SetViewport(width, height, 0, 0, width, height);
        last_play_time_ = timeGetTime();
    }

    void FxTest::AdvanceAndDisplay() {
        if (!ui_movie_) {
            return;
        }
        BeginDisplay();

        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

        DWORD mtime = timeGetTime();
        float deltaTime = ((float)(mtime - last_play_time_)) / 1000.0f;

        ui_movie_->Advance(deltaTime, 0);
        ui_movie_->Display();

        last_play_time_ = mtime;

        EndDisplay();
    }

    void FxTest::BeginDisplay() {
        glPushAttrib(GL_ALL_ATTRIB_BITS);
    }

    void FxTest::EndDisplay() {
        glPopAttrib();
    }

    void FxTest::OnMouse(GFxEvent::EventType event_type, UInt button, SInt x, SInt y) {
        if (!ui_movie_) {
            return;
        }
        GFxMouseEvent mouse_event(event_type, 0, x, y);
        ui_movie_->HandleEvent(mouse_event);
    }


过剩场景:



    FxTest* g_FxTest = NULL;

    void display() {
        glMatrixMode(GL_MODELVIEW);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();

        glTranslatef(0.0, 0.0, -4.5);
        glScalef(1.0, 1.0, 1.0);

        glPushMatrix();
            glutSolidTeapot(1);
        glPopMatrix();

        if (g_FxTest->IsInitialized()) {
            g_FxTest->AdvanceAndDisplay();
        }

        glFlush();
        glutSwapBuffers();
    }

    void reshapeFunc(int x, int y) {
        if (y == 0 || x == 0) {
            return;
        }

        glMatrixMode(GL_PROJECTION);  
        glLoadIdentity();

        gluPerspective(40.0, (GLdouble)x / (GLdouble)y, 0.5, 20.0);
        glViewport(0, 0, x, y);

        if (g_FxTest->IsInitialized()) {
            g_FxTest->UpdateViewport(x, y);
        }
    }

    void idleFunc(void) {
        display();
    }

    void mouseMotion(int x, int y) {
        if (g_FxTest && g_FxTest->IsInitialized()) {
            g_FxTest->OnMouse(GFxEvent::MouseMove, 0, x, y);
        }
    }

    void mouse(int button, int button_state, int x, int y) {
        if (g_FxTest && g_FxTest->IsInitialized()) {
            if (button_state == GLUT_DOWN) {
                g_FxTest->OnMouse(GFxEvent::MouseDown, button, x, y);
            } else if (button_state == GLUT_UP) {
                g_FxTest->OnMouse(GFxEvent::MouseUp, button, x, y);
            }
        }
    }

    int main(int argc, char **argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);

        glutInitWindowSize(1024, 768);
        glutCreateWindow("GFx");

        glutDisplayFunc(display);
        glutReshapeFunc(reshapeFunc);
        glutIdleFunc(idleFunc);
        glutMouseFunc(mouse);
        glutMotionFunc(mouseMotion);
        glutPassiveMotionFunc(mouseMotion);

        /*glut init*/
        glClearColor(0.5, 0.5, 0.5, 0.0);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);

        GFxSystem gfx_init;
        g_FxTest = new FxTest();
        g_FxTest->Initialize("Movie//NpcShop.swf"); // problem start here

        glutMainLoop();
        return 0;
    }


请帮助我理解我做错了什么。

好吧,我发现了什么问题 - 在 FxTest::Initialize 茶壶从 gfx 获取纹理后,这意味着在大项目中需要获得加载的好时机或在开始加载之后/之前启用/禁用 GL_TEXTURE_2D gfx.

glDisable(GL_TEXTURE_2D); // after initialization and all good

谢谢大家。