aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs')
-rw-r--r--OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs586
1 files changed, 0 insertions, 586 deletions
diff --git a/OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs b/OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs
deleted file mode 100644
index a806c7d..0000000
--- a/OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs
+++ /dev/null
@@ -1,586 +0,0 @@
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
28using System;
29using System.Collections;
30using System.Collections.Generic;
31using System.Drawing;
32using System.Drawing.Drawing2D;
33using System.Drawing.Imaging;
34using System.Reflection;
35using Nini.Config;
36using OpenMetaverse.Imaging;
37using log4net;
38using OpenSim.Region.Framework.Interfaces;
39using OpenSim.Region.Framework.Scenes;
40using OpenMetaverse;
41
42namespace OpenSim.Region.Environment.Modules.World.WorldMap
43{
44 public enum DrawRoutine
45 {
46 Rectangle,
47 Polygon,
48 Ellipse
49 }
50
51 public struct face
52 {
53 public Point[] pts;
54 }
55
56 public struct DrawStruct
57 {
58 public DrawRoutine dr;
59 public Rectangle rect;
60 public SolidBrush brush;
61 public face[] trns;
62 }
63
64 public class MapImageModule : IMapImageGenerator, IRegionModule
65 {
66 private static readonly ILog m_log =
67 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
68
69 private Scene m_scene;
70 private IConfigSource m_config;
71 private IMapTileTerrainRenderer terrainRenderer;
72
73 #region IMapImageGenerator Members
74
75 public byte[] WriteJpeg2000Image(string gradientmap)
76 {
77 byte[] imageData = null;
78
79 bool drawPrimVolume = true;
80 bool textureTerrain = true;
81
82 try
83 {
84 IConfig startupConfig = m_config.Configs["Startup"];
85 drawPrimVolume = startupConfig.GetBoolean("DrawPrimOnMapTile", drawPrimVolume);
86 textureTerrain = startupConfig.GetBoolean("TextureOnMapTile", textureTerrain);
87 }
88 catch
89 {
90 m_log.Warn("[MAPTILE]: Failed to load StartupConfig");
91 }
92
93 if (textureTerrain)
94 {
95 terrainRenderer = new TexturedMapTileRenderer();
96 }
97 else
98 {
99 terrainRenderer = new ShadedMapTileRenderer();
100 }
101 terrainRenderer.Initialise(m_scene, m_config);
102
103 Bitmap mapbmp = new Bitmap(256, 256);
104 //long t = System.Environment.TickCount;
105 //for (int i = 0; i < 10; ++i) {
106 terrainRenderer.TerrainToBitmap(mapbmp);
107 //}
108 //t = System.Environment.TickCount - t;
109 //m_log.InfoFormat("[MAPTILE] generation of 10 maptiles needed {0} ms", t);
110
111
112 if (drawPrimVolume)
113 {
114 DrawObjectVolume(m_scene, mapbmp);
115 }
116
117 try
118 {
119 imageData = OpenJPEG.EncodeFromImage(mapbmp, true);
120 }
121 catch (Exception e) // LEGIT: Catching problems caused by OpenJPEG p/invoke
122 {
123 Console.WriteLine("Failed generating terrain map: " + e);
124 }
125
126 return imageData;
127 }
128
129 #endregion
130
131 #region IRegionModule Members
132
133 public void Initialise(Scene scene, IConfigSource source)
134 {
135 m_scene = scene;
136 m_config = source;
137
138 IConfig startupConfig = m_config.Configs["Startup"];
139 if (startupConfig.GetString("MapImageModule", "MapImageModule") !=
140 "MapImageModule")
141 return;
142
143 m_scene.RegisterModuleInterface<IMapImageGenerator>(this);
144 }
145
146 public void PostInitialise()
147 {
148 }
149
150 public void Close()
151 {
152 }
153
154 public string Name
155 {
156 get { return "MapImageModule"; }
157 }
158
159 public bool IsSharedModule
160 {
161 get { return false; }
162 }
163
164 #endregion
165
166// TODO: unused:
167// private void ShadeBuildings(Bitmap map)
168// {
169// lock (map)
170// {
171// lock (m_scene.Entities)
172// {
173// foreach (EntityBase entity in m_scene.Entities.Values)
174// {
175// if (entity is SceneObjectGroup)
176// {
177// SceneObjectGroup sog = (SceneObjectGroup) entity;
178//
179// foreach (SceneObjectPart primitive in sog.Children.Values)
180// {
181// int x = (int) (primitive.AbsolutePosition.X - (primitive.Scale.X / 2));
182// int y = (int) (primitive.AbsolutePosition.Y - (primitive.Scale.Y / 2));
183// int w = (int) primitive.Scale.X;
184// int h = (int) primitive.Scale.Y;
185//
186// int dx;
187// for (dx = x; dx < x + w; dx++)
188// {
189// int dy;
190// for (dy = y; dy < y + h; dy++)
191// {
192// if (x < 0 || y < 0)
193// continue;
194// if (x >= map.Width || y >= map.Height)
195// continue;
196//
197// map.SetPixel(dx, dy, Color.DarkGray);
198// }
199// }
200// }
201// }
202// }
203// }
204// }
205// }
206
207 private Bitmap DrawObjectVolume(Scene whichScene, Bitmap mapbmp)
208 {
209 int tc = 0;
210 double[,] hm = whichScene.Heightmap.GetDoubles();
211 tc = System.Environment.TickCount;
212 m_log.Info("[MAPTILE]: Generating Maptile Step 2: Object Volume Profile");
213 List<EntityBase> objs = whichScene.GetEntities();
214 Dictionary<uint, DrawStruct> z_sort = new Dictionary<uint, DrawStruct>();
215 //SortedList<float, RectangleDrawStruct> z_sort = new SortedList<float, RectangleDrawStruct>();
216 List<float> z_sortheights = new List<float>();
217 List<uint> z_localIDs = new List<uint>();
218
219 lock (objs)
220 {
221 foreach (EntityBase obj in objs)
222 {
223 // Only draw the contents of SceneObjectGroup
224 if (obj is SceneObjectGroup)
225 {
226 SceneObjectGroup mapdot = (SceneObjectGroup)obj;
227 Color mapdotspot = Color.Gray; // Default color when prim color is white
228 // Loop over prim in group
229 foreach (SceneObjectPart part in mapdot.Children.Values)
230 {
231 if (part == null)
232 continue;
233
234 // Draw if the object is at least 1 meter wide in any direction
235 if (part.Scale.X > 1f || part.Scale.Y > 1f || part.Scale.Z > 1f)
236 {
237 // Try to get the RGBA of the default texture entry..
238 //
239 try
240 {
241 // get the null checks out of the way
242 // skip the ones that break
243 if (part == null)
244 continue;
245
246 if (part.Shape == null)
247 continue;
248
249 if (part.Shape.PCode == (byte)PCode.Tree || part.Shape.PCode == (byte)PCode.NewTree || part.Shape.PCode == (byte)PCode.Grass)
250 continue; // eliminates trees from this since we don't really have a good tree representation
251 // if you want tree blocks on the map comment the above line and uncomment the below line
252 //mapdotspot = Color.PaleGreen;
253
254 if (part.Shape.Textures == null)
255 continue;
256
257 if (part.Shape.Textures.DefaultTexture == null)
258 continue;
259
260 Color4 texcolor = part.Shape.Textures.DefaultTexture.RGBA;
261
262 // Not sure why some of these are null, oh well.
263
264 int colorr = 255 - (int)(texcolor.R * 255f);
265 int colorg = 255 - (int)(texcolor.G * 255f);
266 int colorb = 255 - (int)(texcolor.B * 255f);
267
268 if (!(colorr == 255 && colorg == 255 && colorb == 255))
269 {
270 //Try to set the map spot color
271 try
272 {
273 // If the color gets goofy somehow, skip it *shakes fist at Color4
274 mapdotspot = Color.FromArgb(colorr, colorg, colorb);
275 }
276 catch (ArgumentException)
277 {
278 }
279 }
280 }
281 catch (IndexOutOfRangeException)
282 {
283 // Windows Array
284 }
285 catch (ArgumentOutOfRangeException)
286 {
287 // Mono Array
288 }
289
290 Vector3 pos = part.GetWorldPosition();
291
292 // skip prim outside of retion
293 if (pos.X < 0f || pos.X > 256f || pos.Y < 0f || pos.Y > 256f)
294 continue;
295
296 // skip prim in non-finite position
297 if (Single.IsNaN(pos.X) || Single.IsNaN(pos.Y) ||
298 Single.IsInfinity(pos.X) || Single.IsInfinity(pos.Y))
299 continue;
300
301 // Figure out if object is under 256m above the height of the terrain
302 bool isBelow256AboveTerrain = false;
303
304 try
305 {
306 isBelow256AboveTerrain = (pos.Z < ((float)hm[(int)pos.X, (int)pos.Y] + 256f));
307 }
308 catch (Exception)
309 {
310 }
311
312 if (isBelow256AboveTerrain)
313 {
314 // Translate scale by rotation so scale is represented properly when object is rotated
315 Vector3 lscale = new Vector3(part.Shape.Scale.X, part.Shape.Scale.Y, part.Shape.Scale.Z);
316 Vector3 scale = new Vector3();
317 Vector3 tScale = new Vector3();
318 Vector3 axPos = new Vector3(pos.X,pos.Y,pos.Z);
319
320 Quaternion llrot = part.GetWorldRotation();
321 Quaternion rot = new Quaternion(llrot.W, llrot.X, llrot.Y, llrot.Z);
322 scale = lscale * rot;
323
324 // negative scales don't work in this situation
325 scale.X = Math.Abs(scale.X);
326 scale.Y = Math.Abs(scale.Y);
327 scale.Z = Math.Abs(scale.Z);
328
329 // This scaling isn't very accurate and doesn't take into account the face rotation :P
330 int mapdrawstartX = (int)(pos.X - scale.X);
331 int mapdrawstartY = (int)(pos.Y - scale.Y);
332 int mapdrawendX = (int)(pos.X + scale.X);
333 int mapdrawendY = (int)(pos.Y + scale.Y);
334
335 // If object is beyond the edge of the map, don't draw it to avoid errors
336 if (mapdrawstartX < 0 || mapdrawstartX > 255 || mapdrawendX < 0 || mapdrawendX > 255
337 || mapdrawstartY < 0 || mapdrawstartY > 255 || mapdrawendY < 0
338 || mapdrawendY > 255)
339 continue;
340
341#region obb face reconstruction part duex
342 Vector3[] vertexes = new Vector3[8];
343
344 // float[] distance = new float[6];
345 Vector3[] FaceA = new Vector3[6]; // vertex A for Facei
346 Vector3[] FaceB = new Vector3[6]; // vertex B for Facei
347 Vector3[] FaceC = new Vector3[6]; // vertex C for Facei
348 Vector3[] FaceD = new Vector3[6]; // vertex D for Facei
349
350 tScale = new Vector3(lscale.X, -lscale.Y, lscale.Z);
351 scale = ((tScale * rot));
352 vertexes[0] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
353 // vertexes[0].x = pos.X + vertexes[0].x;
354 //vertexes[0].y = pos.Y + vertexes[0].y;
355 //vertexes[0].z = pos.Z + vertexes[0].z;
356
357 FaceA[0] = vertexes[0];
358 FaceB[3] = vertexes[0];
359 FaceA[4] = vertexes[0];
360
361 tScale = lscale;
362 scale = ((tScale * rot));
363 vertexes[1] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
364
365 // vertexes[1].x = pos.X + vertexes[1].x;
366 // vertexes[1].y = pos.Y + vertexes[1].y;
367 //vertexes[1].z = pos.Z + vertexes[1].z;
368
369 FaceB[0] = vertexes[1];
370 FaceA[1] = vertexes[1];
371 FaceC[4] = vertexes[1];
372
373 tScale = new Vector3(lscale.X, -lscale.Y, -lscale.Z);
374 scale = ((tScale * rot));
375
376 vertexes[2] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
377
378 //vertexes[2].x = pos.X + vertexes[2].x;
379 //vertexes[2].y = pos.Y + vertexes[2].y;
380 //vertexes[2].z = pos.Z + vertexes[2].z;
381
382 FaceC[0] = vertexes[2];
383 FaceD[3] = vertexes[2];
384 FaceC[5] = vertexes[2];
385
386 tScale = new Vector3(lscale.X, lscale.Y, -lscale.Z);
387 scale = ((tScale * rot));
388 vertexes[3] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
389
390 //vertexes[3].x = pos.X + vertexes[3].x;
391 // vertexes[3].y = pos.Y + vertexes[3].y;
392 // vertexes[3].z = pos.Z + vertexes[3].z;
393
394 FaceD[0] = vertexes[3];
395 FaceC[1] = vertexes[3];
396 FaceA[5] = vertexes[3];
397
398 tScale = new Vector3(-lscale.X, lscale.Y, lscale.Z);
399 scale = ((tScale * rot));
400 vertexes[4] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
401
402 // vertexes[4].x = pos.X + vertexes[4].x;
403 // vertexes[4].y = pos.Y + vertexes[4].y;
404 // vertexes[4].z = pos.Z + vertexes[4].z;
405
406 FaceB[1] = vertexes[4];
407 FaceA[2] = vertexes[4];
408 FaceD[4] = vertexes[4];
409
410 tScale = new Vector3(-lscale.X, lscale.Y, -lscale.Z);
411 scale = ((tScale * rot));
412 vertexes[5] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
413
414 // vertexes[5].x = pos.X + vertexes[5].x;
415 // vertexes[5].y = pos.Y + vertexes[5].y;
416 // vertexes[5].z = pos.Z + vertexes[5].z;
417
418 FaceD[1] = vertexes[5];
419 FaceC[2] = vertexes[5];
420 FaceB[5] = vertexes[5];
421
422 tScale = new Vector3(-lscale.X, -lscale.Y, lscale.Z);
423 scale = ((tScale * rot));
424 vertexes[6] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
425
426 // vertexes[6].x = pos.X + vertexes[6].x;
427 // vertexes[6].y = pos.Y + vertexes[6].y;
428 // vertexes[6].z = pos.Z + vertexes[6].z;
429
430 FaceB[2] = vertexes[6];
431 FaceA[3] = vertexes[6];
432 FaceB[4] = vertexes[6];
433
434 tScale = new Vector3(-lscale.X, -lscale.Y, -lscale.Z);
435 scale = ((tScale * rot));
436 vertexes[7] = (new Vector3((pos.X + scale.X), (pos.Y + scale.Y), (pos.Z + scale.Z)));
437
438 // vertexes[7].x = pos.X + vertexes[7].x;
439 // vertexes[7].y = pos.Y + vertexes[7].y;
440 // vertexes[7].z = pos.Z + vertexes[7].z;
441
442 FaceD[2] = vertexes[7];
443 FaceC[3] = vertexes[7];
444 FaceD[5] = vertexes[7];
445#endregion
446
447 //int wy = 0;
448
449 //bool breakYN = false; // If we run into an error drawing, break out of the
450 // loop so we don't lag to death on error handling
451 DrawStruct ds = new DrawStruct();
452 ds.brush = new SolidBrush(mapdotspot);
453 //ds.rect = new Rectangle(mapdrawstartX, (255 - mapdrawstartY), mapdrawendX - mapdrawstartX, mapdrawendY - mapdrawstartY);
454
455 ds.trns = new face[FaceA.Length];
456
457 for (int i = 0; i < FaceA.Length; i++)
458 {
459 Point[] working = new Point[5];
460 working[0] = project(FaceA[i], axPos);
461 working[1] = project(FaceB[i], axPos);
462 working[2] = project(FaceD[i], axPos);
463 working[3] = project(FaceC[i], axPos);
464 working[4] = project(FaceA[i], axPos);
465
466 face workingface = new face();
467 workingface.pts = working;
468
469 ds.trns[i] = workingface;
470 }
471
472 z_sort.Add(part.LocalId, ds);
473 z_localIDs.Add(part.LocalId);
474 z_sortheights.Add(pos.Z);
475
476 //for (int wx = mapdrawstartX; wx < mapdrawendX; wx++)
477 //{
478 //for (wy = mapdrawstartY; wy < mapdrawendY; wy++)
479 //{
480 //m_log.InfoFormat("[MAPDEBUG]: {0},{1}({2})", wx, (255 - wy),wy);
481 //try
482 //{
483 // Remember, flip the y!
484 // mapbmp.SetPixel(wx, (255 - wy), mapdotspot);
485 //}
486 //catch (ArgumentException)
487 //{
488 // breakYN = true;
489 //}
490
491 //if (breakYN)
492 // break;
493 //}
494
495 //if (breakYN)
496 // break;
497 //}
498 } // Object is within 256m Z of terrain
499 } // object is at least a meter wide
500 } // loop over group children
501 } // entitybase is sceneobject group
502 } // foreach loop over entities
503
504 float[] sortedZHeights = z_sortheights.ToArray();
505 uint[] sortedlocalIds = z_localIDs.ToArray();
506
507 // Sort prim by Z position
508 Array.Sort(sortedZHeights, sortedlocalIds);
509
510 Graphics g = Graphics.FromImage(mapbmp);
511
512 for (int s = 0; s < sortedZHeights.Length; s++)
513 {
514 if (z_sort.ContainsKey(sortedlocalIds[s]))
515 {
516 DrawStruct rectDrawStruct = z_sort[sortedlocalIds[s]];
517 for (int r = 0; r < rectDrawStruct.trns.Length; r++ )
518 {
519 g.FillPolygon(rectDrawStruct.brush,rectDrawStruct.trns[r].pts);
520 }
521 //g.FillRectangle(rectDrawStruct.brush , rectDrawStruct.rect);
522 }
523 }
524
525 g.Dispose();
526 } // lock entities objs
527
528 m_log.Info("[MAPTILE]: Generating Maptile Step 2: Done in " + (System.Environment.TickCount - tc) + " ms");
529 return mapbmp;
530 }
531
532 private Point project(Vector3 point3d, Vector3 originpos)
533 {
534 Point returnpt = new Point();
535 //originpos = point3d;
536 //int d = (int)(256f / 1.5f);
537
538 //Vector3 topos = new Vector3(0, 0, 0);
539 // float z = -point3d.z - topos.z;
540
541 returnpt.X = (int)point3d.X;//(int)((topos.x - point3d.x) / z * d);
542 returnpt.Y = (int)(255 - point3d.Y);//(int)(255 - (((topos.y - point3d.y) / z * d)));
543
544 return returnpt;
545 }
546
547// TODO: unused:
548// #region Deprecated Maptile Generation. Adam may update this
549// private Bitmap TerrainToBitmap(string gradientmap)
550// {
551// Bitmap gradientmapLd = new Bitmap(gradientmap);
552//
553// int pallete = gradientmapLd.Height;
554//
555// Bitmap bmp = new Bitmap(m_scene.Heightmap.Width, m_scene.Heightmap.Height);
556// Color[] colours = new Color[pallete];
557//
558// for (int i = 0; i < pallete; i++)
559// {
560// colours[i] = gradientmapLd.GetPixel(0, i);
561// }
562//
563// lock (m_scene.Heightmap)
564// {
565// ITerrainChannel copy = m_scene.Heightmap;
566// for (int y = 0; y < copy.Height; y++)
567// {
568// for (int x = 0; x < copy.Width; x++)
569// {
570// // 512 is the largest possible height before colours clamp
571// int colorindex = (int) (Math.Max(Math.Min(1.0, copy[x, y] / 512.0), 0.0) * (pallete - 1));
572//
573// // Handle error conditions
574// if (colorindex > pallete - 1 || colorindex < 0)
575// bmp.SetPixel(x, copy.Height - y - 1, Color.Red);
576// else
577// bmp.SetPixel(x, copy.Height - y - 1, colours[colorindex]);
578// }
579// }
580// ShadeBuildings(bmp);
581// return bmp;
582// }
583// }
584// #endregion
585 }
586}