diff options
author | onefang | 2019-09-11 16:36:50 +1000 |
---|---|---|
committer | onefang | 2019-09-11 16:36:50 +1000 |
commit | 50cd1ffd32f69228e566f2b0b89f86ea0d9fe489 (patch) | |
tree | 52f2ab0c04f1a5d7d6ac5dc872981b4b156447e7 /OpenSim/Region/PhysicsModules/ubOdeMeshing/SculptMap.cs | |
parent | Renamed branch to SledjChisl. (diff) | |
parent | Bump to release flavour, build 0. (diff) | |
download | opensim-SC-50cd1ffd32f69228e566f2b0b89f86ea0d9fe489.zip opensim-SC-50cd1ffd32f69228e566f2b0b89f86ea0d9fe489.tar.gz opensim-SC-50cd1ffd32f69228e566f2b0b89f86ea0d9fe489.tar.bz2 opensim-SC-50cd1ffd32f69228e566f2b0b89f86ea0d9fe489.tar.xz |
Merge branch 'SledjChisl'
Diffstat (limited to 'OpenSim/Region/PhysicsModules/ubOdeMeshing/SculptMap.cs')
-rw-r--r-- | OpenSim/Region/PhysicsModules/ubOdeMeshing/SculptMap.cs | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/OpenSim/Region/PhysicsModules/ubOdeMeshing/SculptMap.cs b/OpenSim/Region/PhysicsModules/ubOdeMeshing/SculptMap.cs new file mode 100644 index 0000000..ebe2523 --- /dev/null +++ b/OpenSim/Region/PhysicsModules/ubOdeMeshing/SculptMap.cs | |||
@@ -0,0 +1,238 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors | ||
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.Text; | ||
31 | |||
32 | using System.Drawing; | ||
33 | using System.Drawing.Imaging; | ||
34 | |||
35 | namespace PrimMesher | ||
36 | { | ||
37 | public class SculptMap | ||
38 | { | ||
39 | public int width; | ||
40 | public int height; | ||
41 | public byte[] redBytes; | ||
42 | public byte[] greenBytes; | ||
43 | public byte[] blueBytes; | ||
44 | |||
45 | public SculptMap() | ||
46 | { | ||
47 | } | ||
48 | |||
49 | public SculptMap(Bitmap bm, int lod) | ||
50 | { | ||
51 | int bmW = bm.Width; | ||
52 | int bmH = bm.Height; | ||
53 | |||
54 | if (bmW == 0 || bmH == 0) | ||
55 | throw new Exception("SculptMap: bitmap has no data"); | ||
56 | |||
57 | int numLodPixels = lod * lod; // (32 * 2)^2 = 64^2 pixels for default sculpt map image | ||
58 | |||
59 | bool needsScaling = false; | ||
60 | bool smallMap = false; | ||
61 | |||
62 | width = bmW; | ||
63 | height = bmH; | ||
64 | |||
65 | while (width * height > numLodPixels * 4) | ||
66 | { | ||
67 | width >>= 1; | ||
68 | height >>= 1; | ||
69 | needsScaling = true; | ||
70 | } | ||
71 | |||
72 | if (needsScaling) | ||
73 | bm = ScaleImage(bm, width, height); | ||
74 | |||
75 | if (width * height > numLodPixels) | ||
76 | { | ||
77 | smallMap = false; | ||
78 | width >>= 1; | ||
79 | height >>= 1; | ||
80 | } | ||
81 | else | ||
82 | smallMap = true; | ||
83 | |||
84 | int numBytes = (width + 1) * (height + 1); | ||
85 | redBytes = new byte[numBytes]; | ||
86 | greenBytes = new byte[numBytes]; | ||
87 | blueBytes = new byte[numBytes]; | ||
88 | |||
89 | int byteNdx = 0; | ||
90 | Color c; | ||
91 | |||
92 | try | ||
93 | { | ||
94 | for (int y = 0; y <= height; y++) | ||
95 | { | ||
96 | for (int x = 0; x < width; x++) | ||
97 | { | ||
98 | if (smallMap) | ||
99 | c = bm.GetPixel(x, y < height ? y : y - 1); | ||
100 | else | ||
101 | c = bm.GetPixel(x * 2, y < height ? y * 2 : y * 2 - 1); | ||
102 | |||
103 | redBytes[byteNdx] = c.R; | ||
104 | greenBytes[byteNdx] = c.G; | ||
105 | blueBytes[byteNdx] = c.B; | ||
106 | |||
107 | ++byteNdx; | ||
108 | } | ||
109 | |||
110 | if (smallMap) | ||
111 | c = bm.GetPixel(width - 1, y < height ? y : y - 1); | ||
112 | else | ||
113 | c = bm.GetPixel(width * 2 - 1, y < height ? y * 2 : y * 2 - 1); | ||
114 | |||
115 | redBytes[byteNdx] = c.R; | ||
116 | greenBytes[byteNdx] = c.G; | ||
117 | blueBytes[byteNdx] = c.B; | ||
118 | |||
119 | ++byteNdx; | ||
120 | } | ||
121 | } | ||
122 | catch (Exception e) | ||
123 | { | ||
124 | if (needsScaling) | ||
125 | bm.Dispose(); | ||
126 | throw new Exception("Caught exception processing byte arrays in SculptMap(): e: " + e.ToString()); | ||
127 | } | ||
128 | |||
129 | width++; | ||
130 | height++; | ||
131 | if(needsScaling) | ||
132 | bm.Dispose(); | ||
133 | } | ||
134 | |||
135 | public List<List<Coord>> ToRows(bool mirror) | ||
136 | { | ||
137 | int numRows = height; | ||
138 | int numCols = width; | ||
139 | |||
140 | List<List<Coord>> rows = new List<List<Coord>>(numRows); | ||
141 | |||
142 | float pixScale = 1.0f / 255; | ||
143 | |||
144 | int rowNdx, colNdx; | ||
145 | int smNdx = 0; | ||
146 | |||
147 | for (rowNdx = 0; rowNdx < numRows; rowNdx++) | ||
148 | { | ||
149 | List<Coord> row = new List<Coord>(numCols); | ||
150 | for (colNdx = 0; colNdx < numCols; colNdx++) | ||
151 | { | ||
152 | |||
153 | if (mirror) | ||
154 | row.Add(new Coord(-((float)redBytes[smNdx] * pixScale - 0.5f), ((float)greenBytes[smNdx] * pixScale - 0.5f), (float)blueBytes[smNdx] * pixScale - 0.5f)); | ||
155 | else | ||
156 | row.Add(new Coord((float)redBytes[smNdx] * pixScale - 0.5f, (float)greenBytes[smNdx] * pixScale - 0.5f, (float)blueBytes[smNdx] * pixScale - 0.5f)); | ||
157 | |||
158 | ++smNdx; | ||
159 | } | ||
160 | rows.Add(row); | ||
161 | } | ||
162 | return rows; | ||
163 | } | ||
164 | |||
165 | private Bitmap ScaleImage(Bitmap srcImage, int destWidth, int destHeight) | ||
166 | { | ||
167 | Bitmap scaledImage = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb); | ||
168 | |||
169 | Color c; | ||
170 | |||
171 | // will let last step to be eventually diferent, as seems to be in sl | ||
172 | |||
173 | float xscale = (float)srcImage.Width / (float)destWidth; | ||
174 | float yscale = (float)srcImage.Height / (float)destHeight; | ||
175 | |||
176 | int lastsx = srcImage.Width - 1; | ||
177 | int lastsy = srcImage.Height - 1; | ||
178 | int lastdx = destWidth - 1; | ||
179 | int lastdy = destHeight - 1; | ||
180 | |||
181 | float sy = 0.5f; | ||
182 | float sx; | ||
183 | |||
184 | for (int y = 0; y < lastdy; y++) | ||
185 | { | ||
186 | sx = 0.5f; | ||
187 | for (int x = 0; x < lastdx; x++) | ||
188 | { | ||
189 | try | ||
190 | { | ||
191 | c = srcImage.GetPixel((int)(sx), (int)(sy)); | ||
192 | scaledImage.SetPixel(x, y, Color.FromArgb(c.R, c.G, c.B)); | ||
193 | } | ||
194 | catch (IndexOutOfRangeException) | ||
195 | { | ||
196 | } | ||
197 | sx += xscale; | ||
198 | } | ||
199 | try | ||
200 | { | ||
201 | c = srcImage.GetPixel(lastsx, (int)(sy)); | ||
202 | scaledImage.SetPixel(lastdx, y, Color.FromArgb(c.R, c.G, c.B)); | ||
203 | } | ||
204 | catch (IndexOutOfRangeException) | ||
205 | { | ||
206 | } | ||
207 | |||
208 | sy += yscale; | ||
209 | } | ||
210 | |||
211 | sx = 0.5f; | ||
212 | for (int x = 0; x < lastdx; x++) | ||
213 | { | ||
214 | try | ||
215 | { | ||
216 | c = srcImage.GetPixel((int)(sx), lastsy); | ||
217 | scaledImage.SetPixel(x, lastdy, Color.FromArgb(c.R, c.G, c.B)); | ||
218 | } | ||
219 | catch (IndexOutOfRangeException) | ||
220 | { | ||
221 | } | ||
222 | |||
223 | sx += xscale; | ||
224 | } | ||
225 | try | ||
226 | { | ||
227 | c = srcImage.GetPixel(lastsx, lastsy); | ||
228 | scaledImage.SetPixel(lastdx, lastdy, Color.FromArgb(c.R, c.G, c.B)); | ||
229 | } | ||
230 | catch (IndexOutOfRangeException) | ||
231 | { | ||
232 | } | ||
233 | |||
234 | srcImage.Dispose(); | ||
235 | return scaledImage; | ||
236 | } | ||
237 | } | ||
238 | } \ No newline at end of file | ||