Пример #1
0
    void SpawnPlayers()
    {
        PersistentGameState pgs = FindObjectOfType <PersistentGameState>();

        players = new Player[2];

        if (!pgs)         // This will ONLY happen when testing in unity
        {
            pgs = PersistentGameState.CreateDefault();
        }

        for (int i = 0; i < 2; i++)
        {
            GameObject o = (GameObject)Instantiate(playerPrefab, spawnPoints[i].position, Quaternion.identity);

            Player p = o.GetComponent <Player>();
            p.playerNum   = i;
            p.inputMethod = pgs.inputMethods[i];

            FreeLook fl = p.lookCamera.GetComponent <FreeLook>();
            fl.inputMethod    = pgs.inputMethods[i];
            fl.sensitivity.y *= pgs.invertAxes[i] == 0 ? 1 : -1;

            var r = p.lookCamera.rect;
            r.x = i * 0.5f;
            p.lookCamera.rect   = r;
            p.fridgeCamera.rect = r;
            // p.skin = pgs.skin[i];

            players[i] = p;
        }

        Destroy(pgs);
    }
Пример #2
0
        protected override void Initialize()
        {
            base.Initialize();

            // collision configuration contains default setup for memory, collision setup
            CollisionConf = new DefaultCollisionConfiguration();
            Dispatcher    = new CollisionDispatcher(CollisionConf);

            Broadphase = new DbvtBroadphase();

            World             = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf);
            World.Gravity     = new Vector3(0, -10, 0);
            World.DebugDrawer = DebugDrawer;

            // create the ground
            _groundShape = new BoxShape(50, 1, 50);
            LoadModel("ground", _groundShape);
            CollisionShapes.Add(_groundShape);
            CollisionObject ground = LocalCreateRigidBody(0, Matrix.Identity, _groundShape);

            // create a few dynamic rigidbodies
            const float mass = 1.0f;

            _boxShape = new BoxShape(1);
            LoadModel("cube", _boxShape);
            CollisionShapes.Add(_boxShape);

            var rbInfo = new RigidBodyConstructionInfo(mass, null, _boxShape);

            rbInfo.LocalInertia = _boxShape.CalculateLocalInertia(mass);

            for (int k = 0; k < ArraySizeY; k++)
            {
                for (int i = 0; i < ArraySizeX; i++)
                {
                    for (int j = 0; j < ArraySizeZ; j++)
                    {
                        Matrix startTransform = Matrix.CreateTranslation(
                            _start + new Vector3(2 * i, 2 * k, 2 * j));

                        // using motionstate is recommended, it provides interpolation capabilities
                        // and only synchronizes 'active' objects
                        rbInfo.MotionState = new DefaultMotionState(startTransform);

                        var body = new RigidBody(rbInfo);

                        // make it drop from a height
                        body.Translate(new Vector3(0, 20, 0));

                        World.AddRigidBody(body);
                    }
                }
            }

            rbInfo.Dispose();

            FreeLook.SetEyeTarget(ref _eye, ref _target);
        }
Пример #3
0
 public override void UpdateView()
 {
     if (effect != null)
     {
         FreeLook freelook = Demo.Freelook;
         effect.View  = Matrix.CreateLookAt(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), MathHelper.Convert(freelook.Up));
         effect.View *= Matrix.CreateScale(-1, 1, 1);
     }
 }
Пример #4
0
        public void Render()
        {
            Demo.OnHandleInput();
            Demo.OnUpdate();

            GL.UseProgram(shaderProgram);
            GL.Enable(EnableCap.DepthTest);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            if (viewChanged)
            {
                GL.Viewport(0, 0, glControl.Width, glControl.Height);

                FreeLook freelook = Demo.Freelook;
                lookat = Matrix4.LookAt(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), MathHelper.Convert(freelook.Up));
                GL.UniformMatrix4(viewMatrixLocation, false, ref lookat);

                Matrix4.CreatePerspectiveFieldOfView(FieldOfView, AspectRatio, 0.1f, FarPlane, out perspective);
                perspective *= Matrix4.CreateScale(-1.0f, 1.0f, 1.0f);
                GL.UniformMatrix4(projectionMatrixLocation, false, ref perspective);

                Vector3 lightPosition = new Vector3(30, 20, 10);
                GL.Uniform3(lightPositionVectorLocation, lightPosition);
                GL.Uniform3(eyePositionVectorLocation, MathHelper.Convert(freelook.Eye));

                if (CullingEnabled)
                {
                    GL.Enable(EnableCap.CullFace);
                }
                else
                {
                    GL.Disable(EnableCap.CullFace);
                }

                viewChanged = false;
            }

            if (Demo.World != null)
            {
                _meshFactory.InitInstancedRender(Demo.World.CollisionObjectArray);
                _meshFactory.RenderInstanced();
            }

            GL.UseProgram(0);
            if (Demo.IsDebugDrawEnabled)
            {
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadMatrix(ref lookat);
                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadMatrix(ref perspective);
                (Demo.World.DebugDrawer as PhysicsDebugDraw).DrawDebugWorld(Demo.World);
            }

            info.OnRender(Demo.FramesPerSecond);

            glControl.SwapBuffers();
        }
Пример #5
0
        void SetSceneConstants()
        {
            FreeLook freelook    = Demo.FreeLook;
            Vector3  up          = MathHelper.Convert(freelook.Up);
            Vector3  eye         = MathHelper.Convert(freelook.Eye);
            Vector4  eyePosition = new Vector4(eye, 1);

            sceneConstants.View = Matrix.LookAtLH(eye, MathHelper.Convert(freelook.Target), up);
            Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, _nearPlane, FarPlane, out sceneConstants.Projection);
            Matrix.Invert(ref sceneConstants.View, out sceneConstants.ViewInverse);

            Texture2DDescription depthBuffer = lightDepthTexture.Description;
            Vector3 lightPosition            = sunLightDirection * -60;
            Matrix  lightView = Matrix.LookAtLH(lightPosition, Vector3.Zero, up);
            //Matrix lightProjection = Matrix.OrthoLH(depthBuffer.Width / 8.0f, depthBuffer.Height / 8.0f, _nearPlane, FarPlane);
            Matrix lightProjection = Matrix.PerspectiveFovLH(FieldOfView, (float)depthBuffer.Width / (float)depthBuffer.Height, _nearPlane, FarPlane);

            sceneConstants.LightViewProjection = lightView * lightProjection;

            DataStream data;

            _immediateContext.MapSubresource(sceneConstantsBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out data);
            Marshal.StructureToPtr(sceneConstants, data.DataPointer, false);
            _immediateContext.UnmapSubresource(sceneConstantsBuffer, 0);
            data.Dispose();

            sunLightDirectionVar.Set(new Vector4(sunLightDirection, 1));

            Matrix overlayMatrix = Matrix.Scaling(info.Width / _width, info.Height / _height, 1.0f);

            overlayMatrix *= Matrix.Translation(-(_width - info.Width) / _width, (_height - info.Height) / _height, 0.0f);
            overlayViewProjectionVar.SetMatrix(overlayMatrix);


            lightProjectionVar.SetMatrixTranspose(ref sceneConstants.Projection);
            lightViewVar.SetMatrixTranspose(ref sceneConstants.View);
            lightViewInverseVar.SetMatrix(ref sceneConstants.ViewInverse);
            lightViewportWidthVar.Set(_width);
            lightViewportHeightVar.Set(_height);
            lightEyePositionVar.Set(ref eyePosition);

            float   tanHalfFovY    = (float)Math.Tan(FieldOfView * 0.5f);
            float   tanHalfFovX    = tanHalfFovY * AspectRatio;
            float   projectionA    = FarPlane / (FarPlane - _nearPlane);
            float   projectionB    = -projectionA * _nearPlane;
            Vector4 viewParameters = new Vector4(tanHalfFovX, tanHalfFovY, projectionA, projectionB);

            lightViewParametersVar.Set(ref viewParameters);


            viewportWidthVar.Set(_width);
            viewportHeightVar.Set(_height);
            viewParametersVar.Set(ref viewParameters);
        }
Пример #6
0
        void SetSceneConstants()
        {
            FreeLook freelook = Demo.Freelook;
            Vector3  up       = MathHelper.Convert(freelook.Up);

            sceneConstants.View        = Matrix.LookAtLH(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), up);
            sceneConstants.Projection  = Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, _nearPlane, FarPlane);
            sceneConstants.ViewInverse = Matrix.Invert(sceneConstants.View);

            Vector3 light = new Vector3(20, 30, 10);
            Texture2DDescription depthBuffer = lightDepthTexture.Description;
            Matrix lightView       = Matrix.LookAtLH(light, Vector3.Zero, up);
            Matrix lightProjection = Matrix.OrthoLH(depthBuffer.Width / 8.0f, depthBuffer.Height / 8.0f, _nearPlane, FarPlane);

            sceneConstants.LightViewProjection = lightView * lightProjection;

            DataStream data;

            _immediateContext.MapSubresource(sceneConstantsBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out data);
            Marshal.StructureToPtr(sceneConstants, data.DataPointer, false);
            _immediateContext.UnmapSubresource(sceneConstantsBuffer, 0);
            data.Dispose();

            inverseProjectionVar.SetMatrix(Matrix.Invert(sceneConstants.Projection));
            inverseViewVar.SetMatrix(sceneConstants.ViewInverse);
            lightInverseViewProjectionVar.SetMatrix(Matrix.Invert(sceneConstants.LightViewProjection));
            lightPositionVar.Set(new Vector4(light, 1));
            eyePositionVar.Set(new Vector4(MathHelper.Convert(freelook.Eye), 1));
            eyeZAxisVar.Set(new Vector4(Vector3.Normalize(MathHelper.Convert(freelook.Target - freelook.Eye)), 1));

            float tanHalfFOVY = (float)Math.Tan(FieldOfView * 0.5f);

            tanHalfFOVXVar.Set(tanHalfFOVY * AspectRatio);
            tanHalfFOVYVar.Set(tanHalfFOVY);
            float projectionA = FarPlane / (FarPlane - _nearPlane);
            float projectionB = -projectionA * _nearPlane;

            projectionAVar.Set(projectionA);
            projectionBVar.Set(projectionB);

            Matrix overlayMatrix = Matrix.Scaling(info.Width / _width, info.Height / _height, 1.0f);

            overlayMatrix *= Matrix.Translation(-(_width - info.Width) / _width, (_height - info.Height) / _height, 0.0f);
            overlayViewProjectionVar.SetMatrix(overlayMatrix);
        }
Пример #7
0
        public override void UpdateView()
        {
            if (shaderProgram != 0)
            {
                GL.Viewport(0, 0, glControl.Width, glControl.Height);

                perspective  = Matrix4.CreatePerspectiveFieldOfView(FieldOfView, AspectRatio, 0.1f, FarPlane);
                perspective *= Matrix4.CreateScale(-1.0f, 1.0f, 1.0f);
                GL.UniformMatrix4(projectionMatrixLocation, false, ref perspective);

                Vector3 lightDirection = Vector3.Normalize(new Vector3(1.0f, 1.0f, 1.0f));
                GL.Uniform3(lightDirectionVectorLocation, lightDirection);
            }

            FreeLook freelook = Demo.Freelook;

            lookat = Matrix4.LookAt(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), MathHelper.Convert(freelook.Up));
        }
Пример #8
0
        void SetSceneConstants()
        {
            FreeLook freelook = Demo.Freelook;
            Vector3  up       = MathHelper.Convert(freelook.Up);

            sceneConstants.View        = Matrix.LookAtLH(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), up);
            sceneConstants.Projection  = Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, _nearPlane, FarPlane);
            sceneConstants.ViewInverse = Matrix.Invert(sceneConstants.View);

            Vector3 light = new Vector3(20, 30, 10);
            Texture2DDescription depthBuffer = lightDepthTexture.Description;
            Matrix lightView       = Matrix.LookAtLH(light, Vector3.Zero, up);
            Matrix lightProjection = Matrix.OrthoLH(depthBuffer.Width / 8.0f, depthBuffer.Height / 8.0f, _nearPlane, FarPlane);

            sceneConstants.LightViewProjection = lightView * lightProjection;

            using (var data = sceneConstantsBuffer.Map(MapMode.WriteDiscard))
            {
                Marshal.StructureToPtr(sceneConstants, data.DataPointer, false);
                sceneConstantsBuffer.Unmap();
            }

            effect2.GetVariableByName("InverseProjection").AsMatrix().SetMatrix(Matrix.Invert(sceneConstants.Projection));
            effect2.GetVariableByName("InverseView").AsMatrix().SetMatrix(sceneConstants.ViewInverse);
            effect2.GetVariableByName("LightInverseViewProjection").AsMatrix().SetMatrix(Matrix.Invert(sceneConstants.LightViewProjection));
            effect2.GetVariableByName("LightPosition").AsVector().Set(new Vector4(light, 1));
            effect2.GetVariableByName("EyePosition").AsVector().Set(new Vector4(MathHelper.Convert(freelook.Eye), 1));
            effect2.GetVariableByName("EyeZAxis").AsVector().Set(new Vector4(Vector3.Normalize(MathHelper.Convert(freelook.Target - freelook.Eye)), 1));

            float tanHalfFOVY = (float)Math.Tan(FieldOfView * 0.5f);

            effect2.GetVariableByName("TanHalfFOVX").AsScalar().Set(tanHalfFOVY * AspectRatio);
            effect2.GetVariableByName("TanHalfFOVY").AsScalar().Set(tanHalfFOVY);
            float projectionA = FarPlane / (FarPlane - _nearPlane);
            float projectionB = -projectionA * _nearPlane;

            effect2.GetVariableByName("ProjectionA").AsScalar().Set(projectionA);
            effect2.GetVariableByName("ProjectionB").AsScalar().Set(projectionB);

            Matrix overlayMatrix = Matrix.Scaling(2 * info.Width / _width, 2 * info.Height / _height, 1.0f);

            overlayMatrix *= Matrix.Translation(-(_width - info.Width) / _width, (_height - info.Height) / _height, 0.0f);
            effect2.GetVariableByName("OverlayViewProjection").AsMatrix().SetMatrix(overlayMatrix);
        }
Пример #9
0
        public override void UpdateView()
        {
            FreeLook freelook = Demo.Freelook;

            Device.SetTransform(TransformState.View, Matrix.LookAtLH(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), MathHelper.Convert(freelook.Up)));
        }
Пример #10
0
        public void Run()
        {
            using (Graphics = GraphicsLibraryManager.GetGraphics(this))
            {
                Input = new Input(Graphics.Form);
                Freelook = new FreeLook(Input);

                Graphics.Initialize();
                Graphics.CullingEnabled = isCullingEnabled;
                OnInitialize();
                if (World == null)
                {
                    OnInitializePhysics();
                    BulletExampleRunner.Get().PostOnInitializePhysics();
                }
                if (_isDebugDrawEnabled)
                {
                    if (_debugDrawer == null)
                    {
                        _debugDrawer = Graphics.GetPhysicsDebugDrawer();
                        _debugDrawer.DebugMode = DebugDrawMode;
                    }
                    if (World != null)
                    {
                        World.DebugDrawer = _debugDrawer;
                    }
                }
                Graphics.UpdateView();

                clock.Start();
                Graphics.Run();

                /*
                if (_debugDrawer != null)
                {
                    if (World != null)
                    {
                        World.DebugDrawer = null;
                    }
                    if (_debugDrawer is IDisposable)
                    {
                        (_debugDrawer as IDisposable).Dispose();
                    }
                    _debugDrawer = null;
                }
                */
            }
            //Graphics = null;
        }
Пример #11
0
 void Start()
 {
     freeLook = lookCamera.GetComponent <FreeLook>();
 }