-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.cs
214 lines (205 loc) · 6.16 KB
/
import.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Drawing.Imaging;
using System.Reflection;
using System.Drawing;
using System.Linq;
using System.IO;
using System;
public class import
{
public Tuple<double,double,double>[] vert;
public Tuple<double,double>[] txCoord;
public Tuple<int,int,int,int,int,int,string>[] tri;
public double objScl = 0.1f;
public Dictionary<string,double[]> material = new Dictionary<string, double[]>();
public Dictionary<string,Tuple<byte[],int,int>> texture = new Dictionary<string, Tuple<byte[],int,int>>();
private bool debug = false;
private int vCount = 0, vtCount = 0, fCount = 0;
public void import_obj(string filename = "")
{
//filename = "C:/Users/princ/Desktop/3d models/projects/Lopunny - Copy/lopunny.obj";
string obj;
if(filename != "")
{
obj = File.ReadAllText(filename);
}
else
{
debug = true;
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "cube.obj";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
obj = reader.ReadToEnd();
}
}
string[] result = obj.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
for(int i = 0; i < result.Length; i++)
{
string[] X = result[i].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if(X[0] == "v")
{
vCount++;
}
if(X[0] == "vt")
{
vtCount++;
}
if(X[0] == "f")
{
fCount++;
}
if(X[0] == "mtllib")
{
if(Convert.ToString(X[1][2]) != ":" && !debug)
{
mtlDecode(Path.GetDirectoryName(filename) + "/" + string.Join(" ",X).Remove(0,7));
}
else
{
mtlDecode( X[1]);
}
}
}
vert = new Tuple<double,double,double>[vCount];
tri = new Tuple<int,int,int,int,int,int,string>[fCount];
txCoord = new Tuple<double, double>[vtCount];
int vRow = 0, vtRow = 0, fRow = 0;
string currentmtl = "";
for(int l = 0; l < result.Length; l++)
{
string[] X = result[l].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if(X[0] == "v")
{
string[] parse = result[l].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
double a = (Convert.ToDouble(parse[1])*objScl);
double b = -(Convert.ToDouble(parse[2])*objScl);
double c = -(Convert.ToDouble(parse[3])*objScl);
vert[vRow] = Tuple.Create(a,b,c);
vRow++;
}
if(X[0] == "vt")
{
string[] parse = result[l].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
txCoord[vtRow] = Tuple.Create(Convert.ToDouble(parse[1]),Convert.ToDouble(parse[2]));
vtRow++;
}
if(X[0] == "usemtl")
{
currentmtl = result[l].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1];
}
if(X[0] == "f")
{
string[] parse = result[l].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
int a = Convert.ToInt32(parse[1].Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0]);
int b = Convert.ToInt32(parse[2].Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0]);
int c = Convert.ToInt32(parse[3].Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0]);
int d = -1;
int e = -1;
int f = -1;
if(parse[1].Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1] != "")
{
d = Convert.ToInt32(parse[1].Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1]);
e = Convert.ToInt32(parse[2].Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1]);
f = Convert.ToInt32(parse[3].Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1]);
}
tri[fRow] = Tuple.Create(a,b,c,d,e,f,currentmtl);
fRow++;
}
}
}
private void mtlDecode(string mtlfile)
{
string mtl;
if(!debug)
{
mtl = File.ReadAllText(mtlfile);
}
else
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "cube.mtl";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
mtl = reader.ReadToEnd();
}
}
string[] result = mtl.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string currentmtl = "";
for(int l = 0; l < result.Length; l++)
{
string[] X = result[l].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if(X[0] == "newmtl")
{
currentmtl = X[1];
material.Add(currentmtl,new double[]{0.75,0.75,0.75,1.0});
texture[currentmtl] = null;
}
if(X[0] == "Kd")
{
material[currentmtl][0] = Convert.ToDouble(X[1]);
material[currentmtl][1] = Convert.ToDouble(X[2]);
material[currentmtl][2] = Convert.ToDouble(X[3]);
}
if(X[0] == "d")
{
material[currentmtl][3] = Convert.ToDouble(X[1]);
}
if(X[0] == "map_Kd")
{
try
{
if(Convert.ToString(X[1][2]) != ":" && !debug)
{
texImport(currentmtl,Path.GetDirectoryName(mtlfile) + "/" + X[1]);
}
else
{
texImport(currentmtl, X[1]);
}
}
catch
{
}
}
}
}
private void texImport(string currentmtl, string file)
{
Bitmap png;
if(!debug)
{
png = new Bitmap(file);
}
else
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "test cube.png";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
png = new Bitmap(stream);
}
}
texture[currentmtl] = Tuple.Create(rgbArray(png),png.Width,png.Height);
}
unsafe public byte[] rgbArray(Bitmap bmp)
{
byte[] data = new byte[bmp.Width*bmp.Height*4];
BitmapData bmpData = new BitmapData();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
bmpData = bmp.LockBits(rect,
ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb);
byte* Scan0 = (byte*)bmpData.Scan0.ToPointer();
int size = bmp.Width * bmp.Height * 4;
for (int i=0; i<size-1; i++)
{
data[i] = *(Scan0++);
}
bmp.UnlockBits(bmpData);
return data;
}
}