-2
\$\begingroup\$

If I have the obj file like below, what would be my indices. How do I find the indices? What would be the order of the indices also.(the triangles)

v 1.000000 0.000000 -1.000000

v -1.000000 0.000000 -1.000000

v 1.000000 0.000000 1.000000

v -1.000000 0.000000 1.000000


vt 0.9562 0.0001

vt 0.0001 0.9999

vt 0.0001 0.0001

vt 0.9562 0.9999


vn 0.0000 1.0000 0.0000

f 2/1/1 3/2/1 1/3/1

f 2/1/1 4/4/1 3/2/1
\$\endgroup\$
  • \$\begingroup\$ Anyone. Been stuck on it for a while. \$\endgroup\$ – Terry May 19 '17 at 2:43
  • 3
    \$\begingroup\$ I'm pretty sure any site about wavefront files tells you that \$\endgroup\$ – Bálint May 19 '17 at 8:00
0
\$\begingroup\$

Wavefront files don't contain the indices in a traditional sense. The lines starting with "f" are the closest one you get. Each one of those marks a triangle with 3 vertices. These vertices are represented in the v/vt/n format. The first value is the id of the vertex, the second is the uv coordinate and the third is the normal.

Neither D3D, nor OpenGL can deal with a format like this, so you'll have to manually convert it to the preferred format.

This makes the already pretty large file a bit smaller, because for example a flat shaded cube would require 24 different vertex declarations if you use the traditional way, but only 8 this way.

\$\endgroup\$
-2
\$\begingroup\$
//================needed classes or structs =================
   public struct Vertex 
    {
        private Vector3 position;
        private Vector3 normal;
        private Vector2 textureCoords;
        public Vertex(Vector3 position, Vector3 normal, Vector2 textureCoords )
        {
            this.normal = normal;
            this.position = position;
            this.textureCoords = textureCoords;

        }

        public static int Size => (3 + 3 + 2) * 4;  

    }


 public class OBJLoader
    {

   public static RawModel LoadObjModel(String name)
        {
            string path = @"Models\" + name + ".obj";
            List<Vertex> Vertices = new List<Vertex>();
            List<string> VertexString = new List<string>();
            List<int> Indices = new List<int>();
            List<Vector3> Positions = new List<Vector3>();
            List<Vector2> TextureCoordinates = new List<Vector2>();
            List<Vector3> Normals = new List<Vector3>();
            List<string> Lines = new List<string>();
            string Line;
            System.IO.StreamReader file = new System.IO.StreamReader(path);
            while ((Line = file.ReadLine()) != null)
            {
                Lines.Add(Line);
            }
            file.Close();
            string FacePattern = "^f .*$";
            string PositionPattern = "^v .*$";
            string TextureCorrdinatesPattern = "^vt .*$";
            string NormalPattern = "^vn .*$";
            string FloatingPointPattern = @"[-+]?[0-9]*\.?[0-9]+";
            string VertexFacePattern = @"[0-9]*/[0-9]*/[0-9]*";
            string NumberPatten = @"\d+";
            MatchCollection FloatingPoints = null;
            MatchCollection FaceVertices = null;
            Lines.Where(L => Regex.IsMatch(L, PositionPattern)).ToList().ForEach(L =>
            {
                FloatingPoints = Regex.Matches(L, FloatingPointPattern);
                Positions.Add(new Vector3(float.Parse(FloatingPoints[0].ToString()), float.Parse(FloatingPoints[1].ToString()), float.Parse(FloatingPoints[2].ToString())));
            });
            Lines.Where(L => Regex.IsMatch(L, NormalPattern)).ToList().ForEach(L =>
            {
                FloatingPoints = Regex.Matches(L, FloatingPointPattern);
                Normals.Add(new Vector3(float.Parse(FloatingPoints[0].ToString()), float.Parse(FloatingPoints[1].ToString()), float.Parse(FloatingPoints[2].ToString())));
            });
            Lines.Where(L => Regex.IsMatch(L, TextureCorrdinatesPattern)).ToList().ForEach(L =>
            {
                FloatingPoints = Regex.Matches(L, FloatingPointPattern);
                TextureCoordinates.Add(new Vector2(float.Parse(FloatingPoints[0].ToString()), float.Parse(FloatingPoints[1].ToString())));
            });
            Lines.Where(L => Regex.IsMatch(L, FacePattern)).ToList().ForEach(L =>
            {
                FaceVertices = Regex.Matches(L, VertexFacePattern);
                VertexString.Add(FaceVertices[0].ToString());
                VertexString.Add(FaceVertices[1].ToString());
                VertexString.Add(FaceVertices[2].ToString());
            });
            List<string> AllVerticesStrings = new List<string>();
            AllVerticesStrings = VertexString;
            VertexString = VertexString.Distinct().ToList();
            MatchCollection indices = null;
            VertexString.ForEach(Vertex =>
            {
                indices = Regex.Matches(Vertex, NumberPatten);
                Vertices.Add(new VertexData.Vertex(Positions[int.Parse(indices[0].ToString()) - 1], Normals[int.Parse(indices[2].ToString()) - 1], TextureCoordinates[int.Parse(indices[1].ToString()) - 1]));
            });
            Dictionary<string, int> IndexKeyDictionary = new Dictionary<string, int>();
            for (int i = 0; i < VertexString.Count; i++)
            {
                IndexKeyDictionary.Add(VertexString[i], i);
            }
            AllVerticesStrings.ToList().ForEach(Key => Indices.Add(IndexKeyDictionary[Key]));
            return new RawModel(Vertices.ToArray(), Indices.ToArray());
        }

}
\$\endgroup\$
  • 2
    \$\begingroup\$ How does this answer the question? \$\endgroup\$ – Vaillancourt Jun 22 '18 at 13:13
  • \$\begingroup\$ Just posting a bunch of code without any comments is a terrible idea. Someone would have to spend so much time to read and understand what you wrote, before they can decide if it works or not. It would benefit us all if you could give some explanation and also some comments about what the code is actually doing. \$\endgroup\$ – TomTsagk Jun 23 '18 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.