corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
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 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27 using System;
28 using System.Collections.Generic;
29 using OpenMetaverse.Rendering;
30 using OpenMetaverse.StructuredData;
31  
32 namespace OpenMetaverse.ImportExport
33 {
34  
35 public class ModelMaterial
36 {
37 public string ID;
38 public Color4 DiffuseColor = Color4.White;
39 public string Texture;
40 public byte[] TextureData;
41 }
42  
43 public class ModelFace
44 {
45 public List<Vertex> Vertices = new List<Vertex>();
46 public List<uint> Indices = new List<uint>();
47 public string MaterialID = string.Empty;
48 public ModelMaterial Material = new ModelMaterial();
49  
50 Dictionary<Vertex, int> LookUp = new Dictionary<Vertex, int>();
51  
52 public void AddVertex(Vertex v)
53 {
54 int index;
55  
56 if (LookUp.ContainsKey(v))
57 {
58 index = LookUp[v];
59 }
60 else
61 {
62 index = Vertices.Count;
63 Vertices.Add(v);
64 LookUp[v] = index;
65 }
66  
67 Indices.Add((uint)index);
68 }
69  
70 }
71  
72 public class ModelPrim
73 {
74 public List<Vector3> Positions;
75 public Vector3 BoundMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
76 public Vector3 BoundMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);
77 public Vector3 Position;
78 public Vector3 Scale;
79 public Quaternion Rotation = Quaternion.Identity;
80 public List<ModelFace> Faces = new List<ModelFace>();
81 public string ID;
82 public byte[] Asset;
83  
84 public void CreateAsset(UUID creator)
85 {
86 OSDMap header = new OSDMap();
87 header["version"] = 1;
88 header["creator"] = creator;
89 header["date"] = DateTime.Now;
90  
91 OSDArray faces = new OSDArray();
92 foreach (var face in Faces)
93 {
94 OSDMap faceMap = new OSDMap();
95  
96 // Find UV min/max
97 Vector2 uvMin = new Vector2(float.MaxValue, float.MaxValue);
98 Vector2 uvMax = new Vector2(float.MinValue, float.MinValue);
99 foreach (var v in face.Vertices)
100 {
101 if (v.TexCoord.X < uvMin.X) uvMin.X = v.TexCoord.X;
102 if (v.TexCoord.Y < uvMin.Y) uvMin.Y = v.TexCoord.Y;
103  
104 if (v.TexCoord.X > uvMax.X) uvMax.X = v.TexCoord.X;
105 if (v.TexCoord.Y > uvMax.Y) uvMax.Y = v.TexCoord.Y;
106 }
107 OSDMap uvDomain = new OSDMap();
108 uvDomain["Min"] = uvMin;
109 uvDomain["Max"] = uvMax;
110 faceMap["TexCoord0Domain"] = uvDomain;
111  
112  
113 OSDMap positionDomain = new OSDMap();
114 positionDomain["Min"] = new Vector3(-0.5f, -0.5f, -0.5f);
115 positionDomain["Max"] = new Vector3(0.5f, 0.5f, 0.5f);
116 faceMap["PositionDomain"] = positionDomain;
117  
118 List<byte> posBytes = new List<byte>(face.Vertices.Count * sizeof(UInt16) * 3);
119 List<byte> norBytes = new List<byte>(face.Vertices.Count * sizeof(UInt16) * 3);
120 List<byte> uvBytes = new List<byte>(face.Vertices.Count * sizeof(UInt16) * 2);
121  
122 foreach (var v in face.Vertices)
123 {
124 posBytes.AddRange(Utils.UInt16ToBytes(Utils.FloatToUInt16(v.Position.X, -0.5f, 0.5f)));
125 posBytes.AddRange(Utils.UInt16ToBytes(Utils.FloatToUInt16(v.Position.Y, -0.5f, 0.5f)));
126 posBytes.AddRange(Utils.UInt16ToBytes(Utils.FloatToUInt16(v.Position.Z, -0.5f, 0.5f)));
127  
128 norBytes.AddRange(Utils.UInt16ToBytes(Utils.FloatToUInt16(v.Normal.X, -1f, 1f)));
129 norBytes.AddRange(Utils.UInt16ToBytes(Utils.FloatToUInt16(v.Normal.Y, -1f, 1f)));
130 norBytes.AddRange(Utils.UInt16ToBytes(Utils.FloatToUInt16(v.Normal.Z, -1f, 1f)));
131  
132 uvBytes.AddRange(Utils.UInt16ToBytes(Utils.FloatToUInt16(v.TexCoord.X, uvMin.X, uvMax.X)));
133 uvBytes.AddRange(Utils.UInt16ToBytes(Utils.FloatToUInt16(v.TexCoord.Y, uvMin.Y, uvMax.Y)));
134 }
135  
136 faceMap["Position"] = posBytes.ToArray();
137 faceMap["Normal"] = norBytes.ToArray();
138 faceMap["TexCoord0"] = uvBytes.ToArray();
139  
140 List<byte> indexBytes = new List<byte>(face.Indices.Count * sizeof(UInt16));
141 foreach (var t in face.Indices)
142 {
143 indexBytes.AddRange(Utils.UInt16ToBytes((ushort)t));
144 }
145 faceMap["TriangleList"] = indexBytes.ToArray();
146  
147 faces.Add(faceMap);
148 }
149  
150 byte[] physicStubBytes = Helpers.ZCompressOSD(PhysicsStub());
151  
152 byte[] meshBytes = Helpers.ZCompressOSD(faces);
153 int n = 0;
154  
155 OSDMap lodParms = new OSDMap();
156 lodParms["offset"] = n;
157 lodParms["size"] = meshBytes.Length;
158 header["high_lod"] = lodParms;
159 n += meshBytes.Length;
160  
161 lodParms = new OSDMap();
162 lodParms["offset"] = n;
163 lodParms["size"] = physicStubBytes.Length;
164 header["physics_convex"] = lodParms;
165 n += physicStubBytes.Length;
166  
167 byte[] headerBytes = OSDParser.SerializeLLSDBinary(header, false);
168 n += headerBytes.Length;
169  
170 Asset = new byte[n];
171  
172 int offset = 0;
173 Buffer.BlockCopy(headerBytes, 0, Asset, offset, headerBytes.Length);
174 offset += headerBytes.Length;
175  
176 Buffer.BlockCopy(meshBytes, 0, Asset, offset, meshBytes.Length);
177 offset += meshBytes.Length;
178  
179 Buffer.BlockCopy(physicStubBytes, 0, Asset, offset, physicStubBytes.Length);
180 offset += physicStubBytes.Length;
181  
182 }
183  
184 public static OSD PhysicsStub()
185 {
186 OSDMap ret = new OSDMap();
187 ret["Max"] = new Vector3(0.5f, 0.5f, 0.5f);
188 ret["Min"] = new Vector3(-0.5f, -0.5f, -0.5f);
189 ret["BoundingVerts"] = new byte[] { 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 255, 255, 255, 127, 0, 0, 255, 255, 255, 127, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 127, 255, 255, 255, 255, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255 };
190 return ret;
191 }
192  
193 }
194 }