corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.Text; |
||
4 | using System.IO; |
||
5 | using OpenMetaverse; |
||
6 | using OpenMetaverse.Rendering; |
||
7 | |||
8 | namespace PrimWorkshop |
||
9 | { |
||
10 | public static class MeshToOBJ |
||
11 | { |
||
12 | public static bool MeshesToOBJ(List<FacetedMesh> meshes, string filename) |
||
13 | { |
||
14 | StringBuilder obj = new StringBuilder(); |
||
15 | StringBuilder mtl = new StringBuilder(); |
||
16 | |||
17 | FileInfo objFileInfo = new FileInfo(filename); |
||
18 | |||
19 | string mtlFilename = objFileInfo.FullName.Substring(objFileInfo.DirectoryName.Length + 1, |
||
20 | objFileInfo.FullName.Length - (objFileInfo.DirectoryName.Length + 1) - 4) + ".mtl"; |
||
21 | |||
22 | obj.AppendLine("# Created by libOpenMetaverse"); |
||
23 | obj.AppendLine("mtllib ./" + mtlFilename); |
||
24 | obj.AppendLine(); |
||
25 | |||
26 | mtl.AppendLine("# Created by libOpenMetaverse"); |
||
27 | mtl.AppendLine(); |
||
28 | |||
29 | for (int i = 0; i < meshes.Count; i++) |
||
30 | { |
||
31 | FacetedMesh mesh = meshes[i]; |
||
32 | |||
33 | for (int j = 0; j < mesh.Faces.Count; j++) |
||
34 | { |
||
35 | Face face = mesh.Faces[j]; |
||
36 | |||
37 | if (face.Vertices.Count > 2) |
||
38 | { |
||
39 | string mtlName = String.Format("material{0}-{1}", i, face.ID); |
||
40 | Primitive.TextureEntryFace tex = face.TextureFace; |
||
41 | string texName = tex.TextureID.ToString() + ".tga"; |
||
42 | |||
43 | // FIXME: Convert the source to TGA (if needed) and copy to the destination |
||
44 | |||
45 | float shiny = 0.00f; |
||
46 | switch (tex.Shiny) |
||
47 | { |
||
48 | case Shininess.High: |
||
49 | shiny = 1.00f; |
||
50 | break; |
||
51 | case Shininess.Medium: |
||
52 | shiny = 0.66f; |
||
53 | break; |
||
54 | case Shininess.Low: |
||
55 | shiny = 0.33f; |
||
56 | break; |
||
57 | } |
||
58 | |||
59 | mtl.AppendLine("newmtl " + mtlName); |
||
60 | mtl.AppendFormat("Ka {0} {1} {2}{3}", tex.RGBA.R, tex.RGBA.G, tex.RGBA.B, Environment.NewLine); |
||
61 | mtl.AppendFormat("Kd {0} {1} {2}{3}", tex.RGBA.R, tex.RGBA.G, tex.RGBA.B, Environment.NewLine); |
||
62 | //mtl.AppendFormat("Ks {0} {1} {2}{3}"); |
||
63 | mtl.AppendLine("Tr " + tex.RGBA.A); |
||
64 | mtl.AppendLine("Ns " + shiny); |
||
65 | mtl.AppendLine("illum 1"); |
||
66 | if (tex.TextureID != UUID.Zero && tex.TextureID != Primitive.TextureEntry.WHITE_TEXTURE) |
||
67 | mtl.AppendLine("map_Kd ./" + texName); |
||
68 | mtl.AppendLine(); |
||
69 | |||
70 | #region Vertices |
||
71 | |||
72 | for (int k = 0; k < face.Vertices.Count; k++) |
||
73 | { |
||
74 | Vertex vertex = face.Vertices[k]; |
||
75 | Vector3 pos = vertex.Position; |
||
76 | Vector3 norm = vertex.Normal; |
||
77 | Vector2 texc = vertex.TexCoord; |
||
78 | |||
79 | // Apply scaling |
||
80 | pos *= mesh.Prim.Scale; |
||
81 | |||
82 | // Apply rotation |
||
83 | pos *= mesh.Prim.Rotation; |
||
84 | |||
85 | // The root prim position is sim-relative, while child prim positions are |
||
86 | // parent-relative. We want to apply parent-relative translations but not |
||
87 | // sim-relative ones |
||
88 | if (mesh.Prim.ParentID != 0) |
||
89 | pos += mesh.Prim.Position; |
||
90 | |||
91 | // Normal |
||
92 | if (vertex.Normal.IsFinite()) |
||
93 | obj.AppendFormat("vn {0} {1} {2}{3}", |
||
94 | norm.X.ToString("N6"), |
||
95 | norm.Y.ToString("N6"), |
||
96 | norm.Z.ToString("N6"), |
||
97 | Environment.NewLine); |
||
98 | else |
||
99 | obj.AppendLine("vn 0.0 1.0 0.0"); |
||
100 | |||
101 | // Texture Coord |
||
102 | obj.AppendFormat("vt {0} {1}{2}", |
||
103 | texc.X.ToString("N6"), |
||
104 | texc.Y.ToString("N6"), |
||
105 | Environment.NewLine); |
||
106 | |||
107 | // Position |
||
108 | obj.AppendFormat("v {0} {1} {2}{3}", |
||
109 | pos.X.ToString("N6"), |
||
110 | pos.Y.ToString("N6"), |
||
111 | pos.Z.ToString("N6"), |
||
112 | Environment.NewLine); |
||
113 | } |
||
114 | obj.AppendLine(); |
||
115 | |||
116 | #endregion Vertices |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | int startOffset = 0; |
||
122 | |||
123 | for (int i = 0; i < meshes.Count; i++) |
||
124 | { |
||
125 | FacetedMesh mesh = meshes[i]; |
||
126 | |||
127 | for (int j = 0; j < mesh.Faces.Count; j++) |
||
128 | { |
||
129 | Face face = mesh.Faces[j]; |
||
130 | |||
131 | if (face.Vertices.Count > 2) |
||
132 | { |
||
133 | //obj.AppendFormat("g face{0}-{1}{2}", i, face.ID, Environment.NewLine); |
||
134 | |||
135 | string mtlName = String.Format("material{0}-{1}", i, face.ID); |
||
136 | //obj.AppendLine("usemtl " + mtlName); |
||
137 | |||
138 | #region Elements |
||
139 | |||
140 | // Write all of the faces (triangles) for this side |
||
141 | for (int k = 0; k < face.Indices.Count / 3; k++) |
||
142 | { |
||
143 | obj.AppendFormat("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}{3}", |
||
144 | startOffset + face.Indices[k * 3 + 0] + 1, |
||
145 | startOffset + face.Indices[k * 3 + 1] + 1, |
||
146 | startOffset + face.Indices[k * 3 + 2] + 1, |
||
147 | Environment.NewLine); |
||
148 | } |
||
149 | |||
150 | obj.AppendFormat("# {0} faces{1}", face.Indices.Count / 3, Environment.NewLine); |
||
151 | obj.AppendLine(); |
||
152 | |||
153 | for (int k = 0; k < face.Vertices.Count; k++) |
||
154 | ++startOffset; |
||
155 | |||
156 | #endregion Elements |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | try |
||
162 | { |
||
163 | File.WriteAllText(filename, obj.ToString()); |
||
164 | File.WriteAllText(mtlFilename, mtl.ToString()); |
||
165 | } |
||
166 | catch (Exception) |
||
167 | { |
||
168 | return false; |
||
169 | } |
||
170 | |||
171 | return true; |
||
172 | } |
||
173 | } |
||
174 | } |