/// <summary> /// Test shadow mapping /// </summary> public static void TestShadowMapping() { Model testModel = null; Model testGround = null; TestGame.Start("TestShadowMapping", delegate { testModel = new Model("Building"); testGround = new Model("BackgroundGround"); }, delegate { if (Input.Keyboard.IsKeyDown(Keys.Tab)) BaseGame.ViewMatrix = Matrix.CreateLookAt( Mission.LookAtPosition + BaseGame.LightDirection * Mission.ViewDistance, Mission.LookAtPosition, new Vector3(0, 1, 0)); Matrix renderMatrix = Matrix.CreateScale(15) * Matrix.CreateRotationZ(0.85f) * Matrix.CreateTranslation(0, 0, -16); Matrix groundMatrix = Matrix.CreateScale(100); if (Input.Keyboard.IsKeyUp(Keys.LeftAlt) && Input.GamePadXPressed == false) { // Generate shadows ShaderEffect.shadowMapping.GenerateShadows( delegate { testModel.GenerateShadow(renderMatrix); }); // Render shadows ShaderEffect.shadowMapping.RenderShadows( delegate { testModel.UseShadow(renderMatrix); testGround.UseShadow(groundMatrix); }); } // if testModel.Render(renderMatrix); testGround.Render(groundMatrix); BaseGame.MeshRenderManager.Render(); if (Input.Keyboard.IsKeyUp(Keys.LeftAlt) && Input.GamePadXPressed == false) { ShaderEffect.shadowMapping.ShowShadows(); } // if if (Input.Keyboard.IsKeyDown(Keys.LeftShift) || Input.GamePadAPressed) { //if (Input.KeyboardRightPressed)// == false) ShaderEffect.shadowMapping.shadowMapTexture.RenderOnScreen( new Rectangle(10, 10, 256, 256)); //if (Input.KeyboardLeftPressed)// == false) ShaderEffect.shadowMapping.shadowMapBlur.SceneMapTexture. RenderOnScreen( new Rectangle(10 + 256 + 10, 10, 256, 256)); // Unused on xbox360 //#if !XBOX360 //ShaderEffect.shadowMapping.shadowMapBlur.BlurMapTexture. // RenderOnScreen( // new Rectangle(10 + 256 + 10+256+10, 10, 256, 256)); //#endif } // if (Input.Keyboard.IsKeyDown) //tst: //XnaShooterGame.GlowShader.Show(); TextureFont.WriteText(2, 510, "Press left Shift or A to show all shadow pass textures."); TextureFont.WriteText(2, 540, "Press Alt or X to skip shadow map rendering."); TextureFont.WriteText(2, 570, "Camera pos="+BaseGame.CameraPos);//+ //", "+ShaderEffect.shadowMapping.shadowMapTexture.ZBufferSurface); }); }
/// <summary> /// Helper method to simply add a new model for rendering. /// </summary> /// <param name="setModel">Model</param> /// <param name="renderMatrix">Render Matrix</param> internal void AddModelToRender(Model setModel, Matrix renderMatrix) { if (numOfModelsToRender >= MaxNumberOfModelsToRender) return; modelsToRender[numOfModelsToRender].model = setModel; modelsToRender[numOfModelsToRender].matrix = renderMatrix; numOfModelsToRender++; }
/// <summary> /// Allows the game to perform any initialization it needs. /// </summary> protected override void Initialize() { base.Initialize(); // Make sure mouse is centered Input.Update(); Input.MousePos = new Point( Window.ClientBounds.X + width / 2, Window.ClientBounds.Y + height / 2); Input.Update(); // Load menu textures mainMenuTexture = new Texture("MainMenu"); mouseCursorTexture = new Texture("MouseCursor"); hudTopTexture = new Texture("HudTop"); hudBottomTexture = new Texture("HudBottom"); // Load explosion effect //explosionTexture = new AnimatedTexture("Destroy"); // Load background landscape and wall models landscapeModels = new Model[] { new Model("BackgroundGround"), new Model("Building"), new Model("Building2"), new Model("Building3"), new Model("Building4"), new Model("Building5"), new Model("Kaktus"), new Model("Kaktus2"), new Model("KaktusBenny"), new Model("KaktusSeg"), }; shipModels = new Model[] { new Model("OwnShip"), new Model("Corvette"), new Model("SmallTransporter"), new Model("Firebird"), new Model("RocketFrigate"), new Model("Rocket"), new Model("Asteroid"), }; itemModels = new Model[] { new Model("ItemHealth"), new Model("ItemMg"), new Model("ItemGattling"), new Model("ItemPlasma"), new Model("ItemRockets"), new Model("ItemEmp"), }; // Create main menu screen gameScreens.Push(new MainMenu()); // Start game //gameScreens.Push(new Mission()); //inGame = true; // Start music Sound.StartMusic(); }
/// <summary> /// Render level background /// </summary> public void RenderLevelBackground(float levelPosition) { #if DEBUG // Landscape or unit ship models not initialized yet? // Can happen in certain unit tests, initialize them for us! if (landscapeModels == null) landscapeModels = new Model[] { new Model("BackgroundGround"), new Model("Building"), new Model("Building2"), new Model("Building3"), new Model("Building4"), new Model("Building5"), new Model("Kaktus"), new Model("Kaktus2"), new Model("KaktusBenny"), new Model("KaktusSeg"), }; if (shipModels == null) shipModels = new Model[] { new Model("OwnShip"), new Model("Corvette"), new Model("SmallTransporter"), new Model("Firebird"), new Model("RocketFrigate"), new Model("Rocket"), new Model("Asteroid"), }; if (itemModels == null) itemModels = new Model[] { new Model("ItemMg"), new Model("ItemGattling"), new Model("ItemPlasma"), new Model("ItemRockets"), new Model("ItemEmp"), }; #endif #region Update camera // Construct camera position, it will just move up. Vector3 cameraPosition = new Vector3(0, levelPosition, ViewDistance); // For widescreen look closer to the ground, else it does not fit on the screen if ((float)BaseGame.Width / (float)BaseGame.Height >= 1.5f) { cameraPosition.Z -= 10; cameraPosition.Y -= 2; } // if lookAtPosition = new Vector3(0, levelPosition + LookAheadYValue, GroundZValue); BaseGame.ViewMatrix = Matrix.CreateLookAt( cameraPosition, lookAtPosition, new Vector3(0, 1, 0)); if (Player.GameOver) { cameraPosition += new Vector3(0, 0, -20) + Vector3.TransformNormal(new Vector3(30, 0, 0), Matrix.CreateRotationZ(BaseGame.TotalTimeMs / 2593.0f)); BaseGame.ViewMatrix = Matrix.CreateLookAt( cameraPosition, lookAtPosition, new Vector3(0, 1, 0)); } // if #endregion #region Update landscape position // Show current landscape block and the next one with a wall // segment between them. We usually just see one block. int blockPosition = (int)((levelPosition + LookAheadYValue) / SegmentLength); if (blockPosition + 1 != generatedLandscapeSegmentNumber) { // Copy over last objects thisLandscapeSegmentObjects = nextLandscapeSegmentObjects; nextLandscapeSegmentObjects = GenerateLandscapeSegment(blockPosition + 1); } // if #endregion #region Prepare rendering Vector3 levelVector = new Vector3(0, levelPosition + LookAheadYValue - 5, 0); // Start new list numOfModelsToRender = 0; #endregion #region Render landscape Model landscapeModel = landscapeModels[(int)0]; Matrix landscapeScaleMatrix = Matrix.CreateScale( LandscapeModelSize[(int)0]); AddModelToRender(landscapeModel, landscapeScaleMatrix * Matrix.CreateTranslation( new Vector3(0, blockPosition * SegmentLength, 0))); AddModelToRender(landscapeModel, landscapeScaleMatrix * Matrix.CreateTranslation( new Vector3(0, blockPosition * SegmentLength + SegmentLength, 0))); #endregion #region Render ships Player.shipPos = new Vector3(Player.position, AllShipsZHeight) + levelVector; AddModelToRender( shipModels[(int)ShipModelTypes.OwnShip], Matrix.CreateScale(ShipModelSize[(int)ShipModelTypes.OwnShip]) * Matrix.CreateRotationZ(MathHelper.Pi) * Matrix.CreateRotationX(Player.shipRotation.Y) * Matrix.CreateRotationY(Player.shipRotation.X) * Matrix.CreateTranslation(Player.shipPos)); // Add smoke effects for our ship EffectManager.AddRocketOrShipFlareAndSmoke( Player.shipPos + new Vector3(-0.3f, -2.65f, +0.35f), 1.35f, 5 * Player.MovementSpeedPerSecond); EffectManager.AddRocketOrShipFlareAndSmoke( Player.shipPos + new Vector3(0.3f, -2.65f, +0.35f), 1.35f, 5 * Player.MovementSpeedPerSecond); // Render enemy units and all weapon projectiles RenderEnemyUnits(); RenderWeaponProjectiles(); RenderItems(); #endregion #region Render landscape objects // Show all landscape models // Note: Avoid foreach to optimize performance on Xbox 360! for (int num = 0; num < thisLandscapeSegmentObjects.Count; num++) { MatrixAndNumber obj = thisLandscapeSegmentObjects[num]; AddModelToRender(landscapeModels[obj.number], obj.renderMatrix); } // for for (int num = 0; num < nextLandscapeSegmentObjects.Count; num++) { MatrixAndNumber obj = nextLandscapeSegmentObjects[num]; AddModelToRender(landscapeModels[obj.number], obj.renderMatrix); } // for #endregion #region Render all and add shadows #region Generate shadows // Generate shadows ShaderEffect.shadowMapping.GenerateShadows( delegate { // Generate shadows for all models except the first two (landscape) for (int num = 2; num < numOfModelsToRender; num++) modelsToRender[num].model.GenerateShadow( modelsToRender[num].matrix); }); // Render shadows ShaderEffect.shadowMapping.RenderShadows( delegate { // Show and render shadows for all models including the landscape for (int num = 0; num < numOfModelsToRender; num++) modelsToRender[num].model.UseShadow( modelsToRender[num].matrix); }); #endregion #region Render normally // Time to render all models the normal way (with normal mapping) for (int num = 0; num < numOfModelsToRender; num++) modelsToRender[num].model.Render( modelsToRender[num].matrix); // And finally bring everything on the screen BaseGame.MeshRenderManager.Render(); #endregion #region Show shadows ShaderEffect.shadowMapping.ShowShadows(); #endregion #endregion }
//*/ /// <summary> /// Test create render to texture /// </summary> public static void TestCreateRenderToTexture() { Model testModel = null; RenderToTexture renderToTexture = null; TestGame.Start( "TestCreateRenderToTexture", delegate { testModel = new Model("apple"); renderToTexture = new RenderToTexture( //SizeType.QuarterScreen); SizeType.HalfScreen); //SizeType.HalfScreenWithZBuffer); //SizeType.FullScreen); //SizeType.ShadowMap); }, delegate { bool renderToTextureWay = Input.Keyboard.IsKeyUp(Keys.Space) && Input.GamePadAPressed == false; BaseGame.Device.RenderState.DepthBufferEnable = true; if (renderToTextureWay) { // Set render target to our texture renderToTexture.SetRenderTarget(); // Clear background renderToTexture.Clear(Color.Blue); // Draw background lines //Line.DrawGrid(); //Ui.LineManager.RenderAll3DLines(); // And draw object testModel.Render(Matrix.CreateScale(7.5f)); //BaseGame.RenderManager.RenderAllMeshes(); // Do we need to resolve? renderToTexture.Resolve(true); //BaseGame.Device.ResolveRenderTarget(0); // Reset background buffer //not longer required, done in Resolve now: //RenderToTexture.ResetRenderTarget(true); } // if (renderToTextureWay) else { // Copy backbuffer way, render stuff normally first // Clear background BaseGame.Device.Clear(Color.Blue); // Draw background lines //Line.DrawGrid(); //Ui.LineManager.RenderAll3DLines(); // And draw object testModel.Render(Matrix.CreateScale(7.5f)); //BaseGame.RenderManager.RenderAllMeshes(); } // else // Show render target in a rectangle on our screen renderToTexture.RenderOnScreen( //tst: new Rectangle(100, 100, 256, 256)); //BaseGame.ScreenRectangle); //no need: BaseGame.UI.FlushUI(); TextureFont.WriteText(2, 0, " Press Space to toogle full screen rendering"); TextureFont.WriteText(2, 30, "renderToTexture.Width=" + renderToTexture.Width); TextureFont.WriteText(2, 60, "renderToTexture.Height=" + renderToTexture.Height); TextureFont.WriteText(2, 90, "renderToTexture.Valid=" + renderToTexture.IsValid); TextureFont.WriteText(2, 120, "renderToTexture.XnaTexture=" + renderToTexture.XnaTexture); TextureFont.WriteText(2, 150, "renderToTexture.ZBufferSurface=" + renderToTexture.ZBufferSurface); TextureFont.WriteText(2, 180, "renderToTexture.Filename=" + renderToTexture.Filename); }); }