diff options
author | dahlia | 2010-05-06 21:36:27 -0700 |
---|---|---|
committer | dahlia | 2010-05-06 21:36:27 -0700 |
commit | 5d1e9947ed43490fb458af62bd9e8596a816f7b8 (patch) | |
tree | b89886e0faf5bd2194491669ad8a8b3c1ea340a0 /OpenSim/Region/Physics/Meshing/SculptMap.cs | |
parent | Also remove sale and search flags on god owner change. (diff) | |
download | opensim-SC-5d1e9947ed43490fb458af62bd9e8596a816f7b8.zip opensim-SC-5d1e9947ed43490fb458af62bd9e8596a816f7b8.tar.gz opensim-SC-5d1e9947ed43490fb458af62bd9e8596a816f7b8.tar.bz2 opensim-SC-5d1e9947ed43490fb458af62bd9e8596a816f7b8.tar.xz |
Sculpt meshing refactoring - improves mesh accuracy and UV mapping
Sync with PrimMesher r55
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/Physics/Meshing/SculptMap.cs | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/Meshing/SculptMap.cs b/OpenSim/Region/Physics/Meshing/SculptMap.cs new file mode 100644 index 0000000..4d3f82b --- /dev/null +++ b/OpenSim/Region/Physics/Meshing/SculptMap.cs | |||
@@ -0,0 +1,169 @@ | |||
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 | width = bmW; | ||
64 | height = bmH; | ||
65 | while (width * height > numLodPixels) | ||
66 | { | ||
67 | width >>= 1; | ||
68 | height >>= 1; | ||
69 | } | ||
70 | |||
71 | width >>= 1; | ||
72 | height >>= 1; | ||
73 | |||
74 | try | ||
75 | { | ||
76 | if (!(bmW == width * 2 && bmH == height * 2)) | ||
77 | bm = ScaleImage(bm, width * 2, height * 2, | ||
78 | System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor); | ||
79 | } | ||
80 | |||
81 | catch (Exception e) | ||
82 | { | ||
83 | throw new Exception("Exception in ScaleImage(): e: " + e.ToString()); | ||
84 | } | ||
85 | |||
86 | |||
87 | int numBytes = (width + 1) * (height + 1); | ||
88 | redBytes = new byte[numBytes]; | ||
89 | greenBytes = new byte[numBytes]; | ||
90 | blueBytes = new byte[numBytes]; | ||
91 | |||
92 | int byteNdx = 0; | ||
93 | |||
94 | try | ||
95 | { | ||
96 | for (int y = 0; y <= height; y++) | ||
97 | { | ||
98 | for (int x = 0; x <= width; x++) | ||
99 | { | ||
100 | int bmY = y < height ? y * 2 : y * 2 - 1; | ||
101 | int bmX = x < width ? x * 2 : x * 2 - 1; | ||
102 | Color c = bm.GetPixel(bmX, bmY); | ||
103 | |||
104 | redBytes[byteNdx] = c.R; | ||
105 | greenBytes[byteNdx] = c.G; | ||
106 | blueBytes[byteNdx] = c.B; | ||
107 | |||
108 | ++byteNdx; | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | catch (Exception e) | ||
113 | { | ||
114 | throw new Exception("Caught exception processing byte arrays in SculptMap(): e: " + e.ToString()); | ||
115 | } | ||
116 | |||
117 | width++; | ||
118 | height++; | ||
119 | } | ||
120 | |||
121 | public List<List<Coord>> ToRows(bool mirror) | ||
122 | { | ||
123 | int numRows = height; | ||
124 | int numCols = width; | ||
125 | |||
126 | List<List<Coord>> rows = new List<List<Coord>>(numRows); | ||
127 | |||
128 | float pixScale = 1.0f / 255; | ||
129 | |||
130 | int rowNdx, colNdx; | ||
131 | int smNdx = 0; | ||
132 | |||
133 | for (rowNdx = 0; rowNdx < numRows; rowNdx++) | ||
134 | { | ||
135 | List<Coord> row = new List<Coord>(numCols); | ||
136 | for (colNdx = 0; colNdx < numCols; colNdx++) | ||
137 | { | ||
138 | if (mirror) | ||
139 | row.Add(new Coord(-(redBytes[smNdx] * pixScale - 0.5f), (greenBytes[smNdx] * pixScale - 0.5f), blueBytes[smNdx] * pixScale - 0.5f)); | ||
140 | else | ||
141 | row.Add(new Coord(redBytes[smNdx] * pixScale - 0.5f, greenBytes[smNdx] * pixScale - 0.5f, blueBytes[smNdx] * pixScale - 0.5f)); | ||
142 | |||
143 | ++smNdx; | ||
144 | } | ||
145 | rows.Add(row); | ||
146 | } | ||
147 | return rows; | ||
148 | } | ||
149 | |||
150 | private Bitmap ScaleImage(Bitmap srcImage, int destWidth, int destHeight, | ||
151 | System.Drawing.Drawing2D.InterpolationMode interpMode) | ||
152 | { | ||
153 | Bitmap scaledImage = new Bitmap(srcImage, destWidth, destHeight); | ||
154 | scaledImage.SetResolution(96.0f, 96.0f); | ||
155 | |||
156 | Graphics grPhoto = Graphics.FromImage(scaledImage); | ||
157 | grPhoto.InterpolationMode = interpMode; | ||
158 | |||
159 | grPhoto.DrawImage(srcImage, | ||
160 | new Rectangle(0, 0, destWidth, destHeight), | ||
161 | new Rectangle(0, 0, srcImage.Width, srcImage.Height), | ||
162 | GraphicsUnit.Pixel); | ||
163 | |||
164 | grPhoto.Dispose(); | ||
165 | return scaledImage; | ||
166 | } | ||
167 | } | ||
168 | } | ||
169 | #endif | ||