void IApplicationLifetimeAware.Started() { // Create our state state = new RendererState(); // Get the application's ContentManager and load the tank and sky ContentManager content = (Application.Current as App).Content; tank = new Tank(); tank.Load(content, state); sky = content.Load <Sky>("sky"); // Create the ground plane groundEffect = new BasicEffect(SharedGraphicsDeviceManager.Current.GraphicsDevice) { TextureEnabled = true, Texture = content.Load <Texture2D>("rocks"), LightingEnabled = false, }; const float groundHalfSize = 50f; const float groundTiling = 15f; groundVertices = new VertexBuffer(SharedGraphicsDeviceManager.Current.GraphicsDevice, typeof(VertexPositionTexture), 4, BufferUsage.WriteOnly); groundVertices.SetData(new[] { new VertexPositionTexture(new Vector3(-groundHalfSize, 0, -groundHalfSize), new Vector2(0, 0)), new VertexPositionTexture(new Vector3(groundHalfSize, 0, -groundHalfSize), new Vector2(groundTiling, 0)), new VertexPositionTexture(new Vector3(-groundHalfSize, 0, groundHalfSize), new Vector2(0, groundTiling)), new VertexPositionTexture(new Vector3(groundHalfSize, 0, groundHalfSize), new Vector2(groundTiling, groundTiling)), }); }
/// <summary> /// Draws the tank model, using the current animation settings. /// </summary> public void Draw(Matrix world, Matrix view, Matrix projection, RendererState rendererState) { // Set the world matrix as the root transform of the model. tankModel.Root.Transform = world; // Calculate matrices based on the current animation position. Matrix wheelRotation = Matrix.CreateRotationX(wheelRotationValue); Matrix steerRotation = Matrix.CreateRotationY(steerRotationValue); Matrix turretRotation = Matrix.CreateRotationY(turretRotationValue); Matrix cannonRotation = Matrix.CreateRotationX(cannonRotationValue); Matrix hatchRotation = Matrix.CreateRotationX(hatchRotationValue); // Apply matrices to the relevant bones. leftBackWheelBone.Transform = wheelRotation * leftBackWheelTransform; rightBackWheelBone.Transform = wheelRotation * rightBackWheelTransform; leftFrontWheelBone.Transform = wheelRotation * leftFrontWheelTransform; rightFrontWheelBone.Transform = wheelRotation * rightFrontWheelTransform; leftSteerBone.Transform = steerRotation * leftSteerTransform; rightSteerBone.Transform = steerRotation * rightSteerTransform; turretBone.Transform = turretRotation * turretTransform; cannonBone.Transform = cannonRotation * cannonTransform; hatchBone.Transform = hatchRotation * hatchTransform; // Look up combined bone matrices for the entire model. tankModel.CopyAbsoluteBoneTransformsTo(boneTransforms); // Reset some graphics states to make the 3D render properly var device = SharedGraphicsDeviceManager.Current.GraphicsDevice; device.BlendState = BlendState.Opaque; device.RasterizerState = rendererState.DrawWireframe ? wireframeState : RasterizerState.CullCounterClockwise; device.DepthStencilState = DepthStencilState.Default; // Draw the model. foreach (ModelMesh mesh in tankModel.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.World = boneTransforms[mesh.ParentBone.Index]; effect.View = view; effect.Projection = projection; effect.TextureEnabled = rendererState.EnableTexture; effect.LightingEnabled = rendererState.EnableLighting; // update the effect's lights with our state settings rendererState.Light1.Apply(effect.DirectionalLight0); rendererState.Light2.Apply(effect.DirectionalLight1); rendererState.Light3.Apply(effect.DirectionalLight2); } mesh.Draw(); } }
/// <summary> /// Loads the tank model. /// </summary> public void Load(ContentManager content, RendererState state) { if (tankModel != null) { return; } // Load the tank model from the ContentManager. tankModel = content.Load <Model>("tank"); // Look up shortcut references to the bones we are going to animate. leftBackWheelBone = tankModel.Bones["l_back_wheel_geo"]; rightBackWheelBone = tankModel.Bones["r_back_wheel_geo"]; leftFrontWheelBone = tankModel.Bones["l_front_wheel_geo"]; rightFrontWheelBone = tankModel.Bones["r_front_wheel_geo"]; leftSteerBone = tankModel.Bones["l_steer_geo"]; rightSteerBone = tankModel.Bones["r_steer_geo"]; turretBone = tankModel.Bones["turret_geo"]; cannonBone = tankModel.Bones["canon_geo"]; hatchBone = tankModel.Bones["hatch_geo"]; // Store the original transform matrix for each animating bone. leftBackWheelTransform = leftBackWheelBone.Transform; rightBackWheelTransform = rightBackWheelBone.Transform; leftFrontWheelTransform = leftFrontWheelBone.Transform; rightFrontWheelTransform = rightFrontWheelBone.Transform; leftSteerTransform = leftSteerBone.Transform; rightSteerTransform = rightSteerBone.Transform; turretTransform = turretBone.Transform; cannonTransform = cannonBone.Transform; hatchTransform = hatchBone.Transform; // Allocate the transform matrix array. boneTransforms = new Matrix[tankModel.Bones.Count]; // Set up the default lights in our RendererState BasicEffect effect = tankModel.Meshes[0].Effects[0] as BasicEffect; effect.EnableDefaultLighting(); state.Light1.ReadDataFrom(effect.DirectionalLight0); state.Light2.ReadDataFrom(effect.DirectionalLight1); state.Light3.ReadDataFrom(effect.DirectionalLight2); }