-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbr_2_obj
138 lines (110 loc) · 6.45 KB
/
mbr_2_obj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж
using System; using System.IO; using System.Linq; using System.Text; using System.Collections;
using System.Collections.Generic; using System.Text.RegularExpressions; using System.Globalization;
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж
sealed class Test
{
static uint vertexCount;
static uint faceCount;
public static void Main()
{
SearchOption SOAD = SearchOption.AllDirectories;
string[] filesName = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.mbr", SOAD);
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
foreach (var fileIn in filesName)
{
using (BinaryReader br = new BinaryReader(File.Open(fileIn, FileMode.Open)))
{
br.ReadUInt16();
int submesh_count = br.ReadInt32();
for (int o = 0; o < submesh_count; o++)
{
using (StreamWriter sw = new StreamWriter(fileIn + o + ".obj"))
{
O3dModel o3d = new O3dModel();
ushort type = br.ReadUInt16();
for(int i = 0; i < 2; i++) br.ReadBytes(48);
for(int i = 0; i < 27; i++) br.ReadSingle();
vertexCount = br.ReadUInt32();
faceCount = br.ReadUInt32();
for(int i = 0; i < vertexCount; i++)
{
o3d_vertex newv = new o3d_vertex();
newv.x = br.ReadSingle();
newv.y = br.ReadSingle();
newv.z = br.ReadSingle();
o3d.vertexes.Add(newv);
}
for(int i = 0; i < faceCount; i++)
{
o3d_face newf = new o3d_face();
br.ReadByte();br.ReadByte();br.ReadByte();br.ReadByte();
br.ReadSingle();br.ReadSingle();br.ReadSingle();br.ReadSingle();
br.ReadSingle();br.ReadSingle();br.ReadSingle();br.ReadSingle();
for (int ii = 0; ii < 4; ii++)
{
newf.index[ii] = br.ReadUInt16();
//Console.Write(newf.index[ii] + " ");
} //Console.WriteLine();
o3d.faces.Add(newf);
br.ReadUInt16();br.ReadUInt16();br.ReadUInt16();
}
for(int i = 0; i < vertexCount; i++) br.ReadUInt32();
if (type == 2)
{
int count_float = br.ReadInt32();
if (count_float > 0)
{
for(int i = 0; i < count_float; i++)
{
o3d_face newf = new o3d_face();
br.ReadByte();br.ReadByte();br.ReadByte();br.ReadByte();
br.ReadSingle();br.ReadSingle();br.ReadSingle();br.ReadSingle();
br.ReadSingle();br.ReadSingle();br.ReadSingle();br.ReadSingle();
for (int ii = 0; ii < 4; ii++)
{
newf.index[ii] = br.ReadUInt16();
//Console.Write(newf.index[ii] + " ");
} //Console.WriteLine();
//o3d.faces.Add(newf); // это грани ? слишком много 65***
br.ReadUInt16();br.ReadUInt16();br.ReadUInt16();
}
for(int i = 0; i < count_float; i++)
for(int iii = 0; iii < 5; iii++) br.ReadUInt16();
}
for(int i = 0; i < 6; i++) br.ReadSingle();
}
// ========================================================
foreach (var v in o3d.vertexes)
sw.WriteLine("v " + v.x + " " + v.y + " " + v.z);
var enumerator = o3d.faces.GetEnumerator();
foreach (var v in o3d.faces)
{
if (v.index[0] == 65535 || v.index[1] == 65535 || v.index[2] == 65535 ) enumerator.MoveNext();
sw.WriteLine("f " + (v.index[0]+1) + " " + (v.index[1]+1) + " " + (v.index[2]+1));
if (v.index[3] != 65535) sw.WriteLine("f " + (v.index[0]+1) + " " + (v.index[2]+1) + " " + (v.index[3]+1));
}
}
}
}
} // foreach (var file in filesName)
} // public static void Main()
} // sealed class Test {
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж
public class O3dModel
{
public List<o3d_vertex> vertexes = new List<o3d_vertex>();
public List<o3d_face> faces = new List<o3d_face>();
}
// ========================================================
public class o3d_vertex
{
public float x, y, z; // Model vertex position (XYZ)
}
// ========================================================
public class o3d_face // грань/полигон (треугольник или четырехугольник)
{
public ushort[] index = new ushort[4];
}