This project is read-only.

WPF OpenGL

Jan 16, 2013 at 3:44 PM

How do I add OpenGL to an existing WPF project?  I performed the following steps but it gave me an error message saying that OpenGLControl_OpenGLDraw was not found.   I added references to SharpGL.dll and SharpGL.WPF.dll in Visual Studio, added the following to my xaml file:

xmlns:sharpGL="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF"

<sharpGL:OpenGLControl  OpenGLDraw="OpenGLControl_OpenGLDraw"

OpenGLInitialized="OpenGLControl_OpenGLInitialized"

DrawFPS="True"

Resized="OpenGLControl_Resized" />

Note that when I typed in OpenGLDraw above and hit tab twice it did not automatically fill in OpenGLControl_OpenGLDraw like it would when I created a new SharpGL WPF Application.

Any ideas on how to fix that?

Jan 17, 2013 at 9:29 AM

Find the sharpgl assemblies you are referencing, right click on them, choose 'properties' - make sure they're not blocked (if they are, press the 'unblock' button) - also make sure you're using the .NET Framework 4.0 - not the .NET Framework 4.0 Client Profile :) that should help!

Jan 17, 2013 at 3:02 PM

The sharpgl assemblies were not blocked and I'm using the .NET Framework 4.0.

I'm sure VS is getting the assemblies fine because as soon as I typed in <sharpGL:OpenGLControl /> in my xaml file, the panel turns black similar to the step you have on CodeProject Article 265903.

The problem seems to be sharpgl not knowing where to drop OpenGLControl_OpenGLDraw when I typed OpenGLDraw="OpenGLControl_OpenGLDraw" since my project isn't started as a sharpgl project from scratch.  So maybe I have to specify that manually.  If so, how?

Jan 17, 2013 at 3:33 PM

Drop the code below into your window:

 

 Collapse | Copy Code
private void OpenGLControl_OpenGLDraw(object sender, SharpGL.OpenGLEventArgs args)
{
} 
Jan 17, 2013 at 4:37 PM

I've tried that but it's still complaining about not finding OpenGLControl_OpenGLDraw.  I'm trying to display the OpenGL content within one of the panels under the main window.  Following are my xaml file and its corresponding cs file.  Maybe you can see something obvious that I missed.

<UserControl x:Class="Test.Windows.ViewChassis.TestPanels.ThreeDVisualization"

   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   xmlns:c="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"

   xmlns:cp="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"

   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

   xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"

   xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

   xmlns:local="clr-namespace:Test.Windows.ViewChassis.TestPanels;assembly=TestPanels"

   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

   xmlns:swi="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

   xmlns:v="clr-namespace:Test.Windows.ViewChassis;assembly=ViewChassis"

   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   xmlns:sharpGL="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF"

   mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"

   v:View.BaseLocalizationPath="Test.Windows.ViewChassis.TestPanels.ThreeDVisualization">

   <UserControl.Resources>

       <ResourceDictionary>

           <CollectionViewSource

              x:Key="ThreeDVisualizationViewModelViewSource"

               d:DesignSource="{d:DesignInstance local:ThreeDVisualizationViewModel, CreateList=True}" />

           <ResourceDictionary.MergedDictionaries>

               <v:DesignerResourceDictionary>

               </v:DesignerResourceDictionary>

           </ResourceDictionary.MergedDictionaries>

       </ResourceDictionary>

   </UserControl.Resources>

   <Grid x:Name="LayoutRoot" DataContext="{StaticResource ThreeDVisualizationViewModelViewSource}">

       <Grid>

           <sharpGL:OpenGLControl x:Name="OpenGLControl" OpenGLDraw="OpenGLControl_OpenGLDraw"/>

       </Grid>

   </Grid>

</UserControl>

And the partial code of the corresponding cs file:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Linq;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Threading;

 using System.Windows.Forms;

 using System.Xml.Linq;

using SharpGL;

namespace Test.Windows.ViewChassis.TestPanels

{

  //**************************************************************************

 /// <summary>

   /// <PLEASE ENTER A CLASS DESCRIPTION>.

   /// </summary>

   //**************************************************************************

   // WARNING: IsAlwaysAuthorized needs to be replaced with something more like what appears below it

   [Component(InstanceName = "ThreeDVisualization", ImageName = "", IsAlwaysAuthorized = true)]

   public class ThreeDVisualizationViewModel : DisplayPanelViewModel

   {

       // Constructor/Destructor ----------------------------------------------

 

       /// <summary>

       /// Constructor.

       /// </summary>

       ///

       private void openGLControl_OpenGLDraw(object sender, SharpGL.OpenGLEventArgs e)

       {

             DrawScene();

       }

       public ThreeDVisualizationViewModel()

       {

       }

....etc.

}

Jan 17, 2013 at 6:53 PM
Edited Jan 17, 2013 at 6:54 PM

Maybe because your Xaml has OpenGLControl_OpenGLDraw and your cs has openGLControl_OpenGLDraw?

Jan 17, 2013 at 7:06 PM

Good eyes but that's not it.  It's a typo when I entered that line of code here but they do match in my actual code.  Thanks though.

Jan 24, 2013 at 2:59 PM
Edited Jan 24, 2013 at 3:02 PM

I've just begun looking at SharpGL as a solution for OpenGL in WPF.  It looks exciting and useful!  I followed an older example (here) that made use of older binaries and had no problems integrating it into my software.  Worked great, except the older binaries have a memory leak that has been solved in the latest binaries (so I read).  When I update to the latest binaries I start having the exact same problem lieminator is having - it's just not recognizing the OpenGLDraw event in the opengGLControl object.  I can 'see' the public OpenGLDraw event when I use the object browser.  

One thing that may be a piece of the puzzle is that I'm developing in Visual Studio 2012.   Anyone have any suggestions on how to pursue debugging this?  I'd like to get SharpGL working for my product, as it's really slick. 

Jan 24, 2013 at 3:41 PM

A small update: When I add references to the other SharpGL dependencies I am able to recognize the OpenGLDraw Event, but I have found in the object viewer that SharpGL no longer has anything available about OpenGLEventArgs  (When I check the older assemblies in the object viewer I do see them available). 

I'm going to go out on a limb and guess that I'll need to download and sort through source code for this.  Please let me know if I'm wrong!

Jan 24, 2013 at 6:30 PM

Problem solved:  I was doing all those SharpGL stuff in the wrong place.  I moved them to my Custom Control section and all is well.  Thank you all for your input.