From 7fcffa3a3a231a77d00bf2ec1772f0914073d28f Mon Sep 17 00:00:00 2001 From: MW Date: Sat, 29 Mar 2008 17:18:47 +0000 Subject: [PATCH] Re-enabled terrain texture generation for the world map. Adam can clean up/ sort it out when he gets time. Most likely doesn't really work in grid mode as the generated textures are marked as temporary and I don't think they are updated to the asset server. We have to either live with these textures being sent to the asset server, and manually clean them out from time to time or wait until there is some asset management system in place. Also currently the texture is only generated at region startup, it is not updated after terraforming. --- .../Communications/InventoryServiceBase.cs | 2 +- OpenSim/Region/Application/OpenSimMain.cs | 5 ++ .../ClientStack/RegionApplicationBase.cs | 5 +- .../Region/Environment/Interfaces/ITerrain.cs | 5 ++ .../Environment/Interfaces/ITerrainChannel.cs | 1 + .../Modules/Terrain/TerrainChannel.cs | 8 +++ .../Modules/Terrain/TerrainModule.cs | 50 ++++++++++++++++++- OpenSim/Region/Environment/Scenes/Scene.cs | 29 ++++++----- 8 files changed, 90 insertions(+), 15 deletions(-) diff --git a/OpenSim/Framework/Communications/InventoryServiceBase.cs b/OpenSim/Framework/Communications/InventoryServiceBase.cs index d105069d74..6e909dae18 100644 --- a/OpenSim/Framework/Communications/InventoryServiceBase.cs +++ b/OpenSim/Framework/Communications/InventoryServiceBase.cs @@ -260,7 +260,7 @@ namespace OpenSim.Framework.Communications { AddFolder(folder); } - } + } private class UsersInventory { diff --git a/OpenSim/Region/Application/OpenSimMain.cs b/OpenSim/Region/Application/OpenSimMain.cs index afaf988583..e77aa6b09e 100644 --- a/OpenSim/Region/Application/OpenSimMain.cs +++ b/OpenSim/Region/Application/OpenSimMain.cs @@ -506,6 +506,11 @@ namespace OpenSim m_moduleLoader.InitialiseSharedModules(scene); scene.SetModuleInterfaces(); + //moved these here as the terrain texture has to be created after the modules are initialized + // and has to happen before the region is registered with the grid. + scene.CreateTerrainTexture(true); + scene.RegisterRegionWithGrid(); + //Server side object editing permissions checking scene.PermissionsMngr.BypassPermissions = !m_permissions; diff --git a/OpenSim/Region/ClientStack/RegionApplicationBase.cs b/OpenSim/Region/ClientStack/RegionApplicationBase.cs index ed29e8ec48..4dd58f2ebd 100644 --- a/OpenSim/Region/ClientStack/RegionApplicationBase.cs +++ b/OpenSim/Region/ClientStack/RegionApplicationBase.cs @@ -124,7 +124,10 @@ namespace OpenSim.Region.ClientStack udpServer.LocalScene = scene; scene.LoadWorldMap(); - scene.RegisterRegionWithGrid(); + + //moved to opensimMain as these have to happen after modules are initialised + // scene.CreateTerrainTexture(true); + // scene.RegisterRegionWithGrid(); scene.PhysicsScene = GetPhysicsScene(); scene.PhysicsScene.SetTerrain(scene.Heightmap.GetFloatsSerialised()); diff --git a/OpenSim/Region/Environment/Interfaces/ITerrain.cs b/OpenSim/Region/Environment/Interfaces/ITerrain.cs index d3070b28eb..2cef17e80f 100644 --- a/OpenSim/Region/Environment/Interfaces/ITerrain.cs +++ b/OpenSim/Region/Environment/Interfaces/ITerrain.cs @@ -70,4 +70,9 @@ namespace OpenSim.Region.Environment.Interfaces void ExportImage(string filename, string gradientmap); byte[] ExportJpegImage(string gradientmap); } + + public interface ITerrainTemp + { + byte[] WriteJpegImage(string gradientmap); + } } diff --git a/OpenSim/Region/Environment/Interfaces/ITerrainChannel.cs b/OpenSim/Region/Environment/Interfaces/ITerrainChannel.cs index c8b9c982f9..0db1294ae6 100644 --- a/OpenSim/Region/Environment/Interfaces/ITerrainChannel.cs +++ b/OpenSim/Region/Environment/Interfaces/ITerrainChannel.cs @@ -35,5 +35,6 @@ namespace OpenSim.Region.Environment.Interfaces float[] GetFloatsSerialised(); double[,] GetDoubles(); bool Tainted(int x, int y); + ITerrainChannel MakeCopy(); } } diff --git a/OpenSim/Region/Environment/Modules/Terrain/TerrainChannel.cs b/OpenSim/Region/Environment/Modules/Terrain/TerrainChannel.cs index 4ab7782eec..74af2c1f07 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/TerrainChannel.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/TerrainChannel.cs @@ -56,6 +56,14 @@ namespace OpenSim.Region.Environment.Modules.Terrain return copy; } + public ITerrainChannel MakeCopy() + { + TerrainChannel copy = new TerrainChannel(false); + copy.map = (double[,])this.map.Clone(); + + return copy; + } + public float[] GetFloatsSerialised() { float[] heights = new float[Width * Height]; diff --git a/OpenSim/Region/Environment/Modules/Terrain/TerrainModule.cs b/OpenSim/Region/Environment/Modules/Terrain/TerrainModule.cs index cdcc648acb..742ea5b371 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/TerrainModule.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/TerrainModule.cs @@ -27,15 +27,17 @@ using System; using System.Collections.Generic; +using System.Drawing; using libsecondlife; using Nini.Config; using OpenSim.Framework; using OpenSim.Region.Environment.Interfaces; using OpenSim.Region.Environment.Scenes; + namespace OpenSim.Region.Environment.Modules.Terrain { - public class TerrainModule : IRegionModule + public class TerrainModule : IRegionModule , ITerrainTemp { public enum StandardTerrainEffects : byte { @@ -139,6 +141,7 @@ namespace OpenSim.Region.Environment.Modules.Terrain public void Initialise(Scene scene, IConfigSource config) { m_scene = scene; + m_scene.RegisterModuleInterface(this); m_gConfig = config; // Install terrain module in the simulator @@ -312,6 +315,51 @@ namespace OpenSim.Region.Environment.Modules.Terrain } } + public byte[] WriteJpegImage(string gradientmap) + { + byte[] imageData = null; + try + { + Bitmap bmp = TerrainToBitmap(gradientmap); + + imageData = OpenJPEGNet.OpenJPEG.EncodeFromImage(bmp, true); + + } + catch (Exception e) // LEGIT: Catching problems caused by OpenJPEG p/invoke + { + Console.WriteLine("Failed generating terrain map: " + e.ToString()); + } + + return imageData; + } + + private Bitmap TerrainToBitmap(string gradientmap) + { + Bitmap gradientmapLd = new Bitmap(gradientmap); + + int pallete = gradientmapLd.Height; + + Bitmap bmp = new Bitmap(m_channel.Width, m_channel.Height); + Color[] colours = new Color[pallete]; + + for (int i = 0; i < pallete; i++) + { + colours[i] = gradientmapLd.GetPixel(0, i); + } + + TerrainChannel copy =(TerrainChannel) m_channel.MakeCopy(); + for (int y = 0; y < copy.Height; y++) + { + for (int x = 0; x < copy.Width; x++) + { + // 512 is the largest possible height before colours clamp + int colorindex = (int)(Math.Max(Math.Min(1.0, copy[x, y] / 512.0), 0.0) * (pallete - 1)); + bmp.SetPixel(x, copy.Height - y - 1, colours[colorindex]); + } + } + return bmp; + } + public void PostInitialise() { InstallDefaultEffects(); diff --git a/OpenSim/Region/Environment/Scenes/Scene.cs b/OpenSim/Region/Environment/Scenes/Scene.cs index b8a483136b..c07f718c3c 100644 --- a/OpenSim/Region/Environment/Scenes/Scene.cs +++ b/OpenSim/Region/Environment/Scenes/Scene.cs @@ -946,6 +946,7 @@ namespace OpenSim.Region.Environment.Scenes { Heightmap = new Modules.Terrain.TerrainChannel(map); } + } catch (Exception e) { @@ -981,19 +982,23 @@ namespace OpenSim.Region.Environment.Scenes public void CreateTerrainTexture(bool temporary) { //TODOADAM: Move this to TerrainModule - /* + //create a texture asset of the terrain - byte[] data = Terrain.WriteJpegImage("defaultstripe.png"); - m_regInfo.EstateSettings.terrainImageID = LLUUID.Random(); - AssetBase asset = new AssetBase(); - asset.FullID = m_regInfo.EstateSettings.terrainImageID; - asset.Data = data; - asset.Name = "terrainImage"; - asset.Description = RegionInfo.RegionName; - asset.Type = 0; - asset.Temporary = temporary; - AssetCache.AddAsset(asset); - */ + ITerrainTemp terrain = RequestModuleInterface(); + if (terrain != null) + { + byte[] data = terrain.WriteJpegImage("defaultstripe.png"); + m_regInfo.EstateSettings.terrainImageID = LLUUID.Random(); + AssetBase asset = new AssetBase(); + asset.FullID = m_regInfo.EstateSettings.terrainImageID; + asset.Data = data; + asset.Name = "terrainImage"; + asset.Description = RegionInfo.RegionName; + asset.Type = 0; + asset.Temporary = temporary; + AssetCache.AddAsset(asset); + } + } #endregion