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:
What advantages exist in C++ over C# when working with OpenGL
Using OpenGL via C# has some difficulties.
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.
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#.
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
Can You Create SQL Views/Stored Procedure Using Entity Framework 4.1 Code First Approach
How to Parse Command Line Output from C#
How to Check If a File Exists in a Folder
Why Is Linq .Where(Predicate).First() Faster Than .First(Predicate)
How to Use a C# Class Library in a Project
Recommendations on Parsing .Eml Files in C#
Start May Not Be Called on a Promise-Style Task. Exception Is Coming
With Unity How to Inject a Named Dependency into a Constructor
How Is Memory Allocated for a Static Variable
A Textbox/Richtextbox That Has Syntax Highlighting? [C#]
Any Way to Turn the "Internet Off" in Windows Using C#
How to Check If Int Is Legal Enum in C#
ASP.NET MVC 3: Override "Name" Attribute with Textboxfor
How to Register a .Net Dll File in the Gac
Casting VS Converting an Object Tostring, When Object Really Is a String
Why Do Some C# Lambda Expressions Compile to Static Methods