Example #1
0
        void Prepare()
        {
            Console.WriteLine("Prepare");

            // The GL COntext must be active for these functions to have an effect
            glContext = this.OpenGLContext;
            glContext.MakeCurrentContext();

            // Configure the view
            GL.ShadeModel(ShadingModel.Smooth);
            GL.Enable(EnableCap.Lighting);
            GL.Enable(EnableCap.DepthTest);

            // Add some ambient lighting
            float[] ambient = { 0.2f, 0.2f, 0.2f, 1.0f };
            GL.LightModel(LightModelParameter.LightModelAmbient, ambient);

            // Initialize the light
            float[] diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
            GL.Light(LightName.Light0, LightParameter.Diffuse, diffuse);
            // and switch it on
            GL.Enable((EnableCap)LightName.Light0);

            // Set the properties of the material under ambient light
            float[] mat = { 0.1f, 0.1f, 0.7f, 1.0f };
            GL.Material(MaterialFace.Front, MaterialParameter.Ambient, mat);

            GL.Material(MaterialFace.Front, MaterialParameter.Diffuse, mat);
        }
Example #2
0
        public CCGameView(CGRect frame, NSOpenGLContext context) : base(frame, context)
        {
            ViewSize      = new CCSizeI((int)frame.Width, (int)frame.Height);
            viewportDirty = true;

            BeginInitialise();
        }
Example #3
0
		public MyOpenGLView (CGRect frame,NSOpenGLContext context) : base(frame)
		{
			var attribs = new object [] {
				NSOpenGLPixelFormatAttribute.Accelerated,
				NSOpenGLPixelFormatAttribute.NoRecovery,
				NSOpenGLPixelFormatAttribute.DoubleBuffer,
				NSOpenGLPixelFormatAttribute.ColorSize, 24,
				NSOpenGLPixelFormatAttribute.DepthSize, 16 };

			pixelFormat = new NSOpenGLPixelFormat (attribs);

			if (pixelFormat == null)
				Console.WriteLine ("No OpenGL pixel format");

			// NSOpenGLView does not handle context sharing, so we draw to a custom NSView instead
			openGLContext = new NSOpenGLContext (pixelFormat, context);

			openGLContext.MakeCurrentContext ();

			// Synchronize buffer swaps with vertical refresh rate
			openGLContext.SwapInterval = true;

			// Initialize our newly created view.
			InitGL ();

			SetupDisplayLink ();

			// Look for changes in view size
			// Note, -reshape will not be called automatically on size changes because NSView does not export it to override 
			notificationProxy = NSNotificationCenter.DefaultCenter.AddObserver (NSView.GlobalFrameChangedNotification, HandleReshape);
		}
Example #4
0
        public MonoMacGameView(RectangleF frame, NSOpenGLContext context) : base(frame)
        {
            var attribs = new object [] {
                NSOpenGLPixelFormatAttribute.Accelerated,
                NSOpenGLPixelFormatAttribute.NoRecovery,
                NSOpenGLPixelFormatAttribute.DoubleBuffer,
                NSOpenGLPixelFormatAttribute.ColorSize, 24,
                NSOpenGLPixelFormatAttribute.DepthSize, 16
            };

            pixelFormat = new NSOpenGLPixelFormat(attribs);

            if (pixelFormat == null)
            {
                Console.WriteLine("No OpenGL pixel format");
            }

            // NSOpenGLView does not handle context sharing, so we draw to a custom NSView instead
            openGLContext = new NSOpenGLContext(pixelFormat, context);

            openGLContext.MakeCurrentContext();

            // default the swap interval and displaylink
            SwapInterval         = true;
            DisplaylinkSupported = true;

            // Look for changes in view size
            // Note, -reshape will not be called automatically on size changes because NSView does not export it to override
            notificationProxy = NSNotificationCenter.DefaultCenter.AddObserver(NSView.NSViewGlobalFrameDidChangeNotification, HandleReshape);
        }
Example #5
0
 public NSGameView(Lime.Input input, CGRect frame, GraphicsMode mode)
     : base(frame)
 {
     this.input  = input;
     this.Hidden = true;
     // Avoid of double scaling on high DPI displays
     this.WantsBestResolutionOpenGLSurface = true;
     pixelFormat = SelectPixelFormat(mode, 1, 0);
     if (pixelFormat == null)
     {
         throw new InvalidOperationException(string.Format("Failed to contruct NSOpenGLPixelFormat for GraphicsMode {0}", mode));
     }
     if (openGLContext == null)
     {
         openGLContext = new NSOpenGLContext(pixelFormat, null);
     }
     if (openGLContext == null)
     {
         throw new InvalidOperationException(string.Format("Failed to construct NSOpenGLContext {0}", mode));
     }
     openGLContext.MakeCurrentContext();
     if (platformRenderContext == null)
     {
         platformRenderContext = new Graphics.Platform.OpenGL.PlatformRenderContext();
         PlatformRenderer.Initialize(platformRenderContext);
     }
     swapInterval = true;
 }
        public MyOpenGLView(RectangleF frame, NSOpenGLContext context) : base(frame)
        {
            var attribs = new object[] { NSOpenGLPixelFormatAttribute.Accelerated, NSOpenGLPixelFormatAttribute.NoRecovery, NSOpenGLPixelFormatAttribute.DoubleBuffer, NSOpenGLPixelFormatAttribute.ColorSize, 24, NSOpenGLPixelFormatAttribute.DepthSize, 16 };

            pixelFormat = new NSOpenGLPixelFormat(attribs);

            if (pixelFormat == null)
            {
                Console.WriteLine("No OpenGL pixel format");
            }

            // NSOpenGLView does not handle context sharing, so we draw to a custom NSView instead
            openGLContext = new NSOpenGLContext(pixelFormat, context);

            openGLContext.MakeCurrentContext();

            // Synchronize buffer swaps with vertical refresh rate
            openGLContext.SwapInterval = true;

            // Initialize our newly created view.
            InitGL();

            SetupDisplayLink();

            // Look for changes in view size
            // Note, -reshape will not be called automatically on size changes because NSView does not export it to override
            notificationProxy = NSNotificationCenter.DefaultCenter.AddObserver(NSView.NSViewGlobalFrameDidChangeNotification, HandleReshape);
        }
Example #7
0
        public void Flush(NSOpenGLContext ctx)
        {
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            GL.Flush();             // We flush to be sure the framebuffer texture has the data we want

            SurfaceRendered.OnNext(_backbuffer.Value.Surface);

            Swap();
        }
Example #8
0
            /*    void Win_DidChangeScreen(object sender, EventArgs e)
             *  {
             *  }
             *
             *
             *  void Win_DidMiniaturize(object sender, EventArgs e)
             *  {
             *  }
             *
             *
             *  void Win_DidExpose(object sender, EventArgs e)
             *  {
             *  }
             *
             *
             *  void Win_DidChangeScreenProfile(object sender, EventArgs e)
             *  {
             *  }
             *
             *
             *  void Win_DidChangeBackingProperties(object sender, EventArgs e)
             *  {
             *  }
             *
             *
             *  void Win_DidChangeValue(string obj)
             *  {
             *  }
             *  public override void ViewDidHide()
             *  {
             *      base.ViewDidHide();
             *  }
             *  public override void ViewDidUnhide()
             *  {
             *      base.ViewDidUnhide();
             *  }*/

            internal void EndRender()
            {
                /*  if (object.ReferenceEquals(Thread.CurrentThread, this.mainthread))
                 * {*/
                Debug.Assert(FrameFactory.getcurrentfunc().Handle == this.openglctx.CGLContext.Handle);
                NSOpenGLContext.ClearCurrentContext();
                if (Thread.CurrentThread == typeof(Application).GetProperty("UIThread", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, new object[0]) as Thread)
                {
                    this.openglctx.CGLContext.Unlock();
                }

                /*   }
                 * else
                 * {
                 *     layer.CGLSetCurrentContext(IntPtr.Zero);
                 * }*/
            }
Example #9
0
            public viewwindow(IRenderer render, IRenderOwner renderer, Widget widget, CGRect frame, NSOpenGLContext ctx)
                : base(frame)
            {
                this.mainthread = Thread.CurrentThread;

                this.render   = render;
                this.renderer = renderer;
                this.window   = widget;

                base.WantsBestResolutionOpenGLSurface = true;
                this.WantsLayer = true;

                this.openglctx = ctx;

                Trace.Assert(this.openglctx != null, "no NSOpenGLContext context");

                //                    this.openglctx.CGLContext.Handle

                this.openglctx.CGLContext.Lock();
                try
                {
                    this.openglctx.MakeCurrentContext();

                    this._ctx = new _GraphicsContext(); // created from active content
                }
                catch { throw; }
                finally
                {
                    NSOpenGLContext.ClearCurrentContext();
                    this.openglctx.CGLContext.Unlock();
                }

                /*      var wBackend = Xwt.Toolkit.CurrentEngine.GetSafeBackend(widget.ParentWindow) as Xwt.Backends.IWindowFrameBackend;
                 *    var win = wBackend.Window as NSWindow;
                 *
                 *    while (win.ParentWindow != null)
                 *    {
                 *        win = win.ParentWindow;
                 *    }
                 * //     win.DidChangeValue+= Win_DidChangeValue;
                 *    win.DidChangeBackingProperties+= Win_DidChangeBackingProperties;
                 *    win.DidChangeScreenProfile+= Win_DidChangeScreenProfile;
                 *    win.DidExpose+= Win_DidExpose;
                 *    win.DidMiniaturize+= Win_DidMiniaturize;
                 *    win.DidChangeScreen+= Win_DidChangeScreen;*/
            }
Example #10
0
        public MyOpenGLView(RectangleF frame, NSOpenGLContext context) : base(frame, context)
        {
            scene = new Scene();

            Resize += delegate {
                scene.ResizeGLScene(Bounds);
            };

            Load += loader;

            UpdateFrame += delegate(object src, FrameEventArgs fea) {
                //Console.WriteLine("update " + fea.Time);
            };

            RenderFrame += delegate(object src, FrameEventArgs fea) {
                scene.DrawGLScene();
            };
        }
Example #11
0
 public NSServoView(ServoView control)
 {
     _servoView = control;
     Object[] attributes =
     {
         NSOpenGLPixelFormatAttribute.DoubleBuffer,
         NSOpenGLPixelFormatAttribute.ClosestPolicy,
         NSOpenGLPixelFormatAttribute.ColorSize,                                 32,
         NSOpenGLPixelFormatAttribute.AlphaSize,                                  8,
         NSOpenGLPixelFormatAttribute.DepthSize,                                 24,
         NSOpenGLPixelFormatAttribute.StencilSize,                                8,
         NSOpenGLPixelFormatAttribute.OpenGLProfile,NSOpenGLProfile.Version3_2Core,
         0
     };
     pixelFormat   = new NSOpenGLPixelFormat(attributes);
     openGLContext = new NSOpenGLContext(pixelFormat, null);
     openGLContext.MakeCurrentContext();
 }
Example #12
0
        public MyOpenGLView(CGRect frame, NSOpenGLContext context) : base(frame)
        {
            var attribs = new object [] {
                NSOpenGLPixelFormatAttribute.Accelerated,
                NSOpenGLPixelFormatAttribute.NoRecovery,
                NSOpenGLPixelFormatAttribute.DoubleBuffer,
                NSOpenGLPixelFormatAttribute.ColorSize, 24,
                NSOpenGLPixelFormatAttribute.DepthSize, 16
            };

            try
            {
                pixelFormat = new NSOpenGLPixelFormat(attribs);
            }
            catch (Exception)
            {
                // Fails on VM because there is no hardware-acceleration
                // https://github.com/xamarin/xamarin-macios/issues/4417
                attribs     = attribs.Skip(1).ToArray();
                pixelFormat = new NSOpenGLPixelFormat(attribs);
            }

            if (pixelFormat == null)
            {
                Console.WriteLine("No OpenGL pixel format");
            }

            // NSOpenGLView does not handle context sharing, so we draw to a custom NSView instead
            openGLContext = new NSOpenGLContext(pixelFormat, context);

            openGLContext.MakeCurrentContext();

            // Synchronize buffer swaps with vertical refresh rate
            openGLContext.SwapInterval = true;

            // Initialize our newly created view.
            InitGL();

            SetupDisplayLink();

            // Look for changes in view size
            // Note, -reshape will not be called automatically on size changes because NSView does not export it to override
            notificationProxy = NSNotificationCenter.DefaultCenter.AddObserver(NSView.GlobalFrameChangedNotification, HandleReshape);
        }
Example #13
0
		public MyOpenGLView (CGRect frame, NSOpenGLContext context) : base(frame, context)
		{
			scene = new Scene();
			
			Resize += delegate {
				scene.ResizeGLScene(Bounds);	
			};
			
			Load += loader;
			
			UpdateFrame += delegate(object src, FrameEventArgs fea) {
				//Console.WriteLine("update " + fea.Time);	
			};
			
			RenderFrame += delegate(object src, FrameEventArgs fea) {
				scene.DrawGLScene();
			};
			
		}
Example #14
0
        public override void Init(IntPtr display, IntPtr window, uint samples, bool vsync, bool sRGB)
        {
            _display = display;
            _window  = window;

            NSOpenGLPixelFormatAttribute[] attrs =
            {
                NSOpenGLPixelFormatAttribute.OpenGLProfile, (NSOpenGLPixelFormatAttribute)NSOpenGLProfile.Version3_2Core,
                NSOpenGLPixelFormatAttribute.DoubleBuffer,
                NSOpenGLPixelFormatAttribute.Accelerated,
                NSOpenGLPixelFormatAttribute.ColorSize,     (NSOpenGLPixelFormatAttribute)24,
                NSOpenGLPixelFormatAttribute.Multisample,
                NSOpenGLPixelFormatAttribute.SampleBuffers, (NSOpenGLPixelFormatAttribute)(samples > 1 ? 1 : 0),
                NSOpenGLPixelFormatAttribute.Samples,       (NSOpenGLPixelFormatAttribute)samples,
                NSOpenGLPixelFormatAttribute.AlphaSize,     (NSOpenGLPixelFormatAttribute)0,
                NSOpenGLPixelFormatAttribute.DepthSize,     (NSOpenGLPixelFormatAttribute)0,
                NSOpenGLPixelFormatAttribute.StencilSize,   (NSOpenGLPixelFormatAttribute)0,
                (NSOpenGLPixelFormatAttribute)0
            };

            NSOpenGLPixelFormat pixelFormat = new NSOpenGLPixelFormat(attrs);

            if (pixelFormat == null)
            {
                attrs[1]    = (NSOpenGLPixelFormatAttribute)NSOpenGLProfile.VersionLegacy;
                pixelFormat = new NSOpenGLPixelFormat(attrs);
            }

            _context = new NSOpenGLContext(pixelFormat, null);
            _context.SwapInterval = vsync ? true : false;
            NSView  view  = (NSView)ObjCRuntime.Runtime.GetNSObject(window);
            CALayer layer = new CALayer();

            view.Layer = layer;

            _context.View = view;
            _context.MakeCurrentContext();

            _device = new RenderDeviceGL();
        }
Example #15
0
                public override NSOpenGLContext GetOpenGLContext(NSOpenGLPixelFormat pixelFormat)
                {
                    this._ctx2?.Dispose();
                    this._ctx2 = null;

                    var r = new NSOpenGLContext(pixelFormat /*GetOpenGLPixelFormat(0)*/, this.owner.openglctx);

                    if (r != null)
                    {
                        var error = CGLEnable(r.CGLContext.Handle, 313); //
                        Trace.Assert(r.CGLContext.Handle != this.owner.openglctx.CGLContext.Handle);
                        {
                            r.CGLContext.Lock();
                            r.MakeCurrentContext();
                            this._ctx2 = new _GraphicsContext(); // register active context handle
                            NSOpenGLContext.ClearCurrentContext();
                            r.CGLContext.Unlock();
                        }
                        return(r);
                    }
                    return(null);
                }
Example #16
0
        public GraphicsContext(MacWindowInfo info)
        {
            var attribs = new object[] {
                NSOpenGLPixelFormatAttribute.Accelerated,
                NSOpenGLPixelFormatAttribute.NoRecovery,
                NSOpenGLPixelFormatAttribute.DoubleBuffer,
                NSOpenGLPixelFormatAttribute.ColorSize, info.ColorSize,
                NSOpenGLPixelFormatAttribute.DepthSize, info.DepthSize,
                NSOpenGLPixelFormatAttribute.StencilSize, info.StencilSize,
                NSOpenGLPixelFormatAttribute.Multisample, info.MultiSample,
            };

            PixelFormat = new NSOpenGLPixelFormat(attribs);

            if (PixelFormat == null)
            {
                Console.WriteLine("No OpenGL pixel format");
            }

            Context = new NSOpenGLContext(PixelFormat, null);
            Context.MakeCurrentContext();
        }
Example #17
0
        public MonoMacGameView(CGRect frame, NSOpenGLContext context) : base(frame)
        {
            var attribs = new object [] {
                NSOpenGLPixelFormatAttribute.NoRecovery,
                NSOpenGLPixelFormatAttribute.DoubleBuffer,
                NSOpenGLPixelFormatAttribute.ColorSize,
                24,
                NSOpenGLPixelFormatAttribute.DepthSize,
                16,
                NSOpenGLPixelFormatAttribute.Accelerated,
            };

            try {
                pixelFormat = new NSOpenGLPixelFormat(attribs);
            } catch (Exception) {
                // Fails on VM because there is no hardware-acceleration -> https://github.com/xamarin/xamarin-macios/issues/4417
                // Therefore, remove 'NSOpenGLPixelFormatAttribute.Accelerated' and try again
                attribs [attribs.Length - 1] = 0;
                pixelFormat = new NSOpenGLPixelFormat(attribs);
            }

            if (pixelFormat == null)
            {
                Console.WriteLine("No OpenGL pixel format");
            }

            // NSOpenGLView does not handle context sharing, so we draw to a custom NSView instead
            openGLContext = new NSOpenGLContext(pixelFormat, context);

            openGLContext.MakeCurrentContext();

            // default the swap interval and displaylink
            SwapInterval         = true;
            DisplaylinkSupported = true;

            // Look for changes in view size
            // Note, -reshape will not be called automatically on size changes because NSView does not export it to override
            notificationProxy = NSNotificationCenter.DefaultCenter.AddObserver(NSView.GlobalFrameChangedNotification, HandleReshape);
        }
Example #18
0
        public UnoGLView(CGRect rect)
            : base(rect)
        {
            var attribs = new object[] {
                NSOpenGLPixelFormatAttribute.NoRecovery,
                NSOpenGLPixelFormatAttribute.DoubleBuffer,
                NSOpenGLPixelFormatAttribute.ColorSize, 24,
                NSOpenGLPixelFormatAttribute.DepthSize, 16
            };

            var pixelFormat = new NSOpenGLPixelFormat(attribs);

            _openGLContext = new NSOpenGLContext(pixelFormat, null);
            _openGLContext.MakeCurrentContext();

            _notificationProxy = NSNotificationCenter.DefaultCenter.AddObserver(NSView.GlobalFrameChangedNotification, HandleReshape);

            WantsBestResolutionOpenGLSurface = true;

            _unoWindow = new MonoMacPlatformWindow(this);
            _unoGC     = new MonoMacGraphicsContext(this);
        }
Example #19
0
        void IXwtRender.CreateForWidgetContext(IRenderer renderer, IRenderOwner rendererimpl, Canvas widget)
        {
            var o        = Xwt.Toolkit.CurrentEngine.GetSafeBackend(widget);
            var wBackend = Xwt.Toolkit.CurrentEngine.GetSafeBackend(widget) as Xwt.Backends.IWidgetBackend;
            //  var nsWindow = wBackend.NativeHandle;l;
            var nsView = (wBackend.NativeWidget as NSView);

            var ctx = new NSOpenGLContext(GetOpenGLPixelFormat(0), null);

            var error = viewwindow.layer.CGLEnable(ctx.CGLContext.Handle, 313); // enable multithread

            viewwindow view = null;

            //    ctx.CGLContext.Lock();
            //   ctx.MakeCurrentContext();

            Application.InvokeAsync(() =>
            {
                view = new viewwindow(renderer, rendererimpl, widget, ViewPostion(widget, wBackend), ctx);

                nsView.AddSubview(view);

                Debug.Assert(!views.ContainsKey(widget));

                views[widget] = new viewinfo()
                {
                    orgview = nsView, /* backend = wBackend,*/ myview = view, widget = widget
                };

                widget.BoundsChanged += sizechanged;
            }).Wait();

            /*    (this as IXwtRender).StartRender(renderer, widget);
             *  view._ctx = new _GraphicsContext();
             *  (this as IXwtRender).EndRender(renderer, widget);*/

            this.QueueOnUI(() => view.initdone.Set());
        }
Example #20
0
 public GameWindow(Game game, RectangleF frame, NSOpenGLContext context) :
     this(game, frame)
 {
 }
Example #21
0
        public Video(IDisposableResource parent, IApplication application, DepthStencilFormats depthStencilFormats, bool vSync)
            : base(parent)
        {
            try
            {
                int depthBit = 16, stencilBit = 0;
                switch (depthStencilFormats)
                {
                case DepthStencilFormats.None:
                    depthBit   = 0;
                    stencilBit = 0;
                    break;

                case DepthStencilFormats.Defualt:
                                                #if iOS || ANDROID || NaCl || RPI
                    depthBit   = 16;
                    stencilBit = 0;
                                                #else
                    depthBit   = 24;
                    stencilBit = 0;
                                                #endif
                    break;

                case DepthStencilFormats.Depth24Stencil8:
                    depthBit   = 24;
                    stencilBit = 8;
                    break;

                case DepthStencilFormats.Depth16:
                    depthBit   = 16;
                    stencilBit = 0;
                    break;

                case DepthStencilFormats.Depth24:
                    depthBit   = 24;
                    stencilBit = 0;
                    break;

                case DepthStencilFormats.Depth32:
                    depthBit   = 32;
                    stencilBit = 0;
                    break;

                default:
                    Debug.ThrowError("Video", "Unsuported DepthStencilFormat type");
                    break;
                }

                currentTextures      = new Texture2D[8];
                currentSamplerStates = new SamplerState[8];

                                #if WIN32 || OSX || LINUX || NaCl
                BackBufferSize   = application.FrameSize;
                this.application = application;
                                #else
                BackBufferSize   = application.FrameSize;
                this.application = application;
                                #endif

                                #if WIN32
                //Get DC
                handle = application.Handle;
                dc     = WGL.GetDC(handle);
                WGL.SwapBuffers(dc);

                //Set BackBuffer format
                WGL.PIXELFORMATDESCRIPTOR pfd = new WGL.PIXELFORMATDESCRIPTOR();
                WGL.ZeroPixelDescriptor(ref pfd);
                pfd.nVersion   = 1;
                pfd.dwFlags    = WGL.PFD_DRAW_TO_WINDOW | WGL.PFD_SUPPORT_OPENGL | WGL.PFD_DOUBLEBUFFER;
                pfd.iPixelType = (byte)WGL.PFD_TYPE_RGBA;
                //pfd.cColorBits = 24;
                //pfd.cAlphaBits = 8;
                pfd.cDepthBits   = (byte)depthBit;
                pfd.cStencilBits = (byte)stencilBit;
                pfd.iLayerType   = (byte)WGL.PFD_MAIN_PLANE;
                unsafe { pfd.nSize = (ushort)sizeof(WGL.PIXELFORMATDESCRIPTOR); }

                int pixelFormatIndex          = WGL.ChoosePixelFormat(dc, ref pfd);
                if (pixelFormatIndex == 0)
                {
                    Debug.ThrowError("Video", "ChoosePixelFormat failed");
                }
                if (WGL.SetPixelFormat(dc, pixelFormatIndex, ref pfd) == 0)
                {
                    Debug.ThrowError("Video", "Failed to set PixelFormat");
                }

                ctx = WGL.CreateContext(dc);
                if (ctx == IntPtr.Zero)
                {
                    Debug.ThrowError("Video", "Failed to create GL context");
                }
                if (WGL.MakeCurrent(dc, ctx) == 0)
                {
                    Debug.ThrowError("Video", "Failed to make GL context current");
                }

                WGL.Init();
                WGL.SwapInterval(vSync ? 1 : 0);                // -1 for smart vsync
                                #endif

                                #if RPI
                unsafe
                {
                    // Init Pi video
                    RaspberryPi.bcm_host_init();

                    const int piDisplay = 0;
                    uint      piWidth = 0, piHeight = 0;
                    if (RaspberryPi.graphics_get_display_size(piDisplay, &piWidth, &piHeight) < 0)
                    {
                        Debug.ThrowError("Video", "Failed to get display size");
                    }
                    Console.WriteLine("piWidth - " + piWidth);
                    Console.WriteLine("piHeight - " + piHeight);
                    BackBufferSize = new Size2((int)piWidth, (int)piHeight);

                    IntPtr dispman_display = RaspberryPi.vc_dispmanx_display_open(piDisplay);
                    if (dispman_display == IntPtr.Zero)
                    {
                        Debug.ThrowError("Video", "Failed: vc_dispmanx_display_open");
                    }

                    IntPtr dispman_update = RaspberryPi.vc_dispmanx_update_start(0);
                    if (dispman_update == IntPtr.Zero)
                    {
                        Debug.ThrowError("Video", "Failed: vc_dispmanx_update_start");
                    }

                    var dstRect = new RaspberryPi.VC_RECT_T()
                    {
                        x      = 0,
                        y      = 0,
                        width  = (int)piWidth,
                        height = (int)piHeight
                    };
                    var srcRect = new RaspberryPi.VC_RECT_T()
                    {
                        x          = 0,
                        y          = 0,
                        width      = ((int)piWidth) << 16,
                            height = ((int)piHeight) << 16
                    };
                    IntPtr dispman_element = RaspberryPi.vc_dispmanx_element_add(dispman_update, dispman_display, 0, &dstRect, IntPtr.Zero, &srcRect, RaspberryPi.DISPMANX_PROTECTION_NONE, IntPtr.Zero, IntPtr.Zero, 0);
                    if (dispman_element == IntPtr.Zero)
                    {
                        Debug.ThrowError("Video", "Failed: vc_dispmanx_element_add");
                    }

                    nativeWindow = new RaspberryPi.DISPMANX_WINDOW_T()
                    {
                        element = dispman_element,
                        width   = (int)piWidth,
                        height  = (int)piHeight
                    };
                    windowHandle = GCHandle.Alloc(nativeWindow, GCHandleType.Pinned);
                    RaspberryPi.vc_dispmanx_update_submit_sync(dispman_update);

                    // Init EGL
                    handle = application.Handle;
                    GL.GetError();                    //NOTE: THIS MUST BE HERE SO THAT libGLES LOADS BEFORE libEGL
                    dc = EGL.GetDisplay(IntPtr.Zero);

                    int major, minor;
                    if (!EGL.Initialize(dc, out major, out minor))
                    {
                        Debug.ThrowError("Video", string.Format("Failed to initialize display connection, Error {0}", EGL.GetError()));
                    }

                    int[] pixelFormat = new int[]
                    {
                        //EGL.RENDERABLE_TYPE, EGL.OPENGL_ES2_BIT,

                        //EGL.RED_SIZE, 5,
                        //EGL.GREEN_SIZE, 6,
                        //EGL.BLUE_SIZE, 5,
                        //EGL.ALPHA_SIZE, 0,//EGL.DONT_CARE,
                        EGL.SURFACE_TYPE, EGL.WINDOW_BIT,

                        EGL.DEPTH_SIZE, depthBit,
                        //EGL.STENCIL_SIZE, stencilBit,//EGL.DONT_CARE,//<<<<<<<<<< Test enabling this!

                        //EGL.SAMPLE_BUFFERS, 0,
                        //EGL.SAMPLES, 0,

                        //EGL.MIN_SWAP_INTERVAL, 0,
                        //EGL.MAX_SWAP_INTERVAL, 1,

                        EGL.NONE,
                    };

                    int num_configs;
                    var configs = new IntPtr();
                    if (EGL.ChooseConfig(dc, pixelFormat, &configs, 1, &num_configs) == 0 || num_configs == 0)
                    {
                        Debug.ThrowError("Video", string.Format("Failed to retrieve GraphicsMode, error {0}", EGL.GetError()));
                    }

                    if (EGL.BindAPI(EGL.OPENGL_ES_API) == 0)
                    {
                        Debug.ThrowError("Video", "Failed to bind GLES API");
                    }
                    ;

                    int[] attrib_list = new int[]
                    {
                        EGL.CONTEXT_CLIENT_VERSION, 2,
                        EGL.NONE
                    };

                    ctx = EGL.CreateContext(dc, configs, IntPtr.Zero, attrib_list);
                    if (ctx == IntPtr.Zero)
                    {
                        Debug.ThrowError("Video", "Failed to create context");
                    }

                    surface = EGL.CreateWindowSurface(dc, configs, windowHandle.AddrOfPinnedObject(), null);
                    //surface = EGL.CreateWindowSurface(dc, configs[0], handle, null);// <<<<<<<<<<<<<<<<<<<<<<<<<<<< used in x11
                    if (surface == IntPtr.Zero)
                    {
                        Debug.ThrowError("Video", "Failed to create window surface");
                    }

                    if (EGL.MakeCurrent(dc, surface, surface, ctx) == 0)
                    {
                        Debug.ThrowError("Video", "Failed to make EGL context current");
                    }
                    //if (EGL.SwapInterval(dc, vSync ? 1 : 0) == 0) Debug.ThrowError("Video", "Failed to set vSync");
                    checkForEGLError();
                }
                                #elif LINUX
                //Get DC
                handle = application.Handle;
                dc     = X11.XOpenDisplay(IntPtr.Zero);
                int screen = X11.XDefaultScreen(dc);

                //Set Pixel format
                int[] attrbs =
                {
                    GLX.RGBA,
                    GLX.DOUBLEBUFFER,// 1,
                    //GLX.RED_SIZE, 8,
                    //GLX.GREEN_SIZE, 8,
                    //GLX.BLUE_SIZE, 8,
                    //GLX.ALPHA_SIZE, 8,
                    GLX.DEPTH_SIZE,depthBit,
                    GLX.NONE
                };

                IntPtr visual = GLX.ChooseVisual(dc, screen, attrbs);
                if (visual == IntPtr.Zero)
                {
                    Debug.ThrowError("Video", "Failed to get visual");
                }

                ctx = GLX.CreateContext(dc, visual, new IntPtr(0), true);
                GLX.MakeCurrent(dc, handle, ctx);

                bool failed = false;
                try { GLX.SwapIntervalSGI(vSync ? 1 : 0); }
                catch { failed = true; }

                if (failed)
                {
                    GLX.Init();
                    GLX.SwapIntervalMesa(vSync ? 1 : 0);
                }
                                #endif

                                #if OSX
                var attribs = new object[]
                {
                    NSOpenGLPixelFormatAttribute.Window,
                    NSOpenGLPixelFormatAttribute.Accelerated,
                    NSOpenGLPixelFormatAttribute.NoRecovery,
                    NSOpenGLPixelFormatAttribute.DoubleBuffer,
                    NSOpenGLPixelFormatAttribute.ColorSize, 24,
                    NSOpenGLPixelFormatAttribute.AlphaSize, 8,
                    NSOpenGLPixelFormatAttribute.DepthSize, depthBit
                };

                var pixelFormat = new NSOpenGLPixelFormat(attribs);
                if (pixelFormat == null)
                {
                    Debug.ThrowError("Video", "NSOpenGLPixelFormat failed");
                }

                NSContext = new NSOpenGLContext(pixelFormat, null);
                if (NSContext == null)
                {
                    Debug.ThrowError("Video", "Failed to create GL context");
                }
                NSContext.MakeCurrentContext();
                NSContext.SwapInterval = vSync;
                NSContext.View         = application.View;
                ctx          = NSContext.CGLContext.Handle;
                OS.NSContext = NSContext;

                /*//Get DC
                 * dc = CGL.DisplayIDToOpenGLDisplayMask(CGL.MainDisplayID());
                 * if (dc == IntPtr.Zero) Debug.ThrowError("Video", "Failed to get DC");
                 *
                 * //Set BackBuffer format
                 * int[] attributes =
                 * {
                 *      //(int)CGL.PixelFormatAttribute.kCGLPFADoubleBuffer,
                 *      //(int)CGL.PixelFormatAttribute.kCGLPFADisplayMask, dc.ToInt32(),
                 *      //(int)CGL.PixelFormatAttribute.kCGLPFAWindow,
                 *      (int)CGL.PixelFormatAttribute.kCGLPFAAccelerated,
                 *      (int)CGL.PixelFormatAttribute.kCGLPFAColorSize, 32,
                 *      (int)CGL.PixelFormatAttribute.kCGLPFADepthSize, depthBit,
                 *      0
                 * };
                 *
                 * IntPtr pixelFormat = IntPtr.Zero;
                 * int pixelFormatCount = 0;
                 * int error = CGL.ChoosePixelFormat(attributes, ref pixelFormat, ref pixelFormatCount);
                 * if (error != 0 || pixelFormat == IntPtr.Zero)
                 * {
                 *      Debug.ThrowError("Video", "ChoosePixelFormat failed");
                 * }
                 *
                 * error = CGL.CreateContext(pixelFormat, IntPtr.Zero, ref ctx);
                 * if (error != 0 || ctx == IntPtr.Zero)
                 * {
                 *      Debug.ThrowError("Video", "Failed to create GL fullscreen context");
                 * }
                 *
                 * CGL.SetCurrentContext(ctx);*/
                                #endif

                                #if iOS
                // GL will be created by the Application
                                #endif

                                #if ANDROID
                // GL will be created by the Application
                                #endif

                                #if NaCl
                var   frame   = application.FrameSize;
                int[] attribs =
                {
                    (int)PPAPI.Graphics3DAttrib.PP_GRAPHICS3DATTRIB_ALPHA_SIZE,                8,// Try to disable this to see if it gets rid of alpha blending presintation issues
                    (int)PPAPI.Graphics3DAttrib.PP_GRAPHICS3DATTRIB_DEPTH_SIZE,     depthBit,
                    (int)PPAPI.Graphics3DAttrib.PP_GRAPHICS3DATTRIB_STENCIL_SIZE,   stencilBit,
                    (int)PPAPI.Graphics3DAttrib.PP_GRAPHICS3DATTRIB_SAMPLES,                   0,
                    (int)PPAPI.Graphics3DAttrib.PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS,            0,
                    (int)PPAPI.Graphics3DAttrib.PP_GRAPHICS3DATTRIB_WIDTH,          frame.Width,
                    (int)PPAPI.Graphics3DAttrib.PP_GRAPHICS3DATTRIB_HEIGHT,         frame.Height,
                    (int)PPAPI.Graphics3DAttrib.PP_GRAPHICS3DATTRIB_NONE
                };

                graphics = OS.GetGraphics();
                if (graphics == IntPtr.Zero)
                {
                    Debug.ThrowError("Video", "Failed to get Graphics object");
                }

                IntPtr pbbInstance = OS.GetPBBInstance();
                if (graphics == IntPtr.Zero)
                {
                    Debug.ThrowError("Video", "Failed to get PBBInstance object");
                }

                IntPtr gles = OS.GetGlES();
                if (gles == IntPtr.Zero)
                {
                    Debug.ThrowError("Video", "Failed to get GLES object");
                }

                context = PPAPI.InitOpenGL(OS.GetInstance(), graphics, pbbInstance, attribs, gles);
                if (context == 0)
                {
                    Debug.ThrowError("Video", "Failed to create GL context");
                }

                PPAPI.SetCurrentContextPPAPI(context);
                PPAPI.StartSwapBufferLoop(graphics, context);
                                #endif

                checkForError();

                //Setup defualt OpenGL characteristics
                GL.FrontFace(GL.CCW);

                // Get Caps
                Caps = new Caps();
                Caps.MaxShaderVersion = ShaderVersions.Unknown;
                unsafe
                {
                    int max = 0;
                    GL.GetIntegerv(GL.MAX_TEXTURE_SIZE, &max);
                    Caps.MaxTextureSize = max;

                    // SEEMS TO MESS UP ON OSX
                    //GL.GetIntegerv(GL.MAX_VERTEX_UNIFORM_VECTORS, &max);
                    //Caps.MaxVertexConstants = max;

                    //GL.GetIntegerv(GL.MAX_FRAGMENT_UNIFORM_VECTORS, &max);
                    //Caps.MaxPixelConstants = max;

                                        #if RPI
                    byte *shaderVersionPtr = null;
                                        #else
                    byte *shaderVersionPtr = GL.GetString(GL.SHADING_LANGUAGE_VERSION);
                                        #endif
                    string shaderVersion = "";
                    while (shaderVersionPtr != null && shaderVersionPtr[0] != 0)
                    {
                        shaderVersion += (char)shaderVersionPtr[0];
                        shaderVersionPtr++;
                    }

                                        #if iOS || RPI
                    shaderVersion = "1.0";
                                        #elif ANDROID
                    shaderVersion = shaderVersion.Substring(shaderVersion.Length - 4, 4);
                                        #elif NaCl
                    shaderVersion = shaderVersion.Substring(18, 3);
                                        #else
                    shaderVersion = shaderVersion.Substring(0, 4);
                                        #endif
                    float shaderValue = Convert.ToSingle(shaderVersion);

                    Caps.Version = Versions.GL1;
                    FileTag      = "";
                                        #if iOS || ANDROID || NaCl || RPI
                    if (shaderValue >= 1.0f)
                    {
                        Caps.Version          = Versions.GL2;
                        FileTag               = "GLES2_";
                        Caps.MaxShaderVersion = ShaderVersions.GLSL_1_00;
                    }
                                        #else
                    if (shaderValue >= 1.1f)
                    {
                        Caps.Version          = Versions.GL2;
                        FileTag               = "GL2_";
                        Caps.MaxShaderVersion = ShaderVersions.GLSL_1_10;
                    }
                    if (shaderValue >= 1.2f)
                    {
                        Caps.Version          = Versions.GL2;
                        FileTag               = "GL2_";
                        Caps.MaxShaderVersion = ShaderVersions.GLSL_1_20;
                    }
                    // GL3 not supported in GLSL 1.30 becuase 'gl_InstanceID' only exists in GLSL 1.40

                    /*if (shaderValue >= 1.3f)
                     * {
                     *      Caps.Version = Versions.GL3;
                     *      IShader.FileTag = "GL3_";
                     *      Caps.MaxShaderVersion = ShaderVersions.GLSL_1_30;
                     * }*/
                    if (shaderValue >= 1.4f)
                    {
                        Caps.Version          = Versions.GL3;
                        FileTag               = "GL3_";
                        Caps.MaxShaderVersion = ShaderVersions.GLSL_1_40;
                    }
                    if (shaderValue >= 1.5f)
                    {
                        Caps.Version          = Versions.GL3;
                        FileTag               = "GL3_";
                        Caps.MaxShaderVersion = ShaderVersions.GLSL_1_50;
                    }
                    if (shaderValue >= 3.3f)
                    {
                        Caps.Version          = Versions.GL3;
                        FileTag               = "GL3_";
                        Caps.MaxShaderVersion = ShaderVersions.GLSL_3_30;
                    }
                    //if (shaderValue >= 4.0f) Caps.MaxShaderVersion = ShaderVersions.GLSL_4_00;
                    //if (shaderValue >= 4.1f) Caps.MaxShaderVersion = ShaderVersions.GLSL_4_10;
                    //if (shaderValue >= 4.2f) Caps.MaxShaderVersion = ShaderVersions.GLSL_4_20;
                                        #endif

                    if (Caps.Version == Versions.GL1)
                    {
                        Debug.ThrowError("Video", "OpenGL 2 or higher is required.\nAre your video card drivers up to date?");
                    }

                    byte * extensionsPtr   = GL.GetString(GL.EXTENSIONS);
                    string extensionValues = "";
                    while (extensionsPtr != null && extensionsPtr[0] != 0)
                    {
                        extensionValues += (char)extensionsPtr[0];
                        extensionsPtr++;
                    }

                    var extensions = extensionValues.Split(' ');
                    foreach (var ext in extensions)
                    {
                        switch (ext)
                        {
                        case "GL_instanced_arrays": Caps.HardwareInstancing = true; break;

                        case "GL_ARB_instanced_arrays": Caps.HardwareInstancing = true; break;

                        case "GL_EXT_texture_compression_s3tc": Caps.TextureCompression_S3TC = true; break;

                        case "GL_AMD_compressed_ATC_texture": Caps.TextureCompression_ATC = true; break;

                        case "GL_ATI_texture_compression_atitc": Caps.TextureCompression_ATC = true; break;

                        case "GL_IMG_texture_compression_pvrtc": Caps.TextureCompression_PVR = true; break;
                        }
                        //Console.WriteLine(ext);
                    }
                }

                checkForError();

                // Init Ext
                GL.Init();
            }
            catch (Exception e)
            {
                Dispose();
                throw e;
            }
        }
Example #22
0
        void Prepare()
        {
            Console.WriteLine("Prepare");

            // The GL COntext must be active for these functions to have an effect
            glContext = this.OpenGLContext;
            glContext.MakeCurrentContext();

            // Configure the view
            GL.ShadeModel(ShadingModel.Smooth);
            GL.Enable(EnableCap.Lighting);
            GL.Enable(EnableCap.DepthTest);

            // Add some ambient lighting
            float[] ambient = {0.2f, 0.2f, 0.2f, 1.0f};
            GL.LightModel(LightModelParameter.LightModelAmbient, ambient);

            // Initialize the light
            float[] diffuse = {1.0f, 1.0f, 1.0f, 1.0f};
            GL.Light(LightName.Light0, LightParameter.Diffuse, diffuse);
            // and switch it on
            GL.Enable((EnableCap)LightName.Light0);

            // Set the properties of the material under ambient light
            float[] mat = {0.1f, 0.1f, 0.7f, 1.0f};
            GL.Material(MaterialFace.Front, MaterialParameter.Ambient, mat);

            GL.Material(MaterialFace.Front, MaterialParameter.Diffuse, mat);
        }
Example #23
0
 public void Flush(NSOpenGLContext ctx)
 {
     ctx.FlushBuffer();
 }
Example #24
0
 public GameWindow(RectangleF frame, NSOpenGLContext context) : base(frame)
 {
 }
Example #25
0
                public override void DrawInCGLContext(CGLContext glContext, CGLPixelFormat pixelFormat, double timeInterval, ref CVTimeStamp timeStamp)
                {
                    try
                    {
                        long now = DateTime.Now.Ticks;

                        /*     if (lastupdate != -1 && lastupdate - now > 1000000)
                         *   {
                         *       // this.owner.renderer.SkipRender(lastupdate - now-400000);
                         *   }
                         *
                         *   Interlocked.Exchange(ref lastupdate, DateTime.Now.Ticks);
                         *   var now = DateTime.Now.Ticks;*/

                        long time;

                        if (lastupdate == -1)
                        {
                            time          = lastupdate = this.timebase;
                            this.timebase = now - this.timebase;
                            //      Log.Verbose($"viewtime={0} first");
                        }
                        else
                        {
                            time = now - this.timebase;

                            if ((time - lastupdate) > 25000000)
                            {
                                this.timebase = now - lastupdate;
                                time          = lastupdate;
                                Log.Verbose($"viewtime={time} reset");
                            }
                        }
                        //       Log.Verbose($"viewtime={time}");

                        lastupdate = time;
                        if (this.owner.renderer.preparerender(null, time, false))
                        {
                            //  lock (this.owner)
                            {
                                using (this.owner.render.GetDrawLock(true))
                                {
                                    this.OpenGLContext.CGLContext.Lock();
                                    this.OpenGLContext.MakeCurrentContext();

                                    try
                                    {
                                        var r = new rectangle(0, 0, Convert.ToInt32(this.owner.Bounds.Width), Convert.ToInt32(this.owner.Bounds.Height));

                                        GL.Viewport(r.x, r.y, r.width, r.height);

                                        this.owner.renderer.render(null, time, r);
                                    }
                                    catch (Exception e)
                                    {
                                        //    Log.LogException(e);
                                    }

                                    this.OpenGLContext.FlushBuffer();
                                    NSOpenGLContext.ClearCurrentContext();
                                    this.OpenGLContext.CGLContext.Unlock();
                                }
                            }
                        }
                        //         Log.Verbose($"viewtime-done={time}");
                    }
                    catch (Exception e)
                    {
                    }
                }