29 using System.Collections.Generic;
 
   31 using System.Reflection;
 
   36 using OpenMetaverse.Imaging;
 
   37 using OpenSim.Framework;
 
   38 using OpenSim.Region.Framework.Interfaces;
 
   39 using OpenSim.Region.Framework.Scenes;
 
   41 namespace OpenSim.
Region.CoreModules.
World.LegacyMap
 
   63     [Extension(Path = 
"/OpenSim/RegionModules", NodeName = 
"RegionModule", Id = 
"MapImageModule")]
 
   66         private static readonly ILog m_log =
 
   67             LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 
   69         private Scene m_scene;
 
   70         private IConfigSource m_config;
 
   72         private bool m_Enabled = 
false;
 
   74         #region IMapImageGenerator Members 
   78             bool drawPrimVolume = 
true;
 
   79             bool textureTerrain = 
false;
 
   80             bool generateMaptiles = 
true;
 
   83             string[] configSections = 
new string[] { 
"Map", 
"Startup" };
 
   86                 = Util.GetConfigVarFromSections<
bool>(m_config, 
"DrawPrimOnMapTile", configSections, drawPrimVolume);
 
   88                 = Util.GetConfigVarFromSections<
bool>(m_config, 
"TextureOnMapTile", configSections, textureTerrain);
 
   90                 = Util.GetConfigVarFromSections<
bool>(m_config, 
"GenerateMaptiles", configSections, generateMaptiles);
 
   94                 if (
String.IsNullOrEmpty(m_scene.RegionInfo.MaptileStaticFile))
 
  105                     terrainRenderer.Initialise(m_scene, m_config);
 
  107                     mapbmp = 
new Bitmap((
int)m_scene.Heightmap.Width, (
int)m_scene.Heightmap.Height,
 
  108                                             System.Drawing.Imaging.PixelFormat.Format24bppRgb);
 
  111                     terrainRenderer.TerrainToBitmap(mapbmp);
 
  117                         DrawObjectVolume(m_scene, mapbmp);
 
  124                         mapbmp = 
new Bitmap(m_scene.RegionInfo.MaptileStaticFile);
 
  129                             "[MAPTILE]: Failed to load Static map image texture file: {0} for {1}", 
 
  130                             m_scene.RegionInfo.MaptileStaticFile, m_scene.Name);
 
  137                             "[MAPTILE]: Static map image texture file {0} found for {1}", 
 
  138                             m_scene.RegionInfo.MaptileStaticFile, m_scene.Name);
 
  143                 mapbmp = FetchTexture(m_scene.RegionInfo.RegionSettings.TerrainImageID);
 
  153                 using (Bitmap mapbmp = CreateMapTile())
 
  156                         return OpenJPEG.EncodeFromImage(mapbmp, 
true);
 
  161                 m_log.Error(
"Failed generating terrain map: " + e);
 
  169         #region Region Module interface 
  175             if (Util.GetConfigVarFromSections<
string>(
 
  176                 m_config, 
"MapImageModule", 
new string[] { 
"Startup", 
"Map" }, 
"MapImageModule") != 
"MapImageModule")
 
  206             get { 
return "MapImageModule"; }
 
  209         public Type ReplaceableInterface
 
  257         private Bitmap FetchTexture(UUID 
id)
 
  259             AssetBase asset = m_scene.AssetService.Get(id.ToString());
 
  263                 m_log.DebugFormat(
"[MAPTILE]: Static map image texture {0} found for {1}", id, m_scene.Name);
 
  267                 m_log.WarnFormat(
"[MAPTILE]: Static map image texture {0} not found for {1}", id, m_scene.Name);
 
  271             ManagedImage managedImage;
 
  276                 if (OpenJPEG.DecodeToImage(asset.Data, out managedImage, out image))
 
  277                     return new Bitmap(image);
 
  281             catch (DllNotFoundException)
 
  283                 m_log.ErrorFormat(
"[MAPTILE]: OpenJpeg is not installed correctly on this system.   Asset Data is empty for {0}", id);
 
  286             catch (IndexOutOfRangeException)
 
  288                 m_log.ErrorFormat(
"[MAPTILE]: OpenJpeg was unable to decode this.   Asset Data is empty for {0}", id);
 
  293                 m_log.ErrorFormat(
"[MAPTILE]: OpenJpeg was unable to decode this.   Asset Data is empty for {0}", id);
 
  300         private Bitmap DrawObjectVolume(
Scene whichScene, Bitmap mapbmp)
 
  304             tc = Environment.TickCount;
 
  305             m_log.Debug(
"[MAPTILE]: Generating Maptile Step 2: Object Volume Profile");
 
  307             List<float> z_sortheights = 
new List<float>();
 
  308             List<uint> z_localIDs = 
new List<uint>();
 
  309             Dictionary<uint, DrawStruct> z_sort = 
new Dictionary<uint, DrawStruct>();
 
  320                             SceneObjectGroup mapdot = (SceneObjectGroup)obj;
 
  321                             Color mapdotspot = Color.Gray; 
 
  340                                         if (part.
Shape == null)
 
  343                                         if (part.
Shape.
PCode == (byte)PCode.Tree || part.Shape.PCode == (byte)PCode.NewTree || part.
Shape.
PCode == (byte)PCode.Grass)
 
  348                                         Primitive.TextureEntry textureEntry = part.Shape.Textures;
 
  350                                         if (textureEntry == null || textureEntry.DefaultTexture == null)
 
  353                                         Color4 texcolor = textureEntry.DefaultTexture.RGBA;
 
  357                                         int colorr = 255 - (int)(texcolor.R * 255f);
 
  358                                         int colorg = 255 - (int)(texcolor.G * 255f);
 
  359                                         int colorb = 255 - (int)(texcolor.B * 255f);
 
  361                                         if (!(colorr == 255 && colorg == 255 && colorb == 255))
 
  367                                                 mapdotspot = Color.FromArgb(colorr, colorg, colorb);
 
  369                                             catch (ArgumentException)
 
  374                                     catch (IndexOutOfRangeException)
 
  378                                     catch (ArgumentOutOfRangeException)
 
  383                                     Vector3 pos = part.GetWorldPosition();
 
  386                                     if (!m_scene.PositionIsInCurrentRegion(pos))
 
  390                                     if (Single.IsNaN(pos.X) || Single.IsNaN(pos.Y) ||
 
  391                                         Single.IsInfinity(pos.X) || Single.IsInfinity(pos.Y))
 
  395                                     bool isBelow256AboveTerrain = 
false;
 
  399                                         isBelow256AboveTerrain = (pos.Z < ((float)hm[(
int)pos.X, (
int)pos.Y] + 256f));
 
  405                                     if (isBelow256AboveTerrain)
 
  409                                         Vector3 scale = 
new Vector3();
 
  410                                         Vector3 tScale = 
new Vector3();
 
  411                                         Vector3 axPos = 
new Vector3(pos.X, pos.Y, pos.Z);
 
  413                                         Quaternion llrot = part.GetWorldRotation();
 
  414                                         Quaternion rot = 
new Quaternion(llrot.W, llrot.X, llrot.Y, llrot.Z);
 
  415                                         scale = lscale * rot;
 
  418                                         scale.X = Math.Abs(scale.X);
 
  419                                         scale.Y = Math.Abs(scale.Y);
 
  420                                         scale.Z = Math.Abs(scale.Z);
 
  423                                         int mapdrawstartX = (int)(pos.X - scale.X);
 
  424                                         int mapdrawstartY = (int)(pos.Y - scale.Y);
 
  425                                         int mapdrawendX = (int)(pos.X + scale.X);
 
  426                                         int mapdrawendY = (int)(pos.Y + scale.Y);
 
  429                                         if (mapdrawstartX < 0
 
  430                                                     || mapdrawstartX > (hm.
Width - 1)
 
  432                                                     || mapdrawendX > (hm.Width - 1)
 
  434                                                     || mapdrawstartY > (hm.
Height - 1)
 
  436                                                     || mapdrawendY > (hm.Height - 1))
 
  439                                         #region obb face reconstruction part duex 
  440                                         Vector3[] vertexes = 
new Vector3[8];
 
  443                                         Vector3[] FaceA = 
new Vector3[6]; 
 
  444                                         Vector3[] FaceB = 
new Vector3[6]; 
 
  445                                         Vector3[] FaceC = 
new Vector3[6]; 
 
  446                                         Vector3[] FaceD = 
new Vector3[6]; 
 
  448                                         tScale = 
new Vector3(lscale.X, -lscale.Y, lscale.Z);
 
  449                                         scale = ((tScale * rot));
 
  450                                         vertexes[0] = (
new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
 
  455                                         FaceA[0] = vertexes[0];
 
  456                                         FaceB[3] = vertexes[0];
 
  457                                         FaceA[4] = vertexes[0];
 
  460                                         scale = ((tScale * rot));
 
  461                                         vertexes[1] = (
new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
 
  467                                         FaceB[0] = vertexes[1];
 
  468                                         FaceA[1] = vertexes[1];
 
  469                                         FaceC[4] = vertexes[1];
 
  471                                         tScale = 
new Vector3(lscale.X, -lscale.Y, -lscale.Z);
 
  472                                         scale = ((tScale * rot));
 
  474                                         vertexes[2] = (
new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
 
  480                                         FaceC[0] = vertexes[2];
 
  481                                         FaceD[3] = vertexes[2];
 
  482                                         FaceC[5] = vertexes[2];
 
  484                                         tScale = 
new Vector3(lscale.X, lscale.Y, -lscale.Z);
 
  485                                         scale = ((tScale * rot));
 
  486                                         vertexes[3] = (
new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
 
  492                                         FaceD[0] = vertexes[3];
 
  493                                         FaceC[1] = vertexes[3];
 
  494                                         FaceA[5] = vertexes[3];
 
  496                                         tScale = 
new Vector3(-lscale.X, lscale.Y, lscale.Z);
 
  497                                         scale = ((tScale * rot));
 
  498                                         vertexes[4] = (
new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
 
  504                                         FaceB[1] = vertexes[4];
 
  505                                         FaceA[2] = vertexes[4];
 
  506                                         FaceD[4] = vertexes[4];
 
  508                                         tScale = 
new Vector3(-lscale.X, lscale.Y, -lscale.Z);
 
  509                                         scale = ((tScale * rot));
 
  510                                         vertexes[5] = (
new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
 
  516                                         FaceD[1] = vertexes[5];
 
  517                                         FaceC[2] = vertexes[5];
 
  518                                         FaceB[5] = vertexes[5];
 
  520                                         tScale = 
new Vector3(-lscale.X, -lscale.Y, lscale.Z);
 
  521                                         scale = ((tScale * rot));
 
  522                                         vertexes[6] = (
new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
 
  528                                         FaceB[2] = vertexes[6];
 
  529                                         FaceA[3] = vertexes[6];
 
  530                                         FaceB[4] = vertexes[6];
 
  532                                         tScale = 
new Vector3(-lscale.X, -lscale.Y, -lscale.Z);
 
  533                                         scale = ((tScale * rot));
 
  534                                         vertexes[7] = (
new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
 
  540                                         FaceD[2] = vertexes[7];
 
  541                                         FaceC[3] = vertexes[7];
 
  542                                         FaceD[5] = vertexes[7];
 
  549                                         DrawStruct ds = 
new DrawStruct();
 
  550                                         ds.brush = 
new SolidBrush(mapdotspot);
 
  553                                         ds.trns = 
new face[FaceA.Length];
 
  555                                         for (
int i = 0; i < FaceA.Length; i++)
 
  557                                             Point[] working = 
new Point[5];
 
  558                                             working[0] = project(hm, FaceA[i], axPos);
 
  559                                             working[1] = project(hm, FaceB[i], axPos);
 
  560                                             working[2] = project(hm, FaceD[i], axPos);
 
  561                                             working[3] = project(hm, FaceC[i], axPos);
 
  562                                             working[4] = project(hm, FaceA[i], axPos);
 
  564                                             face workingface = 
new face();
 
  565                                             workingface.pts = working;
 
  567                                             ds.trns[i] = workingface;
 
  570                                         z_sort.Add(part.LocalId, ds);
 
  571                                         z_localIDs.Add(part.LocalId);
 
  572                                         z_sortheights.Add(pos.Z);
 
  600                     float[] sortedZHeights = z_sortheights.ToArray();
 
  601                     uint[] sortedlocalIds = z_localIDs.ToArray();
 
  604                     Array.Sort(sortedZHeights, sortedlocalIds);
 
  606                     using (Graphics g = Graphics.FromImage(mapbmp))
 
  608                         for (
int s = 0; s < sortedZHeights.Length; s++)
 
  610                             if (z_sort.ContainsKey(sortedlocalIds[s]))
 
  612                                 DrawStruct rectDrawStruct = z_sort[sortedlocalIds[s]];
 
  613                                 for (
int r = 0; r < rectDrawStruct.trns.Length; r++)
 
  615                                     g.FillPolygon(rectDrawStruct.brush,rectDrawStruct.trns[r].pts);
 
  626                 foreach (DrawStruct ds 
in z_sort.Values)
 
  630             m_log.Debug(
"[MAPTILE]: Generating Maptile Step 2: Done in " + (Environment.TickCount - tc) + 
" ms");
 
  635         private Point project(
ITerrainChannel hm, Vector3 point3d, Vector3 originpos)
 
  637             Point returnpt = 
new Point();
 
  644             returnpt.X = (int)point3d.X;
 
  645             returnpt.Y = (
int)((hm.Width - 1) - point3d.Y);
 
  650         public Bitmap 
CreateViewImage(Vector3 camPos, Vector3 camDir, 
float fov, 
int width, 
int height, 
bool useTextures)
 
Bitmap CreateViewImage(Vector3 camPos, Vector3 camDir, float fov, int width, int height, bool useTextures)
 
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
 
A scene object group is conceptually an object in the scene. The object is constituted of SceneObject...
 
byte[] WriteJpeg2000Image()
 
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
 
Asset class. All Assets are reference by this class or a class derived from this class ...
 
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
 
Vector3 Scale
Change the scale of this part. 
 
void Initialise(IConfigSource source)
This is called to initialize the region module. For shared modules, this is called exactly once...
 
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...