Using Opengl with C#

Hosting an OpenGL C++ Window in C# WPF

I did not set Loaded="Window_Loaded" in

<Window x:Class="OpenGLinWPF.OpenGLHWND"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:OpenGLinWPF"
mc:Ignorable="d"
Title="OpenGLHWND" Height="450" Width="800"
Loaded="Window_Loaded">
<Grid>
<Border Name="hwndPlaceholder" />
</Grid>
</Window>

The description of the Window.Loaded event here says

Occurs when the element is laid out, rendered, and ready for
interaction.

After setting it I get:

Sample Image

What advantages exist in C++ over C# when working with OpenGL

Using OpenGL via C# has some difficulties.

  1. Getting an up-to-date SDK. Tao hasn't been updated since 2008. OpenTK hasn't had a stable release since June of 2010. OpenGL has had two version releases since June 2010. That's a lot of functionality you simply cannot access from C#. I don't know how stable OpenTK's nightlies are, but I generally don't trust nightlies. With C/C++, you can get whatever function pointers you want to load. With C#, you can only use what your toolkit provides.

  2. Dealing with buffer objects can be quite painful in C#. Yes, there are ways to upload arrays of uniform values to buffer objects. But building interleaved vertex data, where different components have different types, is much more difficult in C#. In C and C++, you have direct access to memory. So it's easy to have a 3-vector of floats followed by a 4-vector of bytes, all stored in 16 bytes per vertex. It's rather more difficult in C#.

  3. Graphics code is generally one of the more performance-critical areas of code. You will generally want the control that C++ affords if you're making a high-performance rendering application like a game. So many of C#'s nice features work against it here.

The only real advantage that using C# provides is that... it's C#. To the degree that you feel that advantages you, then it is an advantage.

How Do I Initialize OpenGL.NET with GLFW.Net?

On the first glace, I can see an obvious issue. The size of the buffer data has to be specified in bytes (see glBufferData). Hence the size has to be 6*4 rather than 6:

var vertices = new float[] { -0.5f, -0.5f, 0.5f, -0.5f, 0.0f, 0.5f };
Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(4 * vertices.Length),
vertices, BufferUsage.StaticDraw);

It turns out that the OpenGL.NET library has to be initialized before making the OpenGL Context current (for whatever reason):

Gl.Initialize();
Glfw.MakeContextCurrent(window);


Related Topics



Leave a reply



Submit