//Get the WorldPosition's equivalent of absolute coordinates public WorldPosition GetWorldPos(float PosX, float PosY, float PosZ) { WorldPosition WorldPos = new WorldPosition(); WorldPos.TilePosY = PosY; //Easiest : done ! WorldPos.TileX = (int)PosX / (256 * MapTile.TileSize) - ((PosX < 0) ? 1 : 0); WorldPos.TileZ = (int)PosZ / (256 * MapTile.TileSize) - ((PosZ < 0) ? 1 : 0); WorldPos.TilePosX = PosX % (256 * MapTile.TileSize); WorldPos.TilePosZ = PosZ % (256 * MapTile.TileSize); return WorldPos; }
//Change the player's possition public void SetPlayerPosition(float PosX, float PosY, float PosZ) { PlayerPos = GetWorldPos(PosX, PosY, PosZ); }
//Get a position's height public float GetPositionHeight(WorldPosition Position) { int tilei = Position.TileX - WorldPos.TileX + RenderedTilesDistance; int tilej = Position.TileZ - WorldPos.TileZ + RenderedTilesDistance; if ((tilei >= 0) && (tilei <= 2 * RenderedTilesDistance) && (tilej >= 0) && (tilej <= 2 * RenderedTilesDistance)) { TV_3DVECTOR RealPos = GetRealPos(Position); //return MapTiles[tilei][tilej].Landscape.GetHeight(Position.TilePosX, Position.TilePosZ); return MapTiles[tilei][tilej].Landscape.GetHeight(RealPos.x, RealPos.z); } else { Debug.WriteLine("HEIGHT_NOT_FOUND"); return 0; } }
public TV_3DVECTOR GetRealPos(WorldPosition WorldPos) { TV_3DVECTOR RealPos = new TV_3DVECTOR(); RealPos.y = WorldPos.TilePosY; //As always, the easiest first ! RealPos.x = (WorldPos.TileX < 0 ? (WorldPos.TileX + 1) : WorldPos.TileX) * (256 * MapTile.TileSize) + WorldPos.TilePosX; RealPos.z = (WorldPos.TileZ < 0 ? (WorldPos.TileZ + 1) : WorldPos.TileZ) * (256 * MapTile.TileSize) + WorldPos.TilePosZ; return RealPos; }
//Add splatting to a tile public void AddSplattingToTile(WorldPosition Position, int SplattingTexture) { int tilei = Position.TileX - WorldPos.TileX + RenderedTilesDistance; int tilej = Position.TileX - WorldPos.TileX + RenderedTilesDistance; if ((tilei >= 0) && (tilei <= 2 * RenderedTilesDistance) && (tilej >= 0) && (tilej <= 2 * RenderedTilesDistance)) { /* MapTiles[tilei][tilej].Landscape.AddSplattingTexture(SplattingTexture, 1, 15, 15, 0, 0); //MapTiles[tilei][tilej].Landscape.ExpandSplattingTexture(IDAlpha, IDGrass, 0, 0, 4, 4); MapTiles[tilei][tilej].Landscape.SetSplattingEnable(true, -1, 1); * */ } }
//Force : force the reload of all the tiles //Check if there are tiles to load and load them public bool CheckLoadTiles(bool force = false) { System.Diagnostics.Debug.WriteLine("Start loadtiles (" + WorldPos.TileX + "," + WorldPos.TileZ + ")"); if (force || ((PlayerPos.TileX != WorldPos.TileX) || (PlayerPos.TileZ != WorldPos.TileZ))) { //The player moved too much LoadingMapTiles = true; System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest; int TilesMoveX = PlayerPos.TileX - WorldPos.TileX; int TilesMoveZ = PlayerPos.TileZ - WorldPos.TileZ; TimeSpan CheckLandscapeBegin = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; //We create the new MapTile which will contain the new terrains MapTile[][] MapTiles_New = new MapTile[2 * RenderedTilesDistance + 1][];// = MapTiles = new MapTile[2 * RenderedTilesDistance + 1, 2 * RenderedTilesDistance + 1]; LinkedList<MapTile> TilesList_New = new LinkedList<MapTile>(); //We check all the new tiles to see if we have to create a new one or copy an old one for (short i = 0; i < (2 * RenderedTilesDistance + 1); i++) { MapTiles_New[i] = new MapTile[2 * RenderedTilesDistance + 1]; for (short j = 0; j < (2 * RenderedTilesDistance + 1); j++) { int oldIndexX = i + TilesMoveX; //Correspondance to the old MapTiles int oldIndexZ = j + TilesMoveZ; //Correspondance to the old MapTiles if (!force && (((oldIndexX >= 0) && (oldIndexX <= 2 * RenderedTilesDistance)) && ((oldIndexZ >= 0) && (oldIndexZ <= 2 * RenderedTilesDistance)))) { //The old index is valid, we just move the tile MapTiles_New[i][j] = MapTiles[oldIndexX][oldIndexZ]; } else { //New landscape, we create it TimeSpan NewLandscapeBegin = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; MapTiles_New[i][j] = new MapTile(new TilePosition(PlayerPos.TileX + i - RenderedTilesDistance, PlayerPos.TileZ + j - RenderedTilesDistance)); TimeSpan NewLandscapeEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; System.Diagnostics.Debug.WriteLine("New tile i:" + i + "(" + (PlayerPos.TileX + i - RenderedTilesDistance) + ");j:" + j + "(" + (PlayerPos.TileZ + j - RenderedTilesDistance) + ") - " + (NewLandscapeEnd - NewLandscapeBegin).TotalMilliseconds + " ms."); } if ((i - RenderedTilesDistance == 0) && (j - RenderedTilesDistance == 0)) { //Player's tile MapTiles_New[i][j].Landscape.SetTexture(GlobalVars.GameEngine.Globals.GetTex("LandTexture"), -1); MapTiles_New[i][j].Landscape.SetTextureScale(3, 3, -1); } else { //Another tile MapTiles_New[i][j].Landscape.SetTexture(GlobalVars.GameEngine.Globals.GetTex("LandTexture2"), -1); MapTiles_New[i][j].Landscape.SetTextureScale(3, 3, -1); } TilePosition Position = new TilePosition(i - RenderedTilesDistance + PlayerPos.TileX, j - RenderedTilesDistance + PlayerPos.TileZ); TilesHeightmapToLoad.Enqueue(Position, Position.TileDistanceTo(PlayerPos)); //LoadTileHeightmap(new TilePosition(i - RenderedTilesDistance, j - RenderedTilesDistance)); //LoadTileHeightmap2(MapTiles_New[i][j]); WorldPosition SplatPosition = new WorldPosition(Position); /* MapTiles_New[i][j].Landscape.AddSplattingTexture(GlobalVars.GameEngine.Globals.GetTex("SplattingTexture"), 1, 1, 1, 0, 0); MapTiles_New[i][j].Landscape.ExpandSplattingTexture(GlobalVars.GameEngine.Globals.GetTex("SplattingAlphaTexture"), GlobalVars.GameEngine.Globals.GetTex("SplattingTexture"), 0, 0, 4, 4); MapTiles_New[i][j].Landscape.SetSplattingEnable(true);*/ //AddSplattingToTile(SplatPosition, GlobalVars.GameEngine.Globals.GetTex("SplattingTexture")); TilesList_New.AddLast(MapTiles_New[i][j]); } } TimeSpan CheckLandscapeEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; //We replace the old tiles with the new ones MapTiles = MapTiles_New; TilesList = TilesList_New; TimeSpan CopyMapTilesEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; WorldPos.TileX = PlayerPos.TileX; WorldPos.TileZ = PlayerPos.TileZ; //DEBUG : add a mesh to each tile /*for (short i = 0; i < (2 * RenderedTilesDistance + 1); i++) { for (short j = 0; j < (2 * RenderedTilesDistance + 1); j++) { if (!MapTiles[i][j].MeshesLoaded) //DEBUG { MapTiles[i][j].MeshesLoaded = true; //DEBUG //Debug : we add a mesh TVMesh newmesh = new TVMesh(); newmesh = GlobalVars.GameEngine.Scene.CreateMeshBuilder(); newmesh.CreateTeapot(); newmesh.SetScale(50, 50, 50); newmesh.SetCullMode(CONST_TV_CULLING.TV_BACK_CULL); AddMeshToTile(new WorldPosition(i - RenderedTilesDistance + PlayerPos.TileX, i - RenderedTilesDistance + PlayerPos.TileZ, 0, 0, 0), newmesh); } } }*/ LoadingMapTiles = false; Thread.CurrentThread.Priority = ThreadPriority.Normal; System.Diagnostics.Debug.WriteLine("Finished load new MapTiles (" + WorldPos.TileX + "," + WorldPos.TileZ + ") - " + (CheckLandscapeEnd - CheckLandscapeBegin).TotalMilliseconds + " ms|" + (CopyMapTilesEnd - CheckLandscapeEnd).TotalMilliseconds + " ms"); return true; } System.Diagnostics.Debug.WriteLine("Nothing loaded"); return false; }
private void Main_Loop() { // Okay, we start the main loop here. We are going to loop over // and over until the user click on the "Quit" button and by this, // change the value of DoLoop to false. // We loop all of this until the DoLoop isn't True. while (DoLoop == true) { // Let us the capacity to use buttons of the form. System.Windows.Forms.Application.DoEvents(); // New : We moved the movement code in an other sub to make // the code clearer. Check_Input(); if (InputEngine.IsKeyPressed(CONST_TV_KEY.TV_KEY_Z)) { Scene.SetRenderMode(CONST_TV_RENDERMODE.TV_LINE); } else { Scene.SetRenderMode(CONST_TV_RENDERMODE.TV_SOLID); } // New : We moved the checking of maximum camera "look at" and // also the camera movement smoothing in an other sub too. Check_Movement(); //render surfaces before tv3d.clear or you get funkey results RenderSurf1.StartRender(false); Atmos.Atmosphere_Render(); //Land.Render(); RenderSurf1.EndRender(); RenderSurf2.StartRender(false); Atmos.Atmosphere_Render(); //Land.Render(); RenderSurf2.EndRender(); // Clear the the last frame. TV.Clear(false); // New : if we are below the waterheight, this means the we are // underwater. To give a cool underwater effect, we will add fog. // If we are over the ground, then don't add the fog but render // the lens flare. /* * if (sngPositionY < sngWaterHeight) * { * //' Render a blue fog to simulate under water. * Atmos.Fog_Enable(true); * Atmos.Fog_SetColor(0f, 0.4f, 0.5f); * Atmos.Fog_SetParameters(0f, 0f, 0.01f); * Atmos.Fog_SetType(CONST_TV_FOG.TV_FOG_EXP, CONST_TV_FOGTYPE.TV_FOGTYPE_RANGE); * Atmos.LensFlare_Enable(false); * } * else * {*/ // New : we have to render the lens flare. Atmos.LensFlare_Enable(true); Atmos.Fog_Enable(false); /*Atmos.Fog_Enable(true); * Atmos.Fog_SetColor(1, 1, 1); * Atmos.Fog_SetParameters(50f, 100f, 0.01f); * Atmos.Fog_SetType(CONST_TV_FOG.TV_FOG_LINEAR, CONST_TV_FOGTYPE.TV_FOGTYPE_VERTEX);*/ //} // New have to render the sky, the sun and lens flares Atmos.Atmosphere_Render(); // New : we have to render the landscape. //Land.Render(); WMap.Render(); //Scene.RenderAll(false); // We render all the 3D objects contained in the scene. //Scene.RenderAllMeshes(true); // We display everything that we have rendered TV.RenderToScreen(); WorldPosition PlayerPos = WMap.GetPlayerPosition(); GlobalVars.GameForm.Text = "Pos:" + sngPositionX + " (" + PlayerPos.TileX + ");" + sngPositionY + ";" + sngPositionZ + " (" + PlayerPos.TileZ + ") + (" + PlayerPos.TilePosX + ";" + PlayerPos.TilePosY + ";" + PlayerPos.TilePosZ + ")"; //GlobalVars.GameForm.Text = Scene.GetTriangleNumber().ToString(); } // We ask to quit. this.Quit(); }
//Add a mesh to a tile public void AddMeshToTile(WorldPosition Position, TVMesh Mesh) { int tilei = Position.TileX - WorldPos.TileX + RenderedTilesDistance; int tilej = Position.TileX - WorldPos.TileX + RenderedTilesDistance; if ((tilei >= 0) && (tilei <= 2 * RenderedTilesDistance) && (tilej >= 0) && (tilej <= 2 * RenderedTilesDistance)) { Mesh.SetPosition(Position.TileX * MapTile.TileSize * 256 + Position.TilePosX, Position.TilePosY, Position.TileZ * MapTile.TileSize * 256 + Position.TilePosZ); MapTiles[tilei][tilej].Meshes.Add(Mesh); } }
//Check if there are tiles to load and load them public bool CheckLoadTiles(bool force = false) //Force : force the reload of all the tiles { System.Diagnostics.Debug.WriteLine("Start loadtiles (" + WorldPos.TileX + "," + WorldPos.TileZ + ")"); if (force || ((PlayerPos.TileX != WorldPos.TileX) || (PlayerPos.TileZ != WorldPos.TileZ))) { //The player moved too much LoadingMapTiles = true; System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest; int TilesMoveX = PlayerPos.TileX - WorldPos.TileX; int TilesMoveZ = PlayerPos.TileZ - WorldPos.TileZ; TimeSpan CheckLandscapeBegin = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; //We create the new MapTile which will contain the new terrains MapTile[][] MapTiles_New = new MapTile[2 * RenderedTilesDistance + 1][];// = MapTiles = new MapTile[2 * RenderedTilesDistance + 1, 2 * RenderedTilesDistance + 1]; LinkedList <MapTile> TilesList_New = new LinkedList <MapTile>(); //We check all the new tiles to see if we have to create a new one or copy an old one for (short i = 0; i < (2 * RenderedTilesDistance + 1); i++) { MapTiles_New[i] = new MapTile[2 * RenderedTilesDistance + 1]; for (short j = 0; j < (2 * RenderedTilesDistance + 1); j++) { int oldIndexX = i + TilesMoveX; //Correspondance to the old MapTiles int oldIndexZ = j + TilesMoveZ; //Correspondance to the old MapTiles if (!force && (((oldIndexX >= 0) && (oldIndexX <= 2 * RenderedTilesDistance)) && ((oldIndexZ >= 0) && (oldIndexZ <= 2 * RenderedTilesDistance)))) { //The old index is valid, we just move the tile MapTiles_New[i][j] = MapTiles[oldIndexX][oldIndexZ]; } else { //New landscape, we create it TimeSpan NewLandscapeBegin = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; MapTiles_New[i][j] = new MapTile(new TilePosition(PlayerPos.TileX + i - RenderedTilesDistance, PlayerPos.TileZ + j - RenderedTilesDistance)); TimeSpan NewLandscapeEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; System.Diagnostics.Debug.WriteLine("New tile i:" + i + "(" + (PlayerPos.TileX + i - RenderedTilesDistance) + ");j:" + j + "(" + (PlayerPos.TileZ + j - RenderedTilesDistance) + ") - " + (NewLandscapeEnd - NewLandscapeBegin).TotalMilliseconds + " ms."); } if ((i - RenderedTilesDistance == 0) && (j - RenderedTilesDistance == 0)) { //Player's tile MapTiles_New[i][j].Landscape.SetTexture(GlobalVars.GameEngine.Globals.GetTex("LandTexture"), -1); MapTiles_New[i][j].Landscape.SetTextureScale(3, 3, -1); } else { //Another tile MapTiles_New[i][j].Landscape.SetTexture(GlobalVars.GameEngine.Globals.GetTex("LandTexture2"), -1); MapTiles_New[i][j].Landscape.SetTextureScale(3, 3, -1); } TilePosition Position = new TilePosition(i - RenderedTilesDistance + PlayerPos.TileX, j - RenderedTilesDistance + PlayerPos.TileZ); TilesHeightmapToLoad.Enqueue(Position, Position.TileDistanceTo(PlayerPos)); //LoadTileHeightmap(new TilePosition(i - RenderedTilesDistance, j - RenderedTilesDistance)); //LoadTileHeightmap2(MapTiles_New[i][j]); WorldPosition SplatPosition = new WorldPosition(Position); /*MapTiles_New[i][j].Landscape.AddSplattingTexture(GlobalVars.GameEngine.Globals.GetTex("SplattingTexture"), 1, 1, 1, 0, 0); * MapTiles_New[i][j].Landscape.ExpandSplattingTexture(GlobalVars.GameEngine.Globals.GetTex("SplattingAlphaTexture"), GlobalVars.GameEngine.Globals.GetTex("SplattingTexture"), 0, 0, 4, 4); * MapTiles_New[i][j].Landscape.SetSplattingEnable(true);*/ //AddSplattingToTile(SplatPosition, GlobalVars.GameEngine.Globals.GetTex("SplattingTexture")); TilesList_New.AddLast(MapTiles_New[i][j]); } } TimeSpan CheckLandscapeEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; //We replace the old tiles with the new ones MapTiles = MapTiles_New; TilesList = TilesList_New; TimeSpan CopyMapTilesEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; WorldPos.TileX = PlayerPos.TileX; WorldPos.TileZ = PlayerPos.TileZ; //DEBUG : add a mesh to each tile /*for (short i = 0; i < (2 * RenderedTilesDistance + 1); i++) * { * for (short j = 0; j < (2 * RenderedTilesDistance + 1); j++) * { * if (!MapTiles[i][j].MeshesLoaded) //DEBUG * { * MapTiles[i][j].MeshesLoaded = true; //DEBUG * * //Debug : we add a mesh * TVMesh newmesh = new TVMesh(); * newmesh = GlobalVars.GameEngine.Scene.CreateMeshBuilder(); * newmesh.CreateTeapot(); * newmesh.SetScale(50, 50, 50); * newmesh.SetCullMode(CONST_TV_CULLING.TV_BACK_CULL); * AddMeshToTile(new WorldPosition(i - RenderedTilesDistance + PlayerPos.TileX, i - RenderedTilesDistance + PlayerPos.TileZ, 0, 0, 0), newmesh); * } * } * }*/ LoadingMapTiles = false; Thread.CurrentThread.Priority = ThreadPriority.Normal; System.Diagnostics.Debug.WriteLine("Finished load new MapTiles (" + WorldPos.TileX + "," + WorldPos.TileZ + ") - " + (CheckLandscapeEnd - CheckLandscapeBegin).TotalMilliseconds + " ms|" + (CopyMapTilesEnd - CheckLandscapeEnd).TotalMilliseconds + " ms"); return(true); } System.Diagnostics.Debug.WriteLine("Nothing loaded"); return(false); }
//Change the player's possition public void ChangePlayerPos(float PosX, float PosY, float PosZ) { PlayerPos = GetWorldPos(PosX, PosY, PosZ); }
//Get a position's height public float GetPositionHeight(WorldPosition Position) { int tilei = Position.TileX - WorldPos.TileX + RenderedTilesDistance; int tilej = Position.TileX - WorldPos.TileX + RenderedTilesDistance; if ((tilei >= 0) && (tilei <= 2 * RenderedTilesDistance) && (tilej >= 0) && (tilej <= 2 * RenderedTilesDistance)) { return MapTiles[tilei][tilej].Landscape.GetHeight(Position.TilePosX, Position.TilePosZ); } else { return 0; } }