OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
ShadedMapTileRenderer.cs
Go to the documentation of this file.
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.Drawing;
30 using System.Reflection;
31 using log4net;
32 using Nini.Config;
33 using OpenSim.Framework;
34 using OpenSim.Region.Framework.Interfaces;
35 using OpenSim.Region.Framework.Scenes;
36 
37 namespace OpenSim.Region.CoreModules.World.LegacyMap
38 {
40  {
41  private static readonly Color WATER_COLOR = Color.FromArgb(29, 71, 95);
42 
43  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44  private static readonly string LogHeader = "[SHADED MAPTILE RENDERER]";
45 
46  private Scene m_scene;
47  //private IConfigSource m_config; // not used currently
48 
49  public void Initialise(Scene scene, IConfigSource config)
50  {
51  m_scene = scene;
52  // m_config = config; // not used currently
53  }
54 
55  public void TerrainToBitmap(Bitmap mapbmp)
56  {
57  m_log.DebugFormat("{0} Generating Maptile Step 1: Terrain", LogHeader);
58  int tc = Environment.TickCount;
59 
60  ITerrainChannel hm = m_scene.Heightmap;
61 
62  if (mapbmp.Width != hm.Width || mapbmp.Height != hm.Height)
63  {
64  m_log.ErrorFormat("{0} TerrainToBitmap. Passed bitmap wrong dimensions. passed=<{1},{2}>, size=<{3},{4}>",
65  LogHeader, mapbmp.Width, mapbmp.Height, hm.Width, hm.Height);
66  }
67 
68  bool ShadowDebugContinue = true;
69 
70  bool terraincorruptedwarningsaid = false;
71 
72  float low = 255;
73  float high = 0;
74  for (int x = 0; x < hm.Width; x++)
75  {
76  for (int y = 0; y < hm.Height; y++)
77  {
78  float hmval = (float)hm[x, y];
79  if (hmval < low)
80  low = hmval;
81  if (hmval > high)
82  high = hmval;
83  }
84  }
85 
86  float waterHeight = (float)m_scene.RegionInfo.RegionSettings.WaterHeight;
87 
88  for (int x = 0; x < hm.Width; x++)
89  {
90  for (int y = 0; y < hm.Height; y++)
91  {
92  // Y flip the cordinates for the bitmap: hf origin is lower left, bm origin is upper left
93  int yr = ((int)hm.Height - 1) - y;
94 
95  float heightvalue = (float)hm[x, y];
96 
97  if (heightvalue > waterHeight)
98  {
99  // scale height value
100  // No, that doesn't scale it:
101  // heightvalue = low + mid * (heightvalue - low) / mid; => low + (heightvalue - low) * mid / mid = low + (heightvalue - low) * 1 = low + heightvalue - low = heightvalue
102 
103  if (Single.IsInfinity(heightvalue) || Single.IsNaN(heightvalue))
104  heightvalue = 0;
105  else if (heightvalue > 255f)
106  heightvalue = 255f;
107  else if (heightvalue < 0f)
108  heightvalue = 0f;
109 
110  Color color = Color.FromArgb((int)heightvalue, 100, (int)heightvalue);
111 
112  mapbmp.SetPixel(x, yr, color);
113 
114  try
115  {
116  //X
117  // .
118  //
119  // Shade the terrain for shadows
120  if (x < (hm.Width - 1) && yr < (hm.Height - 1))
121  {
122  float hfvalue = (float)hm[x, y];
123  float hfvaluecompare = 0f;
124 
125  if ((x + 1 < hm.Width) && (y + 1 < hm.Height))
126  {
127  hfvaluecompare = (float)hm[x + 1, y + 1]; // light from north-east => look at land height there
128  }
129  if (Single.IsInfinity(hfvalue) || Single.IsNaN(hfvalue))
130  hfvalue = 0f;
131 
132  if (Single.IsInfinity(hfvaluecompare) || Single.IsNaN(hfvaluecompare))
133  hfvaluecompare = 0f;
134 
135  float hfdiff = hfvalue - hfvaluecompare; // => positive if NE is lower, negative if here is lower
136 
137  int hfdiffi = 0;
138  int hfdiffihighlight = 0;
139  float highlightfactor = 0.18f;
140 
141  try
142  {
143  // hfdiffi = Math.Abs((int)((hfdiff * 4) + (hfdiff * 0.5))) + 1;
144  hfdiffi = Math.Abs((int)(hfdiff * 4.5f)) + 1;
145  if (hfdiff % 1f != 0)
146  {
147  // hfdiffi = hfdiffi + Math.Abs((int)(((hfdiff % 1) * 0.5f) * 10f) - 1);
148  hfdiffi = hfdiffi + Math.Abs((int)((hfdiff % 1f) * 5f) - 1);
149  }
150 
151  hfdiffihighlight = Math.Abs((int)((hfdiff * highlightfactor) * 4.5f)) + 1;
152  if (hfdiff % 1f != 0)
153  {
154  // hfdiffi = hfdiffi + Math.Abs((int)(((hfdiff % 1) * 0.5f) * 10f) - 1);
155  hfdiffihighlight = hfdiffihighlight + Math.Abs((int)(((hfdiff * highlightfactor) % 1f) * 5f) - 1);
156  }
157  }
158  catch (OverflowException)
159  {
160  m_log.Debug("[MAPTILE]: Shadow failed at value: " + hfdiff.ToString());
161  ShadowDebugContinue = false;
162  }
163 
164  if (hfdiff > 0.3f)
165  {
166  // NE is lower than here
167  // We have to desaturate and lighten the land at the same time
168  // we use floats, colors use bytes, so shrink are space down to
169  // 0-255
170 
171  if (ShadowDebugContinue)
172  {
173  int r = color.R;
174  int g = color.G;
175  int b = color.B;
176  color = Color.FromArgb((r + hfdiffihighlight < 255) ? r + hfdiffihighlight : 255,
177  (g + hfdiffihighlight < 255) ? g + hfdiffihighlight : 255,
178  (b + hfdiffihighlight < 255) ? b + hfdiffihighlight : 255);
179  }
180  }
181  else if (hfdiff < -0.3f)
182  {
183  // here is lower than NE:
184  // We have to desaturate and blacken the land at the same time
185  // we use floats, colors use bytes, so shrink are space down to
186  // 0-255
187 
188  if (ShadowDebugContinue)
189  {
190  if ((x - 1 > 0) && (yr + 1 < hm.Height))
191  {
192  color = mapbmp.GetPixel(x - 1, yr + 1);
193  int r = color.R;
194  int g = color.G;
195  int b = color.B;
196  color = Color.FromArgb((r - hfdiffi > 0) ? r - hfdiffi : 0,
197  (g - hfdiffi > 0) ? g - hfdiffi : 0,
198  (b - hfdiffi > 0) ? b - hfdiffi : 0);
199 
200  mapbmp.SetPixel(x-1, yr+1, color);
201  }
202  }
203  }
204  }
205  }
206  catch (ArgumentException)
207  {
208  if (!terraincorruptedwarningsaid)
209  {
210  m_log.WarnFormat("[SHADED MAP TILE RENDERER]: Your terrain is corrupted in region {0}, it might take a few minutes to generate the map image depending on the corruption level", m_scene.RegionInfo.RegionName);
211  terraincorruptedwarningsaid = true;
212  }
213  color = Color.Black;
214  mapbmp.SetPixel(x, yr, color);
215  }
216  }
217  else
218  {
219  // We're under the water level with the terrain, so paint water instead of land
220 
221  // Y flip the cordinates
222  heightvalue = waterHeight - heightvalue;
223  if (Single.IsInfinity(heightvalue) || Single.IsNaN(heightvalue))
224  heightvalue = 0f;
225  else if (heightvalue > 19f)
226  heightvalue = 19f;
227  else if (heightvalue < 0f)
228  heightvalue = 0f;
229 
230  heightvalue = 100f - (heightvalue * 100f) / 19f;
231 
232  try
233  {
234  mapbmp.SetPixel(x, yr, WATER_COLOR);
235  }
236  catch (ArgumentException)
237  {
238  if (!terraincorruptedwarningsaid)
239  {
240  m_log.WarnFormat("[SHADED MAP TILE RENDERER]: Your terrain is corrupted in region {0}, it might take a few minutes to generate the map image depending on the corruption level", m_scene.RegionInfo.RegionName);
241  terraincorruptedwarningsaid = true;
242  }
243  Color black = Color.Black;
244  mapbmp.SetPixel(x, (hm.Width - y) - 1, black);
245  }
246  }
247  }
248  }
249 
250  m_log.Debug("[SHADED MAP TILE RENDERER]: Generating Maptile Step 1: Done in " + (Environment.TickCount - tc) + " ms");
251  }
252  }
253 }