diff options
Diffstat (limited to 'OpenSim/Region/PhysicsModules/Meshing/SculptMap.cs')
-rw-r--r-- | OpenSim/Region/PhysicsModules/Meshing/SculptMap.cs | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/OpenSim/Region/PhysicsModules/Meshing/SculptMap.cs b/OpenSim/Region/PhysicsModules/Meshing/SculptMap.cs new file mode 100644 index 0000000..740424e --- /dev/null +++ b/OpenSim/Region/PhysicsModules/Meshing/SculptMap.cs | |||
@@ -0,0 +1,183 @@ | |||
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 | // to build without references to System.Drawing, comment this out | ||
29 | #define SYSTEM_DRAWING | ||
30 | |||
31 | using System; | ||
32 | using System.Collections.Generic; | ||
33 | using System.Text; | ||
34 | |||
35 | #if SYSTEM_DRAWING | ||
36 | using System.Drawing; | ||
37 | using System.Drawing.Imaging; | ||
38 | |||
39 | namespace PrimMesher | ||
40 | { | ||
41 | public class SculptMap | ||
42 | { | ||
43 | public int width; | ||
44 | public int height; | ||
45 | public byte[] redBytes; | ||
46 | public byte[] greenBytes; | ||
47 | public byte[] blueBytes; | ||
48 | |||
49 | public SculptMap() | ||
50 | { | ||
51 | } | ||
52 | |||
53 | public SculptMap(Bitmap bm, int lod) | ||
54 | { | ||
55 | int bmW = bm.Width; | ||
56 | int bmH = bm.Height; | ||
57 | |||
58 | if (bmW == 0 || bmH == 0) | ||
59 | throw new Exception("SculptMap: bitmap has no data"); | ||
60 | |||
61 | int numLodPixels = lod * 2 * lod * 2; // (32 * 2)^2 = 64^2 pixels for default sculpt map image | ||
62 | |||
63 | bool needsScaling = false; | ||
64 | |||
65 | bool smallMap = bmW * bmH <= lod * lod; | ||
66 | |||
67 | width = bmW; | ||
68 | height = bmH; | ||
69 | while (width * height > numLodPixels) | ||
70 | { | ||
71 | width >>= 1; | ||
72 | height >>= 1; | ||
73 | needsScaling = true; | ||
74 | } | ||
75 | |||
76 | |||
77 | |||
78 | try | ||
79 | { | ||
80 | if (needsScaling) | ||
81 | bm = ScaleImage(bm, width, height, | ||
82 | System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor); | ||
83 | } | ||
84 | |||
85 | catch (Exception e) | ||
86 | { | ||
87 | throw new Exception("Exception in ScaleImage(): e: " + e.ToString()); | ||
88 | } | ||
89 | |||
90 | if (width * height > lod * lod) | ||
91 | { | ||
92 | width >>= 1; | ||
93 | height >>= 1; | ||
94 | } | ||
95 | |||
96 | int numBytes = (width + 1) * (height + 1); | ||
97 | redBytes = new byte[numBytes]; | ||
98 | greenBytes = new byte[numBytes]; | ||
99 | blueBytes = new byte[numBytes]; | ||
100 | |||
101 | int byteNdx = 0; | ||
102 | |||
103 | try | ||
104 | { | ||
105 | for (int y = 0; y <= height; y++) | ||
106 | { | ||
107 | for (int x = 0; x <= width; x++) | ||
108 | { | ||
109 | Color c; | ||
110 | |||
111 | if (smallMap) | ||
112 | c = bm.GetPixel(x < width ? x : x - 1, | ||
113 | y < height ? y : y - 1); | ||
114 | else | ||
115 | c = bm.GetPixel(x < width ? x * 2 : x * 2 - 1, | ||
116 | y < height ? y * 2 : y * 2 - 1); | ||
117 | |||
118 | redBytes[byteNdx] = c.R; | ||
119 | greenBytes[byteNdx] = c.G; | ||
120 | blueBytes[byteNdx] = c.B; | ||
121 | |||
122 | ++byteNdx; | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | catch (Exception e) | ||
127 | { | ||
128 | throw new Exception("Caught exception processing byte arrays in SculptMap(): e: " + e.ToString()); | ||
129 | } | ||
130 | |||
131 | width++; | ||
132 | height++; | ||
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 | if (mirror) | ||
153 | row.Add(new Coord(-(redBytes[smNdx] * pixScale - 0.5f), (greenBytes[smNdx] * pixScale - 0.5f), blueBytes[smNdx] * pixScale - 0.5f)); | ||
154 | else | ||
155 | row.Add(new Coord(redBytes[smNdx] * pixScale - 0.5f, greenBytes[smNdx] * pixScale - 0.5f, blueBytes[smNdx] * pixScale - 0.5f)); | ||
156 | |||
157 | ++smNdx; | ||
158 | } | ||
159 | rows.Add(row); | ||
160 | } | ||
161 | return rows; | ||
162 | } | ||
163 | |||
164 | private Bitmap ScaleImage(Bitmap srcImage, int destWidth, int destHeight, | ||
165 | System.Drawing.Drawing2D.InterpolationMode interpMode) | ||
166 | { | ||
167 | Bitmap scaledImage = new Bitmap(srcImage, destWidth, destHeight); | ||
168 | scaledImage.SetResolution(96.0f, 96.0f); | ||
169 | |||
170 | Graphics grPhoto = Graphics.FromImage(scaledImage); | ||
171 | grPhoto.InterpolationMode = interpMode; | ||
172 | |||
173 | grPhoto.DrawImage(srcImage, | ||
174 | new Rectangle(0, 0, destWidth, destHeight), | ||
175 | new Rectangle(0, 0, srcImage.Width, srcImage.Height), | ||
176 | GraphicsUnit.Pixel); | ||
177 | |||
178 | grPhoto.Dispose(); | ||
179 | return scaledImage; | ||
180 | } | ||
181 | } | ||
182 | } | ||
183 | #endif | ||