/// <summary>
        /// Updates the state of the game. This method checks the vxGameBaseScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you tab away to a different application.
        /// </summary>
        public sealed override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                           bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);


            // Gradually fade in or out depending on whether we are covered by the pause screen.
            if (coveredByOtherScreen && IsPausable)
            {
                pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
            }
            else
            {
                pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
            }

            if (IsActive || IsPausable == false)
            {
                #region Set Debug Info

                BEPUDebugDrawer.Update();

                // check for other keys pressed on keyboard
                if (vxEngine.InputManager.IsNewKeyPress(Keys.P))
                {
                    var previousSceneShadowMode = mSceneShadowMode;
                    mSceneShadowMode = nextEnumValue(mSceneShadowMode);

                    if (previousSceneShadowMode < vxEnumSceneShadowMode.BlockPattern && mSceneShadowMode >= vxEnumSceneShadowMode.BlockPattern ||
                        previousSceneShadowMode >= vxEnumSceneShadowMode.BlockPattern && mSceneShadowMode < vxEnumSceneShadowMode.BlockPattern)
                    {
                        vxEngine.Renderer.swapShadowMapWithBlockTexture();
                    }

                    foreach (vxEntity entity in Entities)
                    {
                        ((vxEntity3D)entity).renderShadowSplitIndex = mSceneShadowMode >= vxEnumSceneShadowMode.SplitColors;
                    }
                }

                vxEngine.Renderer.mSnapShadowMaps = true;
                if (vxEngine.InputManager.IsNewKeyPress(Keys.F))
                {
                    vxEngine.Renderer.mSnapShadowMaps = !vxEngine.Renderer.mSnapShadowMaps;
                }
                //vxConsole.WriteToInGameDebug("f:" + vxEngine.Renderer.mSnapShadowMaps);

                if (vxEngine.InputManager.IsNewKeyPress(Keys.T))
                {
                    foreach (vxEntity entity in Entities)
                    {
                        ((vxEntity3D)entity).TextureEnabled = !((vxEntity3D)entity).TextureEnabled;
                    }
                }

                #endregion


                //Update Audio Manager
                //**********************************************************************************************
                AudioManager.Listener.Position = Camera.Position / 100;
                AudioManager.Listener.Forward  = Camera.View.Forward;
                AudioManager.Listener.Up       = Camera.View.Up;
                AudioManager.Listener.Velocity = Camera.View.Forward;


                // Update Physics
                //**********************************************************************************************

                // Start measuring time for "Physics".
                vxEngine.DebugSystem.TimeRuler.BeginMark("Physics", Color.LimeGreen);

                //Update the Physics System.
                //vxConsole.WriteToInGameDebug(((float)gameTime.ElapsedGameTime.Milliseconds)/1000);
                //vxConsole.WriteToInGameDebug((float)gameTime.ElapsedGameTime.TotalSeconds);
                //BEPUPhyicsSpace.Update ((float)gameTime.ElapsedGameTime.TotalSeconds);
                BEPUPhyicsSpace.Update();

                // Stop measuring time for "Draw".
                vxEngine.DebugSystem.TimeRuler.EndMark("Physics");



                // Update the Scene
                //**********************************************************************************************
                UpdateScene(gameTime, otherScreenHasFocus, false);



                // Update Scene Entities
                //**********************************************************************************************
                for (int i = 0; i < Entities.Count; i++)
                {
                    Entities [i].Update(gameTime);

                    if (Entities [i].KeepUpdating == false)
                    {
                        Entities.RemoveAt(i);
                    }
                }

                // Update Particle System
                //**********************************************************************************************
                ParticleSystem.Update(gameTime);


                // Update Camera
                //**********************************************************************************************
                UpdateCameraChaseTarget();
                Camera.Update(gameTime);


                vxEngine.Renderer.setLightPosition(-LightPositions);

                // Tell the lensflare component where our camera is positioned.
                lensFlare.LightDir   = SunEmitter.LightDirection;
                lensFlare.View       = Camera.View;
                lensFlare.Projection = Camera.Projection;
            }
            else
            {
                vxEngine.InputManager.ShowCursor = true;
            }
        }
        /// <summary>
        /// Draws the gameplay screen.
        /// </summary>
        public override void DrawScene(GameTime gameTime)
        {
#if VRTC_PLTFRM_XNA
            foreach (InstanceSet instSet in InstanceSetCollection)
            {
                instSet.Update();
            }

            foreach (KeyValuePair <object, InstanceSet> entry in Instances)
            {
                // do something with entry.Value or entry.Key
                entry.Value.Update();
            }
#endif

            // Draw Shadow Map
            //**********************************************************************************************
            // determine shadow frustums
            var shadowCamera = mVirtualCameraMode != vxEnumVirtualCameraMode.None ? mVirtualCamera : Camera;
            vxEngine.Renderer.setShadowTransforms(shadowCamera);

            // render shadow maps first, then scene
            DrawShadows(gameTime, shadowCamera);


            // Draw Main Render Passes
            //**********************************************************************************************
            DrawMain(gameTime, Camera);

            //Draw the Sun (And any post processing that comes with)
            //lensFlare.Draw(gameTime);

            //Now Draw the Distortion (Note, this will not show up in the edge detection)
            //DrawDistortion(vxEngine.Renderer.RT_MainScene, gameTime);

            //Get Blurred Scene for a number of different Processes (Depth of Field, Menu Background blurring etc...)
            //Do This before the Edge Detection, otherwise you get edge bleeding that over saturates the scene with black.


            // Apply Post Processing Effects
            //**********************************************************************************************
            vxEngine.Renderer.ApplyPostProcessors();


            //Draw Menu Blur Only if this screen is Pausable
            if (IsPausable)
            {
                vxEngine.SpriteBatch.Begin();
                vxEngine.SpriteBatch.Draw(vxEngine.Renderer.RT_BlurredScene, Vector2.Zero, Color.White * pauseAlpha);
                vxEngine.SpriteBatch.End();
            }

            // Draw any inherited or overriden code
            //**********************************************************************************************
            DrawGameplayScreen(gameTime);



            // Draw Overlay items such as 3D Sandbox Cursor and HUD
            //**********************************************************************************************
            DrawOverlayItems();

            DrawHUD();



            // Debug Rendering
            //**********************************************************************************************
            if (vxEnviroment.GetVar(vxEnumEnvVarType.DEBUG_MESH).GetAsBool())
            {
                BEPUDebugDrawer.Draw(Camera.View, Camera.Projection);
            }

            vxDebugShapeRenderer.Draw(gameTime, Camera.View, Camera.Projection);

            if (vxEnviroment.GetVar(vxEnumEnvVarType.DEBUG_RNDRTRGT).GetAsBool())
            {
                DrawDebugRenderTargetsToScreen();
            }

            // If the game is transitioning on or off, fade it out to black.
            if (TransitionPosition > 0 || pauseAlpha > 0)
            {
                vxEngine.FadeBackBufferToBlack(MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2));
            }
        }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        public override void LoadContent()
        {
            InitialiseLevel();


            ///////////////////////////////////////////////////////////////////////
            //Initialise Camera Code
            ///////////////////////////////////////////////////////////////////////
            #region Set Up Camera

            base.LoadContent();
            Camera.CameraType = CameraType.CharacterFPS;

            character = new CharacterControllerInput(BEPUPhyicsSpace, Camera, vxEngine);

            //Since this is the character playground, turn on the character by default.
            character.Activate();

            //Having the character body visible would be a bit distracting.
            character.CharacterController.Body.Tag = "noDisplayObject";

            SimulationStart();
            SimulationStop();

            //
            //Grabbers
            //
            grabber = new MotorizedGrabSpring();
            BEPUPhyicsSpace.Add(grabber);
            rayCastFilter = RayCastFilter;


            #endregion

            DoFog   = true;
            FogNear = 20;
            FogFar  = Camera.FarPlane / 4;

            Envrio envr = new Envrio(vxEngine, vxEngine.vxContentManager.LoadModel("Models/courtyard/td_courtyard"), Vector3.Zero);

            //Envrio envr = new Envrio(vxEngine, vxEngine.vxContentManager.LoadModel("Models/castle/mdl_castle"), new Vector3(0, 1, 0));

            //waterItems.Add(new vxWaterEntity(vxEngine, Vector3.Up, new Vector3(50, 0.25f, 50)));


            envr.SpecularIntensity = 1;
            //envr.SpecularIntensity = 100;
            //envr.SpecularPower = 5f;
            //envr.DoFog = false;

            light = new vxLightEntity(vxEngine, new Vector3(0, 2, 0), LightType.Point, Color.Orange, 10, 1);

            //This is a little convenience method used to extract vertices and indices from a model.
            //It doesn't do anything special; any approach that gets valid vertices and indices will work.

                        #if !TECHDEMO_PLTFRM_GL
            ModelDataExtractor.GetVerticesAndIndicesFromModel(envr.vxModel.ModelMain, out staticTriangleVertices, out staticTriangleIndices);

            //var staticMesh = new StaticMesh(staticTriangleVertices, staticTriangleIndices, new AffineTransform(Matrix3X3.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi), new Vector3(0, -10, 0)));
            var staticMesh = new StaticMesh(staticTriangleVertices, staticTriangleIndices,
                                            new AffineTransform(new Vector3(1),
                                                                Quaternion.CreateFromRotationMatrix(Matrix.CreateRotationX(-MathHelper.PiOver2) * Matrix.CreateRotationY(0)),
                                                                new Vector3(0)));
            staticMesh.Sidedness = TriangleSidedness.Counterclockwise;

            BEPUPhyicsSpace.Add(staticMesh);
            BEPUDebugDrawer.Add(staticMesh);
                        #endif

            int size = 100;
            BEPUPhyicsSpace.Add(new Box(new Vector3(0, -5, 0), size, 10, size));

            vxTabPage Straights = new vxTabPage(vxEngine, tabControl, "Items");
            tabControl.AddItem(Straights);

            vxScrollPanel ScrollPanel_GeneralItemsPage = new vxScrollPanel(new Vector2(0, 0),
                                                                           vxEngine.GraphicsDevice.Viewport.Width - 150, vxEngine.GraphicsDevice.Viewport.Height - 75);

            //Cubes
            ScrollPanel_GeneralItemsPage.AddItem(new vxScrollPanelSpliter(vxEngine, "Items"));
            ScrollPanel_GeneralItemsPage.AddItem(RegisterNewSandboxItem(WoodenCrate.EntityDescription));

            //Add the scrollpanel to the slider tab page.
            Straights.AddItem(ScrollPanel_GeneralItemsPage);

            //IndexedCubeTest cube = new IndexedCubeTest(vxEngine, new Vector3(4, 4, 0));


            Teapot t = new Teapot((GameEngine)vxEngine, new Vector3(4, 4, 0));
            t.SetMesh(Matrix.CreateTranslation(new Vector3(4, 2, 0)), true, true);

            ConcreteCube cc = new ConcreteCube((GameEngine)vxEngine, new Vector3(0, 5, 0));
            cc.SetMesh(Matrix.CreateTranslation(new Vector3(0, 2, 0)), true, true);


            ModelObjs mo = new ModelObjs((GameEngine)vxEngine, new Vector3(-4, 4, 0));
            mo.SetMesh(Matrix.CreateTranslation(new Vector3(0, 2, 8)), true, true);

            vxEngine.InputManager.ShowCursor = true;
        }