schragnasher
Topic Author
Posts: 33
Joined: 07 Mar 2015, 01:58

Opentk

02 Sep 2017, 23:22

Is noesis capable of working with OpenTK?

Tags:
 
User avatar
jsantos
Site Admin
Posts: 2906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Opentk

06 Sep 2017, 18:11

Never used it, but, as far as I understand, being Opentk just a GL context creation helper, it should work perfectly with Noesis.
 
schragnasher
Topic Author
Posts: 33
Joined: 07 Mar 2015, 01:58

Re: Opentk

06 Sep 2017, 18:38

I gave it an attempt and kept running into some issue with memory corruption/access. Ill try again tonight.
 
schragnasher
Topic Author
Posts: 33
Joined: 07 Mar 2015, 01:58

Re: Opentk

07 Sep 2017, 04:35

So i got it rendering, but as soon as i call view.renderer.InitGL my other renders disappear, Not sure whats happening, i dont even need to actually render the view, i just get a black screen where i should see my other geometry rendering as i usually do.
 
schragnasher
Topic Author
Posts: 33
Joined: 07 Mar 2015, 01:58

Re: Opentk

07 Sep 2017, 04:44

Problem solved! I guess you guys turn on a lot of stuff i dont want. Looking at the Opengl example i disabled a buncha stuff and now i got ui over my model.
 
User avatar
jsantos
Site Admin
Posts: 2906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Opentk

08 Sep 2017, 04:36

Great! Exactly, I was going to tell you about the GL example and the reference GLRenderDevice. There you have all the information about the states we touch.

Thanks for the feedback!
 
alundgren04
Posts: 2
Joined: 19 Aug 2018, 20:21

Re: Opentk

21 Aug 2018, 02:51

I just spent a couple hours hammering this out myself. As of 2018-08-20 this is using the latest version of NoesisGUI and OpenTK. It's largely adapted from this forum post: viewtopic.php?t=1177
using System;
using NoesisApp;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Input;
using OpenTK.Graphics.OpenGL;

namespace IntegrationGLUT
{
    internal sealed class MainWindow : GameWindow
    {
        [STAThread]
        static void Main()
        {
            var program = new MainWindow();
            program.Run();
        }

        private Noesis.View view;
        private Noesis.Renderer renderer;

        public MainWindow() : base(1280, 720, new GraphicsMode(32, 24, 8, 1, ColorFormat.Empty, 3), "Test",
            GameWindowFlags.Default, DisplayDevice.Default, 3, 1, GraphicsContextFlags.Default)
        {
            VSync = VSyncMode.On;
            WindowState = OpenTK.WindowState.Normal; //  OpenTK.WindowState.Fullscreen;
        }

        #region Overrides of GameWindow
        protected override void OnLoad(EventArgs e)
        {
            Noesis.GUI.Init();

            // Setup embedded resource providers
            EmbeddedXaml[] xamls = new EmbeddedXaml[]
            {
                    new EmbeddedXaml { filename = "Reflections.xaml", resource = "Reflections" }
            };
            Noesis.GUI.SetXamlProvider(new EmbeddedXamlProvider(xamls, Properties.Resources.ResourceManager));

            // Data loading
            {
                // get content
                var content = (Noesis.Grid)Noesis.GUI.LoadXaml("Reflections.xaml");

                // create view
                view = Noesis.GUI.CreateView(content);

                // get OpenGL rendering device
                Noesis.RenderDevice device = new Noesis.RenderDeviceGL();

                // init renderer as OpenGL
                renderer = view.Renderer;
                renderer.Init(device);
            }

            view.Update(0.001); // Ensures view is updated before first render call (avoids crash)
        }

        protected override void OnUnload(EventArgs e)
        {
            renderer.Shutdown();
            Noesis.GUI.Shutdown();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            view.SetSize(Width, Height);
        }

        private double totalTimeElapsed = 0;
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            totalTimeElapsed += e.Time;
            view.Update(totalTimeElapsed);
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            renderer.UpdateRenderTree();

            if (renderer.NeedsOffscreen())
                renderer.RenderOffscreen();

            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);
            GL.ClearDepth(1.0f);
            GL.DepthMask(true);
            GL.Disable(EnableCap.CullFace);
            GL.Disable(EnableCap.AlphaTest);
            GL.Disable(EnableCap.StencilTest);
            GL.Disable(EnableCap.Blend);
            GL.Disable(EnableCap.ScissorTest);

            GL.UseProgram(0);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            GL.Viewport(0, 0, Width, Height);
            GL.ColorMask(true, true, true, true);

            GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.LoadIdentity();
            GL.Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

            renderer.Render();

            SwapBuffers();

            base.OnRenderFrame(e);
        }

        protected override void OnMouseMove(MouseMoveEventArgs e)
        {
            view.MouseMove(e.X, e.Y);
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            view.MouseButtonDown(e.X, e.Y, TranslateButton(e.Button));
        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            view.MouseButtonUp(e.X, e.Y, TranslateButton(e.Button));
        }

        protected override void OnKeyDown(KeyboardKeyEventArgs e)
        {
            view.KeyDown(TranslateKey(e.Key));
        }

        protected override void OnKeyUp(KeyboardKeyEventArgs e)
        {
            view.KeyUp(TranslateKey(e.Key));
            if (e.Key == Key.Escape)
                Exit();
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            view.Char(e.KeyChar);
        }
        #endregion

        private static Noesis.Key TranslateKey(Key key)
        {
            switch (key)
            {
                case Key.F1: return Noesis.Key.F1;
                case Key.F2: return Noesis.Key.F2;
                case Key.F3: return Noesis.Key.F3;
                case Key.F4: return Noesis.Key.F4;
                case Key.F5: return Noesis.Key.F5;
                case Key.F6: return Noesis.Key.F6;
                case Key.F7: return Noesis.Key.F7;
                case Key.F8: return Noesis.Key.F8;
                case Key.F9: return Noesis.Key.F9;
                case Key.F10: return Noesis.Key.F10;
                case Key.F11: return Noesis.Key.F11;
                case Key.F12: return Noesis.Key.F12;
                case Key.PageUp: return Noesis.Key.Prior;
                case Key.PageDown: return Noesis.Key.Next;
                case Key.Home: return Noesis.Key.Home;
                case Key.End: return Noesis.Key.End;
                case Key.Insert: return Noesis.Key.Insert;
                case Key.Left: return Noesis.Key.Left;
                case Key.Right: return Noesis.Key.Right;
                case Key.Up: return Noesis.Key.Up;
                case Key.Down: return Noesis.Key.Down;
                case Key.Enter: return Noesis.Key.Return;
                case Key.Tab: return Noesis.Key.Tab;
                case Key.BackSpace: return Noesis.Key.Back;
                case Key.Space: return Noesis.Key.Space;
                case Key.Delete: return Noesis.Key.Delete;
            }

            return Noesis.Key.None;
        }

        private static Noesis.MouseButton TranslateButton(MouseButton button)
        {
            switch (button)
            {
                case MouseButton.Left: return Noesis.MouseButton.Left;
                case MouseButton.Right: return Noesis.MouseButton.Right;
                case MouseButton.Middle: return Noesis.MouseButton.Middle;
                default: return Noesis.MouseButton.XButton1;
            }
        }
    }

}
 
User avatar
jsantos
Site Admin
Posts: 2906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Opentk

23 Aug 2018, 15:40

Thanks for sharing this! Having this in GitHub would be awesome.
 
alundgren04
Posts: 2
Joined: 19 Aug 2018, 20:21

Re: Opentk

24 Aug 2018, 01:38

Good idea. I'm working on abstracting it further into an abstract base class which contains all the common needs, like wiring up to OpenTK's GameWindow. Then a child class can do the GUI specific to the given app. I'll make a note to put it up on GitHub when I'm done.

Who is online

Users browsing this forum: Google [Bot] and 0 guests