Texture coordinates from ObjReader

Mar 22, 2013 at 6:08 PM
Hey! Thanks for an awesome tool.

I am reading some obj files for a ray tracer and I have managed to read the Points of each triangle and the normals but the texture coordinate don´t seem to be working correctly. This is how I am reading the file:
            ObjReader r = new ObjReader();
            Model3DGroup model = r.Read(modelUrl);
            GeometryModel3D gm1 = (GeometryModel3D)model.Children[0];
            MeshGeometry3D mg1 = (MeshGeometry3D)gm1.Geometry;
            Vector3DCollection normals = mg1.Normals;     
            System.Windows.Media.PointCollection textureCoordinates = mg1.TextureCoordinates;
            System.Windows.Media.Int32Collection indices = mg1.TriangleIndices;
            Point3DCollection points = mg1.Positions;
            
            

            for (int index = 0; index < indices.Count; index += 3)
            {
                Point3D p0 = points[indices[index]];
                Point3D p1 = points[indices[index + 1]];
                Point3D p2 = points[indices[index + 2]];          
                
                Vector3D n0 = normals[indices[index]];
                Vector3D n1 = normals[indices[index + 1]];
                Vector3D n2 = normals[indices[index + 2]];

                System.Windows.Point t0 = textureCoordinates[indices[index]];
                System.Windows.Point t1 = textureCoordinates[indices[index + 1]];
                System.Windows.Point t2 = textureCoordinates[indices[index + 2]];
}
      
This loads the correct points and normals but the texture coordinates come out wrong. Am I approacing this incorrectly? How do you read the correct texture coordinate for every face?

The obj file I have been using (Removed text after vertices to get the objReader to read it) : http://cs.wellesley.edu/~cs307/tw/objects/cube-textures.obj

Best regards,
Viktor