Tao w/ WPF?
Submitted by jmeyer on November 27, 2007 - 9:52pm.
I'm working on trying to integrate OpenGL within a WPF app. While I was able to get something rendering, the coordinates seem to be all wonky and the line-strip I'm rendering is being clipped in a strange place. Has anyone else tried doing this with any success, or have any knowledge as to why this doesn't work as "expected"? Thanks.
-Jonathan

Do you happen to have an
Do you happen to have an example application? I'll try to help solve the problem, but there isn't much to do without some code
------
OpenTK
Happy to oblige. Below is
Happy to oblige. Below is the code that wraps the SimpleOpenGlControl. I believe the issue may be that I don't specify anywhere in the code the size of the render area. I saw the SimpleOpenGLControl is setting it's 'Size' field to {50, 50}, but modifying this doesn't seem to change the behavior. However, another thing I've noticed is that the only area in the control that gets rendered is a 50x50 square in the bottom left part of the control. Note that the area outside of this 50x50 square responds to the glClearColor call.
using System;
using System.Windows;
using System.Windows.Controls;
using Tao.OpenGl;
using Tao.Platform.Windows;
using System.Windows.Forms.Integration;
namespace WPFOpenGLInterop
{
///
/// Interaction logic for OpenGLCtl.xaml
///
public partial class OpenGLCtl : System.Windows.Controls.UserControl
{
WindowsFormsHost host;
System.Windows.Forms.Control wfPA;
static double A, B, C, D;
public OpenGLCtl()
{
InitializeComponent();
host = new WindowsFormsHost();
wfPA = new OpenGLRenderWind();
wfPA.Paint += DrawData;
host.Child = wfPA;
this.OpenGLCanvas.Children.Add(host);
Gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
Gl.glColor3f(1.0f, 1.0f, 1.0f);
Gl.glPointSize(2.0f);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Gl.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
internal class OpenGLRenderWind : SimpleOpenGlControl
{
public OpenGLRenderWind()
{
this.InitializeContexts();
// Should I explicitly set this.Size here?
}
}
private void DrawData(object sender, System.Windows.Forms.PaintEventArgs e)
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
Gl.glBegin(Gl.GL_LINE_STRIP);
double x, y;
Random rand = new Random();
for (double i = 0; i < 100; i++)
{
y = (float)(rand.NextDouble());
x = (float)i / (100 / 10);
Gl.glVertex2d(x, y);
}
Gl.glEnd();
Gl.glFlush();
}
}
}
I think the problem is
I think the problem is deeper and just setting the size won't help. What happens here is that the Paint/Resize event is never raised so nothing gets drawn, ever (not even the Gl.Clear call has any effect).
Check the host.ActualWidth and host.ActualHeight properties - are they something other than 0? I have very little knowledge on WPF, I'll read up during the next few days and see what comes up.
BTW, I'm running Vista with an Ati video card, what's your OS?
------
OpenTK
So, just to be clear, I AM
So, just to be clear, I AM getting something to render, it's just regulated to a 50x50 rect at the bottom-left corner of the control. If I increase the right and top fields in my glOrtho call, what is rendered in that 50x50 rect is scaled "appropriately". My ActualWidth and ActualHeight properties are both set to something reasonable.
Currently I'm running XP, and an NVIDIA video card. I'll investigate overriding the Resize method, but I'm not sure what I would be doing other than the default.
You are supposed to set the
You are supposed to set the viewport (glViewPort) on the resize, viewport controls where the image is put, not the size of the control. You also should return to the modelview matrix (glMatrixMode(GL_MODELVIEW); ) after you set your projection, otherwise all your transformations, rotations and scalings will operate on projection matrix.
Setting glViewPort did it!
Setting glViewPort did it! Sweet! Thanks a lot!
-Jonathan
Your little bit of WPF with
Your little bit of WPF with openGL code has been useful for me. Is there any possibility you can post a complete user control example?
Thanks
-- Ofer
Hi Ofer, you might want to
Hi Ofer,
you might want to check out my article at the CodeProject:
"Creating OpenGL Windows in WPF"
http://www.codeproject.com/KB/WPF/WPFOpenGL.aspx
It does not use Tao, but managed C++. However, replacing that is straightforward. I also plan to add a Tao-based control to the article, but I need the DPI problem solved first.
Best Regards,
Chris