MapImageModule.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Drawing;
  31. using System.Drawing.Drawing2D;
  32. using System.Drawing.Imaging;
  33. using System.Reflection;
  34. using Nini.Config;
  35. using OpenMetaverse.Imaging;
  36. using log4net;
  37. using OpenSim.Region.Environment.Interfaces;
  38. using OpenSim.Region.Environment.Scenes;
  39. using OpenMetaverse;
  40. namespace OpenSim.Region.Environment.Modules.World.WorldMap
  41. {
  42. public enum DrawRoutine
  43. {
  44. Rectangle,
  45. Polygon,
  46. Ellipse
  47. }
  48. public struct face
  49. {
  50. public Point[] pts;
  51. }
  52. public struct DrawStruct
  53. {
  54. public DrawRoutine dr;
  55. public Rectangle rect;
  56. public SolidBrush brush;
  57. public face[] trns;
  58. }
  59. public class MapImageModule : IMapImageGenerator, IRegionModule
  60. {
  61. private static readonly ILog m_log =
  62. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  63. private Scene m_scene;
  64. private IConfigSource m_config;
  65. private IMapTileTerrainRenderer terrainRenderer;
  66. #region IMapImageGenerator Members
  67. public byte[] WriteJpeg2000Image(string gradientmap)
  68. {
  69. byte[] imageData = null;
  70. bool drawPrimVolume = true;
  71. bool textureTerrain = true;
  72. try
  73. {
  74. IConfig startupConfig = m_config.Configs["Startup"];
  75. drawPrimVolume = startupConfig.GetBoolean("DrawPrimOnMapTile", drawPrimVolume);
  76. textureTerrain = startupConfig.GetBoolean("TextureOnMapTile", textureTerrain);
  77. }
  78. catch
  79. {
  80. m_log.Warn("[MAPTILE]: Failed to load StartupConfig");
  81. }
  82. if (textureTerrain)
  83. {
  84. terrainRenderer = new TexturedMapTileRenderer();
  85. }
  86. else
  87. {
  88. terrainRenderer = new ShadedMapTileRenderer();
  89. }
  90. terrainRenderer.Initialise(m_scene, m_config);
  91. Bitmap mapbmp = new Bitmap(256, 256);
  92. //long t = System.Environment.TickCount;
  93. //for (int i = 0; i < 10; ++i) {
  94. terrainRenderer.TerrainToBitmap(mapbmp);
  95. //}
  96. //t = System.Environment.TickCount - t;
  97. //m_log.InfoFormat("[MAPTILE] generation of 10 maptiles needed {0} ms", t);
  98. if (drawPrimVolume)
  99. {
  100. DrawObjectVolume(m_scene, mapbmp);
  101. }
  102. try
  103. {
  104. imageData = OpenJPEG.EncodeFromImage(mapbmp, true);
  105. }
  106. catch (Exception e) // LEGIT: Catching problems caused by OpenJPEG p/invoke
  107. {
  108. Console.WriteLine("Failed generating terrain map: " + e);
  109. }
  110. return imageData;
  111. }
  112. #endregion
  113. #region IRegionModule Members
  114. public void Initialise(Scene scene, IConfigSource source)
  115. {
  116. m_scene = scene;
  117. m_config = source;
  118. IConfig startupConfig = m_config.Configs["Startup"];
  119. if (startupConfig.GetString("MapImageModule", "MapImageModule") !=
  120. "MapImageModule")
  121. return;
  122. m_scene.RegisterModuleInterface<IMapImageGenerator>(this);
  123. }
  124. public void PostInitialise()
  125. {
  126. }
  127. public void Close()
  128. {
  129. }
  130. public string Name
  131. {
  132. get { return "MapImageModule"; }
  133. }
  134. public bool IsSharedModule
  135. {
  136. get { return false; }
  137. }
  138. #endregion
  139. // TODO: unused:
  140. // private void ShadeBuildings(Bitmap map)
  141. // {
  142. // lock (map)
  143. // {
  144. // lock (m_scene.Entities)
  145. // {
  146. // foreach (EntityBase entity in m_scene.Entities.Values)
  147. // {
  148. // if (entity is SceneObjectGroup)
  149. // {
  150. // SceneObjectGroup sog = (SceneObjectGroup) entity;
  151. //
  152. // foreach (SceneObjectPart primitive in sog.Children.Values)
  153. // {
  154. // int x = (int) (primitive.AbsolutePosition.X - (primitive.Scale.X / 2));
  155. // int y = (int) (primitive.AbsolutePosition.Y - (primitive.Scale.Y / 2));
  156. // int w = (int) primitive.Scale.X;
  157. // int h = (int) primitive.Scale.Y;
  158. //
  159. // int dx;
  160. // for (dx = x; dx < x + w; dx++)
  161. // {
  162. // int dy;
  163. // for (dy = y; dy < y + h; dy++)
  164. // {
  165. // if (x < 0 || y < 0)
  166. // continue;
  167. // if (x >= map.Width || y >= map.Height)
  168. // continue;
  169. //
  170. // map.SetPixel(dx, dy, Color.DarkGray);
  171. // }
  172. // }
  173. // }
  174. // }
  175. // }
  176. // }
  177. // }
  178. // }
  179. private Bitmap DrawObjectVolume(Scene whichScene, Bitmap mapbmp)
  180. {
  181. int tc = 0;
  182. double[,] hm = whichScene.Heightmap.GetDoubles();
  183. tc = System.Environment.TickCount;
  184. m_log.Info("[MAPTILE]: Generating Maptile Step 2: Object Volume Profile");
  185. List<EntityBase> objs = whichScene.GetEntities();
  186. Dictionary<uint, DrawStruct> z_sort = new Dictionary<uint, DrawStruct>();
  187. //SortedList<float, RectangleDrawStruct> z_sort = new SortedList<float, RectangleDrawStruct>();
  188. List<float> z_sortheights = new List<float>();
  189. List<uint> z_localIDs = new List<uint>();
  190. lock (objs)
  191. {
  192. foreach (EntityBase obj in objs)
  193. {
  194. // Only draw the contents of SceneObjectGroup
  195. if (obj is SceneObjectGroup)
  196. {
  197. SceneObjectGroup mapdot = (SceneObjectGroup)obj;
  198. Color mapdotspot = Color.Gray; // Default color when prim color is white
  199. // Loop over prim in group
  200. foreach (SceneObjectPart part in mapdot.Children.Values)
  201. {
  202. if (part == null)
  203. continue;
  204. // Draw if the object is at least 1 meter wide in any direction
  205. if (part.Scale.X > 1f || part.Scale.Y > 1f || part.Scale.Z > 1f)
  206. {
  207. // Try to get the RGBA of the default texture entry..
  208. //
  209. try
  210. {
  211. // get the null checks out of the way
  212. // skip the ones that break
  213. if (part == null)
  214. continue;
  215. if (part.Shape == null)
  216. continue;
  217. if (part.Shape.PCode == (byte)PCode.Tree || part.Shape.PCode == (byte)PCode.NewTree || part.Shape.PCode == (byte)PCode.Grass)
  218. continue; // eliminates trees from this since we don't really have a good tree representation
  219. // if you want tree blocks on the map comment the above line and uncomment the below line
  220. //mapdotspot = Color.PaleGreen;
  221. if (part.Shape.Textures == null)
  222. continue;
  223. if (part.Shape.Textures.DefaultTexture == null)
  224. continue;
  225. Color4 texcolor = part.Shape.Textures.DefaultTexture.RGBA;
  226. // Not sure why some of these are null, oh well.
  227. int colorr = 255 - (int)(texcolor.R * 255f);
  228. int colorg = 255 - (int)(texcolor.G * 255f);
  229. int colorb = 255 - (int)(texcolor.B * 255f);
  230. if (!(colorr == 255 && colorg == 255 && colorb == 255))
  231. {
  232. //Try to set the map spot color
  233. try
  234. {
  235. // If the color gets goofy somehow, skip it *shakes fist at Color4
  236. mapdotspot = Color.FromArgb(colorr, colorg, colorb);
  237. }
  238. catch (ArgumentException)
  239. {
  240. }
  241. }
  242. }
  243. catch (IndexOutOfRangeException)
  244. {
  245. // Windows Array
  246. }
  247. catch (ArgumentOutOfRangeException)
  248. {
  249. // Mono Array
  250. }
  251. Vector3 pos = part.GetWorldPosition();
  252. // skip prim outside of retion
  253. if (pos.X < 0f || pos.X > 256f || pos.Y < 0f || pos.Y > 256f)
  254. continue;
  255. // skip prim in non-finite position
  256. if (Single.IsNaN(pos.X) || Single.IsNaN(pos.Y) ||
  257. Single.IsInfinity(pos.X) || Single.IsInfinity(pos.Y))
  258. continue;
  259. // Figure out if object is under 256m above the height of the terrain
  260. bool isBelow256AboveTerrain = false;
  261. try
  262. {
  263. isBelow256AboveTerrain = (pos.Z < ((float)hm[(int)pos.X, (int)pos.Y] + 256f));
  264. }
  265. catch (Exception)
  266. {
  267. }
  268. if (isBelow256AboveTerrain)
  269. {
  270. // Translate scale by rotation so scale is represented properly when object is rotated
  271. Vector3 lscale = new Vector3(part.Shape.Scale.X, part.Shape.Scale.Y, part.Shape.Scale.Z);
  272. Vector3 scale = new Vector3();
  273. Vector3 tScale = new Vector3();
  274. Vector3 axPos = new Vector3(pos.X,pos.Y,pos.Z);
  275. Quaternion llrot = part.GetWorldRotation();
  276. Quaternion rot = new Quaternion(llrot.W, llrot.X, llrot.Y, llrot.Z);
  277. scale = lscale * rot;
  278. // negative scales don't work in this situation
  279. scale.X = Math.Abs(scale.X);
  280. scale.Y = Math.Abs(scale.Y);
  281. scale.Z = Math.Abs(scale.Z);
  282. // This scaling isn't very accurate and doesn't take into account the face rotation :P
  283. int mapdrawstartX = (int)(pos.X - scale.X);
  284. int mapdrawstartY = (int)(pos.Y - scale.Y);
  285. int mapdrawendX = (int)(pos.X + scale.X);
  286. int mapdrawendY = (int)(pos.Y + scale.Y);
  287. // If object is beyond the edge of the map, don't draw it to avoid errors
  288. if (mapdrawstartX < 0 || mapdrawstartX > 255 || mapdrawendX < 0 || mapdrawendX > 255
  289. || mapdrawstartY < 0 || mapdrawstartY > 255 || mapdrawendY < 0
  290. || mapdrawendY > 255)
  291. continue;
  292. #region obb face reconstruction part duex
  293. Vector3[] vertexes = new Vector3[8];
  294. // float[] distance = new float[6];
  295. Vector3[] FaceA = new Vector3[6]; // vertex A for Facei
  296. Vector3[] FaceB = new Vector3[6]; // vertex B for Facei
  297. Vector3[] FaceC = new Vector3[6]; // vertex C for Facei
  298. Vector3[] FaceD = new Vector3[6]; // vertex D for Facei
  299. tScale = new Vector3(lscale.X, -lscale.Y, lscale.Z);
  300. scale = ((tScale * rot));
  301. vertexes[0] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
  302. // vertexes[0].x = pos.X + vertexes[0].x;
  303. //vertexes[0].y = pos.Y + vertexes[0].y;
  304. //vertexes[0].z = pos.Z + vertexes[0].z;
  305. FaceA[0] = vertexes[0];
  306. FaceB[3] = vertexes[0];
  307. FaceA[4] = vertexes[0];
  308. tScale = lscale;
  309. scale = ((tScale * rot));
  310. vertexes[1] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
  311. // vertexes[1].x = pos.X + vertexes[1].x;
  312. // vertexes[1].y = pos.Y + vertexes[1].y;
  313. //vertexes[1].z = pos.Z + vertexes[1].z;
  314. FaceB[0] = vertexes[1];
  315. FaceA[1] = vertexes[1];
  316. FaceC[4] = vertexes[1];
  317. tScale = new Vector3(lscale.X, -lscale.Y, -lscale.Z);
  318. scale = ((tScale * rot));
  319. vertexes[2] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
  320. //vertexes[2].x = pos.X + vertexes[2].x;
  321. //vertexes[2].y = pos.Y + vertexes[2].y;
  322. //vertexes[2].z = pos.Z + vertexes[2].z;
  323. FaceC[0] = vertexes[2];
  324. FaceD[3] = vertexes[2];
  325. FaceC[5] = vertexes[2];
  326. tScale = new Vector3(lscale.X, lscale.Y, -lscale.Z);
  327. scale = ((tScale * rot));
  328. vertexes[3] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
  329. //vertexes[3].x = pos.X + vertexes[3].x;
  330. // vertexes[3].y = pos.Y + vertexes[3].y;
  331. // vertexes[3].z = pos.Z + vertexes[3].z;
  332. FaceD[0] = vertexes[3];
  333. FaceC[1] = vertexes[3];
  334. FaceA[5] = vertexes[3];
  335. tScale = new Vector3(-lscale.X, lscale.Y, lscale.Z);
  336. scale = ((tScale * rot));
  337. vertexes[4] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
  338. // vertexes[4].x = pos.X + vertexes[4].x;
  339. // vertexes[4].y = pos.Y + vertexes[4].y;
  340. // vertexes[4].z = pos.Z + vertexes[4].z;
  341. FaceB[1] = vertexes[4];
  342. FaceA[2] = vertexes[4];
  343. FaceD[4] = vertexes[4];
  344. tScale = new Vector3(-lscale.X, lscale.Y, -lscale.Z);
  345. scale = ((tScale * rot));
  346. vertexes[5] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
  347. // vertexes[5].x = pos.X + vertexes[5].x;
  348. // vertexes[5].y = pos.Y + vertexes[5].y;
  349. // vertexes[5].z = pos.Z + vertexes[5].z;
  350. FaceD[1] = vertexes[5];
  351. FaceC[2] = vertexes[5];
  352. FaceB[5] = vertexes[5];
  353. tScale = new Vector3(-lscale.X, -lscale.Y, lscale.Z);
  354. scale = ((tScale * rot));
  355. vertexes[6] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
  356. // vertexes[6].x = pos.X + vertexes[6].x;
  357. // vertexes[6].y = pos.Y + vertexes[6].y;
  358. // vertexes[6].z = pos.Z + vertexes[6].z;
  359. FaceB[2] = vertexes[6];
  360. FaceA[3] = vertexes[6];
  361. FaceB[4] = vertexes[6];
  362. tScale = new Vector3(-lscale.X, -lscale.Y, -lscale.Z);
  363. scale = ((tScale * rot));
  364. vertexes[7] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
  365. // vertexes[7].x = pos.X + vertexes[7].x;
  366. // vertexes[7].y = pos.Y + vertexes[7].y;
  367. // vertexes[7].z = pos.Z + vertexes[7].z;
  368. FaceD[2] = vertexes[7];
  369. FaceC[3] = vertexes[7];
  370. FaceD[5] = vertexes[7];
  371. #endregion
  372. //int wy = 0;
  373. //bool breakYN = false; // If we run into an error drawing, break out of the
  374. // loop so we don't lag to death on error handling
  375. DrawStruct ds = new DrawStruct();
  376. ds.brush = new SolidBrush(mapdotspot);
  377. //ds.rect = new Rectangle(mapdrawstartX, (255 - mapdrawstartY), mapdrawendX - mapdrawstartX, mapdrawendY - mapdrawstartY);
  378. ds.trns = new face[FaceA.Length];
  379. for (int i = 0; i < FaceA.Length; i++)
  380. {
  381. Point[] working = new Point[5];
  382. working[0] = project(FaceA[i], axPos);
  383. working[1] = project(FaceB[i], axPos);
  384. working[2] = project(FaceD[i], axPos);
  385. working[3] = project(FaceC[i], axPos);
  386. working[4] = project(FaceA[i], axPos);
  387. face workingface = new face();
  388. workingface.pts = working;
  389. ds.trns[i] = workingface;
  390. }
  391. z_sort.Add(part.LocalId, ds);
  392. z_localIDs.Add(part.LocalId);
  393. z_sortheights.Add(pos.Z);
  394. //for (int wx = mapdrawstartX; wx < mapdrawendX; wx++)
  395. //{
  396. //for (wy = mapdrawstartY; wy < mapdrawendY; wy++)
  397. //{
  398. //m_log.InfoFormat("[MAPDEBUG]: {0},{1}({2})", wx, (255 - wy),wy);
  399. //try
  400. //{
  401. // Remember, flip the y!
  402. // mapbmp.SetPixel(wx, (255 - wy), mapdotspot);
  403. //}
  404. //catch (ArgumentException)
  405. //{
  406. // breakYN = true;
  407. //}
  408. //if (breakYN)
  409. // break;
  410. //}
  411. //if (breakYN)
  412. // break;
  413. //}
  414. } // Object is within 256m Z of terrain
  415. } // object is at least a meter wide
  416. } // loop over group children
  417. } // entitybase is sceneobject group
  418. } // foreach loop over entities
  419. float[] sortedZHeights = z_sortheights.ToArray();
  420. uint[] sortedlocalIds = z_localIDs.ToArray();
  421. // Sort prim by Z position
  422. Array.Sort(sortedZHeights, sortedlocalIds);
  423. Graphics g = Graphics.FromImage(mapbmp);
  424. for (int s = 0; s < sortedZHeights.Length; s++)
  425. {
  426. if (z_sort.ContainsKey(sortedlocalIds[s]))
  427. {
  428. DrawStruct rectDrawStruct = z_sort[sortedlocalIds[s]];
  429. for (int r = 0; r < rectDrawStruct.trns.Length; r++ )
  430. {
  431. g.FillPolygon(rectDrawStruct.brush,rectDrawStruct.trns[r].pts);
  432. }
  433. //g.FillRectangle(rectDrawStruct.brush , rectDrawStruct.rect);
  434. }
  435. }
  436. g.Dispose();
  437. } // lock entities objs
  438. m_log.Info("[MAPTILE]: Generating Maptile Step 2: Done in " + (System.Environment.TickCount - tc) + " ms");
  439. return mapbmp;
  440. }
  441. private Point project(Vector3 point3d, Vector3 originpos)
  442. {
  443. Point returnpt = new Point();
  444. //originpos = point3d;
  445. //int d = (int)(256f / 1.5f);
  446. //Vector3 topos = new Vector3(0, 0, 0);
  447. // float z = -point3d.z - topos.z;
  448. returnpt.X = (int)point3d.X;//(int)((topos.x - point3d.x) / z * d);
  449. returnpt.Y = (int)(255 - point3d.Y);//(int)(255 - (((topos.y - point3d.y) / z * d)));
  450. return returnpt;
  451. }
  452. // TODO: unused:
  453. // #region Deprecated Maptile Generation. Adam may update this
  454. // private Bitmap TerrainToBitmap(string gradientmap)
  455. // {
  456. // Bitmap gradientmapLd = new Bitmap(gradientmap);
  457. //
  458. // int pallete = gradientmapLd.Height;
  459. //
  460. // Bitmap bmp = new Bitmap(m_scene.Heightmap.Width, m_scene.Heightmap.Height);
  461. // Color[] colours = new Color[pallete];
  462. //
  463. // for (int i = 0; i < pallete; i++)
  464. // {
  465. // colours[i] = gradientmapLd.GetPixel(0, i);
  466. // }
  467. //
  468. // lock (m_scene.Heightmap)
  469. // {
  470. // ITerrainChannel copy = m_scene.Heightmap;
  471. // for (int y = 0; y < copy.Height; y++)
  472. // {
  473. // for (int x = 0; x < copy.Width; x++)
  474. // {
  475. // // 512 is the largest possible height before colours clamp
  476. // int colorindex = (int) (Math.Max(Math.Min(1.0, copy[x, y] / 512.0), 0.0) * (pallete - 1));
  477. //
  478. // // Handle error conditions
  479. // if (colorindex > pallete - 1 || colorindex < 0)
  480. // bmp.SetPixel(x, copy.Height - y - 1, Color.Red);
  481. // else
  482. // bmp.SetPixel(x, copy.Height - y - 1, colours[colorindex]);
  483. // }
  484. // }
  485. // ShadeBuildings(bmp);
  486. // return bmp;
  487. // }
  488. // }
  489. // #endregion
  490. }
  491. }