opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 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.IO; |
||
30 | using OpenSim.Region.Framework.Interfaces; |
||
31 | using OpenSim.Region.Framework.Scenes; |
||
32 | |||
33 | namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders |
||
34 | { |
||
35 | public class LLRAW : ITerrainLoader |
||
36 | { |
||
37 | public struct HeightmapLookupValue : IComparable<HeightmapLookupValue> |
||
38 | { |
||
39 | public ushort Index; |
||
40 | public float Value; |
||
41 | |||
42 | public HeightmapLookupValue(ushort index, float value) |
||
43 | { |
||
44 | Index = index; |
||
45 | Value = value; |
||
46 | } |
||
47 | |||
48 | public int CompareTo(HeightmapLookupValue val) |
||
49 | { |
||
50 | return Value.CompareTo(val.Value); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /// <summary>Lookup table to speed up terrain exports</summary> |
||
55 | HeightmapLookupValue[] LookupHeightTable; |
||
56 | |||
57 | public LLRAW() |
||
58 | { |
||
59 | LookupHeightTable = new HeightmapLookupValue[256 * 256]; |
||
60 | |||
61 | for (int i = 0; i < 256; i++) |
||
62 | { |
||
63 | for (int j = 0; j < 256; j++) |
||
64 | { |
||
65 | LookupHeightTable[i + (j * 256)] = new HeightmapLookupValue((ushort)(i + (j * 256)), (float)((double)i * ((double)j / 128.0d))); |
||
66 | } |
||
67 | } |
||
68 | Array.Sort<HeightmapLookupValue>(LookupHeightTable); |
||
69 | } |
||
70 | |||
71 | #region ITerrainLoader Members |
||
72 | |||
73 | public ITerrainChannel LoadFile(string filename) |
||
74 | { |
||
75 | FileInfo file = new FileInfo(filename); |
||
76 | FileStream s = file.Open(FileMode.Open, FileAccess.Read); |
||
77 | ITerrainChannel retval = LoadStream(s); |
||
78 | |||
79 | s.Close(); |
||
80 | |||
81 | return retval; |
||
82 | } |
||
83 | |||
84 | public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight) |
||
85 | { |
||
86 | TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight); |
||
87 | |||
88 | FileInfo file = new FileInfo(filename); |
||
89 | FileStream s = file.Open(FileMode.Open, FileAccess.Read); |
||
90 | BinaryReader bs = new BinaryReader(s); |
||
91 | |||
92 | int currFileYOffset = fileHeight - 1; |
||
93 | |||
94 | // if our region isn't on the first Y section of the areas to be landscaped, then |
||
95 | // advance to our section of the file |
||
96 | while (currFileYOffset > offsetY) |
||
97 | { |
||
98 | // read a whole strip of regions |
||
99 | int heightsToRead = sectionHeight * (fileWidth * sectionWidth); |
||
100 | bs.ReadBytes(heightsToRead * 13); // because there are 13 fun channels |
||
101 | currFileYOffset--; |
||
102 | } |
||
103 | |||
104 | // got to the Y start offset within the file of our region |
||
105 | // so read the file bits associated with our region |
||
106 | int y; |
||
107 | // for each Y within our Y offset |
||
108 | for (y = sectionHeight - 1; y >= 0; y--) |
||
109 | { |
||
110 | int currFileXOffset = 0; |
||
111 | |||
112 | // if our region isn't the first X section of the areas to be landscaped, then |
||
113 | // advance the stream to the X start pos of our section in the file |
||
114 | // i.e. eat X upto where we start |
||
115 | while (currFileXOffset < offsetX) |
||
116 | { |
||
117 | bs.ReadBytes(sectionWidth * 13); |
||
118 | currFileXOffset++; |
||
119 | } |
||
120 | |||
121 | // got to our X offset, so write our regions X line |
||
122 | int x; |
||
123 | for (x = 0; x < sectionWidth; x++) |
||
124 | { |
||
125 | // Read a strip and continue |
||
126 | retval[x, y] = bs.ReadByte() * (bs.ReadByte() / 128.0); |
||
127 | bs.ReadBytes(11); |
||
128 | } |
||
129 | // record that we wrote it |
||
130 | currFileXOffset++; |
||
131 | |||
132 | // if our region isn't the last X section of the areas to be landscaped, then |
||
133 | // advance the stream to the end of this Y column |
||
134 | while (currFileXOffset < fileWidth) |
||
135 | { |
||
136 | // eat the next regions x line |
||
137 | bs.ReadBytes(sectionWidth * 13); //The 13 channels again |
||
138 | currFileXOffset++; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | bs.Close(); |
||
143 | s.Close(); |
||
144 | |||
145 | return retval; |
||
146 | } |
||
147 | |||
148 | public ITerrainChannel LoadStream(Stream s) |
||
149 | { |
||
150 | TerrainChannel retval = new TerrainChannel(); |
||
151 | |||
152 | BinaryReader bs = new BinaryReader(s); |
||
153 | int y; |
||
154 | for (y = 0; y < retval.Height; y++) |
||
155 | { |
||
156 | int x; |
||
157 | for (x = 0; x < retval.Width; x++) |
||
158 | { |
||
159 | retval[x, (retval.Height - 1) - y] = bs.ReadByte() * (bs.ReadByte() / 128.0); |
||
160 | bs.ReadBytes(11); // Advance the stream to next bytes. |
||
161 | } |
||
162 | } |
||
163 | |||
164 | bs.Close(); |
||
165 | |||
166 | return retval; |
||
167 | } |
||
168 | |||
169 | public void SaveFile(string filename, ITerrainChannel map) |
||
170 | { |
||
171 | FileInfo file = new FileInfo(filename); |
||
172 | FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write); |
||
173 | SaveStream(s, map); |
||
174 | |||
175 | s.Close(); |
||
176 | } |
||
177 | |||
178 | public void SaveStream(Stream s, ITerrainChannel map) |
||
179 | { |
||
180 | BinaryWriter binStream = new BinaryWriter(s); |
||
181 | |||
182 | // Output the calculated raw |
||
183 | for (int y = 0; y < map.Height; y++) |
||
184 | { |
||
185 | for (int x = 0; x < map.Width; x++) |
||
186 | { |
||
187 | double t = map[x, (map.Height - 1) - y]; |
||
188 | //if height is less than 0, set it to 0 as |
||
189 | //can't save -ve values in a LLRAW file |
||
190 | if (t < 0d) |
||
191 | { |
||
192 | t = 0d; |
||
193 | } |
||
194 | |||
195 | int index = 0; |
||
196 | |||
197 | // The lookup table is pre-sorted, so we either find an exact match or |
||
198 | // the next closest (smaller) match with a binary search |
||
199 | index = Array.BinarySearch<HeightmapLookupValue>(LookupHeightTable, new HeightmapLookupValue(0, (float)t)); |
||
200 | if (index < 0) |
||
201 | index = ~index - 1; |
||
202 | |||
203 | index = LookupHeightTable[index].Index; |
||
204 | |||
205 | byte red = (byte) (index & 0xFF); |
||
206 | byte green = (byte) ((index >> 8) & 0xFF); |
||
207 | const byte blue = 20; |
||
208 | const byte alpha1 = 0; |
||
209 | const byte alpha2 = 0; |
||
210 | const byte alpha3 = 0; |
||
211 | const byte alpha4 = 0; |
||
212 | const byte alpha5 = 255; |
||
213 | const byte alpha6 = 255; |
||
214 | const byte alpha7 = 255; |
||
215 | const byte alpha8 = 255; |
||
216 | byte alpha9 = red; |
||
217 | byte alpha10 = green; |
||
218 | |||
219 | binStream.Write(red); |
||
220 | binStream.Write(green); |
||
221 | binStream.Write(blue); |
||
222 | binStream.Write(alpha1); |
||
223 | binStream.Write(alpha2); |
||
224 | binStream.Write(alpha3); |
||
225 | binStream.Write(alpha4); |
||
226 | binStream.Write(alpha5); |
||
227 | binStream.Write(alpha6); |
||
228 | binStream.Write(alpha7); |
||
229 | binStream.Write(alpha8); |
||
230 | binStream.Write(alpha9); |
||
231 | binStream.Write(alpha10); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | binStream.Close(); |
||
236 | } |
||
237 | |||
238 | public string FileExtension |
||
239 | { |
||
240 | get { return ".raw"; } |
||
241 | } |
||
242 | |||
243 | public virtual void SaveFile(ITerrainChannel m_channel, string filename, |
||
244 | int offsetX, int offsetY, |
||
245 | int fileWidth, int fileHeight, |
||
246 | int regionSizeX, int regionSizeY) |
||
247 | { |
||
248 | throw new System.Exception("Not Implemented"); |
||
249 | } |
||
250 | |||
251 | #endregion |
||
252 | |||
253 | public override string ToString() |
||
254 | { |
||
255 | return "LL/SL RAW"; |
||
256 | } |
||
257 | |||
258 | //Returns true if this extension is supported for terrain save-tile |
||
259 | public bool SupportsTileSave() |
||
260 | { |
||
261 | return false; |
||
262 | } |
||
263 | |||
264 | } |
||
265 | } |