Ejemplo n.º 1
0
        private Image Output()
        {
            gl.Flush();
            gl.Blit(IntPtr.Zero);
            var provider = gl.RenderContextProvider as FBORenderContextProvider;
            //  TODO: We have to remove the alpha channel - for some reason it comes out as 0.0
            //  meaning the drawing comes out transparent.
            var newFormatedBitmapSource = new FormatConvertedBitmap();

            if (provider == null)
            {
                return(null);
            }
            newFormatedBitmapSource.BeginInit();
            newFormatedBitmapSource.Source            = BitmapConversion.HBitmapToBitmapSource(provider.InternalDIBSection.HBitmap);
            newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24;
            newFormatedBitmapSource.EndInit();

            var           outStream = new MemoryStream();
            BitmapEncoder enc       = new BmpBitmapEncoder();

            enc.Frames.Add(BitmapFrame.Create(newFormatedBitmapSource));
            enc.Save(outStream);
            //Console.ReadLine();

            //gl.Flush();
            //Console.ReadLine();
            return(new Bitmap(outStream));
        }
Ejemplo n.º 2
0
        public void Render()
        {
            _gl.MakeCurrent();
            _gl.ClearColor(0.2f, 0.2f, 0.2f, 0);
            _gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            _gl.LoadIdentity();

            if (RenderScene != null)
            {
                RenderScene(this, new SimpleOpenGlEventArgs
                {
                    OpenGl = _gl
                });
            }

            _gl.Blit(IntPtr.Zero);

            var fboRenderContextProvider = (FBORenderContextProvider)_gl.RenderContextProvider;

            var formatConvertedBitmap = new FormatConvertedBitmap();

            formatConvertedBitmap.BeginInit();
            formatConvertedBitmap.Source            = BitmapConversion.HBitmapToBitmapSource(fboRenderContextProvider.InternalDIBSection.HBitmap);
            formatConvertedBitmap.DestinationFormat = PixelFormats.Rgb24;
            formatConvertedBitmap.EndInit();

            OpenGlImage.Source = formatConvertedBitmap;
        }
Ejemplo n.º 3
0
        public void Draw()
        {
            if (this.IsDisposed)
            {
                return;
            }

            if (null == gl)
            {
                return;
            }

            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _texture[0]);

            gl.Begin(OpenGL.GL_QUADS);

            gl.TexCoord(0.0, 0.0); gl.Vertex(-1.0, -1.0);
            gl.TexCoord(1.0, 0.0); gl.Vertex(+1.0, -1.0);
            gl.TexCoord(1.0, 1.0); gl.Vertex(+1.0, +1.0);
            gl.TexCoord(0.0, 1.0); gl.Vertex(-1.0, +1.0);
            gl.End();

            using (Graphics g = CreateGraphics())
            {
                var hdc = g.GetHdc();
                gl.Blit(hdc);
                g.ReleaseHdc(hdc);
            }
        }
Ejemplo n.º 4
0
        protected override void OnPaint(PaintEventArgs e)
        {
            //  Start the stopwatch so that we can time the rendering.
            stopwatch.Restart();

            //  Only draw if we're not in the designer
            if (!DesignMode)
            {
                OpenGL.MakeCurrent();

                DoOpenGLDraw(new RenderEventArgs(e.Graphics));

                //  Draw the FPS.
                if (DrawFPS)
                {
                    OpenGL.DrawText(5, 5, 1.0f, 0.0f, 0.0f, "Courier New", 12.0f,
                                    string.Format("Draw Time: {0:0.0000} ms ~ {1:0.0} FPS", frameTime, 1000.0 / frameTime));
                    OpenGL.Flush();
                }

                //	Blit our offscreen bitmap.
                IntPtr handleDeviceContext = e.Graphics.GetHdc();
                OpenGL.Blit(handleDeviceContext);
                e.Graphics.ReleaseHdc(handleDeviceContext);
            }

            //  Stop the stopwatch.
            stopwatch.Stop();

            //  Store the frame time.
            frameTime = stopwatch.Elapsed.TotalMilliseconds;
        }
Ejemplo n.º 5
0
        public override ImageSource Render()
        {
            GL.MakeCurrent();

            GL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
            GL.ClearColor(1f, 0f, 0f, 0f);

            _postProcesser.Capture(() =>
            {
                GL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
                _backgroundRenderer.Render();
                _octreeRenderer.Render();
            });

            _postProcesser.Render();

            GL.Blit(IntPtr.Zero);
            var provider = GL.RenderContextProvider as FBORenderContextProvider;

            if (provider == null)
            {
                return(null);
            }

            var newFormatedBitmapSource = new FormatConvertedBitmap();

            newFormatedBitmapSource.BeginInit();
            newFormatedBitmapSource.Source            = BitmapConversion.HBitmapToBitmapSource(provider.InternalDIBSection.HBitmap);
            newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24;
            newFormatedBitmapSource.EndInit();
            return(newFormatedBitmapSource);
        }
Ejemplo n.º 6
0
 public override void Flush()
 {
     if (_drawn && Canvas.Sketch.Graphics != null)
     {
         IntPtr dc = Canvas.Sketch.Graphics.GetHdc();
         _openGL.Flush();
         _openGL.Blit(dc);
         Canvas.Sketch.Graphics.ReleaseHdc(dc);
     }
 }
Ejemplo n.º 7
0
 public override void EndDraw()
 {
     if (Canvas.Sketch.Graphics != null)
     {
         IntPtr dc = Canvas.Sketch.Graphics.GetHdc();
         _openGL.Blit(dc);
         _openGL.PopMatrix();
         Canvas.Sketch.Graphics.ReleaseHdc(dc);
     }
 }
Ejemplo n.º 8
0
        private void _renderTimer_Tick(object sender, EventArgs e)
        {
            GL.MakeCurrent();

            if (Render != null)
            {
                Render.Invoke(GL);
            }

            GL.Blit(IntPtr.Zero);
            UpdateImage();
        }
Ejemplo n.º 9
0
        protected override void OnPaint(PaintEventArgs e)
        {
            //  Start the stopwatch so that we can time the rendering.
            stopwatch.Restart();

            //	Make sure it's our instance of openSharpGL that's active.
            OpenGL.MakeCurrent();

            //	Do the scene drawing.
            Scene.Draw();

            if (this.CameraType == CameraTypes.Ortho)
            {
                // Redraw model container's bounding box so that it appears in front of models.
                // TODO: this is not needed in ECameraType.Perspecitive mode. fix this.
                //this.modelContainer.Render(this.OpenGL, SceneGraph.Core.RenderMode.Render);
            }

            UIScene.Draw();

            //	If there is a draw handler, then call it.
            DoOpenGLDraw(new RenderEventArgs(e.Graphics));

            //  Draw the FPS.
            if (DrawFPS)
            {
                OpenGL.DrawText(5, 5, 1.0f, 0.0f, 0.0f, "Courier New", 12.0f,
                                string.Format("Draw Time: {0:0.0000} ms ~ {1:0.0} FPS", frameTime, 1000.0 / frameTime));
                OpenGL.Flush();
            }

            //	Blit our offscreen bitmap.
            IntPtr handleDeviceContext = e.Graphics.GetHdc();

            OpenGL.Blit(handleDeviceContext);
            e.Graphics.ReleaseHdc(handleDeviceContext);

            //	If's there's a GDI draw handler, then call it.
            DoGDIDraw(new RenderEventArgs(e.Graphics));

            //  Stop the stopwatch.
            stopwatch.Stop();

            //  Store the frame time.
            frameTime = stopwatch.Elapsed.TotalMilliseconds;
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Renders to the specified graphics.
        /// </summary>
        /// <param name="graphics">The graphics to render to.</param>
        protected void Render(Graphics graphics)
        {
            //  Start the stopwatch so that we can time the rendering.
            stopwatch.Restart();

            //	Make sure it's our instance of openSharpGL that's active.
            OpenGL.MakeCurrent();

            //	If there is a draw handler, then call it.
            DoOpenGLDraw(new RenderEventArgs(graphics));

            //  Draw the FPS.
            if (DrawFPS)
            {
                OpenGL.DrawText(5, 5, 1.0f, 0.0f, 0.0f, "Courier New", 12.0f,
                                string.Format("Draw Time: {0:0.0000} ms ~ {1:0.0} FPS", frameTime, 1000.0 / frameTime));
                OpenGL.Flush();
            }

            //	Blit our offscreen bitmap.
            var handleDeviceContext = graphics.GetHdc();

            OpenGL.Blit(handleDeviceContext);
            graphics.ReleaseHdc(handleDeviceContext);

            //  Perform GDI drawing.
            if (OpenGL.RenderContextProvider != null && OpenGL.RenderContextProvider.GDIDrawingEnabled)
            {
                DoGDIDraw(new RenderEventArgs(graphics));
            }

            //  Stop the stopwatch.
            stopwatch.Stop();

            //  Store the frame time.
            frameTime = stopwatch.Elapsed.TotalMilliseconds;
        }
Ejemplo n.º 11
0
        protected override void OnPaint(PaintEventArgs e)
        {
            //  Start the stopwatch so that we can time the rendering.
            stopwatch.Restart();

            //	Make sure it's our instance of openSharpGL that's active.
            OpenGL.MakeCurrent();

            //	Do the scene drawing.
            scene.Draw();

            //	If there is a draw handler, then call it.
            DoOpenGLDraw(new RenderEventArgs(e.Graphics));

            //  Draw the FPS.
            if (DrawFPS)
            {
                OpenGL.DrawText(5, 5, 1.0f, 0.0f, 0.0f, "Courier New", 12.0f,
                                string.Format("Draw Time: {0:0.0000} ms ~ {1:0.0} FPS", frameTime, 1000.0 / frameTime));
                OpenGL.Flush();
            }

            //	Blit our offscreen bitmap.
            IntPtr handleDeviceContext = e.Graphics.GetHdc();

            OpenGL.Blit(handleDeviceContext);
            e.Graphics.ReleaseHdc(handleDeviceContext);

            //	If's there's a GDI draw handler, then call it.
            DoGDIDraw(new RenderEventArgs(e.Graphics));

            //  Stop the stopwatch.
            stopwatch.Stop();

            //  Store the frame time.
            frameTime = stopwatch.Elapsed.TotalMilliseconds;
        }
Ejemplo n.º 12
0
        private void RenderLoop()
        {
            var queuecontext = glContext as QueueSynchronizationContext;

            SynchronizationContext.SetSynchronizationContext(glContext);

            int fps_count = 0;
            int fps_ticks = 0;
            int pre_ticks = Environment.TickCount & Int32.MaxValue;
            var updatearg = new OGLDeviceUpdateArgs(gl);

            int width = -1, height = -1; IntPtr handle = IntPtr.Zero;

            uiContext.Send(d => {   // WindowsFormsSynchronizationContext обрабатывает синхронные сообщения в
                width  = Width;     // общем обработчике сообщений окна (WndProc). Это означает, что главный
                height = Height;    // поток обратает синхронное сообщение лишь после того как завершится
                handle = Handle;    // выполнение текущего кода. Таким образом в неявном виде тут происходит
            }, null);               // синхронизация потоков - поток ожидаяет выполнения OnMainWindowLoad()

            // Создание и работа с устройством OpenGL должны осуществляться в одном потоке
            gl.Create(_glVersion, _glRenderContextType, width, height, 32, handle);
            _glVersion = ((RenderContextProvider)gl.RenderContextProvider).CreatedOpenGLVersion;

            // Установка наиболее распространенных стилей
            gl.ShadeModel(OpenGL.GL_SMOOTH);

            gl.ClearColor(0.5f, 0.5f, 0.5f, 0.0f);
            gl.ClearDepth(1.0f);
            gl.Enable(OpenGL.GL_DEPTH_TEST);
            gl.DepthFunc(OpenGL.GL_LEQUAL);

            gl.Enable(OpenGL.GL_BLEND);                                       // Правило наложения (смешивания) цветов
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); // result_color = src * src.a + dst * (1 - src.a)

            Initialize(Width, Height, false);                                 // задаем проецирование
            OpenGL.MakeCurrent();

            if (Inited != null)
            {
                Inited(this, new OpenGLEventArgs(gl));
            }

            while (true)
            {
                queuecontext.Run();
                if (ogl_resize_request_param != -1)
                {
                    Initialize(ogl_resize_request_param & 0xFFFF, ogl_resize_request_param >> 16, false);
                    ogl_resize_request_param = -1;
                }

                var cur_ticks = Environment.TickCount & Int32.MaxValue;
                if (cur_ticks - fps_ticks > 1000)
                {
                    uiContext.Post(s => {
                        if (ParentForm != null)
                        {
                            ParentForm.Text = String.Format("FPS: {0}", (int)s);
                        }
                    }, fps_count);
                    fps_count = 1;
                    fps_ticks = cur_ticks;
                }
                else
                {
                    ++fps_count;
                }

                lock (LockObj) {
                    if (DeviceUpdate != null)
                    {
                        DeviceUpdate(this, updatearg.Update(cur_ticks - pre_ticks));

                        gl.Flush();
                        OpenGL.Blit(handle);    // Для ContextType.NativeWindow тут выполняется переключение буфферов,
                        pre_ticks = cur_ticks;  // сам же параметр handle при этом вообще никак и ни где не используется.
                    }
                }

                Thread.Sleep(1);
            }
        }