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; }
/// <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; }
private void runGlThread(object args) { var inputs = (Tuple<CancellationToken>) args; var cancelToken = inputs.Item1; var gl = new OpenGL(); int localRenderWidth = 1; int localRenderHeight = 1; if (!createRenderContext(gl, localRenderWidth, localRenderHeight)) { //TODO better error handling here Console.WriteLine("*** Unable to create OpenGL Render Context"); return; } uint activeVaoHandle; uint activeGlProgramHandle; initGLObjects(gl, out activeVaoHandle, out activeGlProgramHandle); ActiveProgramValues localActiveProgramValues = null; while (!cancelToken.IsCancellationRequested) { bool resizeRenderContext; lock (this) { if (!ReferenceEquals(localActiveProgramValues, activeProgramValues)) { localActiveProgramValues = activeProgramValues; if (localActiveProgramValues.Valid) { linkProgram(gl, localActiveProgramValues, activeGlProgramHandle); } else { // Leave the old program running. This prevents the user from seeing // a black screen while they are in the process of modifying a shader. } } resizeRenderContext = localRenderWidth != renderWidth || localRenderHeight != renderHeight; localRenderWidth = renderWidth; localRenderHeight = renderHeight; } if (resizeRenderContext) { localActiveProgramValues = null; deleteGlObjects(gl, activeVaoHandle, activeGlProgramHandle); if (!createRenderContext(gl, localRenderWidth, localRenderHeight)) { //TODO better error handling here Console.WriteLine("*** Unable to resize OpenGL Render Context"); return; } initGLObjects(gl, out activeVaoHandle, out activeGlProgramHandle); } gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT); gl.BindVertexArray(activeVaoHandle); gl.UseProgram(activeGlProgramHandle); gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3); // do some other stuff while the image is rendering, to give it a chance to finish ShaderCompiler.ValidateShaders(gl); var provider = gl.RenderContextProvider as FBORenderContextProvider; Debug.Assert(provider != null, "Render context provider is not an FBO renderer"); //TODO this call to blit will probably block. Find a better way to copy the image to CPU memory. gl.Blit(IntPtr.Zero); var hBitmap = provider.InternalDIBSection.HBitmap; if (hBitmap != IntPtr.Zero) { var bitmap = GetFormattedBitmapSource(hBitmap); // the bitmap needs to be frozen in order to share it between threads bitmap.Freeze(); ImageRendered(bitmap); } } deleteGlObjects(gl, activeVaoHandle, activeGlProgramHandle); }