using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace WindowsGame1 { /// /// This is the main type for your game /// public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; float specularPower = 5f; float specularIntensity = 64f; EntityModel testModel; EntityModel testPlane; EntitySprite cursorPixel; SpriteBatch m_SpriteBatch; SpriteFont m_SpriteFont; Matrix projectionMatrix; Matrix viewMatrix; //EffectParameter light; //EffectParameter light2; //EffectParameter lightArray; Vector3 lightPosition = Vector3.Backward * 2; Vector3 light2Position = Vector3.Right * 2; Vector2 lightAngle = Vector2.Zero; Vector2 light2Angle = Vector2.Zero; //Effect basicShader; AnimatedSprite testAnimation; int shaderTechniqueIndex = 0; KeyState lastTechniqueKeyState = KeyState.Up; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); } /// /// LoadContent will be called once per game and is the place to load /// all of your content. /// protected override void LoadContent() { Vector3 cameraPosition = new Vector3(0, 0, -50); //basicShader = Content.Load("BasicShader10"); LightingManager.Initialize(this); //LightingManager.MaxShaderVersion = ShaderProfile.PS_1_1; //lightArray = basicShader.Parameters["lights"]; //light = basicShader.Parameters["light"]; //light.StructureMembers["range"].SetValue(1000f); //light.StructureMembers["falloff"].SetValue(0f); //light2 = lightArray.Elements[1]; if (LightingManager.MaxShaderVersion >= ShaderProfile.PS_2_0) { LightingManager.SetLightColor(1, Color.White.ToVector4()); LightingManager.SetLightPosition(1, light2Position); LightingManager.SetLightFalloff(1, 0f); LightingManager.SetLightRange(1, 1000f); } //light2.StructureMembers["range"].SetValue(1000f); //light2.StructureMembers["falloff"].SetValue(0f); //light2.StructureMembers["position"].SetValue(light2Position); //light2.StructureMembers["color"].SetValue(Color.White.ToVector4()); //basicShader.Parameters["numLights"].SetValue(2); viewMatrix = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up); projectionMatrix = Matrix.CreatePerspectiveFieldOfView(.6f, 4f / 3f, .1f, 100); LightingManager.CurrentEffect.Parameters["View"].SetValue(viewMatrix); LightingManager.CurrentEffect.Parameters["Projection"].SetValue(projectionMatrix); LightingManager.AmbientColor = new Vector4(.1f, .1f, .1f, 1); LightingManager.SetLightColor(0,new Vector4(.4f,.4f,.4f,1f)); LightingManager.SetLightPosition(0,lightPosition); if (LightingManager.MaxShaderVersion >= ShaderProfile.PS_2_0) { LightingManager.SetLightRange(0, 1000f); LightingManager.SetLightFalloff(0, 0f); } //basicShader.Parameters["ambientColor"].SetValue(new Vector4(.1f,.1f,.1f,1.0f)); //light.StructureMembers["position"].SetValue(lightPosition); LightingManager.CurrentEffect.Parameters["cameraPosition"].SetValue(cameraPosition); //basicShader.CurrentTechnique = basicShader.Techniques[shaderTechniqueIndex]; // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); EntitySprite testSprite = new EntitySprite(this,Content.Load("TestTexture"),new Vector2(100,100)); testSprite.SpriteBatch = spriteBatch; testAnimation = new AnimatedSprite(this, Content.Load("TestSpriteSheet"), new Vector2(200, 300), new Vector2(64, 64), 16); testAnimation.SpriteBatch = spriteBatch; Model temp = Content.Load("Water_Fountain_10x10"); foreach (ModelMesh mesh in temp.Meshes) { foreach (ModelMeshPart part in mesh.MeshParts) { part.Effect = LightingManager.CurrentEffect; } } Model temp2 = Content.Load("testPlane"); temp2.Meshes[0].MeshParts[0].Effect = LightingManager.CurrentEffect; testModel = new EntityModel(this, temp); testPlane = new EntityModel(this, temp2); Texture2D pixelText = Content.Load("singlePixel"); cursorPixel = new EntitySprite(this, pixelText); cursorPixel.SpriteBatch = spriteBatch; testModel.TransformationMatrix = testModel.TransformationMatrix * Matrix.CreateRotationX((float)Math.PI/2); testModel.Texture = Content.Load("TestSpriteSheet"); //testModel.TextureOffset = new Vector2(64, 0); testPlane.TransformationMatrix = testPlane.TransformationMatrix * Matrix.CreateTranslation(new Vector3(0,-2,0)); //testPlane.DrawOrder = 0; testPlane.Texture = Content.Load("TestTexture"); //testModel.DrawOrder = 1; this.Components.Add(testSprite); this.Components.Add(testModel); this.Components.Add(testPlane); this.Components.Add(testAnimation); this.Components.Add(cursorPixel); foreach (GameComponent comp in this.Components) comp.Initialize(); GraphicsDevice.RenderState.DepthBufferEnable = true; GraphicsDevice.RenderState.DepthBufferWriteEnable = true; //GraphicsDevice.RenderState.DepthBufferFunction = CompareFunction.Always; m_SpriteBatch = new SpriteBatch(GraphicsDevice); m_SpriteFont = Content.Load(@"Fonts\\SpriteFont1"); // TODO: use this.Content to load your game content here } /// /// UnloadContent will be called once per game and is the place to unload /// all content. /// protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// /// Provides a snapshot of timing values. protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); if(Keyboard.GetState().IsKeyDown(Keys.Space)) { testAnimation.Animate(true); } else { testAnimation.Animate(false); } if (Keyboard.GetState().IsKeyDown(Keys.OemPlus)) { specularPower += .1f; } if (Keyboard.GetState().IsKeyDown(Keys.OemMinus)) { specularPower -= .1f; } if (Keyboard.GetState().IsKeyDown(Keys.OemOpenBrackets)) { specularIntensity += 1f; } if (Keyboard.GetState().IsKeyDown(Keys.OemCloseBrackets)) { specularIntensity -= 1f; } if (Keyboard.GetState().IsKeyDown(Keys.A)) { lightAngle.X += .1f; } if (Keyboard.GetState().IsKeyDown(Keys.D)) { lightAngle.X -= .1f; } if (Keyboard.GetState().IsKeyDown(Keys.W)) { lightAngle.Y += .1f; } if (Keyboard.GetState().IsKeyDown(Keys.S)) { lightAngle.Y -= .1f; } if (Keyboard.GetState().IsKeyDown(Keys.NumPad4)) { light2Angle.X += .1f; } if (Keyboard.GetState().IsKeyDown(Keys.NumPad6)) { light2Angle.X -= .1f; } if (Keyboard.GetState().IsKeyDown(Keys.NumPad8)) { light2Angle.Y += .1f; } if (Keyboard.GetState().IsKeyDown(Keys.NumPad5)) { light2Angle.Y -= .1f; } if (lightAngle.X < -MathHelper.Pi) { lightAngle.X += MathHelper.TwoPi; } else if (lightAngle.X > MathHelper.Pi) { lightAngle.X -= MathHelper.TwoPi; } if (light2Angle.X < -MathHelper.Pi) { light2Angle.X += MathHelper.TwoPi; } else if (light2Angle.X > MathHelper.Pi) { light2Angle.X -= MathHelper.TwoPi; } lightAngle = Vector2.Clamp(lightAngle, new Vector2(-MathHelper.TwoPi, -MathHelper.PiOver2), new Vector2(MathHelper.TwoPi, MathHelper.PiOver2)); lightPosition = new Vector3((float)Math.Cos(lightAngle.X) * 2, (float)Math.Sin(lightAngle.Y) * 2, (float)Math.Sin(lightAngle.X) * 2); light2Angle = Vector2.Clamp(light2Angle, new Vector2(-MathHelper.TwoPi, -MathHelper.PiOver2), new Vector2(MathHelper.TwoPi, MathHelper.PiOver2)); light2Position = new Vector3((float)Math.Cos(light2Angle.X) * 2, (float)Math.Sin(light2Angle.Y) * 2, (float)Math.Sin(light2Angle.X) * 2); if (Keyboard.GetState().IsKeyDown(Keys.T) && lastTechniqueKeyState != KeyState.Down) { lastTechniqueKeyState = KeyState.Down; shaderTechniqueIndex = (shaderTechniqueIndex + 1) % LightingManager.CurrentEffect.Techniques.Count; LightingManager.CurrentEffect.CurrentTechnique = LightingManager.CurrentEffect.Techniques[shaderTechniqueIndex]; } else if (!Keyboard.GetState().IsKeyDown(Keys.T)) { lastTechniqueKeyState = KeyState.Up; } testModel.SpecularIntensity = specularIntensity; testModel.SpecularPower = specularPower; //testModel.TransformationMatrix *= Matrix.CreateTranslation(Vector3.Right * .01f); //testModel.TransformationMatrix *= Matrix.CreateTranslation(Vector3.Forward * .01f); LightingManager.SetLightPosition(0, lightPosition); if (LightingManager.MaxShaderVersion >= ShaderProfile.PS_2_0) { LightingManager.SetLightPosition(1, light2Position); } // light2.StructureMembers["position"].SetValue(light2Position); cursorPixel.Position = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); LightingManager.ApplyLightChanges(); //basicShader.CommitChanges(); testModel.TransformationMatrix *= Matrix.CreateRotationY(.01f); base.Update(gameTime); } /// /// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.Black); // TODO: Add your drawing code here base.Draw(gameTime); //m_SpriteBatch.Begin(); //m_SpriteBatch.DrawString(m_SpriteFont, "A", new Vector2(Mouse.GetState().X, Mouse.GetState().Y), Color.White); //m_SpriteBatch.End(); bool insideBoundingSphere = false; Vector3 vert1 = Vector3.Zero; Vector3 vert2 = Vector3.Zero; Vector3 vert3 = Vector3.Zero; Vector3? pointInSpace = Vector3.Zero; GraphicsDevice.RenderState.FillMode = FillMode.Solid; float? temp = Picker.RayIntersectsModel(Picker.CalculateCursorRay(new Vector2(Mouse.GetState().X, Mouse.GetState().Y), projectionMatrix, viewMatrix, GraphicsDevice), testModel, out insideBoundingSphere, out vert1, out vert2, out vert3, out pointInSpace); if (temp != null) { m_SpriteBatch.Begin(); m_SpriteBatch.DrawString(m_SpriteFont, "Cursor Over Sphere at distance: " + temp, new Vector2(0, 0), Color.White); m_SpriteBatch.DrawString(m_SpriteFont, "Vert1: " + vert1, new Vector2(0, 20), Color.White); m_SpriteBatch.DrawString(m_SpriteFont, "Vert2: " + vert2, new Vector2(0, 40), Color.White); m_SpriteBatch.DrawString(m_SpriteFont, "Vert3: " + vert3, new Vector2(0, 60), Color.White); m_SpriteBatch.DrawString(m_SpriteFont, "Point In Space: " + pointInSpace, new Vector2(0, 80), Color.White); m_SpriteBatch.End(); } else { temp = Picker.RayIntersectsModel(Picker.CalculateCursorRay(new Vector2(Mouse.GetState().X, Mouse.GetState().Y), projectionMatrix, viewMatrix, GraphicsDevice), testPlane, out insideBoundingSphere, out vert1, out vert2, out vert3, out pointInSpace); if (temp != null) { m_SpriteBatch.Begin(); m_SpriteBatch.DrawString(m_SpriteFont, "Cursor Over Plane at distance: " + temp, new Vector2(0, 0), Color.White); m_SpriteBatch.DrawString(m_SpriteFont, "Vert1: " + vert1, new Vector2(0, 20), Color.White); m_SpriteBatch.DrawString(m_SpriteFont, "Vert2: " + vert2, new Vector2(0, 40), Color.White); m_SpriteBatch.DrawString(m_SpriteFont, "Vert3: " + vert3, new Vector2(0, 60), Color.White); m_SpriteBatch.DrawString(m_SpriteFont, "Point In Space: " + pointInSpace, new Vector2(0, 80), Color.White); m_SpriteBatch.End(); } } } } }