Ejemplo n.º 1
0
        public PhysicsTest()
        {
            var scene = Game.World.Scene;

            Game.Invoke(() =>
            {
                Object3dInfo point3dinfo = new Object3dInfo(new VertexInfo[] { new VertexInfo()
                                                                               {
                                                                                   Position = Vector3.One,
                                                                                   Normal   = Vector3.UnitZ,
                                                                                   UV       = Vector2.Zero
                                                                               } });
                Object3dInfo point3dinfo2 = Object3dGenerator.CreateCube(Vector3.One * 0.1f, Vector2.One).AsObject3dInfo();
                OpenTK.Graphics.OpenGL4.GL.PointSize(111);
                GenericMaterial whitemat = new GenericMaterial()
                {
                    DiffuseColor = Vector3.One
                };
                Mesh3d points = Mesh3d.Create(point3dinfo2, whitemat);
                //point3dinfo.DrawMode = OpenTK.Graphics.OpenGL4.PrimitiveType.Points;
                //whitemat.CustomShaderProgram = ShaderProgram.Compile("Points.vertex.glsl", "Points.fragment.glsl");
                points.ClearInstances();

                ComputeShader updateShader       = new ComputeShader("PhysicsUpdater.compute.glsl");
                ShaderStorageBuffer pointsBuffer = new ShaderStorageBuffer();
                var parts         = new List <Particle>();
                List <byte> bytes = new List <byte>();
                for (int i = 0; i < 1024 * 2; i++)
                {
                    var part = new Particle()
                    {
                        Position = new Vector3(rand(-20, 20), rand(0, 20), rand(-10, 10)),
                        Velocity = Vector3.Zero
                    };
                    points.AddInstance(new TransformationManager(part.Position));

                    bytes.AddRange(BitConverter.GetBytes(part.Position.X));
                    bytes.AddRange(BitConverter.GetBytes(part.Position.Y));
                    bytes.AddRange(BitConverter.GetBytes(part.Position.Z));
                    bytes.AddRange(BitConverter.GetBytes(part.Position.X));

                    bytes.AddRange(BitConverter.GetBytes(part.Velocity.X));
                    bytes.AddRange(BitConverter.GetBytes(part.Velocity.Y));
                    bytes.AddRange(BitConverter.GetBytes(part.Velocity.Z));
                    bytes.AddRange(BitConverter.GetBytes(part.Velocity.X));
                }
                pointsBuffer.MapData(bytes.ToArray());

                pointsBuffer.Use(9);
                Game.OnBeforeDraw += (z, x) =>
                {
                    updateShader.Use();
                    pointsBuffer.Use(9);
                    updateShader.SetUniform("ParticlesCount", 1024 * 2);
                    updateShader.Dispatch(2, 1, 1);

                    pointsBuffer.Use(9);
                };

                points.UpdateMatrix();
                scene.Add(points);
            });
        }
Ejemplo n.º 2
0
        public static List <Light> AddControllableLight()
        {
            float fovdegree = 90;

            RedLight = new List <Light>();
            RedLight.Add(new Light(new TransformationManager(new Vector3(0, 5, 0), Quaternion.FromAxisAngle(new Vector3(1, 0, 0), MathHelper.DegreesToRadians(-110))))
            {
                Color = new Vector3(1) * 400,
                ShadowMappingEnabled = true,
                ShadowMapType        = Light.ShadowMapTypeEnum.Single
            });
            Picked = RedLight[0];

            /*
             * //crazyiness
             * var tmp = new List<Light>();
             * for(int i = 0; i < 10; i++)
             * {
             *  for(int g = 0; g < 4; g++)
             *  {
             *      var l = new Light(new TransformationManager(new Vector3((i - 5) * 1.6f, 1 + g * 2, 0)));
             *      tmp.Add(l);
             *      //l.Color = (new Vector3((float)Math.Sin(i), 1.0f - (float)Math.Sin(i), (float)Math.Cos(i)) * 0.5f + new Vector3(0.5f)) * 0.3f;
             *      l.Color = new Vector3(1) * (0.05f * (g + 1));
             *      l.CutOffDistance = 30.0f;
             *      l.StaticShadowMap = true;
             *      l.ShadowMapType = Light.ShadowMapTypeEnum.Cubemap;
             *      l.ShadowsQuality = ShadowMapQuality.Low;
             *      l.ShadowMappingEnabled = true;
             *      Game.World.Scene.Add(l);
             *  }
             * }
             * int ix = 0;
             * Game.OnBeforeDraw += (x, d) =>
             * {
             *  tmp[ix].ShadowMapRefreshNeeded = true;
             *  ix++;
             *  if(ix >= tmp.Count)
             *      ix = 0;
             * };*/

            //RedLight[0].camera.UpdatePerspective(1, MathHelper.DegreesToRadians(90), 0.01f, 100);

            /* RedLight.Add(new ProjectionLight(new Vector3(65, 0, 65), Quaternion.FromAxisAngle(new Vector3(1, 0, -1), MathHelper.DegreesToRadians(fovdegree)), 1, 1, MathHelper.DegreesToRadians(45), 0.1f, 100.0f)
             * {
             *   LightColor = new Vector3(1, 0.84f, 0.93f) * 25,
             *   IsStatic = true
             * });
             * RedLight.Add(new ProjectionLight(new Vector3(65, 0, 65), Quaternion.FromAxisAngle(new Vector3(1, 0, -1), MathHelper.DegreesToRadians(fovdegree)), 1, 1, MathHelper.DegreesToRadians(45), 0.1f, 100.0f)
             * {
             *   LightColor = new Vector3(1, 0.84f, 0.93f) * 25,
             *   IsStatic = true
             * });
             * RedLight.Add(new ProjectionLight(new Vector3(65, 0, 65), Quaternion.FromAxisAngle(new Vector3(1, 0, -1), MathHelper.DegreesToRadians(fovdegree)), 1, 1, MathHelper.DegreesToRadians(45), 0.1f, 100.0f)
             * {
             *   LightColor = new Vector3(1, 0.84f, 0.93f) * 25,
             *   IsStatic = true
             * });*/
            //redConeLight.BuildOrthographicProjection(600, 600, -150, 150);

            Game.OnKeyUp += (o, e) =>
            {
                if (e.Key == OpenTK.Input.Key.J)
                {
                    fovdegree += 5f;
                    if (fovdegree >= 180)
                    {
                        fovdegree = 179;
                    }
                    RedLight.ForEach((ax) => ax.Angle = MathHelper.DegreesToRadians(fovdegree));
                }
                if (e.Key == OpenTK.Input.Key.K)
                {
                    fovdegree -= 5f;
                    if (fovdegree <= 10)
                    {
                        fovdegree = 10;
                    }
                    RedLight.ForEach((ax) => ax.Angle = MathHelper.DegreesToRadians(fovdegree));
                }
            };
            RedLight.ForEach((a) => Game.World.Scene.Add(a));

            Game.OnMouseMove += (o, e) =>
            {
                MouseX = e.X;
                MouseY = e.Y;

                var kb = OpenTK.Input.Keyboard.GetState();
                if (kb.IsKeyDown(OpenTK.Input.Key.T))
                {
                    FreeCam.Freeze = true;
                    if (Picked != null)
                    {
                        Picked.SetPosition(Picked.GetPosition() + FreeCam.Cam.GetOrientation().GetTangent(MathExtensions.TangentDirection.Right) * (float)e.XDelta * -0.01f);
                        Picked.SetPosition(Picked.GetPosition() + FreeCam.Cam.GetOrientation().GetTangent(MathExtensions.TangentDirection.Up) * (float)e.YDelta * -0.01f);
                    }
                }
                else if (kb.IsKeyDown(OpenTK.Input.Key.F))
                {
                    FreeCam.Freeze = true;
                    if (Picked != null)
                    {
                        Picked.SetOrientation(Quaternion.Multiply(Picked.GetOrientation(), Quaternion.FromAxisAngle(FreeCam.Cam.GetOrientation().GetTangent(MathExtensions.TangentDirection.Up), (float)e.XDelta * -0.01f)));
                        Picked.SetOrientation(Quaternion.Multiply(Picked.GetOrientation(), Quaternion.FromAxisAngle(FreeCam.Cam.GetOrientation().GetTangent(MathExtensions.TangentDirection.Left), (float)e.YDelta * -0.01f)));
                    }
                }
                else if (kb.IsKeyDown(OpenTK.Input.Key.C))
                {
                    FreeCam.Freeze = true;
                    if (Picked != null)
                    {
                        Picked.SetScale(Picked.GetScale() + new Vector3((float)e.XDelta * -0.01f));
                    }
                }
                else
                {
                    FreeCam.Freeze = Game.DisplayAdapter.IsCursorVisible;
                }
            };
            Game.OnMouseWheel += (o, e) =>
            {
                Camera.Current.LensBlurAmount -= e.Delta / 2.0f;
            };
            Game.OnAfterDraw += (o, e) =>
            {
                //.ToString(System.Globalization.CultureInfo.InvariantCulture)

                //SettingsController.Instance.UpdatePerformance();

                /*var jpad = OpenTK.Input.GamePad.GetState(0);
                 * float deadzone = 0.15f;
                 * if(Picked != null)
                 * {
                 *  if(Math.Abs(jpad.ThumbSticks.Right.X) > deadzone)
                 *  {
                 *      var ang = Picked.GetOrientation().GetTangent(MathExtensions.TangentDirection.Up);
                 *      Picked.Rotate(Quaternion.FromAxisAngle(ang, jpad.ThumbSticks.Right.X * 0.01f));
                 *  }
                 *  if(Math.Abs(jpad.ThumbSticks.Right.Y) > deadzone)
                 *  {
                 *      var ang = Picked.GetOrientation().GetTangent(MathExtensions.TangentDirection.Left);
                 *      Picked.Rotate(Quaternion.FromAxisAngle(ang, jpad.ThumbSticks.Right.Y * 0.01f));
                 *  }
                 *  if(Math.Abs(jpad.Triggers.Left) > deadzone)
                 *  {
                 *      var ang = Picked.GetOrientation().ToDirection();
                 *      Picked.Rotate(Quaternion.FromAxisAngle(ang, jpad.Triggers.Left * 0.01f));
                 *  }
                 *  if(Math.Abs(jpad.Triggers.Right) > deadzone)
                 *  {
                 *      var ang = Picked.GetOrientation().ToDirection();
                 *      Picked.Rotate(Quaternion.FromAxisAngle(ang, -jpad.Triggers.Right*0.01f));
                 *  }
                 * }*/
                var kb = OpenTK.Input.Keyboard.GetState();
                if (Game.DisplayAdapter.IsCursorVisible)
                {
                    if (!kb.IsKeyDown(OpenTK.Input.Key.LControl))
                    {
                        //Game.DisplayAdapter.Pipeline.PostProcessor.ShowSelected = false;
                    }
                    else
                    {
                        //Game.DisplayAdapter.Pipeline.PostProcessor.ShowSelected = true;
                        PickingResult.MapData(Vector4.One);
                        MousePicker.Use();
                        var state = OpenTK.Input.Mouse.GetState();
                        MousePicker.SetUniform("Mouse", new Vector2(MouseX, Game.Resolution.Height - MouseY));
                        MousePicker.SetUniform("Resolution", new Vector2(Game.Resolution.Width, Game.Resolution.Height));
                        PickingResult.Use(0);
                        GL.ActiveTexture(TextureUnit.Texture29);
                        GL.BindTexture(TextureTarget.Texture2D, Game.DisplayAdapter.MainRenderer.MRT.TexMeshIds);
                        MousePicker.Dispatch(1, 1, 1);
                        OpenTK.Graphics.OpenGL4.GL.MemoryBarrier(OpenTK.Graphics.OpenGL4.MemoryBarrierFlags.ShaderStorageBarrierBit);
                        byte[] result = PickingResult.Read(0, 4);
                        uint   id     = BitConverter.ToUInt32(result, 0);
                        foreach (var m in Game.World.Scene.GetFlatRenderableList())
                        {
                            if (m is Mesh3d)
                            {
                                foreach (var inst in (m as Mesh3d).GetInstances())
                                {
                                    if (inst.Id == id)
                                    {
                                        Picked     = inst;
                                        PickedMesh = (m as Mesh3d);
                                        //SettingsController.Instance.SetMesh(inst);
                                    }
                                }
                            }
                        }
                    }
                }

                /*
                 * if(kb.IsKeyDown(OpenTK.Input.Key.Plus))
                 * {
                 *  var dir = Game.CascadeShadowMaps.GetDirection();
                 *  dir = Quaternion.Multiply(Quaternion.FromAxisAngle(Vector3.UnitX, -0.01f), dir);
                 *  Game.CascadeShadowMaps.SetDirection(dir);
                 * }
                 *
                 * if(kb.IsKeyDown(OpenTK.Input.Key.Minus))
                 * {
                 *  var dir = Game.CascadeShadowMaps.GetDirection();
                 *  dir = Quaternion.Multiply(Quaternion.FromAxisAngle(Vector3.UnitX, 0.01f), dir);
                 *  Game.CascadeShadowMaps.SetDirection(dir);
                 * }*/

                if (kb.IsKeyDown(OpenTK.Input.Key.T) && Picked != null)
                {
                    if (kb.IsKeyDown(OpenTK.Input.Key.Keypad4))
                    {
                        Picked.Translate(new Vector3(-0.01f, 0, 0));
                    }
                    if (kb.IsKeyDown(OpenTK.Input.Key.Keypad6))
                    {
                        Picked.Translate(new Vector3(0.01f, 0, 0));
                    }
                    if (kb.IsKeyDown(OpenTK.Input.Key.Keypad8))
                    {
                        Picked.Translate(new Vector3(0, 0, 0.01f));
                    }
                    if (kb.IsKeyDown(OpenTK.Input.Key.Keypad2))
                    {
                        Picked.Translate(new Vector3(0, 0, -0.01f));
                    }
                    if (kb.IsKeyDown(OpenTK.Input.Key.Keypad7))
                    {
                        Picked.Translate(new Vector3(0, 0.01f, 0));
                    }
                    if (kb.IsKeyDown(OpenTK.Input.Key.Keypad1))
                    {
                        Picked.Translate(new Vector3(0, -0.01f, 0));
                    }
                }
                if (kb.IsKeyDown(OpenTK.Input.Key.C) && Picked != null)
                {
                    if (kb.IsKeyDown(OpenTK.Input.Key.Keypad8))
                    {
                        Picked.Scale(1.01f);
                    }
                    if (kb.IsKeyDown(OpenTK.Input.Key.Keypad1))
                    {
                        Picked.Scale(0.99f);
                    }
                }

                var rd = new Random();/*
                                       * if(kb.IsKeyDown(OpenTK.Input.Key.Left))
                                       * {
                                       * var pos = RedLight[0].camera.Transformation.GetPosition();
                                       * RedLight.ForEach((ax) => ax.camera.Transformation.SetPosition(pos + Vector3.UnitX / 12.0f));
                                       * }
                                       * if(kb.IsKeyDown(OpenTK.Input.Key.Right))
                                       * {
                                       * var pos = RedLight[0].camera.Transformation.GetPosition();
                                       * redConeLight.camera.Transformation.SetPosition(pos - Vector3.UnitX / 12.0f);
                                       * }
                                       * if(kb.IsKeyDown(OpenTK.Input.Key.Up))
                                       * {
                                       * var pos = RedLight[0].camera.Transformation.GetPosition();
                                       * redConeLight.camera.Transformation.SetPosition(pos + Vector3.UnitZ / 12.0f);
                                       * }
                                       * if(kb.IsKeyDown(OpenTK.Input.Key.Down))
                                       * {
                                       * var pos = RedLight[0].camera.Transformation.GetPosition();
                                       * redConeLight.camera.Transformation.SetPosition(pos - Vector3.UnitZ / 12.0f);
                                       * }
                                       * if(kb.IsKeyDown(OpenTK.Input.Key.PageUp))
                                       * {
                                       * var pos = RedLight[0].camera.Transformation.GetPosition();
                                       * redConeLight.camera.Transformation.SetPosition(pos + Vector3.UnitY / 12.0f);
                                       * }
                                       * if(kb.IsKeyDown(OpenTK.Input.Key.PageDown))
                                       * {
                                       * var pos = RedLight[0].camera.Transformation.GetPosition();
                                       * redConeLight.camera.Transformation.SetPosition(pos - Vector3.UnitY / 12.0f);
                                       * }*/
                /*if(kb.IsKeyDown(OpenTK.Input.Key.U))
                 * {
                 *  var quat = Quaternion.FromAxisAngle(sun.Orientation.GetTangent(MathExtensions.TangentDirection.Left), -0.01f);
                 *  sun.Orientation = Quaternion.Multiply(sun.Orientation, quat);
                 * }
                 * if(kb.IsKeyDown(OpenTK.Input.Key.J))
                 * {
                 *  var quat = Quaternion.FromAxisAngle(sun.Orientation.GetTangent(MathExtensions.TangentDirection.Left), 0.01f);
                 *  sun.Orientation = Quaternion.Multiply(sun.Orientation, quat);
                 * }
                 * if(kb.IsKeyDown(OpenTK.Input.Key.H))
                 * {
                 *  var quat = Quaternion.FromAxisAngle(Vector3.UnitY, -0.01f);
                 *  sun.Orientation = Quaternion.Multiply(sun.Orientation, quat);
                 * }
                 * if(kb.IsKeyDown(OpenTK.Input.Key.K))
                 * {
                 *  var quat = Quaternion.FromAxisAngle(Vector3.UnitY, 0.01f);
                 *  sun.Orientation = Quaternion.Multiply(sun.Orientation, quat);
                 * }*/
            };
            return(RedLight);
        }