I'm currently trying to create my own OBJLoader using Fstream to load and search through the OBJ file to read the vertices and such. This is what I've got So far (It's not finished yet)
Mesh* OBJLoader::LoadModel(char* path)
{
Mesh* mesh = new Mesh(); //New Mesh
std::vector<Vector3> vertices;
std::vector<TexCoord> textureCoords;
std::vector<Vector3> normals;
//Loads OBJ file from path
std::ifstream file;
file.open(path);
if (!file.good())
{
std::cout << "Can't open texture file " << path << std::endl;
return nullptr;
}
std::string line;
while (std::getline(file, line))
{
std::string text;
file >> text;
if (text == "v")
{
Vector3 vertex;
file >> vertex.X;
file >> vertex.Y;
file >> vertex.Z;
vertices.push_back(vertex);
//std::cout << vertex.Z << std::endl;
}
file >> text;
if (text == "vt")
{
TexCoord texCoord;
file >> texCoord.u;
file >> texCoord.v;
textureCoords.push_back(texCoord);
}
}
return nullptr;
}
It seemed like it worked at first, I tested it by printing out the values to see if they match the values in the OBJ file and it seems... most don't and I'm not sure why.
I'm putting these vertices I get from the OBJ file in a vertices vector so I tested this loader by printing out the vertices vector to compare the values, this is what I got:
These are the last couple of vertices and should match the last couple of Vertices I have in the OBJ File:
As you can likely see, the values don't exactly match. Some values are missing a single number off the end while some display something completely different like "-3E-06". Does anyone know why this is happening and how I can fix it since it almost works perfectly.
This is also the code I'm using to print out the vertices which is within the function above if you wanted to know:
for (int i = 0; i < vertices.size(); i++)
{
std::cout << vertices[i].X << " " << vertices[i].Y << " " <<
vertices[i].Z << std::endl;
}
EDIT I've noticed that simply pritning out the Vertices using file >> instead of putting them into the vertices vector and then printing them out results in the correct value being printed out. It seems when I put the values into the vertices vector, it changes them slightly. I tested printing out just the first X vertex of the OBJ file after I had put it into "vertex.X" which outputted "-2.97217" while the value should of been "-2.972168". It strangely seems like it's rounding the last two numbers or something Though it only seems to do it for the X and Y vertices and not the Z.