Esempio n. 1
0
 public InputManager(KeyboardDevice keyboard)
 {
     _config = new CameraConfig(new Vector3d(10, 0, 0), new Vector3d(-1, 0, 0), new Vector3d(0, 1, 0), 1, 0, 1);
     _keyboard = keyboard;
     _bindings = new Dictionary<Key, Action<float>>
         {
             {Key.W, dt => _config.Position += _config.Lookat * dt * _config.MoveSpeed},
             {Key.S, dt => _config.Position -= _config.Lookat * dt * _config.MoveSpeed},
             {Key.A, dt => _config.Position += Vector3d.Cross(_config.Up, _config.Lookat) * dt * _config.MoveSpeed},
             {Key.D, dt => _config.Position -= Vector3d.Cross(_config.Up, _config.Lookat) * dt * _config.MoveSpeed},
             {Key.ShiftLeft, dt => _config.Position += _config.Up * dt * _config.MoveSpeed},
             {Key.Space, dt => _config.Position -= _config.Up * dt * _config.MoveSpeed},
             {Key.Q, dt => _config.Up = Vector3d.Transform(_config.Up, Matrix4d.CreateFromAxisAngle(_config.Lookat, TurnSpeed * dt))},
             {Key.E, dt => _config.Up = Vector3d.Transform(_config.Up, Matrix4d.CreateFromAxisAngle(_config.Lookat, -TurnSpeed * dt))},
             {Key.Left, dt => _config.Lookat = Vector3d.Transform(_config.Lookat, Matrix4d.CreateFromAxisAngle(_config.Up, TurnSpeed * dt * _config.Fov))},
             {Key.Right, dt => _config.Lookat = Vector3d.Transform(_config.Lookat, Matrix4d.CreateFromAxisAngle(_config.Up, -TurnSpeed * dt * _config.Fov))},
             {Key.Up, dt => _config.Lookat = Vector3d.Transform(_config.Lookat, Matrix4d.CreateFromAxisAngle(Vector3d.Cross(_config.Up, _config.Lookat), TurnSpeed * dt * _config.Fov))},
             {Key.Down, dt => _config.Lookat = Vector3d.Transform(_config.Lookat, Matrix4d.CreateFromAxisAngle(Vector3d.Cross(_config.Up, _config.Lookat), -TurnSpeed * dt * _config.Fov))},
             {Key.R, dt => _config.MoveSpeed *= 1 + dt},
             {Key.F, dt =>  _config.MoveSpeed *= 1 - dt},
             {Key.N, dt => _config.Fov *= 1 + dt},
             {Key.M, dt => _config. Fov *= 1 - dt}
         };
     _keyboard.KeyDown += KeyboardOnKeyDown;
 }
Esempio n. 2
0
 public static CameraConfig CatmullRom(CameraConfig p0, CameraConfig p1, CameraConfig p2, CameraConfig p3, float t)
 {
     return new CameraConfig(
         CatmullRom(p0._position, p1._position, p2._position, p3._position, t),
         CatmullRom(p0._lookat, p1._lookat, p2._lookat, p3._lookat, t),
         CatmullRom(p0._up, p1._up, p2._up, p3._up, t),
         CatmullRom(p0._moveSpeed, p1._moveSpeed, p2._moveSpeed, p3._moveSpeed, t),
         p1._frame,
         CatmullRom(p0._fov, p1._fov, p2._fov, p3._fov, t));
 }
Esempio n. 3
0
        public Bitmap GetScreenshot(CameraConfig camera, int screenshotHeight, int slowRender)
        {
            var screenshotWidth = (int)(screenshotHeight * ScreenshotAspectRatio);
            var computeBuffer   = new ComputeBuffer <Vector4>(_program.Context, ComputeMemoryFlags.ReadWrite, screenshotWidth * screenshotHeight);
            var queue           = new ComputeCommandQueue(_program.Context, _program.Context.Devices[0], ComputeCommandQueueFlags.None);

            var globalSize = GlobalLaunchsizeFor(screenshotWidth, screenshotHeight);

            for (var i = 0; i < slowRender; i++)
            {
                CoreRender(computeBuffer, queue, _kernels, new Vector4((Vector3)camera.Position), new Vector4((Vector3)camera.Lookat), new Vector4((Vector3)camera.Up), i, camera.Fov, slowRender, camera.FocalDistance, screenshotWidth, screenshotHeight, globalSize, _localSize);
            }
            for (var i = 0; i < camera.Frame * slowRender; i++)
            {
                CoreRender(computeBuffer, queue, _kernels, new Vector4((Vector3)camera.Position), new Vector4((Vector3)camera.Lookat), new Vector4((Vector3)camera.Up), i, camera.Fov, slowRender, camera.FocalDistance, screenshotWidth, screenshotHeight, globalSize, _localSize);
            }

            var pixels = new Vector4[screenshotWidth * screenshotHeight];

            queue.ReadFromBuffer(computeBuffer, ref pixels, true, null);
            queue.Finish();

            computeBuffer.Dispose();
            queue.Dispose();

            var bmp        = new Bitmap(screenshotWidth, screenshotHeight);
            var destBuffer = new int[screenshotWidth * screenshotHeight];

            for (var y = 0; y < screenshotHeight; y++)
            {
                for (var x = 0; x < screenshotWidth; x++)
                {
                    var pixel = pixels[x + y * screenshotWidth];
                    if (float.IsNaN(pixel.X) || float.IsNaN(pixel.Y) || float.IsNaN(pixel.Z))
                    {
                        Console.WriteLine("Warning! Caught NAN pixel while taking screenshot!");
                        continue;
                    }
                    destBuffer[y * screenshotWidth + x] = (byte)(pixel.X * 255) << 16 | (byte)(pixel.Y * 255) << 8 | (byte)(pixel.Z * 255);
                }
            }
            var bmpData = bmp.LockBits(new Rectangle(0, 0, screenshotWidth, screenshotHeight), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);

            Marshal.Copy(destBuffer, 0, bmpData.Scan0, destBuffer.Length);
            bmp.UnlockBits(bmpData);

            return(bmp);
        }
Esempio n. 4
0
        public static bool CheckForVideo(KernelManager kernelManager)
        {
            if (_frame == null)
            {
                return(false);
            }
            var i = _frame.Value / StepsPerPoint;

            if (i >= Frames.Count - 1)
            {
                _frame = null;
                _aviManager.Close();
                RenderWindow.SetStatus("Finished video");
                return(false);
            }
            RenderWindow.SetStatus("Rendering frame " + _frame.Value + " of " + (Frames.Count - 1) * StepsPerPoint);
            var d0     = i == 0 ? Frames[0] : Frames[i - 1];
            var d1     = Frames[i];
            var d2     = Frames[i + 1];
            var d3     = i == Frames.Count - 2 ? Frames[Frames.Count - 1] : Frames[i + 2];
            var t      = (float)(_frame.Value % StepsPerPoint) / StepsPerPoint;
            var config = CameraConfig.CatmullRom(d0, d1, d2, d3, t);
            var bmp    = kernelManager.GetScreenshot(config, 720, 2);

            if (_frame.Value % 256 == 0 || _aviManager == null)
            {
                if (_aviManager != null)
                {
                    _aviManager.Close();
                }
                _videoStream = null;
                _aviManager  = new AviManager(Ext.UniqueFilename("video", "avi"), false);
            }
            if (_videoStream == null)
            {
                _videoStream = _aviManager.AddVideoStream(false, 25, bmp);
            }
            else
            {
                _videoStream.AddFrame(bmp);
            }

            _frame = _frame.Value + 1;

            return(true);
        }
Esempio n. 5
0
 public static XElement SaveStateToElement(CameraConfig config)
 {
     return(new XElement("state",
                         new XElement("position",
                                      new XElement("x", config.Position.X.ToString(CultureInfo.InvariantCulture)),
                                      new XElement("y", config.Position.Y.ToString(CultureInfo.InvariantCulture)),
                                      new XElement("z", config.Position.Z.ToString(CultureInfo.InvariantCulture))),
                         new XElement("lookat",
                                      new XElement("x", config.Lookat.X.ToString(CultureInfo.InvariantCulture)),
                                      new XElement("y", config.Lookat.Y.ToString(CultureInfo.InvariantCulture)),
                                      new XElement("z", config.Lookat.Z.ToString(CultureInfo.InvariantCulture))),
                         new XElement("up",
                                      new XElement("x", config.Up.X.ToString(CultureInfo.InvariantCulture)),
                                      new XElement("y", config.Up.Y.ToString(CultureInfo.InvariantCulture)),
                                      new XElement("z", config.Up.Z.ToString(CultureInfo.InvariantCulture))),
                         new XElement("movespeed", config.MoveSpeed.ToString(CultureInfo.InvariantCulture)),
                         new XElement("frame", config.Frame.ToString(CultureInfo.InvariantCulture)),
                         new XElement("fov", config.Fov.ToString(CultureInfo.InvariantCulture))));
 }
Esempio n. 6
0
        public Bitmap GetScreenshot(CameraConfig camera, int screenshotHeight, int slowRender)
        {
            var screenshotWidth = (int)(screenshotHeight * ScreenshotAspectRatio);
            var computeBuffer = new ComputeBuffer<Vector4>(_program.Context, ComputeMemoryFlags.ReadWrite, screenshotWidth * screenshotHeight);
            var queue = new ComputeCommandQueue(_program.Context, _program.Context.Devices[0], ComputeCommandQueueFlags.None);

            var globalSize = GlobalLaunchsizeFor(screenshotWidth, screenshotHeight);

            for (var i = 0; i < slowRender; i++)
                CoreRender(computeBuffer, queue, _kernels, new Vector4((Vector3)camera.Position), new Vector4((Vector3)camera.Lookat), new Vector4((Vector3)camera.Up), i, camera.Fov, slowRender, camera.FocalDistance, screenshotWidth, screenshotHeight, globalSize, _localSize);
            for (var i = 0; i < camera.Frame * slowRender; i++)
                CoreRender(computeBuffer, queue, _kernels, new Vector4((Vector3)camera.Position), new Vector4((Vector3)camera.Lookat), new Vector4((Vector3)camera.Up), i, camera.Fov, slowRender, camera.FocalDistance, screenshotWidth, screenshotHeight, globalSize, _localSize);

            var pixels = new Vector4[screenshotWidth * screenshotHeight];
            queue.ReadFromBuffer(computeBuffer, ref pixels, true, null);
            queue.Finish();

            computeBuffer.Dispose();
            queue.Dispose();

            var bmp = new Bitmap(screenshotWidth, screenshotHeight);
            var destBuffer = new int[screenshotWidth * screenshotHeight];
            for (var y = 0; y < screenshotHeight; y++)
            {
                for (var x = 0; x < screenshotWidth; x++)
                {
                    var pixel = pixels[x + y * screenshotWidth];
                    if (float.IsNaN(pixel.X) || float.IsNaN(pixel.Y) || float.IsNaN(pixel.Z))
                    {
                        Console.WriteLine("Warning! Caught NAN pixel while taking screenshot!");
                        continue;
                    }
                    destBuffer[y * screenshotWidth + x] = (byte)(pixel.X * 255) << 16 | (byte)(pixel.Y * 255) << 8 | (byte)(pixel.Z * 255);
                }
            }
            var bmpData = bmp.LockBits(new Rectangle(0, 0, screenshotWidth, screenshotHeight), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
            Marshal.Copy(destBuffer, 0, bmpData.Scan0, destBuffer.Length);
            bmp.UnlockBits(bmpData);

            return bmp;
        }
Esempio n. 7
0
 public static XElement SaveStateToElement(CameraConfig config)
 {
     return new XElement("state",
         new XElement("position",
             new XElement("x", config.Position.X.ToString(CultureInfo.InvariantCulture)),
             new XElement("y", config.Position.Y.ToString(CultureInfo.InvariantCulture)),
             new XElement("z", config.Position.Z.ToString(CultureInfo.InvariantCulture))),
         new XElement("lookat",
             new XElement("x", config.Lookat.X.ToString(CultureInfo.InvariantCulture)),
             new XElement("y", config.Lookat.Y.ToString(CultureInfo.InvariantCulture)),
             new XElement("z", config.Lookat.Z.ToString(CultureInfo.InvariantCulture))),
         new XElement("up",
             new XElement("x", config.Up.X.ToString(CultureInfo.InvariantCulture)),
             new XElement("y", config.Up.Y.ToString(CultureInfo.InvariantCulture)),
             new XElement("z", config.Up.Z.ToString(CultureInfo.InvariantCulture))),
         new XElement("movespeed", config.MoveSpeed.ToString(CultureInfo.InvariantCulture)),
         new XElement("frame", config.Frame.ToString(CultureInfo.InvariantCulture)),
         new XElement("fov", config.Fov.ToString(CultureInfo.InvariantCulture)));
 }
Esempio n. 8
0
 private void LoadStateInst()
 {
     if (System.IO.File.Exists(StateFilename) == false)
         return;
     _config = LoadState();
 }
Esempio n. 9
0
 public static void AddFrame(CameraConfig config)
 {
     config.Frame = FrameCount;
     Frames.Add(config);
     Save();
 }
Esempio n. 10
0
 public static void AddFrame(CameraConfig config)
 {
     config.Frame = FrameCount;
     Frames.Add(config);
     Save();
 }
Esempio n. 11
0
 public static CameraConfig CatmullRom(CameraConfig p0, CameraConfig p1, CameraConfig p2, CameraConfig p3, float t)
 {
     return(new CameraConfig(
                CatmullRom(p0._position, p1._position, p2._position, p3._position, t),
                CatmullRom(p0._lookat, p1._lookat, p2._lookat, p3._lookat, t),
                CatmullRom(p0._up, p1._up, p2._up, p3._up, t),
                CatmullRom(p0._moveSpeed, p1._moveSpeed, p2._moveSpeed, p3._moveSpeed, t),
                p1._frame,
                CatmullRom(p0._fov, p1._fov, p2._fov, p3._fov, t)));
 }