/******************************************************************************/ /*! \file Graphics.cs \author Kevin Carey Manages graphics related functionality Copyright © 2007 DigiPen(USA) Corporation. All rights reserved. */ /******************************************************************************/ using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework; using System.Collections.Generic; namespace Shooting_Range { // Graphics Class // Manages graphics related functionality class GraphicsManager { // Constructor public GraphicsManager(Game1 game) { // Initialize members m_Graphics = new GraphicsDeviceManager(game); m_Content = new ContentManager(game.Services); m_WorldObjectList = new List(); m_ViewObjectList = new List(); m_SpriteList = new List(); m_TextList = new List(); m_BulletRayList = new List(); m_Camera = new Camera(); } // Initialize functions // Stuff that cant be done while the GraphicsDevice is null // InitializeSpriteBatch function public void InitializeSpriteBatch() { // Create a new sprite batch m_SpriteBatch = new SpriteBatch(m_Graphics.GraphicsDevice); } // InitializeBulletRayEffect function public void InitializeBulletRayEffect() { // Make a new effect m_BulletRayEffect = new BasicEffect(m_Graphics.GraphicsDevice, null); // Set the world transform m_BulletRayEffect.World = Matrix.Identity; // Allow for alpha blending m_Graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true; m_Graphics.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha; m_Graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha; } // AddWorldObject function // Adds an object to the world object list public void AddWorldObject(Object obj) { // Add the object to the list m_WorldObjectList.Add(obj); // Sort the list by depth m_WorldObjectList.Sort(); } // AddViewObject function // Adds an object to the view object list public void AddViewObject(Object obj) { // Add the object to the list m_ViewObjectList.Add(obj); } // AddSprite function public void AddSprite(Sprite sprite) { // Add the sprite to the list m_SpriteList.Add(sprite); } // AddText function public void AddText(Text text) { // Add the text to the list m_TextList.Add(text); } // AddBulletRay function public void AddBulletRay(BulletRay bulletRay) { // Add the bullet ray to the list m_BulletRayList.Add(bulletRay); } // DrawWorldObjects function // Draws all of the objects that have position in world space public void DrawWorldObjects() { // LookAt matrix (constant per frame) Matrix lookAt = Matrix.CreateLookAt(m_Camera.WorldPosition, m_Camera.LookAtPosition, m_Camera.Up); // Projection matrix (constant per frame) Matrix projection = Matrix.CreatePerspectiveFieldOfView(m_Camera.FOV, m_Camera.AspectRatio, m_Camera.NearPlane, m_Camera.FarPlane); // For each object in world space for (int i = 0; i < m_WorldObjectList.Count; ++i) { // If the object is inactive if (!m_WorldObjectList[i].IsActive) { // Skip it continue; } // Copy any parent transforms Model model = m_WorldObjectList[i].ModelData; Matrix[] transforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(transforms); // For each mesh in the model foreach (ModelMesh mesh in model.Meshes) { // For each effect in the mesh foreach (BasicEffect effect in mesh.Effects) { // Enable lighting effect.EnableDefaultLighting(); // Set the world transform effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateScale(m_WorldObjectList[i].Scale) * Matrix.CreateFromYawPitchRoll(MathHelper.ToRadians(m_WorldObjectList[i].YawY), MathHelper.ToRadians(m_WorldObjectList[i].PitchX), MathHelper.ToRadians(m_WorldObjectList[i].RollZ)) * Matrix.CreateTranslation(m_WorldObjectList[i].Position); // Set the view transform effect.View = lookAt; // Set the projection transform effect.Projection = projection; } //Draw the mesh, will use the effects set above. mesh.Draw(); } } } // DrawViewObjects function // Draws all of the objects that have position in view space public void DrawViewObjects() { // LookAt matrix (constant per frame, used more than once) Matrix lookAt = Matrix.CreateLookAt(m_Camera.WorldPosition, m_Camera.LookAtPosition, m_Camera.Up); // Projection matrix (constant per frame) Matrix projection = Matrix.CreatePerspectiveFieldOfView(m_Camera.FOV, m_Camera.AspectRatio, m_Camera.NearPlane, m_Camera.FarPlane); // For each object in view space for (int i = 0; i < m_ViewObjectList.Count; ++i) { // If the object is inactive if (!m_ViewObjectList[i].IsActive) { // Skip it continue; } // Copy any parent transforms Model model = m_ViewObjectList[i].ModelData; Matrix[] transforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(transforms); // For each mesh in the model foreach (ModelMesh mesh in model.Meshes) { // For each effect in the mesh foreach (BasicEffect effect in mesh.Effects) { // Enable lighting effect.EnableDefaultLighting(); // Set the world transform effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateScale(m_ViewObjectList[i].Scale) * Matrix.CreateFromYawPitchRoll(MathHelper.ToRadians(m_ViewObjectList[i].YawY), MathHelper.ToRadians(m_ViewObjectList[i].PitchX), MathHelper.ToRadians(m_ViewObjectList[i].RollZ)) * Matrix.CreateTranslation(m_ViewObjectList[i].Position) * Matrix.Invert(lookAt); // Set the view transform effect.View = lookAt; // Set the projection transform effect.Projection = projection; } //Draw the mesh, will use the effects set above. mesh.Draw(); } } } // DrawSprites function // Draws all of the sprites public void DrawSprites() { // Begin drawing sprites m_SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState); // For each sprite for (int i = 0; i < m_SpriteList.Count; ++i) { // If the sprite is active if (m_SpriteList[i].IsActive) { // Draw the sprite m_SpriteBatch.Draw(m_SpriteList[i].Texture, m_SpriteList[i].Rect, Color.White); } } // End drawing sprites m_SpriteBatch.End(); } // DrawText function // Draws all of the Text objects public void DrawText() { // Begin drawing text m_SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState); // For each text for (int i = 0; i < m_TextList.Count; ++i) { // If the text is active if (m_TextList[i].IsActive) { // Draw the sprite m_SpriteBatch.DrawString(m_TextList[i].Font, m_TextList[i].TextString, m_TextList[i].Position, m_TextList[i].Color, 0.0f, Vector2.Zero, m_TextList[i].Scale, SpriteEffects.None, 0.0f); } } // End drawing text m_SpriteBatch.End(); } // DrawBulletRays function // Draws all of the BulletRay objects public void DrawBulletRays() { // LookAt matrix (constant per frame) Matrix lookAt = Matrix.CreateLookAt(m_Camera.WorldPosition, m_Camera.LookAtPosition, m_Camera.Up); // Projection matrix (constant per frame) Matrix projection = Matrix.CreatePerspectiveFieldOfView(m_Camera.FOV, m_Camera.AspectRatio, m_Camera.NearPlane, m_Camera.FarPlane); // Set the view transform m_BulletRayEffect.View = lookAt; // Set the projection transform m_BulletRayEffect.Projection = projection; // Begin the effect m_BulletRayEffect.Begin(); // For each bullet ray for (int i = 0; i < m_BulletRayList.Count; ++i) { // If the object is inactive if (!m_BulletRayList[i].IsActive) { // Skip it continue; } // Set the alpha of the effect if (m_BulletRayList[i].Alpha > 1.0f) { m_BulletRayEffect.Alpha = 1.0f; } else { m_BulletRayEffect.Alpha = m_BulletRayList[i].Alpha; } // For each pass in the effect foreach (EffectPass pass in m_BulletRayEffect.CurrentTechnique.Passes) { // Begin the pass pass.Begin(); // Draw the ray m_Graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, m_BulletRayList[i].LinePoints, 0, 1); // End the pass pass.End(); } } // End the effect m_BulletRayEffect.End(); } // GraphicsDeviceManager get public GraphicsDeviceManager DeviceManager { get { return m_Graphics; } } // ContentManager get public ContentManager Content { get { return m_Content; } } // Camera get public Camera CameraData { get { return m_Camera; } } // WorldObjectsList get public List WorldObjects { get { return m_WorldObjectList; } } // BulletRayList get public List BulletRayList { get { return m_BulletRayList; } } // GraphicsDeviceManager private GraphicsDeviceManager m_Graphics; // ContentManager private ContentManager m_Content; // SpriteBatch private SpriteBatch m_SpriteBatch; // Camera private Camera m_Camera; // List of objects in world space private List m_WorldObjectList; // List of objects in view space private List m_ViewObjectList; // List of sprites private List m_SpriteList; // List of texts private List m_TextList; // List of BulletRays private List m_BulletRayList; // Effect for BulletRays private BasicEffect m_BulletRayEffect; } }