在CPF里使用OpenGL做跨平台桌面应用开发

CPF 是开源的C#跨平台UI框架,支持使用OpenGL来渲染,可以用来硬件加速播放视频或者显示3D模型

实现原理其实就是Skia用OpenGL后端,Skia里绑定GLView的OpenGL纹理,将纹理作为Skia的图像混合绘制。

在CPF里使用OpenGL,不能选择NetCore3.0和Net4,需要选择Netcore3.1以及之后的版本。

Nuget里安装最新版的CPF。另外安装Silk.NET或者OpenTK来调用OpenGL绘制。

Program里需要开启GPU,设置 UseGPU = true

复制代码
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Application.Initialize(
                (OperatingSystemType.Windows, new WindowsPlatform(), new SkiaDrawingFactory { UseGPU = true })
                , (OperatingSystemType.OSX, new CPF.Mac.MacPlatform(), new SkiaDrawingFactory { UseGPU = true })//如果需要支持Mac才需要
                , (OperatingSystemType.Linux, new CPF.Linux.LinuxPlatform(), new SkiaDrawingFactory { UseGPU = true })//如果需要支持Linux才需要
            );
            Application.Run(new Window2_1_Colors());
        }
    }

界面上添加 CPF.Skia.GLView 控件,GLLoaded 为GL加载完成事件,GLRender为GL绘制事件。OpenGL就在这两个事件里处理

复制代码
            Children.Add(new WindowFrame(this, new GLView
            {
                Width = "100%",
                Height = "100%",
                [nameof(GLView.GLLoaded)] = new CommandDescribe((s, e) => GLLoaded((GLView)s, (GLEventArgs)e)),
                [nameof(GLView.GLRender)] = new CommandDescribe((s, e) => GLRender((GLView)s, (GLEventArgs)e)),
                [nameof(GLView.MouseDown)] = new CommandDescribe((s, e) => MouseEvent((GLView)s, (MouseEventArgs)e)),
                [nameof(GLView.MouseUp)] = new CommandDescribe((s, e) => MouseEvent((GLView)s, (MouseEventArgs)e)),
                [nameof(GLView.MouseMove)] = new CommandDescribe((s, e) => MouseEvent((GLView)s, (MouseEventArgs)e)),
            })
            {
                MaximizeBox = true
            });

Silk.Net初始化API

复制代码
        GL gl;//可以保存为全局的字段
        void GLLoaded(GLView view, GLEventArgs args)
        {
            gl = GL.GetApi(args.Context.GetProcAddress);
        }

OpenTk初始化API

复制代码
    class Context : OpenTK.IBindingsContext
    {//定义API绑定的上下文
        public IGlContext context;
        public IntPtr GetProcAddress(string procName)
        {
            return context.GetProcAddress(procName);
        }
    }

        void GLLoaded(GLView view, GLEventArgs args)
        {//初始化一次就行
             GL.LoadBindings(new Context { context = args.Context });
        }

在GLRender事件里绘制。默认情况下GLRender不会一直调用,因为绘制方式不是游戏那种实时刷新的。需要刷新的时候要主动调用Invalidate(),或者用定时器来实时调用Invalidate()。

复制代码
        void GLRender(GLView view, GLEventArgs args)
        {
            gl.Enable(GLEnum.DepthTest);//开启深度测试
            gl.DepthMask(true);

            //其他绘制代码。。。。


            //开启深度测试之后要关闭,否则会无法显示界面,同样的,如果开启了其他功能,绘制结束后记得关闭,否则可能影响界面绘制。因为和界面共享OpenGL的上下文。
            gl.Disable(GLEnum.DepthTest);
        }

具体OpenGL的绘制教程你可以直接找Silk.NET或者OpenTK的教程就行。

运行效果

案例源码下载:CPF_OpenGL

CPF开源地址:https://gitee.com/csharpui/CPF

相关推荐
Xin_ye100861 天前
C# 零基础到精通教程 - 第七章:面向对象编程(入门)——类与对象
开发语言·c#
rockey6271 天前
AScript异步执行与await关键字
c#·.net·script·eval·expression·异步执行·动态脚本
程序leo源1 天前
Qt窗口详解
开发语言·数据库·c++·qt·青少年编程·c#
月巴月巴白勺合鸟月半1 天前
质本洁来还洁去,强于污淖陷文本
c#
Xin_ye100861 天前
C# 零基础到精通教程 - 第八章:面向对象编程(进阶)——继承与多态
开发语言·c#
asdzx671 天前
使用 C# 打印 Excel 文档(详细教程)
c#·excel
伽蓝_游戏1 天前
第四章:AssetBundle 核心机制与文件结构
unity·c#·游戏引擎·游戏程序
2501_930707782 天前
使用C#代码拆分 PowerPoint 演示文稿
开发语言·c#·powerpoint
郝学胜-神的一滴2 天前
中级OpenGL教程 006:高光反射原理与 Shader 实现
c++·unity·godot·图形渲染·three.js·opengl·unreal
SenChien2 天前
C#学习笔记-入门篇
笔记·学习·c#·rider