/// <summary> /// Create landscape object /// </summary> /// <param name="setModel">Set model</param> /// <param name="setMatrix">Set matrix</param> public LandscapeObject(Model setModel, Matrix setMatrix) { if (setModel == null) { throw new ArgumentNullException("setModel"); } model = setModel; matrix = setMatrix; // Also include signs no reason to receive shadows for them! // Faster and looks better! isBanner = model.Name.ToLower().Contains("banner") || model.Name.ToLower().Contains("sign"); }
/// <summary> /// Add object to render /// </summary> /// <param name="modelName">Model name</param> /// <param name="rotation">Rotation</param> /// <param name="trackPos">Track position</param> /// <param name="trackRight">Track right</param> /// <param name="distance">Distance</param> public void AddObjectToRender(string modelName, float rotation, Vector3 trackPos, Vector3 trackRight, float distance) { // Find out size float objSize = 1; // Search for combos for (int num = 0; num < combos.Length; num++) { TrackCombiModels combi = combos[num]; //slower: if (StringHelper.Compare(combi.Name, modelName)) if (combi.Name == modelName) { objSize = combi.Size; break; } } // Search model by name! for (int num = 0; num < landscapeModels.Length; num++) { Model model = landscapeModels[num]; //slower: if (StringHelper.Compare(model.Name, modelName)) if (model.Name == modelName) { objSize = model.Size; break; } } // Make sure it is away from the road. if (distance > 0 && distance - 10 < objSize) { distance += objSize; } if (distance < 0 && distance + 10 > -objSize) { distance -= objSize; } AddObjectToRender(modelName, Matrix.CreateRotationZ(rotation) * Matrix.CreateTranslation( trackPos + trackRight * distance + new Vector3(0, 0, -100)), false); }
/// <summary> /// Load car stuff /// </summary> protected override void Initialize() { base.Initialize(); // Load models carModel = new Model("Car"); carSelectionPlate = new Model("CarSelectionPlate"); // Load landscape landscape = new Landscape(Level.Beginner); // Load textures, first one is grabbed from the imported one through // the car.x model, the other two are loaded seperately. carTextures = new Texture[3]; carTextures[0] = new Texture("RacerCar"); carTextures[1] = new Texture("RacerCar2"); carTextures[2] = new Texture("RacerCar3"); colorSelectionTexture = new Texture("ColorSelection"); brakeTrackMaterial = new Material("track"); }
/// <summary> /// Initializes and loads some content, previously referred to as the /// "car stuff". /// </summary> private void LoadResources() { LoadEvent("Models...", null); // Load models carModel = new Model("Car"); carSelectionPlate = new Model("CarSelectionPlate"); LoadEvent("Landscape...", null); // Load landscape landscape = new Landscape(Level.Beginner); LoadEvent("Textures...", null); // Load textures, first one is grabbed from the imported one through // the car.x model, the other two are loaded seperately. carTextures = new Texture[3]; carTextures[0] = new Texture("RacerCar"); carTextures[1] = new Texture("RacerCar2"); carTextures[2] = new Texture("RacerCar3"); colorSelectionTexture = new Texture("ColorSelection"); brakeTrackMaterial = new Material("track"); LoadEvent("All systems go!", null); Thread.Sleep(1000); }
/// <summary> /// Add object to render /// </summary> /// <param name="modelName">Model name</param> /// <param name="renderMatrix">Render matrix</param> /// <param name="isNearTrack">Is near track</param> public void AddObjectToRender(string modelName, Matrix renderMatrix, bool isNearTrackForShadowGeneration) { // Fix wrong model names if (modelName == "OilWell") { modelName = "OilPump"; } else if (modelName == "PalmSmall") { modelName = "AlphaPalmSmall"; } else if (modelName == "AlphaPalm4") { modelName = "AlphaPalmSmall"; } else if (modelName == "Palm") { modelName = "AlphaPalm"; } else if (modelName == "Casino") { modelName = "Casino01"; } else if (modelName == "Combi") { modelName = "CombiPalms"; } // Always include windmills and buildings for shadow generation if (modelName.ToLower() == "windmill" || modelName.ToLower().Contains("hotel") || modelName.ToLower().Contains("building") || modelName.ToLower().Contains("casino01")) { isNearTrackForShadowGeneration = true; } // Search for combos for (int num = 0; num < combos.Length; num++) { TrackCombiModels combi = combos[num]; //slower: if (StringHelper.Compare(combi.Name, modelName)) if (combi.Name == modelName) { // Add all combi objects (calls this method for each model) combi.AddAllModels(this, renderMatrix); // Thats it. return; } } Model foundModel = null; // Search model by name! for (int num = 0; num < landscapeModels.Length; num++) { Model model = landscapeModels[num]; //slower: if (StringHelper.Compare(model.Name, modelName)) if (model.Name == modelName) { foundModel = model; break; } } // Only add if we found the model if (foundModel != null) { // Fix z position to be always ABOVE the landscape Vector3 modelPos = renderMatrix.Translation; // Get landscape height here float landscapeHeight = GetMapHeight(modelPos.X, modelPos.Y); // And make sure we are always above it! if (modelPos.Z < landscapeHeight) { modelPos.Z = landscapeHeight; // Fix render matrix renderMatrix.Translation = modelPos; } // Check if another object is nearby, then skip this one! // Don't skip signs or banners! if (modelName.StartsWith("Banner") == false && modelName.StartsWith("Sign") == false && modelName.StartsWith("StartLight") == false) { for (int num = 0; num < landscapeObjects.Count; num++) { if (Vector3.DistanceSquared( landscapeObjects[num].Position, modelPos) < foundModel.Size * foundModel.Size / 4) { // Don't add return; } } } LandscapeObject newObject = new LandscapeObject(foundModel, // Scale all objects up a little (else world is not filled enough) Matrix.CreateScale(1.2f) * renderMatrix); // Add landscapeObjects.Add(newObject); // Add again to the nearTrackObjects list if near the track if (isNearTrackForShadowGeneration) { nearTrackObjects.Add(newObject); } if (modelName.StartsWith("StartLight")) { startLightObject = newObject; } } #if DEBUG else if (modelName.Contains("Track") == false) { // Add warning in log file Log.Write("Landscape model " + modelName + " is not supported and " + "can't be added for rendering!"); } #endif }
/// <summary> /// Change model /// </summary> /// <param name="setNewModel">Set new model</param> public void ChangeModel(Model setNewModel) { model = setNewModel; }
/// <summary> /// Create landscape object /// </summary> /// <param name="setModel">Set model</param> /// <param name="setMatrix">Set matrix</param> public LandscapeObject(Model setModel, Matrix setMatrix) { if (setModel == null) throw new ArgumentNullException("setModel"); model = setModel; matrix = setMatrix; isBanner = StringHelper.Contains(model.Name, // Also include signs no reason to receive shadows for them! // Faster and looks better! new string[] { "banner", "sign" }); }
/// <summary> /// Test camera /// </summary> public static void TestCamera() { Model carModel = null; TestGame.Start("TestCamera", delegate // Init { carModel = new Model("Car"); }, delegate // Render loop { // Just show background ... free camera is handled automatically. RacingGameManager.UI.RenderGameBackground(); carModel.RenderCar(1, Color.White, false, Matrix.Identity); }); }
/// <summary> /// Test create render to texture /// </summary> public static void TestCreateRenderToTexture() { Model testPlate = null; RenderToTexture renderToTexture = null; TestGame.Start( delegate { testPlate = new Model("CarSelectionPlate"); renderToTexture = new RenderToTexture( //SizeType.ShadowMap1024); //.QuarterScreen); SizeType.FullScreen); //SizeType.HalfScreen); }, delegate { bool renderToTextureWay = Input.Keyboard.IsKeyUp(Keys.Space) && Input.GamePadAPressed == false; if (renderToTextureWay) { // Set render target to our texture renderToTexture.SetRenderTarget(); // Clear background renderToTexture.Clear(Color.Blue); // Draw background lines BaseGame.DrawLine(new Point(0, 200), new Point(200, 0), Color.Blue); BaseGame.DrawLine(new Point(0, 0), new Point(400, 400), Color.Red); BaseGame.FlushLineManager2D(); // And draw object testPlate.Render(Matrix.CreateScale(1.5f)); // And flush render manager to draw all objects BaseGame.MeshRenderManager.Render(); // Resolve renderToTexture.Resolve(true); // Reset background buffer //obs: RenderToTexture.ResetRenderTarget(true); // Show render target in a rectangle on our screen renderToTexture.RenderOnScreen( //tst: new Rectangle(100, 100, 256, 256)); //BaseGame.ResolutionRect); } // if (renderToTextureWay) else { // Copy backbuffer way, render stuff normally first // Clear background BaseGame.Device.Clear(Color.Blue); // Draw background lines BaseGame.DrawLine(new Point(0, 200), new Point(200, 0), Color.Blue); BaseGame.DrawLine(new Point(0, 0), new Point(400, 400), Color.Red); BaseGame.FlushLineManager2D(); // And draw object testPlate.Render(Matrix.CreateScale(1.5f)); } // else 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); }); }
/// <summary> /// Dispose /// </summary> /// <param name="someObject">Some object</param> public static void Dispose(ref Model someObject) { if (someObject != null) someObject.Dispose(); someObject = null; }
/// <summary> /// Create landscape object /// </summary> /// <param name="setModel">Set model</param> /// <param name="setMatrix">Set matrix</param> public LandscapeObject(Model setModel, Matrix setMatrix) { if (setModel == null) throw new ArgumentNullException("setModel"); model = setModel; matrix = setMatrix; // Also include signs no reason to receive shadows for them! // Faster and looks better! isBanner = model.Name.ToLower().Contains("banner") || model.Name.ToLower().Contains("sign"); }
public static void TestTrackScreenshot() { Track track = null; Model testModel = null; TestGame.Start( delegate { track = new Track( //"TrackWithHelpers", null); //"TrackAdvanced", null); "TrackBeginner", null); testModel = new Model("Car"); }, delegate { RacingGameManager.Player.SetCarPosition( track.points[(int)(track.points.Count*0.93f)].pos + new Vector3(0, -50, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1)); track.roadMaterial.ambientColor = Color.Gray; track.Render(); /* testModel.RenderCar(0, Color.White, Matrix.CreateRotationX(Input.MousePos.X/400.0f) * Matrix.CreateTranslation( track.points[(int)(track.points.Count * 0.95f)].pos)); */ }); }
/// <summary> /// Test render track /// </summary> //[Test] public static void TestRenderTrack() { Track track = null; Model testModel = null; TestGame.Start( delegate { track = new Track( //"TrackWithHelpers", null); //"TrackAdvanced", null); "TrackBeginner", null); testModel = new Model("AlphaPalm2"); }, delegate { //RacingGameManager.Player.SetCarPosition(track.points[0].pos, // new Vector3(0, 1, 0), new Vector3(0, 0, 1)); //ShowGroundGrid(); //ShowTrackLines(track); ShowUpVectors(track); track.Render(); //testModel.Render(track.StartPosition); }); }
/// <summary> /// Load car stuff /// </summary> protected override void Initialize() { base.Initialize(); // Load models carModel = new Model("Car"); carSelectionPlate = new Model("CarSelectionPlate"); // Load landscape landscape = new Landscape(Level.Beginner); // Load textures, first one is grabbed from the imported one through // the car.x model, the other two are loaded seperately. carTextures = new Texture[3]; carTextures[0] = new Texture("RacerCar%0"); carTextures[1] = new Texture("RacerCar2"); carTextures[2] = new Texture("RacerCar3"); colorSelectionTexture = new Texture("ColorSelection"); brakeTrackMaterial = new Material("track"); //For testing: //Directly start the game and load a certain level, this code is // only used in the debug mode. Remove it when you are don with // testing! If you press Esc you will also quit the game and not // just end up in the menu again. //gameScreens.Clear(); //gameScreens.Push(new GameScreen()); }
/// <summary> /// Test shadow mapping /// </summary> public static void TestShadowMapping() { Model testPile = null; int carNumber = 0; TestGame.Start("TestShadowMapping", delegate { testPile = new Model("GuardRailHolder"); }, delegate { BaseGame.UI.PostScreenGlowShader.Start(); if (Input.Keyboard.IsKeyUp(Keys.LeftAlt) && Input.GamePadXPressed == false) { // Generate shadows ShaderEffect.shadowMapping.GenerateShadows( delegate { //testPlate.GenerateShadow(Matrix.CreateScale(1.5f)); RacingGameManager.CarModel.GenerateShadow(Matrix.CreateRotationZ(0.85f)); //testPile.GenerateShadow(Matrix.CreateScale(4)); }); // Render shadows ShaderEffect.shadowMapping.RenderShadows( delegate { RacingGameManager.CarSelectionPlate.UseShadow(Matrix.CreateScale(1.5f)); //testPile.UseShadow(Matrix.CreateScale(4)); RacingGameManager.CarModel.UseShadow(Matrix.CreateRotationZ(0.85f)); }); } // if RacingGameManager.ClearBackground(); BaseGame.UI.RenderGameBackground(); RacingGameManager.CarSelectionPlate.Render(Matrix.CreateScale(1.5f)); RacingGameManager.CarModel.RenderCar( carNumber, Color.White, false, Matrix.CreateRotationZ(0.85f)); // And flush render manager to draw all objects BaseGame.MeshRenderManager.Render(); if (Input.Keyboard.IsKeyUp(Keys.LeftAlt) && Input.GamePadXPressed == false) { ShaderEffect.shadowMapping.ShowShadows(); } // if if (Input.KeyboardSpaceJustPressed || Input.GamePadBJustPressed) carNumber = (carNumber + 1) % 3; 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)); if (ShaderEffect.shadowMapping.shadowMapBlur.BlurMapTexture.IsValid) ShaderEffect.shadowMapping.shadowMapBlur.BlurMapTexture. RenderOnScreen( new Rectangle(10 + 256 + 10+256+10, 10, 256, 256)); } // if (Input.Keyboard.IsKeyDown) //tst: BaseGame.UI.PostScreenGlowShader.Show(); int xPos = 2, yPos = 30; #if XBOX360 xPos += BaseGame.XToRes(26); yPos += BaseGame.YToRes(22); #endif TextureFont.WriteText(xPos, yPos, "Press left Shift or A to show all shadow pass textures."); TextureFont.WriteText(xPos, yPos+30, "Press Space or B to toggles the car model."); TextureFont.WriteText(xPos, yPos+60, "Press Alt or X to skip shadow map rendering."); }); }
/// <summary> /// Test car physics on a plane (xy) with some dummy guard rail for /// simple colision checking. /// </summary> public static void TestCarPhysicsOnPlaneWithGuardRails() { PlaneRenderer plane = null; // Use a simple object to simulate our guard rails we have in the game. Model guardRailModel = null; TestGame.Start("TestCarPhysicsOnPlaneWithGuardRails", delegate { plane = new PlaneRenderer(Vector3.Zero, new Plane(new Vector3(0, 0, 1), 0), new Material("CityGround", "CityGroundNormal"), 500.0f); //new Material("Road", "RoadNormal"), 500.0f); guardRailModel = new Model("RoadColumnSegment"); // Put car 10m above the ground to test gravity and ground plane! RacingGameManager.Player.SetCarPosition( new Vector3(0, 0, 10), new Vector3(0, 1, 0), new Vector3(0, 0, 1)); // Make sure we are not in free camera mode and can control the car RacingGameManager.Player.FreeCamera = false; }, delegate { // Test slow computers by slowing down the framerate with Ctrl if (Input.Keyboard.IsKeyDown(Keys.LeftControl)) Thread.Sleep(75); RacingGameManager.Player.SetGroundPlaneAndGuardRails( new Vector3(0, 0, 0), new Vector3(0, 0, 1), // Use our guardrails, always the same in this test! new Vector3(-10, 0, 0), new Vector3(-10, 10, 0), new Vector3(+10, 0, 0), new Vector3(+10, 10, 0)); Matrix carMatrix = RacingGameManager.Player.UpdateCarMatrixAndCamera(); // Generate shadows, just the car does shadows ShaderEffect.shadowMapping.GenerateShadows( delegate { RacingGameManager.CarModel.GenerateShadow(carMatrix); }); // Render shadows (on both the plane and the car) ShaderEffect.shadowMapping.RenderShadows( delegate { RacingGameManager.CarModel.UseShadow(carMatrix); plane.UseShadow(); }); BaseGame.UI.RenderGameBackground(); // Show car and ground plane RacingGameManager.CarModel.RenderCar( 0, Color.White, false, carMatrix); plane.Render(); // Just add brake tracks (we don't render the landscape here) RacingGameManager.Landscape.RenderBrakeTracks(); guardRailModel.Render(new Vector3(-11.5f, 2.5f, 0)); /* for (int num = -100; num < 100; num++) { // Add 1.5m for the size of our dummy guard rail object. guardRailModel.Render(new Vector3(-11.5f, num * 2.5f, 0)); guardRailModel.Render(new Vector3(+11.5f, num * 2.5f, 0)); } // for (num) BaseGame.DrawLine( new Vector3(-10, -1000, 0.1f), new Vector3(-10, +1000, 0.1f)); BaseGame.DrawLine( new Vector3(+10, -1000, 0.1f), new Vector3(+10, +1000, 0.1f)); */ BaseGame.MeshRenderManager.Render(); // Add shadows ShaderEffect.shadowMapping.ShowShadows(); RacingGameManager.Player.DisplayPhysicsValuesAndHelp(); }); }