OpenGL(角度)+ XAML(UWP)

时间:2019-06-24 11:56:17

标签: uwp opengl-es uwp-xaml c++-cx egl

我正在尝试使OpenGL代码在UWP XAML应用程序上运行。 我找到了一些有关此的信息,但是似乎没有完整的演练。 我是UPW,XAML和EGL的初学者,所以我设法从各种教程中创建以下代码:

#include <Windows.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include <angle_windowsstore.h>

using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Platform;
using Windows::ApplicationModel::SuspendingEventArgs;
using Windows::ApplicationModel::Activation::LaunchActivatedEventArgs;
using Windows::Foundation::Collections::PropertySet;
using Windows::Foundation::EventHandler;

static EGLint const openGlAttributes[] = {
    EGL_RED_SIZE, 1,
    EGL_GREEN_SIZE, 1,
    EGL_BLUE_SIZE, 1,
    EGL_NONE
};

ref class MyApp sealed : Application {
private:
    SwapChainPanel^ swapChainPanel;
    CoreWindow^ coreWindow;
    EGLDisplay display;
    EGLSurface surface;
    bool stopped;

public:
    MyApp() {
        stopped = false;
    }

    virtual void OnLaunched(LaunchActivatedEventArgs^ e) override {
        auto canvas = ref new Canvas();

        auto swapChainPanel = ref new SwapChainPanel();
        swapChainPanel->Width = 800;
        swapChainPanel->Width = 600;
        canvas->Children->Append(swapChainPanel);
        canvas->SetLeft(swapChainPanel, 0);
        canvas->SetTop(swapChainPanel, 0);


        Window::Current->Content = canvas;
        Window::Current->Activate();

        coreWindow = Window::Current->CoreWindow;
        InitGL(Window::Current);

        WeakReference selfRef(this);
        Suspending += ref new SuspendingEventHandler([selfRef](Object^ sender, SuspendingEventArgs^ args) {
            auto self = selfRef.Resolve<MyApp>();
            self->stopped = true;
        });
        Resuming += ref new EventHandler<Object^>([selfRef](Object^ sender, Object^ args) {
            auto self = selfRef.Resolve<MyApp>();
            self->stopped = false;
        });

        ScheduleRendering();
    }

private:

    void InitGL(Window^ window) {
        display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
        eglInitialize(display, NULL, NULL);

        EGLConfig config;
        EGLint numConfig;
        eglChooseConfig(display, openGlAttributes, &config, 1, &numConfig);

        auto surfaceCreationProperties = ref new PropertySet();
        surfaceCreationProperties->Insert(ref new String(EGLNativeWindowTypeProperty), swapChainPanel);

        auto context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
        surface = eglCreateWindowSurface(display, config, reinterpret_cast<EGLNativeWindowType>(surfaceCreationProperties), NULL);
        eglMakeCurrent(display, surface, surface, context);
    }

    void RenderScene() {
        glClearColor(1.0, 0, 0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
        glFlush();

        eglSwapBuffers(display, surface);

        ScheduleRendering();
    }

    void ScheduleRendering() {
        if (stopped) {
            return;
        }
        WeakReference selfRef(this);
        coreWindow->Dispatcher->RunIdleAsync(ref new IdleDispatchedHandler([selfRef](IdleDispatchedHandlerArgs^ args) {
            auto self = selfRef.Resolve<MyApp>();
            self->RenderScene();
        }));
    }
};

int main(Array<String^>^ args) {
    Application::Start(ref new ApplicationInitializationCallback([](ApplicationInitializationCallbackParams^ p) {
        ref new MyApp();
    }));
    return 0;
}

但是,它根本不起作用。 至少此代码不会崩溃。 接下来,我在调试器中检查了InitGL,对EGL的所有调用均正常完成。

我不确定几件事:

  1. 我应该根据this guide初始化SwapChainPanel,还是ANGLE对我来说这样?
  2. 我应该在空闲状态下执行渲染,还是出现Paint事件之类的东西?

1 个答案:

答案 0 :(得分:0)

当我写所有东西都正确初始化时,看起来我错了。 此呼叫返回了EGL_NO_SOURCE

eglMakeCurrent(display, surface, surface, context);

我想是因为SwapChainPanel初始化时我们必须等待,所以当我从eglMakeCurrent的{​​{1}}事件中调用Load时,一切开始起作用。 另外,还有一些不明显的事情,例如swapChainPanel可能会意外释放GL上下文,因此当出现问题时必须重新创建它。 我在ANGLE的Microsoft分支中找到了this template,并将其用作灵感的来源。