Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 面值中的obj文件分解错误_C#_Opengl - Fatal编程技术网

C# 面值中的obj文件分解错误

C# 面值中的obj文件分解错误,c#,opengl,C#,Opengl,我试图理解一个obj文件,这样我就可以分解它进行导入。现在,我的示例是一个简单的金字塔,使用3ds max设计。教程中出现了一些小错误 到目前为止,我了解一点,但这些都是正常的: v is vertex(point in space), vn is vertex normal(don't understand why needed) but imported ok vt is vertex texture 但文件中的下一个是f&s,如下所示: s 4 f 1/1/1 2/2/1 3/3/1

我试图理解一个obj文件,这样我就可以分解它进行导入。现在,我的示例是一个简单的金字塔,使用3ds max设计。教程中出现了一些小错误

到目前为止,我了解一点,但这些都是正常的:

v is vertex(point in space),  
vn is vertex normal(don't understand why needed) but imported ok
vt is vertex texture
但文件中的下一个是f&s,如下所示:

s 4
f 1/1/1 2/2/1 3/3/1 
s 2
f 1/1/2 3/4/2 4/5/2 
首先,我不知道什么是
s
,为什么它会以指数级递增

更重要的是,根据wikki的说法,我没有得到
f
的细分:

f v1/vt1/vn1  
对我来说,这看起来像是
v1
是顶点,
vt
是纹理,
vn
是正常的,但他们为什么要在文件中重复这些?所以我想我的猜测是错的

总的来说,我的问题是,我的加载对象能够填充除面以外的所有对象,除非面与文件底部的多边形相同,然后是0,但这也没有意义

请解决我的困惑,谢谢

以下是代码和文件:

[守则]

    public static ObjVolume LoadFromString(string obj)
    {




        // Seperate lines from the file
        List<String> lines = new List<string>(obj.Split('\n'));

        // Lists to hold model data
        List<Vector3> verts = new List<Vector3>();
        List<Vector3> colors = new List<Vector3>();
        List<Vector2> texs = new List<Vector2>();
        List<Tuple<int, int, int>> faces = new List<Tuple<int, int, int>>();

        // Read file line by line
        foreach (String line in lines)
        {
            if (line.StartsWith("v ")) // Vertex definition
            {
                // Cut off beginning of line
                String temp = line.Substring(2);
                temp.TrimStart( );

                Vector3 vec = new Vector3();

                if (temp.Count((char c) => c == ' ') == 3) // Check if there's enough elements for a vertex
                {
                    String[] vertparts = temp.Split(' ');
                    vertparts[3].Replace("\r","");

                    // Attempt to parse each part of the vertice
                    bool success = float.TryParse(vertparts[1], out vec.X);
                    success |= float.TryParse(vertparts[2], out vec.Y);
                    success |= float.TryParse(vertparts[3], out vec.Z);

                    // Dummy color/texture coordinates for now
                    colors.Add(new Vector3((float)Math.Sin(vec.Z), (float)Math.Sin(vec.Z), (float)Math.Sin(vec.Z)));
                    texs.Add(new Vector2((float)Math.Sin(vec.Z), (float)Math.Sin(vec.Z)));

                    // If any of the parses failed, report the error
                    if (!success)
                    {
                        Console.WriteLine("Error parsing vertex: {0}", line);
                    }
                }

                verts.Add(vec);
            }
            else if (line.StartsWith("f ")) // Face definition
            {
                // Cut off beginning of line
                String temp = line.Substring(2);

                Tuple<int, int, int> face = new Tuple<int, int, int>(0, 0, 0);

                if (temp.Count((char c) => c == ' ') == 3) // Check if there's enough elements for a face
                {
                    String[] faceparts = temp.Split(' ');

                    int i1, i2, i3;




                    // Attempt to parse each part of the face
                    bool success = int.TryParse(faceparts[0], out i1);
                    success |= int.TryParse(faceparts[1], out i2);
                    success |= int.TryParse(faceparts[2], out i3); 

                    // If any of the parses failed, report the error
                    if (!success)
                    {
                        Console.WriteLine("Error parsing face: {0}", line);
                    }
                    else
                    {
                        // Decrement to get zero-based vertex numbers
                        face = new Tuple<int, int, int>(i1 - 1, i2 - 1, i3 - 1);
                        faces.Add(face);
                    }
                }
            }
        }

        // Create the ObjVolume
        ObjVolume vol = new ObjVolume();
        vol.vertices = verts.ToArray();
        vol.faces = new List<Tuple<int, int, int>>(faces);
        vol.colors = colors.ToArray();
        vol.texturecoords = texs.ToArray();

        return vol;
    }


# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 27.02.2016 07:40:58

mtllib test1.mtl

#
# object Pyramid001
#

v  -15.7732 14.1346 5.0964
v  -23.0566 0.0050 13.3650
v  -8.4899 0.0050 13.3650
v  -8.4899 0.0050 -3.1722
v  -23.0566 0.0050 -3.1722
v  -15.7732 0.0050 5.0964
# 6 vertices

vn 0.0000 0.5051 0.8631
vn 0.8889 0.4582 -0.0000
vn 0.0000 0.5051 -0.8631
vn -0.8889 0.4582 -0.0000
vn 0.0000 -1.0000 -0.0000
# 5 vertex normals

vt 0.5000 1.0000 0.0000
vt 0.0000 0.0000 0.0000
vt 1.0000 0.0000 0.0000
vt 0.0596 0.0000 0.0000
vt 1.0596 0.0000 0.0000
vt 0.0000 1.0000 0.0000
vt 0.5000 0.5000 0.0000
vt 1.0000 1.0000 0.0000
# 8 texture coords

g Pyramid001
usemtl wire_154215229
s 4
f 1/1/1 2/2/1 3/3/1 
s 2
f 1/1/2 3/4/2 4/5/2 
s 16
f 1/1/3 4/2/3 5/3/3 
s 32
f 1/1/4 5/4/4 2/5/4 
s 8
f 2/6/5 6/7/5 3/8/5 
f 3/8/5 6/7/5 4/3/5 
f 4/3/5 6/7/5 5/2/5 
f 5/2/5 6/7/5 2/6/5 
# 0 polygons - 8 triangles
publicstaticobjvolume LoadFromString(stringobj)
{
//从文件中分离行
列表行=新列表(对象拆分('\n'));
//用于保存模型数据的列表
列表顶点=新列表();
列表颜色=新列表();
List texs=新列表();
列表面=新列表();
//逐行读取文件
foreach(行中的字符串行)
{
if(line.StartsWith(“v”)//顶点定义
{
//断线
字符串温度=行子字符串(2);
temp.TrimStart();
Vector3 vec=新Vector3();
if(temp.Count((char c)=>c=''==3)//检查是否有足够的元素用于顶点
{
字符串[]vertparts=温度拆分(“”);
顶点零件[3]。替换(“\r”和“);
//尝试分析vertice的每个部分
bool success=float.TryParse(vertparts[1],out vec.X);
success |=float.TryParse(vertparts[2],out vec.Y);
success |=float.TryParse(vertparts[3],out vec.Z);
//现在的虚拟颜色/纹理坐标
添加(新向量3((float)Math.Sin(vec.Z),(float)Math.Sin(vec.Z),(float)Math.Sin(vec.Z));
添加(新向量2((float)Math.Sin(vec.Z),(float)Math.Sin(vec.Z));
//如果任何解析失败,请报告错误
如果(!成功)
{
WriteLine(“分析顶点时出错:{0}”,第行);
}
}
垂直添加(vec);
}
else if(line.StartsWith(“f”)//面定义
{
//断线
字符串温度=行子字符串(2);
元组面=新元组(0,0,0);
if(temp.Count((char c)=>c==''==3)//检查一个面是否有足够的元素
{
字符串[]faceparts=临时拆分(“”);
inti1,i2,i3;
//尝试分析面的每个部分
bool success=int.TryParse(faceparts[0],out i1);
success |=int.TryParse(faceparts[1],out i2);
success |=int.TryParse(faceparts[2],out i3);
//如果任何解析失败,请报告错误
如果(!成功)
{
WriteLine(“错误解析面:{0}”,第行);
}
其他的
{
//递减以获得基于零的顶点数
face=新元组(i1-1,i2-1,i3-1);
面。添加(面);
}
}
}
}
//创建ObjVolume
ObjVolume=新的ObjVolume();
vol.vertexts=顶点到阵列();
vol.faces=新列表(面);
vol.colors=colors.ToArray();
vol.texturecoords=texs.ToArray();
返回卷;
}
#3ds Max波前OBJ导出器v0.97b-(c)2007 guruware
#创建的文件:27.02.2016 07:40:58
mtllib test1.mtl
#
#对象金字塔001
#
v-15.7732 14.1346 5.0964
v-23.0566 0.0050 13.3650
v-8.4899 0.0050 13.3650
v-8.48990.0050-3.1722
v-23.0566 0.0050-3.1722
v-15.7732 0.0050 5.0964
#6个顶点
越南盾0.0000 0.5051 0.8631
越南盾0.8889 0.4582-0.0000
越南盾0.0000 0.5051-0.8631
越南盾-0.8889 0.4582-0.0000
越南盾0.0000-1.0000-0.0000
#5顶点法线
瓦特0.5000 1.0000 0.0000
瓦特0.0000 0.0000 0.0000
瓦特1.0000 0.0000 0.0000
瓦特0.0596 0.0000 0.0000
vt 1.0596 0.0000 0.0000
瓦特0.0000 1.0000 0.0000
瓦特0.5000 0.5000 0.0000
瓦特1.0000 1.0000 0.0000
#8纹理坐标
g金字塔001
使用MTL电线_154215229
s 4
f 1/1/1 2/2/1 3/3/1
s 2
f 1/1/2 3/4/2 4/5/2
s 16
f 1/1/3 4/2/3 5/3/3
s 32
f 1/1/4 5/4/4 2/5/4
s 8
f 2/6/5 6/7/5 3/8/5
f 3/8/5 6/7/5 4/3/5
f 4/3/5 6/7/5 5/2/5
f 5/2/5 6/7/5 2/6/5
#0个多边形-8个三角形

[/code]

关于V/Vt/Vn,你是对的。您需要再次了解面是如何连接的每个面的纹理坐标和法线是什么。它们在文件中不重复,f 1/4/10 2/5/6 3/7/11表示此面具有垂直v1、v2和v3,纹理坐标位于vt4、vt5和vt7,法线坐标为vn10、vn6和vn11。请注意,v、vt和vn的索引与它们在obj文件中的显示顺序相同。我建议您暂时忽略vt和vn,只尝试使用f之后的第一个数字显示人脸。对于上一个示例,您可以连接v1、v2和v3,并且应该得到正确的面。稍后,您还可以添加法线和纹理,以获得具有纹理和正确照明的完整形状。请注意,只有当您拥有提供纹理的图像图谱时,vt才是iseful。VT实际上是参考的