# Indices of a obj file format

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
• Anyone. Been stuck on it for a while. – Terry May 19 '17 at 2:43
• I'm pretty sure any site about wavefront files tells you that – Bálint May 19 '17 at 8:00

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.

//================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;

}

{

{
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;
while ((Line = file.ReadLine()) != null)
{
}
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);
});
Lines.Where(L => Regex.IsMatch(L, NormalPattern)).ToList().ForEach(L =>
{
FloatingPoints = Regex.Matches(L, FloatingPointPattern);
});
Lines.Where(L => Regex.IsMatch(L, TextureCorrdinatesPattern)).ToList().ForEach(L =>
{
FloatingPoints = Regex.Matches(L, FloatingPointPattern);
});
Lines.Where(L => Regex.IsMatch(L, FacePattern)).ToList().ForEach(L =>
{
FaceVertices = Regex.Matches(L, VertexFacePattern);
});
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++)
{