I'm trying to draw on a OpenGLControl
from the code behind (or preferably using the MVVM pattern) without using the OpenGLDraw eventhandler
. The reason for this is that I'm using it for augmented reality and the camera is using an external trigger, so I want to draw whenever a new frame comes in from the camera. In the example I've used a timer to simulate the camera.
I've tried the following:
<Window x:Class="Example1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test" Height="400" Width="600"
xmlns:sharpGL="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF">
<Grid>
<sharpGL:OpenGLControl
x:Name="glControl"
OpenGLInitialized="OpenGLControl_OpenGLInitialized"
RenderContextType="DIBSection"
DrawFPS="True"/>
</Grid>
</Window>
And the code behind:
using System.Windows;
using SharpGL;
using SharpGL.SceneGraph;
using System.Timers;
namespace Example1
{
public partial class MainWindow : Window
{
Timer clock = new Timer(1000f / 60f);
float rotatePyramid = 0;
public MainWindow()
{
InitializeComponent();
}
private void OpenGLControl_OpenGLInitialized(object sender, OpenGLEventArgs args)
{
OpenGL gl = args.OpenGL;
gl.Enable(OpenGL.GL_DEPTH_TEST);
clock.Elapsed += OnFrame;
clock.Start();
}
private void OnFrame(object sender, ElapsedEventArgs e)
{
OpenGL gl = glControl.OpenGL;
// Clear the color and depth buffers.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
// Reset the modelview matrix.
gl.LoadIdentity();
// Move the geometry into a fairly central position.
gl.Translate(0f, 0.0f, -6.0f);
// Draw a pyramid. First, rotate the modelview matrix.
gl.Rotate(rotatePyramid, 0.0f, 1.0f, 0.0f);
// Start drawing triangles.
gl.Begin(OpenGL.GL_TRIANGLES);
gl.Color(1.0f, 0.0f, 0.0f);
gl.Vertex(0.0f, 1.0f, 0.0f);
gl.Color(0.0f, 1.0f, 0.0f);
gl.Vertex(-1.0f, -1.0f, 1.0f);
gl.Color(0.0f, 0.0f, 1.0f);
gl.Vertex(1.0f, -1.0f, 1.0f);
gl.Color(1.0f, 0.0f, 0.0f);
gl.Vertex(0.0f, 1.0f, 0.0f);
gl.Color(0.0f, 0.0f, 1.0f);
gl.Vertex(1.0f, -1.0f, 1.0f);
gl.Color(0.0f, 1.0f, 0.0f);
gl.Vertex(1.0f, -1.0f, -1.0f);
gl.Color(1.0f, 0.0f, 0.0f);
gl.Vertex(0.0f, 1.0f, 0.0f);
gl.Color(0.0f, 1.0f, 0.0f);
gl.Vertex(1.0f, -1.0f, -1.0f);
gl.Color(0.0f, 0.0f, 1.0f);
gl.Vertex(-1.0f, -1.0f, -1.0f);
gl.Color(1.0f, 0.0f, 0.0f);
gl.Vertex(0.0f, 1.0f, 0.0f);
gl.Color(0.0f, 0.0f, 1.0f);
gl.Vertex(-1.0f, -1.0f, -1.0f);
gl.Color(0.0f, 1.0f, 0.0f);
gl.Vertex(-1.0f, -1.0f, 1.0f);
gl.End();
gl.Flush();
rotatePyramid += 3.0f;
}
}
}
This only renders a black screen with the FPS counter in it. I think it has to do with not getting the right OpenGL object. I would really like to use the MVVM pattern and using bindings, but if that's not possible then code behind would also work, as long is I can draw whenever I get a frame from the camera.