OBJModel::OBJModel(const std::string& fileName)
{
	hasUVs = false;
	hasNormals = false;
    std::ifstream file;
    file.open(fileName.c_str());

    std::string line;
    if(file.is_open())
    {
        while(file.good())
        {
            getline(file, line);
        
            unsigned int lineLength = line.length();
            
            if(lineLength < 2)
                continue;
            
            const char* lineCStr = line.c_str();
            
            switch(lineCStr[0])
            {
                case 'v':
                    if(lineCStr[1] == 't')
                        this->uvs.push_back(ParseOBJVec2(line));
                    else if(lineCStr[1] == 'n')
                        this->normals.push_back(ParseOBJVec3(line));
                    else if(lineCStr[1] == ' ' || lineCStr[1] == '\t')
                        this->vertices.push_back(ParseOBJVec3(line));
                break;
                case 'f':
                    CreateOBJFace(line);
                break;
                default: break;
            };
        }
    }
    else
    {
        std::cerr << "Unable to load mesh: " << fileName << std::endl;
    }
}
OBJModel::OBJModel(const std::string & fileName)
{
	std::ifstream file;
	file.open(fileName.c_str());

	std::string line;
	if (file.is_open())
	{
		while (file.good())
		{
			getline(file, line);

			unsigned int lineLength = line.length();

			if (lineLength < 2)
				continue;

			const char* lineCStr = line.c_str();

			switch (lineCStr[0])
			{
			case 'g':
				if (line.substr(2) == "default")
				{
					Group newGroup;

					newGroup.id = GroupList.size();

					newGroup.hasUVs = false;
					newGroup.hasNormals = false;

					this->GroupList.push_back(newGroup);
				}
				else
				{
					this->GroupList.back().name = line.substr(2);
				}
				break;
			case 'v':

				if (GroupList.size() == 0)
				{
					Group newGroup;

					newGroup.name = fileName;
					newGroup.id = GroupList.size();

					newGroup.hasUVs = false;
					newGroup.hasNormals = false;

					this->GroupList.push_back(newGroup);
				}

				if (lineCStr[1] == 't')
					this->GroupList.back().uvs.push_back(ParseOBJVec2(line));
				else if (lineCStr[1] == 'n')
					this->GroupList.back().normals.push_back(ParseOBJVec3(line));
				else if (lineCStr[1] == ' ' || lineCStr[1] == '\t')
					this->GroupList.back().vertices.push_back(ParseOBJVec3(line));
			break;
			case 'f':
				CreateOBJFace(line);
			break;
			default: break;
			}
		}
	}
	else
	{
		std::cerr << "Unable to load mesh: " << fileName << std::endl;
	}
}