经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » OpenGL » 查看文章
使用 Silk.NET 创建 OpenGL 空窗口项目例子
来源:cnblogs  作者:lindexi  时间:2022/1/17 10:56:36  对本文有异议

本文告诉大家如何使用 Silk.NET 创建 OpenGL 空窗口项目。在 dotnet 基金会下,开源维护 Silk.NET 仓库,此仓库提供了渲染相关的封装逻辑,包括 DX 和 OpenGL 等等的封装,利用此封装可以用来代替原有的 SharpDx 等库。这是一个全新写的项目,使用上了 dotnet 和 C# 很多新的特性,相对来说也很活跃,我准备开始入坑这个项目

本文的例子完全是从 https://github.com/dotnet/Silk.NET 里面抄的,这是官方的使用 OpenGL 的例子

当前是 2021.12.22 官方完成的应用高层封装的只有 OpenGL 一个,加上 Vulkan 版本。基础底层封装完成了 DirectX 系列(但还完成没有 D2D 部分)和 OpenAL OpenCL OpenGL OpenXR Vulkan SDL 等

本文的例子是采用高层封装的 OpenGL 创建空窗口。根据官方 OpenGL Tutorials 的 Tutorial 1.1 - Hello Window 的代码,只需一个 Program 类即可完成启动应用

在开始之前,先通过 NuGet 安装 Silk.NET 库,对于新项目格式,可以编辑 csproj 项目文件,修改为如下代码

  1. <Project Sdk="Microsoft.NET.Sdk">
  2. <PropertyGroup>
  3. <OutputType>Exe</OutputType>
  4. <TargetFramework>net6.0</TargetFramework>
  5. <ImplicitUsings>enable</ImplicitUsings>
  6. <Nullable>enable</Nullable>
  7. </PropertyGroup>
  8. <ItemGroup>
  9. <PackageReference Include="Silk.NET" Version="2.11.0" />
  10. </ItemGroup>
  11. </Project>

在 Main 函数里面,使用进行创建窗口。创建窗口需要初始化创建所需参数,在 Silk.NET 提供了预设的选项,如下面代码

  1. var options = WindowOptions.Default;

此预设的代码等同于如下配置

  1. var options = new WindowOptions
  2. (
  3. true,
  4. new Vector2D<int>(50, 50),
  5. new Vector2D<int>(1280, 720),
  6. 0.0,
  7. 0.0,
  8. new GraphicsAPI
  9. (
  10. ContextAPI.OpenGL,
  11. ContextProfile.Core,
  12. ContextFlags.ForwardCompatible,
  13. new APIVersion(3, 3)
  14. ),
  15. "",
  16. WindowState.Normal,
  17. WindowBorder.Resizable,
  18. false,
  19. false,
  20. VideoMode.Default
  21. );

通过 GraphicsAPI 可以看到默认采用的是 OpenGL 作为渲染。在 ContextAPI 可选的参数,当前有三个,但是如下面代码注释,当前还没有完成 Vulkan 的高层封装

  1. /// <summary>
  2. /// Represents which API the graphics context should use.
  3. /// </summary>
  4. public enum ContextAPI
  5. {
  6. /// <summary>
  7. /// Don't use any API. This is necessary for linking an external API, such as Vulkan, to the window.
  8. /// </summary>
  9. None = 0,
  10. /// <summary>
  11. /// Use Vulkan. Silk.NET doesn't support this yet.
  12. /// </summary>
  13. Vulkan,
  14. /// <summary>
  15. /// Use core OpenGL. This is standard for software intended for desktop computers.
  16. /// </summary>
  17. OpenGL,
  18. /// <summary>
  19. /// Use OpenGL ES. This is standard for software intended to be compatible with embedded systems, such as phones.
  20. /// </summary>
  21. OpenGLES
  22. }

获取到默认的创建选项之后,如果需要修改窗口创建参数,可以修改对应的属性。例如修改窗口尺寸等

  1. options.Size = new Vector2D<int>(800, 600);
  2. options.Title = "LearnOpenGL with Silk.NET";

完成窗口创建参数配置,即可通过 Silk.NET.Windowing.Window 的 Create 方法创建窗口,如下面代码

  1. private static IWindow _window;
  2. private static void Main(string[] args)
  3. {
  4. //Create a window.
  5. var options = WindowOptions.Default;
  6. options.Size = new Vector2D<int>(800, 600);
  7. options.Title = "LearnOpenGL with Silk.NET";
  8. _window = Window.Create(options);
  9. }

完成窗口创建之后,可以使用 Run 方法执行代码

  1. //Run the window.
  2. _window.Run();

在执行 Run 方法时,将会和 WPF 一样开启消息循环。为了实现窗口里面的初始化逻辑,需要在 Run 之间加上事件

  1. _window.Load += OnLoad;
  2. _window.Update += OnUpdate;
  3. _window.Render += OnRender;

修改之后的 Main 函数如下

  1. private static void Main(string[] args)
  2. {
  3. //Create a window.
  4. var options = WindowOptions.Default;
  5. options.Size = new Vector2D<int>(800, 600);
  6. options.Title = "LearnOpenGL with Silk.NET";
  7. _window = Window.Create(options);
  8. //Assign events.
  9. _window.Load += OnLoad;
  10. _window.Update += OnUpdate;
  11. _window.Render += OnRender;
  12. //Run the window.
  13. _window.Run();
  14. }

在 OnLoad 里面,可以进行很多初始化逻辑,例如获取输入键盘内容,如下面代码

  1. private static void OnLoad()
  2. {
  3. //Set-up input context.
  4. IInputContext input = _window.CreateInput();
  5. for (int i = 0; i < input.Keyboards.Count; i++)
  6. {
  7. input.Keyboards[i].KeyDown += KeyDown;
  8. }
  9. }

默认拿到的键盘只有一个,在 KeyDown 函数,可以通过参数判断当前按键

  1. private static void KeyDown(IKeyboard arg1, Key arg2, int arg3)
  2. {
  3. //Check to close the window on escape.
  4. if (arg2 == Key.Escape)
  5. {
  6. _window.Close();
  7. }
  8. }

而 OnUpdate 和 OnRender 方法分别是做具体的渲染准备和渲染的逻辑,这些就不是本文的内容了

以下是 Program 的代码

  1. using Silk.NET.Input;
  2. using Silk.NET.Maths;
  3. using Silk.NET.Windowing;
  4. namespace WemkuhewhallYekaherehohurnije
  5. {
  6. class Program
  7. {
  8. private static IWindow _window;
  9. private static void Main(string[] args)
  10. {
  11. //Create a window.
  12. var options = WindowOptions.Default;
  13. options = new WindowOptions
  14. (
  15. true,
  16. new Vector2D<int>(50, 50),
  17. new Vector2D<int>(1280, 720),
  18. 0.0,
  19. 0.0,
  20. new GraphicsAPI
  21. (
  22. ContextAPI.OpenGL,
  23. ContextProfile.Core,
  24. ContextFlags.ForwardCompatible,
  25. new APIVersion(3, 3)
  26. ),
  27. "",
  28. WindowState.Normal,
  29. WindowBorder.Resizable,
  30. false,
  31. false,
  32. VideoMode.Default
  33. );
  34. options.Size = new Vector2D<int>(800, 600);
  35. options.Title = "LearnOpenGL with Silk.NET";
  36. _window = Window.Create(options);
  37. //Assign events.
  38. _window.Load += OnLoad;
  39. _window.Update += OnUpdate;
  40. _window.Render += OnRender;
  41. //Run the window.
  42. _window.Run();
  43. }
  44. private static void OnLoad()
  45. {
  46. //Set-up input context.
  47. IInputContext input = _window.CreateInput();
  48. for (int i = 0; i < input.Keyboards.Count; i++)
  49. {
  50. input.Keyboards[i].KeyDown += KeyDown;
  51. }
  52. }
  53. private static void OnRender(double obj)
  54. {
  55. //Here all rendering should be done.
  56. }
  57. private static void OnUpdate(double obj)
  58. {
  59. //Here all updates to the program should be done.
  60. }
  61. private static void KeyDown(IKeyboard arg1, Key arg2, int arg3)
  62. {
  63. //Check to close the window on escape.
  64. if (arg2 == Key.Escape)
  65. {
  66. _window.Close();
  67. }
  68. }
  69. }
  70. }

按下 F5 运行代码,即可看到创建了空窗口

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

博客园博客只做备份,博客发布就不再更新,如果想看最新博客,请到 https://blog.lindexi.com/

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名[林德熙](http://blog.csdn.net/lindexi_gd)(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我[联系](mailto:lindexi_gd@163.com)。

原文链接:http://www.cnblogs.com/lindexi/p/15772649.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号