clockwerk-opensim-stable – Blame information for rev 1

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