0

Im trying to use a program in order to load an obj file and display it. When running the program, I am getting the else statement to print, as it cannot open the file, any ideas as to why?

Here is the code for the opening and parsing portion:

int Model_OBJ::Load(char* filename)
{
    string line;
    ifstream objFile (filename);
    if (objFile.is_open())                                                    // If obj file is open, continue
    {
        objFile.seekg (0, ios::end);                                        // Go to end of the file,
        long fileSize = objFile.tellg();                                    // get file size
        objFile.seekg (0, ios::beg);                                        // we'll use this to register memory for our 3d model
 
        vertexBuffer = (float*) malloc (fileSize);                            // Allocate memory for the verteces
        Faces_Triangles = (float*) malloc(fileSize*sizeof(float));            // Allocate memory for the triangles
        normals  = (float*) malloc(fileSize*sizeof(float));                    // Allocate memory for the normals
 
        int triangle_index = 0;                                                // Set triangle index to zero
        int normal_index = 0;                                                // Set normal index to zero
 
        while (! objFile.eof() )                                            // Start reading file data
        {
            getline (objFile,line);                                            // Get line from file
 
            if (line.c_str()[0] == 'v')                                        // The first character is a v: on this line is a vertex stored.
            {
                line[0] = ' ';                                                // Set first character to 0. This will allow us to use sscanf
 
                sscanf(line.c_str(),"%f %f %f ",                            // Read floats from the line: v X Y Z
                    &vertexBuffer[TotalConnectedPoints],
                    &vertexBuffer[TotalConnectedPoints+1],
                    &vertexBuffer[TotalConnectedPoints+2]);
 
                TotalConnectedPoints += POINTS_PER_VERTEX;                    // Add 3 to the total connected points
            }
            if (line.c_str()[0] == 'f')                                        // The first character is an 'f': on this line is a point stored
            {
                line[0] = ' ';                                                // Set first character to 0. This will allow us to use sscanf
 
                int vertexNumber[4] = { 0, 0, 0 };
                sscanf(line.c_str(),"%i%i%i",                                // Read integers from the line:  f 1 2 3
                    &vertexNumber[0],                                        // First point of our triangle. This is an
                    &vertexNumber[1],                                        // pointer to our vertexBuffer list
                    &vertexNumber[2] );                                        // each point represents an X,Y,Z.
 
                vertexNumber[0] -= 1;                                        // OBJ file starts counting from 1
                vertexNumber[1] -= 1;                                        // OBJ file starts counting from 1
                vertexNumber[2] -= 1;                                        // OBJ file starts counting from 1
 
 
                /********************************************************************
                 * Create triangles (f 1 2 3) from points: (v X Y Z) (v X Y Z) (v X Y Z).
                 * The vertexBuffer contains all verteces
                 * The triangles will be created using the verteces we read previously
                 */
 
                int tCounter = 0;
                for (int i = 0; i < POINTS_PER_VERTEX; i++)
                {
                    Faces_Triangles[triangle_index + tCounter   ] = vertexBuffer[3*vertexNumber[i] ];
                    Faces_Triangles[triangle_index + tCounter +1 ] = vertexBuffer[3*vertexNumber[i]+1 ];
                    Faces_Triangles[triangle_index + tCounter +2 ] = vertexBuffer[3*vertexNumber[i]+2 ];
                    tCounter += POINTS_PER_VERTEX;
                }
 
                /*********************************************************************
                 * Calculate all normals, used for lighting
                 */
                float coord1[3] = { Faces_Triangles[triangle_index], Faces_Triangles[triangle_index+1],Faces_Triangles[triangle_index+2]};
                float coord2[3] = {Faces_Triangles[triangle_index+3],Faces_Triangles[triangle_index+4],Faces_Triangles[triangle_index+5]};
                float coord3[3] = {Faces_Triangles[triangle_index+6],Faces_Triangles[triangle_index+7],Faces_Triangles[triangle_index+8]};
                float *norm = this->calculateNormal( coord1, coord2, coord3 );
 
                tCounter = 0;
                for (int i = 0; i < POINTS_PER_VERTEX; i++)
                {
                    normals[normal_index + tCounter ] = norm[0];
                    normals[normal_index + tCounter +1] = norm[1];
                    normals[normal_index + tCounter +2] = norm[2];
                    tCounter += POINTS_PER_VERTEX;
                }
 
                triangle_index += TOTAL_FLOATS_IN_TRIANGLE;
                normal_index += TOTAL_FLOATS_IN_TRIANGLE;
                TotalConnectedTriangles += TOTAL_FLOATS_IN_TRIANGLE;
            }
        }
        objFile.close();                                                        // Close OBJ file
    }
    else
    {
        cout << "Unable to open file";
    }
    return 0;
}

And here is the way I am calling it in main():

obj.Load("cow.obj");

I tried putting the directory of the file instead of the filename, as well as putting the file into the project director but neither is working.

genpfault
  • 51,148
  • 11
  • 85
  • 139
nickn17
  • 1
  • 1
  • Where is `cow.obj` located and where do you launch your program from? – Yksisarvinen Mar 07 '23 at 13:49
  • 4
    There is a lot of code here, much more than needed for a **minimal** example. There is also not enough code here for a **complete** example. [mcve]. Help us help you. Also, [while eof](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) is probably a problem. – Eljay Mar 07 '23 at 13:51
  • 2
    why do you not use `std::string` ? – 463035818_is_not_an_ai Mar 07 '23 at 13:51
  • 4
    Note that your usage of `while (! objFile.eof() )` is [wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – MikeCAT Mar 07 '23 at 13:54
  • 4
    *it cannot open the file, any ideas as to why?* -- Give us access to your machine, and we will be able to tell you why. Otherwise, how can we know why your file doesn't open? Maybe it isn't found? Maybe you have no permissions to open the file? Also, why have all of this code if the question is essentially why `ifstream objFile("cow.obj")` isn't opening the file? – PaulMcKenzie Mar 07 '23 at 14:04
  • Where did you learn to use `malloc` in C++ like that? Why not `std::vector`? If you read that in a book, you should probably stop reading it now and pick up a [reputable C++ book](https://stackoverflow.com/q/388242/11082165) that won't teach you bad practices. – Brian61354270 Mar 08 '23 at 21:32

0 Answers0