Exemplo n.º 1
0
        private void HandleInput(GameTime gameTime, GamePadState gamePadState,
                                 KeyboardState keyboardState)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            //Handle input for selecting meshes
            if (((gamePadState.Buttons.X == ButtonState.Pressed) &&
                 (lastGamePadState.Buttons.X == ButtonState.Released)) ||
                (keyboardState.IsKeyDown(Keys.Tab) &&
                 lastKeyboardState.IsKeyUp(Keys.Tab)))
            {
                //switch the active mesh
                activeMesh = (activeMesh + 1) % sampleMeshes.Length;
            }


            //Handle input for selecting the active technique
            if (((gamePadState.Buttons.Y == ButtonState.Pressed) &&
                 (lastGamePadState.Buttons.Y == ButtonState.Released)) ||
                (keyboardState.IsKeyDown(Keys.Space) &&
                 lastKeyboardState.IsKeyUp(Keys.Space)))
            {
                activeTechnique = (activeTechnique + 1) % effect.Techniques.Count;
            }


            //handle mesh rotation inputs
            float dx =
                SampleArcBallCamera.ReadKeyboardAxis(keyboardState, Keys.Left,
                                                     Keys.Right) + gamePadState.ThumbSticks.Left.X;
            float dy =
                SampleArcBallCamera.ReadKeyboardAxis(keyboardState, Keys.Down,
                                                     Keys.Up) + gamePadState.ThumbSticks.Left.Y;

            //apply mesh rotation to world matrix
            if (dx != 0)
            {
                world = world * Matrix.CreateFromAxisAngle(camera.Up,
                                                           elapsedTime * dx);
            }
            if (dy != 0)
            {
                world = world * Matrix.CreateFromAxisAngle(camera.Right,
                                                           elapsedTime * -dy);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load the graphics content.
        /// </summary>
        protected override void LoadContent()
        {
            //Set up the reference grid and sample camera
            grid           = new SampleGrid();
            grid.GridColor = Color.LimeGreen;
            grid.GridScale = 1.0f;
            grid.GridSize  = 32;
            grid.LoadGraphicsContent(graphics.GraphicsDevice);


            camera = new SampleArcBallCamera(
                SampleArcBallCameraMode.RollConstrained);
            camera.Distance = 3;
            //orbit the camera so we're looking down the z=-1 axis
            //the acr-ball camera is traditionally oriented to look
            //at the "front" of an object
            camera.OrbitRight(MathHelper.Pi);
            //orbit up a bit for perspective
            camera.OrbitUp(.2f);

            //load meshes
            sampleMeshes    = new Model[5];
            sampleMeshes[0] = Content.Load <Model>("Cube");
            sampleMeshes[1] = Content.Load <Model>("SphereHighPoly");
            sampleMeshes[2] = Content.Load <Model>("SphereLowPoly");
            sampleMeshes[3] = Content.Load <Model>("Cylinder");
            sampleMeshes[4] = Content.Load <Model>("Cone");

            //load texture
            modelTexture = Content.Load <Texture2D>("Clouds");

            //load the effect
            effect = Content.Load <Effect>("TexturesAndColors");

            // The parameters are no longer shared, as they were in the previous
            // Shader Series sample.  There is only one effect, that uses multiple
            // techniques, so there is only one instance of the parameters.
            worldParameter          = effect.Parameters["world"];
            viewParameter           = effect.Parameters["view"];
            projectionParameter     = effect.Parameters["projection"];
            lightColorParameter     = effect.Parameters["lightColor"];
            lightDirectionParameter = effect.Parameters["lightDirection"];
            ambientColorParameter   = effect.Parameters["ambientColor"];
            modelTextureParameter   = effect.Parameters["modelTexture"];

            //create the spritebatch for debug text
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

            //load the sprite font for debug text
            debugTextFont = Content.Load <SpriteFont>("DebugText");



            //Calculate the projection properties first on any
            //load callback.  That way if the window gets resized,
            //the perspective matrix is updated accordingly
            float aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width /
                                (float)graphics.GraphicsDevice.Viewport.Height;
            float fov = MathHelper.PiOver4 * aspectRatio * 3 / 4;

            projection = Matrix.CreatePerspectiveFieldOfView(fov,
                                                             aspectRatio, .1f, 1000f);

            //create a default world matrix
            world = Matrix.Identity;


            //grid requires a projection matrix to draw correctly
            grid.ProjectionMatrix = projection;

            //Set the grid to draw on the x/z plane around the origin
            grid.WorldMatrix = Matrix.Identity;

            // calculate the safe left and top edges of the screen
            safeBounds = new Vector2(
                (float)graphics.GraphicsDevice.Viewport.X +
                (float)graphics.GraphicsDevice.Viewport.Width * 0.1f,
                (float)graphics.GraphicsDevice.Viewport.Y +
                (float)graphics.GraphicsDevice.Viewport.Height * 0.1f
                );
        }