是否可以通过D3DImage和ANGLE在WPF应用程序中使用OpenGL ES代码? [英] Is it possible to use OpenGL ES code with a WPF application via a D3DImage and ANGLE?

查看:116
本文介绍了是否可以通过D3DImage和ANGLE在WPF应用程序中使用OpenGL ES代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要(TL:DR版本)

最终,我们的目标是能够在WPF应用程序中本地使用OpenGL ES代码(即不是SharpGL等),而不会出现空域或驱动程序问题,这可以通过Google的ANGLE项目来实现.

Ultimately our goal is to be able to utilize OpenGL ES code in a WPF application natively (i.e. not SharpGL, etc.) and without Airspace or driver issues, possible using Google's ANGLE project.

背景:

我喜欢DirectX上的OpenGL的一件事是它的跨平台功能.它在OS X和Linux以及通过ES在Android和iOS上均具有出色的支持.但是,在Windows上,使用它会导致驱动程序问题,或者更糟的是,很多卡根本无法正确实现它.

One of the things I like about OpenGL over DirectX is its cross-platform capability. It has excellent support on both OS X and Linux, and also on Android and iOS via ES. However, on Windows, using it is marred with driver issues, or worse, a lot of cards simply don't implement it properly.

输入Google的 ANGLE项目,或Almost-Native-Graphics-Layer-Engine.

Enter Google's ANGLE project, or Almost-Native-Graphics-Layer-Engine.

ANGLE是Direct3D实现的OpenGL ES 2.0包装程序,这意味着您可以编写要在Windows上运行的OpenGL ES 2.0代码,而无需实际的OpenGL驱动程序.它不仅通过了ES兼容性测试,而且实际上是Chrome完成所有图形渲染(包括WebGL)的方式,因此它绝对是一种经过验证的技术.

ANGLE is an OpenGL ES 2.0 wrapper around a Direct3D implementation, meaning you can write OpenGL ES 2.0 code to be run on Windows without the need for actual OpenGL drivers. It not only passes the ES compatibility tests, but it's actually how Chrome does all of its graphics rendering, including WebGL so it definitely is a proven technology.

问题:

我们知道WPF具有D3DImage控件,该控件可让您在WPF中托管Direct3D渲染,并且可以通过将其输出正确堆肥到WPF渲染线程来消除空域问题.我的问题是,由于ANGLE是通过Direct3D实现的,而D3DImage是Direct3D渲染的目标,是否可以将两者结合起来,从而允许我们编写OpenGL ES代码并将其托管在Windows的WPF应用程序中,而没有驱动程序或空间问题?

We know WPF has a D3DImage control which lets you host Direct3D rendering within WPF and it supposedly does away with the airspace issues by properly composting its output to the WPF render thread. My question is since ANGLE is implemented via Direct3D, and D3DImage is a target for Direct3D rendering, is it possible to combine the two, allowing us to write OpenGL ES code and host it in a WPF application on Windows, all without driver or airspace issues?

这将是我们的圣杯".

但是,由于ANGLE想要使用其自身,所以我一直在碰壁,使ANGLE将其渲染目标定位在D3DImage控件创建的D3D曲面上.我不确定这是否有可能.我什至都没有在讨论任何地方找到任何文章或参考,更不用说尝试了.

However, I keep hitting a wall around getting ANGLE to target its rendering on the D3D surface created by the D3DImage control since ANGLE wants to use its own. I'm not sure this is even possible. I can't find a single article or reference anywhere of anyone even discussing this, let alone attempting it.

同样要明确的是,目标是使我们共享的跨平台OpenGL(或ES)代码在WPF应用程序中运行,而没有空域问题或OpenGL驱动程序要求.我对使用ANGLE/D3DImage的建议只是……尝试.到目前为止,这是我想出的手段",但这只是实现我们目标的一种潜在手段,而不是目标本身.任何其他使我们能够采用相同解决方案的方法都将受到欢迎.

Again to be clear though, the goal is to get our shared, cross-platform OpenGL (or ES) code to work in a WPF application without airspace issues or OpenGL driver requirements. My suggestion of using ANGLE/D3DImage is just that... an attempt. It's the 'means' I've come up with so far, but that's just a potential means to our goal, not the goal itself. Anything else that would get us to the same solution would be more than welcome.

推荐答案

我上传了 github项目演示了如何通过 OpenTK.GLControl 将OpenGL渲染集成到WPF应用程序中.

I have uploaded a github project that demonstrates how to integrate OpenGL rendering into a WPF application via OpenTK.GLControl.

代码非常简单:

  1. 向WPF应用程序添加WindowsFormsHost
  2. 构造一个OpenTK.GLControl并将其附加到WindowsFormsHost
    • 通过GraphicsContextFlags.Default获得桌面OpenGL上下文
    • 通过GraphicsContextFlags.Embedded获得OpenGL ES(ANGLE)上下文
  1. Add a WindowsFormsHost to the WPF application
  2. Construct an OpenTK.GLControl and attach it to the WindowsFormsHost
    • Pass GraphicsContextFlags.Default for a desktop OpenGL context
    • Pass GraphicsContextFlags.Embedded for an OpenGL ES (ANGLE) context

提示: OpenTK

Hint: OpenTK and OpenTK.GLControl are also available as NuGet packages. There is a new release due tomorrow with improved ANGLE support.

请注意,WindowsFormsHost方法受空域限制.如果这是一个问题,请在此处查看我的回答寻找解决方案.

Note that the WindowsFormsHost approach is subject to airspace restrictions. If this is an issue, see my answer here for a solution.

// This code is public domain
#define USE_ANGLE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using OpenTK;
using OpenTK.Graphics;

#if USE_ANGLE
using OpenTK.Graphics.ES20;
#else
using OpenTK.Graphics.OpenGL;
#endif

namespace WPF.Angle
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        GLControl glControl;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void WindowsFormsHost_Initialized(object sender, EventArgs e)
        {
            var flags = GraphicsContextFlags.Default;
#if USE_ANGLE
            flags = GraphicsContextFlags.Embedded;
#endif
            glControl = new GLControl(new GraphicsMode(32, 24), 2, 0, flags);
            glControl.MakeCurrent();
            glControl.Paint += GLControl_Paint;
            glControl.Dock = DockStyle.Fill;
            (sender as WindowsFormsHost).Child = glControl;
        }

        private void GLControl_Paint(object sender, PaintEventArgs e)
        {
            GL.ClearColor(
                (float)Red.Value,
                (float)Green.Value,
                (float)Blue.Value,
                1);
            GL.Clear(
                ClearBufferMask.ColorBufferBit |
                ClearBufferMask.DepthBufferBit |
                ClearBufferMask.StencilBufferBit);

            glControl.SwapBuffers();
        }

        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            glControl.Invalidate();
        }
    }
}

这篇关于是否可以通过D3DImage和ANGLE在WPF应用程序中使用OpenGL ES代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆