【问题标题】:OpenGL (ANGLE) + XAML (UWP)OpenGL (角度) + XAML (UWP)
【发布时间】:2019-11-06 05:29:46
【问题描述】:

我正在尝试让 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 事件的东西?

【问题讨论】:

  • 我在 VS2019 中使用 C++ 创建了一个空白应用程序,并从 NuGet 安装了“ANGLE.WindowsStore”,但仍然无法包含 .你能告诉我你采取了哪些步骤来帮助我重现它吗?
  • 我没有从 VS 创建项目。相反,我编写了 CMakeLists.txt 并从中生成了 sln 文件。我包含了来自 vcpkg 的 ANGLE 依赖项。

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


【解决方案1】:

当我写的一切都正确初始化时,看起来我错了。 此调用返回EGL_NO_SOURCE

eglMakeCurrent(display, surface, surface, context);

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

【讨论】:

    猜你喜欢
    • 2018-07-20
    • 2021-09-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    相关资源
    最近更新 更多