luca-piccioni / OpenGL.Net

Modern OpenGL bindings for C#.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with GTK#

opened this issue · comments

I have tried but it doesn't show Gl-Viewport on Gtk.Window -
It always hide me.

Example code for Window or Container If I don't know that.

using Gtk;
using OpenGL;

namespace GtkGame{
    class GtkGameWindow : Window
    {
        public bool GLinit;

        public GtkGameWindow() : base(WindowType.Toplevel)
        {
            GLinit = false;
            WidgetEvent += new WidgetEventHandler(GtkInitalizeHanlder);
        }

        private void GtkInitalizeHanlder(object sender, WidgetEventArgs we)
        {
            int width = 0, height = 0;
            GetSizeRequest(out width, out height);
            float aspectRatio = width / height;
            Gl.Viewport(0, 0, width, height);
            Gl.ClearColor(1.0f, 1.0f, 1.0f, 1.0f);
            Gl.Clear(ClearBufferMask.ColorBufferBit);
            Gl.MatrixMode(MatrixMode.Modelview);
            Gl.LoadIdentity();
            Gl.ShadeModel(ShadingModel.Smooth);
            //Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, aspectRatio, 1.0f, 64.0f);
            Gl.MatrixMode(MatrixMode.Projection);

            Gl.ClearDepth(1);
            Gl.Disable(EnableCap.DepthTest);
            Gl.Enable(EnableCap.Texture2d);
            Gl.Enable(EnableCap.Blend);
            Gl.DepthFunc(DepthFunction.Always);
            Gl.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
            //add idle event handler to process rendering whenever and as long as time is availble.
            GLinit = true;
        }
    }
}

But I found example but it is old of OpenTK StackOverFlow

I try replace - But OpenGL Matrix4x4 hasn't static method CreatePerspectiveFieldOfView(). How do I fix for replacement of your OpenGL.net

Use PerspectiveProjectionMatrix, or OrthoProjectionMatrix if you want an orthographic projection. I'm evaluating the introduction of methods like CreatePerspectiveFieldOfView, it seems more intuitive for many users.

You should elaborate more about your GTK# solution. I've never tried that toolkit, even if I always wished to implement it. For example: who creates the device context? I fear that you really should call Gl.BindAPI() before using Gl methods.

Note: for using matrices, use the ToArray method:

Gl.LoadMatrix(new PerspectiveProjectionMatrix(60.0f, 16.0f / 9.0f, 0.1f, 100.0f).ToArray());

Thanks for resolving.. I will fix it

And I tried but viewport doesn't show - I always tried.

using System;
using Gtk;
using OpenGL;

namespace GtkGame
{
    class GtkGameWindow : Window
    {
        public bool GLinit;

        public GtkGameWindow(string title) : base(WindowType.Toplevel)
        {
            GLinit = false;
            WidgetEvent += new WidgetEventHandler(GtkInitalizeHanlder);
            Title = title;
        }

        private void GtkInitalizeHanlder(object sender, WidgetEventArgs we)
        {
            int width = 0, height = 0;
            GetSizeRequest(out width, out height);
            float aspectRatio = width / height;
            Gl.Viewport(0, 0, width, height);
            Gl.ClearColor(1.0f, 1.0f, 1.0f, 1.0f);
            Gl.Clear(ClearBufferMask.ColorBufferBit);
            Gl.MatrixMode(MatrixMode.Modelview);
            Gl.LoadIdentity();
            Gl.ShadeModel(ShadingModel.Smooth);
            Gl.LoadMatrix(new PerspectiveProjectionMatrix(60.0f, 16.0f / 9.0f, 0.1f, 100.0f).ToArray());
            Gl.MatrixMode(MatrixMode.Projection);

            Gl.ClearDepth(1);
            Gl.Disable(EnableCap.DepthTest);
            Gl.Enable(EnableCap.Texture2d);
            Gl.Enable(EnableCap.Blend);
            Gl.DepthFunc(DepthFunction.Always);
            Gl.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
            //add idle event handler to process rendering whenever and as long as time is availble.
            GLinit = true;
        }
    }
}

And Program.cs

using Gtk;
using OpenGL;

namespace GtkGame
{
    class Program
    {
        static void Main(string[] args)
        {
            Application.Init();
            GtkGameWindow myWin = new GtkGameWindow("My Game");
            myWin.Resize(800, 640);
            myWin.Destroyed += (o, e) =>
            {
                Application.Quit();
            };
            myWin.ShowAll();

            Application.Run();
        }
    }
}

result no viewport to Gtk.Window :(

I think Gtk# can not work - I don't understand why any users have luck to use Gtk# - If you want try Gtk or Xwt and Good luck to try for next release support with Gtk, Xwt or Eto or Xamarin.Mac as Window with built-in viewport of OpenGL.net

Well, the commit reference above is my first shot on GTK3. It actually does not work, I suspect because there is some misunderstanding about the double buffer (essentially the widget does not swap the buffers. The commit introduce a new sample, and try to create a DrawingArea that creates the GL context, and draw on itself. The GL context and its concurrency are managed "correctly", but the buffers are not swapped.

OpenGL.Net is not aware about the windowing system. You need a display handle and a window handle to create a GL context; those handles must be the ones that wglCreateContextAttribs expect to get, and obviously this is platform dependent.

The GTK sample is essentially based on this code, linked to this Question on mono. I had changed the gdk_win32_drawable_get_handle, because it returns always IntPtr.Zero whatever handle I passed to it. Currently I can create a GL context, but I'm not sure if the handles are actually linked to the widget or the window of anything else.

The solution on windows is to pass to DeviceContext.Create the handle returned by CreateWindowW. Indeed the question is: how to get that handle from GTK?

Etoo has support for GTK2... Maybe because nobody targets GTK3?

Thanks I will try later..

So now I say that bad support :( So sad because I already tried and it gots error :( 24/25 not working now.

I think I recommend with OpenGL.net because it has default gtk3 it hasn't problem with OpenTKL. Yay

Now I am working. Thanks!

Sorry for that forget to say... I have problem if I call eventhandler why does GLWidgetEventArgs doesn't exist - if i use

glWidget.ContextCreated += new EventHandler.<GlWidgetEventArgs>(MyCreatedListener);
 
private void MyCreatedListener(object sender, GlWidgetEventArgs e)
{
...
}

Shows error syntax 👎

I try to use delegate function

like this

glWidget.ContentCreated += delegate {
...
};

Why does example gtk app not show triangle :(

I really miss that. :( I wish I understand because I want create my own application for Half-Life :(

I tried example but after compiled shows only white screen????????????

using System;

using Gtk;
using OpenGL.GTK3;
using OpenGL;

namespace HelloTriangle.GTK
{
	class ExampleWindow : Window
	{
        private GlWidget glWidget;
        private static float _Angle;
        private static readonly float[] _ArrayPosition = new float[] {
            0.0f, 0.0f,
            0.5f, 1.0f,
            1.0f, 0.0f
        };
        private static readonly float[] _ArrayColor = new float[] {
            1.0f, 0.0f, 0.0f,
            0.0f, 1.0f, 0.0f,
            0.0f, 0.0f, 1.0f
        };

        private MemoryLock vertexArrayLock = new MemoryLock(_ArrayPosition);
        private MemoryLock vertexColorLock = new MemoryLock(_ArrayColor);

        public ExampleWindow() : base("Hello Triangle (GTK# 3)")
		{
			SetDefaultSize(800, 600);
			SetPosition(WindowPosition.Center);

			glWidget = new GlWidget();

			Add(glWidget);

            glWidget.SetSizeRequest(100, 100);

            glWidget.ContextCreated += delegate
            {
                Gl.MatrixMode(MatrixMode.Projection);
                Gl.LoadIdentity();
                Gl.Ortho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);

                if (glWidget.MultisampleBits > 0)
                {
                    Gl.Enable(EnableCap.Multisample);
                }
            };

            glWidget.Render += delegate
            {
                Gl.Viewport(0, 0, glWidget.WidthRequest, glWidget.HeightRequest);
                Gl.Clear(ClearBufferMask.ColorBufferBit);

                Gl.MatrixMode(MatrixMode.Modelview);
                Gl.LoadIdentity();
                Gl.Rotate(_Angle, 0.0f, 0.0f, 1.0f);

                Gl.VertexPointer(2, VertexPointerType.Float, 0, vertexArrayLock.Address);
                Gl.EnableClientState(EnableCap.VertexArray);

                Gl.ColorPointer(3, ColorPointerType.Float, 0, vertexColorLock.Address);
                Gl.EnableClientState(EnableCap.ColorArray);

                Gl.DrawArrays(PrimitiveType.Triangles, 0, 3);
            };

            glWidget.ContextUpdate += delegate
            {
                _Angle = (_Angle + 0.1f) % 45.0f;
            };

            DeleteEvent += delegate { Application.Quit(); };
			ShowAll();
		}
	}

	static class Program
	{
		/// <summary>
		/// Punto di ingresso principale dell'applicazione.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.Init();
			new ExampleWindow();
			Application.Run();
		}
	}
}

And it doesn't work? Or I should use Gl.Shader feature?

Please give me example..... Thanks!