1

I'm having trouble with my .OBJ file model loader. I am reading in the model fine, but when it comes to reading in the .mtl file the file will only open if I hard code the path.

I have check the path being given, and it matches the path I hard coded (which works just fine). But when ever I try to have the code either read the path from the .obj or just build it in the code, the file won't open.

Bellow is the relevant code. If I have missed anything, let me know and I will provide it.

bool ObjLoader::LoadOBJ(const std::string& filename, 
                        const std::string& filePath, 
                        std::vector<Vertex::PosNormal>& vertices,
                        std::vector<USHORT>& indices,
                        std::vector<MeshGeometry::Subset>& subsets,
                        std::vector<OBJMaterial>& mats)
{
    std::wifstream fin (filename.c_str());  // <-- .obj path

    if (fin)
    {
        while (fin)
        {
           // reads in .obj file
           // works just fine
           // meshMatLib read in from here                                     
        }
    }

    // Close the obj file
    fin.close();
    fin.clear();

    fin.open(meshMatLib);  // <-- uses hardcoded path above for /mtl

    if (fin)    // if the material file is open
    {
        while (fin)
        {
            // loads Mtl file
            // will not open
        }
    }
}

The below line is from the .obj file and is used for the .mtl path. mtllib Models\testTeapot\testTeapot.mtl

the hard coded path which works is:

 "Models\\testTeapot\\testTeapot.mtl"

It is passed into the method when LoadOBJ is called ('filePath')

The fact that this works when I hard code the path, suggests to me that the issue is related to the path. Is there a way I can get any further information on why the std::wifstream has failed?

Thanks in advance for any help, this has been driving me crazy for days now.

codingadventures
  • 2,924
  • 2
  • 19
  • 36
user3640122
  • 15
  • 1
  • 6
  • In my version of the [WaveFront OBJ reader](https://directxmesh.codeplex.com/wikipage?title=Wavefront%20OBJ&referringTitle=Geometry%20formats) for [DirectXMesh](http://go.microsoft.com/fwlink/?LinkID=324981) I assume the .mtl has the same path as the input file and use some splitpath/makepath logic. – Chuck Walbourn Apr 08 '15 at 19:48
  • **Never rely on current directory.** Always use absolute paths, which you can build from `GetModuleFileName(nullptr, ...)`. Remove file name from it and append your asset path, assuming it's in the folder of the exe. – CodeAngry Apr 09 '15 at 11:48
  • Thanks for the comment, i will try this. However, i still don't understand why getting using the path to the file, and adding the filename from the OBJ file doesn't work. I have tried also JUST getting the complete path from the OBJ file and it still wouldn't work. I have used break points in both of these to verify the path being used is identical to the one hard coded and passed into the method. Does anyone know the difference here? – user3640122 Apr 24 '15 at 12:26

0 Answers0