Virdues's picture

[solved] OpenTK with WPF

Hi all,

First of all, would like to say a big thumbs with with OpenTK. Past two months I have been working on a uni project, happily working away using OTK with windows forms and now our group has decided that they want it displayed using WPF.

Been looking around the forums and google about how to do this but nothing really is clearly explained. I know it's best to use windows forms host, which I did use (using VS2010). I simply dragged it across but GLControl is grayed out so I cannot drag it across onto the windows forms host. I've tried adding it via xaml (which I hate) with no success and I can't seems to add it conventionally as code.

So I am at a total loss on how to get an OTK GLControl on the WPF form with or without a windows forms host.

Many thanks for your time.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
hkon's picture

Hello!

What I _just_ did.
Add a "Windows Forms" Control, MyGlControl to your wpf project.
Attach the OpenTK glcontrol to that control. Follow the steps http://www.opentk.com/doc/chapter/2/glcontrol here

In your XAML file. Name your WindowsFormsHost so you can access it from CodeBehind.
In the codebehind, set the child of the windowsformshost to be an instance of MyGlControl.

Hope this helps.

Virdues's picture

Like I mentioned before the GLControl item in the toolbox is grayed out with either the WPF form highlighted or the WindowsFormsHost control in the WPF form highlighted.

hkon's picture

You need to make an ordinary windows forms control, MyOrdinaryWindowsFormsControl, put the glcontrol onto that, no wpf or xaml involved so far.

Then put MyOrdinaryWindowsFormsControl into your WindowsFormsHost in your xaml file?

If you don't get it I can put together a sample.

xtr33me's picture

Hey everyone! Right now working on an effects tool for an iphone game I am working on and I am developing this in WPF and wanted to use the OpenTK to render the viewport.

Now I followed the instructions at the link above and I am able to get the viewport to clear to the color specified, but I can't get the yellow triangle to render. Any ideas on why this is the case? Anyone else seen anything similar? It does step into my Render method and through the begin/end block. Any ideas or direction would be great! thanks all!

Virdues's picture

Provide the code that is meant to be doing the drawing so we can have a better understand of what is going on. The only things I can think of why it wouldn't be drawing is not including the GL.End(); but you said you have included it, the other thing is do you have .Invalidate(); in there?

xtr33me's picture

Thanks Virdues! I was going to include just the rendering but I figured since thee wasn't much here I would just include it all. I pretty much followed the tutorial and then figured once I had that I would start implementing my particles. I also tried the invalidate call at the end since I did not have that but unfortunately it didnt work. Let me know if you need anything else. Thanks for lookin at this. I've been playin with this all morn and still not any luck :p

One other thing, when I remove the swapbuffer call out, the clearcolor of the background doesnt change so it does seem to be swapping also.

public partial class MyGlUserControl : UserControl
    {
        bool loaded = false;
 
        public MyGlUserControl()
        {
            InitializeComponent();
        }
 
        Stopwatch sw = new Stopwatch();
        private void MyGlControl_Load(object sender, EventArgs e)
        {
            loaded = true;
            GL.ClearColor(Color.SkyBlue);
            SetupViewport();
        }
 
        private void MyGlControl_Resize(object sender, EventArgs e)
        {
            if (!loaded)
                return;
 
            SetupViewport();
            MyGlControl.Invalidate();
        }
 
        private void Render()
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
 
            GL.Begin(BeginMode.Triangles);
            GL.Color3(Color.Yellow);
            GL.Vertex2(10, 20);
            GL.Vertex2(100, 20);
            GL.Vertex2(100, 50);
 
            GL.End();
 
            MyGlControl.SwapBuffers();
            //MyGlControl.Invalidate();
        }
 
        private void MyGlControl_Paint(object sender, PaintEventArgs e)
        {
            if (!loaded)
                return;
 
            Render();            
        }
 
        private void SetupViewport()
        {
            int w = MyGlControl.Width;
            int h = MyGlControl.Height;
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(0, w, 0, h, -1, 1); //Bottom left corner pixel has corrdinate 0,0
            GL.Viewport(0, 0, w, h); //Use all of the MyGlControl painting area
        }
    }
xtr33me's picture

Lol...disregard :) I decided to shrink up my GLControl on the Windows User Control object and the triangle appeared. I was putting the window in a docking window and it seems that the area where the triangle was drawing, was getting hidden behind one of my other dock panels. :p