在 C# 中使用 OpenGL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/536065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:11:25  来源:igfitidea点击:

Using OpenGl with C#?

c#opengl

提问by MegaByte

Is there free OpenGL support libraries for C#? If so, which one do I use and where do I find sample projects?

C# 有免费的 OpenGL 支持库吗?如果是这样,我应该使用哪一个以及在哪里可以找到示例项目?

Does C# provide classes for OpenGL?

C# 是否为 OpenGL 提供类?

回答by korona

What would you like these support libraries to do? Just using OpenGL from C# is simple enough and does not require any additional libraries afaik.

您希望这些支持库做什么?仅使用 C# 中的 OpenGL 就足够简单了,不需要任何额外的库 afaik。

回答by ólafur Waage

Taois supposed to be a nice framework.

Tao应该是一个不错的框架。

From their site:

从他们的网站:

The Tao Framework for .NET is a collection of bindings to facilitate cross-platform media application development utilizing the .NET and Mono platforms.

Tao Framework for .NET 是一组绑定,用于促进利用 .NET 和 Mono 平台的跨平台媒体应用程序开发。

回答by Oliver

I would also recommend the Tao Framework. But one additional note:

我也会推荐Tao Framework。但要补充一点:

Take a look at these tutorials: http://www.taumuon.co.uk/jabuka/

看看这些教程:http: //www.taumuon.co.uk/jabuka/

回答by Chris Masterton

XNA 2.0 requires a minimum of a shader 1.1 card. While old tech, not everyone has one. Some newer laptops (in our experience Toshiba tablets with Intel graphics) have no shader 1.1 support. XNA simply wont run on these machines.

XNA 2.0 至少需要一个着色器 1.1 卡。虽然是旧技术,但并不是每个人都有。一些较新的笔记本电脑(根据我们的经验,配备英特尔显卡的东芝平板电脑)不支持着色器 1.1。XNA 根本不会在这些机器上运行。

This is a significant issue for us and we have shifted to Tao and OpenGL. Plus with Tao we have bindings for audio & Lua support.

这对我们来说是一个重要的问题,我们已经转向了 Tao 和 OpenGL。加上 Tao,我们有音频和 Lua 支持的绑定。

回答by Jeff Mc

I think what @korona meant was since it's just a C API, you can consume it from C# directly with a heck of a lot of typing like this:

我认为@korona 的意思是因为它只是一个 C API,你可以直接从 C# 使用它,并输入大量如下内容:

[DllImport("opengl32")]
public static extern void glVertex3f(float x, float y, float z);

You unfortunately would need to do this for every single OpenGL function you call, and is basically what Tao has done for you.

不幸的是,您需要为您调用的每个 OpenGL 函数执行此操作,并且基本上是 Tao 为您所做的。

回答by Jeff Mc

OpenTKis an improvement over the Tao API, as it uses idiomatic C# style with overloading, strongly-typed enums, exceptions, and standard .NET types:

OpenTK是对 Tao API 的改进,因为它使用具有重载、强类型枚举、异常和标准 .NET 类型的惯用 C# 样式:

GL.Begin(BeginMode.Points);
GL.Color3(Color.Yellow);
GL.Vertex3(Vector3.Up);

as opposed to Tao which merely mirrors the C API:

与仅反映 C API 的 Tao 相反:

Gl.glBegin(Gl.GL_POINTS);   // double "gl" prefix
Gl.glColor3ub(255, 255, 0); // have to pass RGB values as separate args
Gl.glVertex3f(0, 1, 0);     // explicit "f" qualifier

This makes for harder porting but is incredibly nice to use.

这使得移植更加困难,但使用起来非常好。

As a bonus it provides font rendering, texture loading, input handling, audio, math...

作为奖励,它提供字体渲染、纹理加载、输入处理、音频、数学......

Update 18.1.2016: Today the OpenTK maintainer has stepped away from the project, leaving its future uncertain. The forums are filled with spam. The maintainer recommends moving to MonoGame or SDL2#.

2016 年 1 月 18 日更新:今天 OpenTK 维护者已退出该项目,使其未来充满不确定性。论坛里充斥着垃圾邮件。维护者建议转向 MonoGame 或 SDL2#。

回答by dharmatech

SharpGLis a project that lets you use OpenGL in your Windows Forms or WPF applications.

SharpGL是一个项目,可让您在 Windows 窗体或 WPF 应用程序中使用 OpenGL。

回答by Harry Glinos

You can OpenGL without a wrapper and use it natively in C#. Just as Jeff Mc said, you would have to import all the functions you need with DllImport.

您可以在没有包装器的情况下使用 OpenGL,并在 C# 中本地使用它。正如 Jeff Mc 所说,您必须使用 DllImport 导入您需要的所有功能。

What he left out is having to create context before you can use any of the OpenGL functions. It's not hard, but there are few other not-so-intuitive DllImports that need to be done.

他遗漏的是必须先创建上下文,然后才能使用任何 OpenGL 函数。这并不难,但还有一些其他不太直观的 DllImports 需要完成。

I have created an example C# project in VS2012 with almost the bare minimum necessary to get OpenGL running on Windows box. It only paints the window blue, but it should be enough to get you started. The example can be found at http://www.glinos-labs.org/?q=programming-opengl-csharp. Look for the No Wrapperexample at the bottom.

我在 VS2012 中创建了一个示例 C# 项目,几乎是在 Windows 机器上运行 OpenGL 所需的最低限度。它只会将窗户涂成蓝色,但它应该足以让您入门。该示例可以在http://www.glinos-labs.org/?q=programming-opengl-csharp找到。在底部寻找No Wrapper示例。

回答by Aycox

Concerning the (somewhat off topic I know but since it was brought up earlier) XNA vs OpenGL choice, it might be beneficial in several cases to go with OpenGL instead of XNA (and in other XNA instead of OpenGL...).

关于(我知道有点偏离主题,但因为它是较早提出的)XNA 与 OpenGL 的选择,在某些情况下使用 OpenGL 而不是 XNA(以及在其他 XNA 而不是 OpenGL 中......)可能是有益的。

If you are going to run the applications on Linux or Mac using Mono, it might be a good choice to go with OpenGL. Also, which isn't so widely known I think, if you have customers that are going to run your applications in a Citrix environment, then DirectX/Direct3D/XNA won't be as economical a choice as OpenGL. The reason for this is that OpenGL applications can be co-hosted on a lesser number of servers (due to performance issues a single server cannot host an infinite number of application instances) than DirectX/XNA applications which demands dedicated virtual servers to run in hardware accelerated mode. Other requirements exists like supported graphics cards etc but I will keep to the XNA vs OpenGL issue. As an IT Architect, Software developer etc this will have to be considered before choosing between OpenGL and DirectX/XNA...

如果您打算使用 Mono 在 Linux 或 Mac 上运行应用程序,那么使用 OpenGL 可能是一个不错的选择。此外,我认为这并不是广为人知,如果您的客户将在 Citrix 环境中运行您的应用程序,那么 DirectX/Direct3D/XNA 将不会像 OpenGL 那样经济实惠。这样做的原因是与需要专用虚拟服务器在硬件中运行的 DirectX/XNA 应用程序相比,OpenGL 应用程序可以在更少数量的服务器上共同托管(由于性能问题,单个服务器不能托管无限数量的应用程序实例)加速模式。存在其他要求,如支持的显卡等,但我将继续讨论 XNA 与 OpenGL 问题。作为 IT 架构师、软件开发人员等,在选择 OpenGL 和 DirectX/XNA 之前必须考虑这一点……

A side note is that WebGL is based on OpenGL ES3 afaik...

附带说明一下,WebGL 基于 OpenGL ES3 afaik ...

As a further note, these are not the only considerations, but they might make the choice easier for some...

进一步说明,这些不是唯一的考虑因素,但它们可能使某些人的选择更容易......