c#字节数组=>;文件=>;c++;阵列? 基本上我编写了一个Objo解析器(C++),效果很好。但是对于更大的“网格”,它开始需要30-60秒来加载,这是不可接受的
所以我读到二进制读取器速度更快,这导致我在c语言中制作了一个obj=>float[]解析器# 浮点[]是C++中直接调用的:GLBuffelDATA();p> 这是我在c#中用来保存到二进制文件的代码(我不知道它是否正确!):c#字节数组=>;文件=>;c++;阵列? 基本上我编写了一个Objo解析器(C++),效果很好。但是对于更大的“网格”,它开始需要30-60秒来加载,这是不可接受的,c#,c++,arrays,binary,converter,C#,C++,Arrays,Binary,Converter,所以我读到二进制读取器速度更快,这导致我在c语言中制作了一个obj=>float[]解析器# 浮点[]是C++中直接调用的:GLBuffelDATA();p> 这是我在c#中用来保存到二进制文件的代码(我不知道它是否正确!): 公共部分类主窗口:窗口 { 结构向量4f { 公共浮点数x,y,z,w; 公共向量4f(向量4f o) { 这个.x=o.x; 这个.y=o.y; 这个.z=o.z; 这个。w=o.w; } 公共向量4F(浮点x、浮点y、浮点z、浮点w) { 这个.x=x; 这个。y=y
公共部分类主窗口:窗口
{
结构向量4f
{
公共浮点数x,y,z,w;
公共向量4f(向量4f o)
{
这个.x=o.x;
这个.y=o.y;
这个.z=o.z;
这个。w=o.w;
}
公共向量4F(浮点x、浮点y、浮点z、浮点w)
{
这个.x=x;
这个。y=y;
这个。z=z;
这个.w=w;
}
};
结构对象面
{
公共向量3f垂直;
公共向量3f规范;
公共矢量3f纹理;
公共向量4f顶点4;
公共向量4f规范4;
公共向量4f纹理4;
公共对象面(向量3F v、向量3F n、向量3F t)
{
垂直=v;
常模=n;
纹理=t;
verts4=新矢量4f(0,0,0,0);
norms4=新向量4f(0,0,0,0);
纹理4=新向量4f(0,0,0,0);
}
公共对象面(向量4F v、向量4F n、向量4F t)
{
verts4=v;
norms4=n;
织构4=t;
顶点=新矢量3f(0,0,0);
范数=新向量3f(0,0,0);
纹理=新矢量3f(0,0,0);
}
};
结构OBJData
{
公共整数点计数;
公开名单F;
公开名单五;
公共列表VN;
公开名单;
公共内部渲染;
公共对象数据(int pc)
{
_点计数=pc;
RenderID=-1;
this.F=新列表();
this.V=新列表();
this.VN=新列表();
this.VT=新列表();
}
};
公共主窗口()
{
初始化组件();
}
私有无效选择文件\u单击\u 1(对象发送方,路由目标)
{
Microsoft.Win32.OpenFileDialog pfd=新的Microsoft.Win32.OpenFileDialog();
if(pfd.ShowDialog()==true)
{
字符串文件=pfd.FileName;
FileInfo=newfileinfo(文件);
如果(信息存在)
{
string[]lines=File.ReadAllLines(pfd.FileName);
OBJData d=新的OBJData(4);
对于(int i=0;ipublic partial class MainWindow : Window
{
struct Vector4f
{
public float x, y, z, w;
public Vector4f(Vector4f o)
{
this.x = o.x;
this.y = o.y;
this.z = o.z;
this.w = o.w;
}
public Vector4f(float x, float y, float z, float w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
};
struct OBJFace
{
public Vector3f verts;
public Vector3f norms;
public Vector3f textures;
public Vector4f verts4;
public Vector4f norms4;
public Vector4f textures4;
public OBJFace(Vector3f v, Vector3f n, Vector3f t)
{
verts = v;
norms = n;
textures = t;
verts4 = new Vector4f(0, 0, 0, 0);
norms4 = new Vector4f(0, 0, 0, 0);
textures4 = new Vector4f(0, 0, 0, 0);
}
public OBJFace(Vector4f v, Vector4f n, Vector4f t)
{
verts4 = v;
norms4 = n;
textures4 = t;
verts = new Vector3f(0, 0, 0);
norms = new Vector3f(0, 0, 0);
textures = new Vector3f(0, 0, 0);
}
};
struct OBJData
{
public int _pointCount;
public List<OBJFace> F;
public List<Vector3f> V;
public List<Vector3f> VN;
public List<Vector3f> VT;
public int RenderID;
public OBJData(int pc)
{
_pointCount = pc;
RenderID = -1;
this.F = new List<OBJFace>();
this.V = new List<Vector3f>();
this.VN = new List<Vector3f>();
this.VT = new List<Vector3f>();
}
};
public MainWindow()
{
InitializeComponent();
}
private void ChooseFile_Click_1(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog pfd = new Microsoft.Win32.OpenFileDialog();
if (pfd.ShowDialog() == true)
{
string file = pfd.FileName;
FileInfo info = new FileInfo(file);
if (info.Exists)
{
string[] lines = File.ReadAllLines(pfd.FileName);
OBJData d = new OBJData(4);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].StartsWith("v "))
{
Vector3f vec = new Vector3f((float)Convert.ToDouble(lines[i].Split(' ')[1].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[2].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[3].Replace(".", ",")));
d.V.Add(vec);
}
else if (lines[i].StartsWith("vn "))
{
Vector3f norm = new Vector3f((float)Convert.ToDouble(lines[i].Split(' ')[1].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[2].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[3].Replace(".", ",")));
d.VN.Add(norm);
}
else if (lines[i].StartsWith("vt "))
{
Vector3f text = new Vector3f((float)Convert.ToDouble(lines[i].Split(' ')[1].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[2].Replace(".", ",")), 0.0F);
d.VT.Add(text);
}else if(lines[i].StartsWith("f "))
{
Vector4f vert = new Vector4f((float)Convert.ToDouble(lines[i].Split(' ')[1].Split('/')[0].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[2].Split('/')[0].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[3].Split('/')[0].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[4].Split('/')[0].Replace(".", ",")));
Vector4f norm = new Vector4f((float)Convert.ToDouble(lines[i].Split(' ')[1].Split('/')[2].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[2].Split('/')[2].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[3].Split('/')[2].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[4].Split('/')[2].Replace(".", ",")));
Vector4f textures = new Vector4f((float)Convert.ToDouble(lines[i].Split(' ')[1].Split('/')[1].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[2].Split('/')[1].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[3].Split('/')[1].Replace(".", ",")), (float)Convert.ToDouble(lines[i].Split(' ')[4].Split('/')[1].Replace(".", ",")));
d.F.Add(new OBJFace(vert, norm, textures));
}
}
Debug.WriteLine("OBJ Parsing Done!");
MessageBox.Show("OBJ Parsing Done, Continuing On To Converting!");
List<float> vertices = new List<float>();
for (int i = 0; i < d.F.Count; i++)
{
OBJFace f = d.F[i];
Vector3f norms = d.VN[((int)f.norms4.x - 1)];
Vector3f textures = d.VT[((int)f.textures4.x - 1)];
Vector3f verts = d.V[((int)f.verts4.x - 1)];
Vector3f norms2 = d.VN[((int)f.norms4.y - 1)];
Vector3f textures2 = d.VT[((int)f.textures4.y - 1)];
Vector3f verts2 = d.V[((int)f.verts4.y - 1)];
Vector3f norms3 = d.VN[((int)f.norms4.z - 1)];
Vector3f textures3 = d.VT[((int)f.textures4.z - 1)];
Vector3f verts3 = d.V[((int)f.verts4.z - 1)];
Vector3f norms4 = d.VN[((int)f.norms4.w - 1)];
Vector3f textures4 = d.VT[((int)f.textures4.w - 1)];
Vector3f verts4 = d.V[((int)f.verts4.w - 1)];
float[] vertic ={
textures.x, textures.y,
norms.x, norms.y, norms.z,
verts.x, verts.y, verts.z,
textures2.x, textures2.y,
norms2.x, norms2.y, norms2.z,
verts2.x, verts2.y, verts2.z,
textures3.x, textures3.y,
norms3.x, norms3.y, norms3.z,
verts3.x, verts3.y, verts3.z,
textures4.x, textures4.y,
norms4.x, norms4.y, norms4.z,
verts4.x, verts4.y, verts4.z
};
for (int a = 0; a < vertic.Length; a++)
{
vertices.Add(vertic[a]);
}
}
float[] objArray = vertices.ToArray();
Debug.WriteLine("Converting Done!");
MessageBox.Show("Converting Done, Starting Save Back!");
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == true)
{
string sfile = sfd.FileName;
FileInfo sinfo = new FileInfo(sfile);
if (!sinfo.Exists)
{
byte[] byteArray = new byte[objArray.Length * 4];
Buffer.BlockCopy(objArray, 0, byteArray, 0, byteArray.Length);
File.WriteAllBytes(sfile, byteArray);
Debug.WriteLine("Save Back Completed!");
MessageBox.Show("Done!");
}
}
}
else MessageBox.Show("File: " + info.FullName + ", Doesn't Exist!");
}
}
}