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