public void Connect(string clientIp, string serverIp, string connectionType) { _isConnected = true; _rigidBodies = new List <RigidBody>() { new RigidBody() { Name = "kinect", ID = 1 }, new RigidBody() { Name = "leapmotion", ID = 2 } }; // call event handler that descriptions have changed OnDataDescriptionsChanged?.Invoke(); var leapMotionRotation = Quaternion.CreateFromAxisAngle(Vector3.UnitX, 30f.ToRadians()); // call frame available event handler OnFrameReady?.Invoke(new FrameOfMocapData() { MarkerSets = new[] { // kinect Mock.MockMarkerSetData("kinect", new[] { Vector3.Zero, new Vector3(3, 0, 3), new Vector3(0, 0, 3) }), // leap motion Mock.MockMarkerSetData("leapmotion", new[] { Vector3.Zero, new Vector3(6, 6, 3), new Vector3(6, 0, 3) }) }, nMarkerSets = 2, RigidBodies = new[] { new RigidBodyData() { ID = 1, x = 1, y = 0, z = 2, qx = 0, qy = 0, qz = 0, qw = 1 }, new RigidBodyData() { ID = 2, x = 4, y = 2, z = 2, qx = leapMotionRotation.X, qy = leapMotionRotation.Y, qz = leapMotionRotation.Z, qw = leapMotionRotation.W } }, nRigidBodies = 2 }); }
private void EndFrame( ) { Performance.EndFrame( ); OnFrameReady?.Invoke(this, new EventArgs( )); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); }
public override void Render(Scene scene, Camera camera) { scene.Preprocess(); if (camera is LensCamera lensCamera && lensCamera.AutoFocus) { var ray = lensCamera.GetRay(0.5f, 0.5f); var hitInfo = new HitInfo(); var hit = scene.HitTest(ray, ref hitInfo, 0.001f, float.PositiveInfinity); if (hit) { lensCamera.FocusDistance = hitInfo.Distance; } else { lensCamera.FocusDistance = 100000f; } } int width = Resolution; int height = (int)(width / camera.AspectRatio); var image = new Texture(width, height); AbstractSampler <Vector2> sampler = new ThreadSafeSampler <Vector2>(Sampling, Samples); for (int k = 0; k < Samples; k++) { try { Parallel.For(0, width, new ParallelOptions { CancellationToken = CancellationToken }, i => { for (int j = 0; j < height; j++) { var sample = sampler.GetSample(k); float u = (i + sample.X) / (width - 1); float v = (j + sample.Y) / (height - 1); Ray ray = camera.GetRay(u, v); var shade = Shade(ray, scene, MaxDepth); image.Bloom(shade, i, j, (int)shade.GetBrightness(), Bloom); image[i, j] += shade; } }); } catch (Exception e) { Log.Warn(e); } if (CancellationToken.IsCancellationRequested) { return; } if (k % SamplesRenderStep == 0 || k == Samples - 1) { var output = new Texture(image); output.Process(c => (c / (k + 1)).Clamp()); if (GammaCorrection) { output.AutoGammaCorrect(); } var percentage = (k + 1) * 100 / Samples; OnFrameReady?.Invoke(percentage, output); } } }
public void StartCapture() { _run = true; var factory = new Factory1(); // SharpDX DXGI Factory var adapter = factory.GetAdapter1(0); // Get first video card (Adapter) var device = new SharpDX.Direct3D11.Device(adapter); // Get D3D11 device from adapter // Get frontal output buffer of the adapter var output = adapter.GetOutput(0); var output1 = output.QueryInterface <Output1>(); // Get video card width/height of the desktop to capture (The resolution of your screen) int screen_width = output.Description.DesktopBounds.Right; int screen_height = output.Description.DesktopBounds.Bottom; // Build a 2D texture to use as staging texture that is accessible by the CPU var texture_description = new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.Read, BindFlags = BindFlags.None, Format = Format.B8G8R8A8_UNorm, Width = screen_width, Height = screen_height, OptionFlags = ResourceOptionFlags.None, MipLevels = 1, ArraySize = 1, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Staging }; var texture_screen = new Texture2D(device, texture_description); Task.Factory.StartNew(() => { using (var output_clone = output1.DuplicateOutput(device)) { // Preload some resources and objects before cycling SharpDX.DXGI.Resource output_frame_resource; OutputDuplicateFrameInformation output_frame_info; var screen_capture_rect = new Rectangle(0, 0, screen_width, screen_height); while (_run) { //int start = DateTime.Now.Millisecond; try { // Try to get duplicated frame within given time is ms output_clone.AcquireNextFrame(5, out output_frame_info, out output_frame_resource); // Copy resource into memory that can be accessed by the CPU using (var texture_screen_acquired = output_frame_resource.QueryInterface <Texture2D>()) device.ImmediateContext.CopyResource(texture_screen_acquired, texture_screen); // Get the desktop capture texture var map_src = device.ImmediateContext.MapSubresource(texture_screen, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None); // Create Drawing.Bitmap using (var bitmap_screen = new Bitmap(screen_width, screen_height, PixelFormat.Format32bppArgb)) { // Copy pixels from screen capture Texture to GDI bitmap var map_dest = bitmap_screen.LockBits( screen_capture_rect, ImageLockMode.WriteOnly, bitmap_screen.PixelFormat ); var pointer_src = map_src.DataPointer; var pointer_dest = map_dest.Scan0; for (int y = 0; y < screen_height; y++) { Utilities.CopyMemory(pointer_dest, pointer_src, screen_width * 4); // Copy a single line (4 bytes per pixel). // Advance pointers pointer_src = IntPtr.Add(pointer_src, map_src.RowPitch); pointer_dest = IntPtr.Add(pointer_dest, map_dest.Stride); } // Release source and destination locks bitmap_screen.UnlockBits(map_dest); device.ImmediateContext.UnmapSubresource(texture_screen, 0); using (MemoryStream ms = new MemoryStream()) { bitmap_screen.Save(ms, ImageFormat.Bmp); // Save bitmap pixels in a stream OnFrameReady?.Invoke(this, ms.ToArray()); // Return bitmap pixels from a stream to the caller _init = true; } } output_frame_resource.Dispose(); output_clone.ReleaseFrame(); } catch (Exception exception) { if (exception is SharpDXException e) { if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code) { Trace.TraceError(e.Message); Trace.TraceError(e.StackTrace); } } else { throw; } } //int end = DateTime.Now.Millisecond; //if((end-start)<15) System.Threading.Thread.Sleep(16-(end-start)); // 60 FPS cap attempt //Console.WriteLine("--TIME_ELAPSED="+(end-start)+"ms"); } } }); while (!_init) { System.Threading.Thread.Sleep(100); } }