public void Draw(Camera camera, Effect effect, bool drawWall) { device.SamplerStates[0] = SamplerState.LinearClamp; ConfigureShader(camera, effect); DrawFloors(camera, effect); if (drawWall) { DrawWalls(camera, effect); } }
/// <summary> /// Draw the enemy model /// </summary> /// <param name="camera">The camera that is seeing this model</param> public void Draw(Camera camera) { // apply transformation to whole model model.Root.Transform = Matrix.Identity * Matrix.CreateScale(0.001f) * Matrix.CreateRotationY(Rotation) * Matrix.CreateTranslation(Position); model.CopyAbsoluteBoneTransformsTo(boneTransforms); // rotate the enemy legs model.Bones["left_leg"].Transform = Matrix.CreateRotationX(MathHelper.ToRadians(leftLegRotation)) * leftLegTransform; model.Bones["right_leg"].Transform = Matrix.CreateRotationX(MathHelper.ToRadians(rightLegRotation)) * rightLegTransform; foreach (ModelMesh mesh in model.Meshes) { foreach (BasicEffect e in mesh.Effects) { e.World = boneTransforms[mesh.ParentBone.Index]; e.View = camera.View; e.Projection = camera.Projection; e.EnableDefaultLighting(); } mesh.Draw(); } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // initialise sound for camera wallCollisionAudio = Content.Load<SoundEffect>("Sounds/wallcollision_sound"); leftFootStepSound = Content.Load<SoundEffect>("Sounds/leftfootstep_sound"); rightFootStepSound = Content.Load<SoundEffect>("Sounds/rightfootstep_sound"); // Setup camera camera = new Camera( new Vector3(0.5f, 0.5f, 0.5f), 0, GraphicsDevice.Viewport.AspectRatio, 0.05f, 100f, wallCollisionAudio, leftFootStepSound, rightFootStepSound); // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch( GraphicsDevice ); // Load shader and set defaults effect = Content.Load<Effect>("shader"); effect.Parameters["material"].StructureMembers["ambient"].SetValue(new Vector4(0.65f, 0.65f, 0.6f, 1.0f)); effect.Parameters["fog"].StructureMembers["FogEnabled"].SetValue(false); effect.Parameters["light"].StructureMembers["radius"].SetValue(0.1f); // Load sound effects wallCollisionAudio = Content.Load<SoundEffect>("Sounds/wallcollision_sound"); leftFootStepSound = Content.Load<SoundEffect>("Sounds/leftfootstep_sound"); rightFootStepSound = Content.Load<SoundEffect>("Sounds/rightfootstep_sound"); musicDay = Content.Load<SoundEffect>("Sounds/medievil_music"); musicNight = Content.Load<SoundEffect>("Sounds/medievil_music2"); currentMusic = musicDay.CreateInstance(); currentMusic.Volume = 1f; currentMusic.IsLooped = true; music = true; maze.LoadContent(Content); // Setup camera camera = new Camera( new Vector3(0.5f, 0.5f, 0.5f), 0, GraphicsDevice.Viewport.AspectRatio, 0.05f, 100f, wallCollisionAudio, leftFootStepSound, rightFootStepSound); enemy = new Enemy(this, GraphicsDevice, Content.Load<Model>("enemy"), new Vector3(2.5f, 0, 2.5f), leftFootStepSound, rightFootStepSound); enemy.Apply3DAudio(listener, enemy.Position, currentMusic); currentMusic.Play(); }
// helper function to draw walls private void DrawWalls(Camera camera, Effect effect) { // iterate each wall and draw for (int i = 0; i < wallEffects.Count(); i++) { effect.Parameters["colorMapTexture"].SetValue(wallTextures[i]); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); device.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, wallVertices[i], 0, wallVertices[i].Count() / 3); } } }
// helper function to draw the floor private void DrawFloors(Camera camera, Effect effect) { effect.Parameters["colorMapTexture"].SetValue(floorTexture); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); device.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, floorVertices, 0, floorVertices.Count() / 3); } }
/// <summary> /// Configures shader with parameters /// </summary> /// <param name="camera">camera used for world,view and projection</param> /// <param name="effect">applies shader to effect</param> private void ConfigureShader(Camera camera, Effect effect) { // Calculate the transposed inverse of the world matrix. Matrix worldInvTrans = Matrix.Transpose(Matrix.Invert(Matrix.Identity)); // Calculate combined world-view-projection matrix. Matrix worldViewProj = Matrix.Identity * camera.View * camera.Projection; // Create camera position from camera rotation Matrix rotationMatrix = Matrix.CreateFromYawPitchRoll(camera.RotationX, -camera.RotationY, 0); Vector3 direction = Vector3.Transform(Vector3.Backward, rotationMatrix); // Set current lighting technieque for flahs light spot effect.CurrentTechnique = effect.Techniques["PerPixelSpotLighting"]; // Set shader matrix parameters. effect.Parameters["worldMatrix"].SetValue(Matrix.Identity); effect.Parameters["worldInverseTransposeMatrix"].SetValue(worldInvTrans); effect.Parameters["worldViewProjectionMatrix"].SetValue(worldViewProj); // Set the shader camera position parameter. effect.Parameters["cameraPos"].SetValue(camera.Position); // Set the shader global ambiance parameters. effect.Parameters["globalAmbient"].SetValue(Color.White.ToVector4()); // Set the shader lighting parameters. effect.Parameters["light"].StructureMembers["dir"].SetValue(Vector3.Transform(Vector3.Backward, rotationMatrix)); effect.Parameters["light"].StructureMembers["pos"].SetValue(camera.Position); effect.Parameters["light"].StructureMembers["ambient"].SetValue(Color.White.ToVector4()); effect.Parameters["light"].StructureMembers["diffuse"].SetValue(Color.White.ToVector4()); effect.Parameters["light"].StructureMembers["specular"].SetValue(Color.White.ToVector4()); effect.Parameters["light"].StructureMembers["spotInnerCone"].SetValue(MathHelper.ToRadians(20.0f)); effect.Parameters["light"].StructureMembers["spotOuterCone"].SetValue(MathHelper.ToRadians(30.0f)); //effect.Parameters["light"].StructureMembers["radius"].SetValue(10.0f); - flash toggle moved to MazeGame // Set the shader material parameters. // ambient set in MazeGame toggleAmbient effect.Parameters["material"].StructureMembers["diffuse"].SetValue(new Vector4(0.50754f, 0.50754f, 0.50754f, 1.0f)); effect.Parameters["material"].StructureMembers["emissive"].SetValue(Color.Black.ToVector4()); effect.Parameters["material"].StructureMembers["specular"].SetValue(new Vector4(0.508273f, 0.508273f, 0.508273f, 1.0f)); effect.Parameters["material"].StructureMembers["shininess"].SetValue(51.2f); // Set the shader fog parameters. // enable and disabled set in MazeGame fog toggle effect.Parameters["fog"].StructureMembers["FogStart"].SetValue(0.5f); effect.Parameters["fog"].StructureMembers["FogEnd"].SetValue(2.5f); effect.Parameters["fog"].StructureMembers["FogColor"].SetValue(Color.Black.ToVector3()); }