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