RawModel OBJLoader::LoadObjModel(const std::string& fileName, Loader& loader)
{
	clock_t startTime = clock();
	// Open the file as read only
	FILE* file;
	if (fopen_s(&file, ("../res/models/" + fileName + ".obj").c_str(), "r") != 0)
	{
		printf("Failed to open: %s\n", fileName);
	}

	// Storage variables
	std::vector<glm::vec2> textures, tempTextures;
	std::vector<glm::vec3> vertices, normals, tempNormals;
	std::vector<int> indices;

	char *type, *token, *stop = 0;
	double x, y, z;
	char line[256];
	while (fgets(line, 256, file) != NULL)
	{
		token = NULL;
		type = strtok_s(line, " ", &token);
		// V is vertex points
		if (type[0] == 'v' && type[1] == NULL)
		{
			x = strtod(token, &stop);
			token = stop + 1; // Move to the next value
			y = strtod(token, &stop);
			token = stop + 1; // Move to the next value
			z = strtod(token, &stop);
			// Store a new vertex
			vertices.push_back(glm::vec3(x, y, z));
		}
		// VT is vertex texture coordinates
		else if (type[0] == 'v' && type[1] == 't')
		{
			x = strtod(token, &stop);
			token = stop + 1; // Move to the next value
			y = 1 - strtod(token, &stop);
			// Store a new texture
			tempTextures.push_back(glm::vec2(x, y));
		}
		else if (type[0] == 'v' && type[1] == 'n')
		{
			x = strtod(token, &stop);
			token = stop + 1; // Move to the next value
			y = strtod(token, &stop);
			token = stop + 1; // Move to the next value
			z = strtod(token, &stop);
			// Store a new normal
			tempNormals.push_back(glm::vec3(x, y, z));
		}
		// F is the index list for faces
		else if (type[0] == 'f')
		{
			if (indices.size() == 0)
			{
				// Set the size of the array
				textures.resize(vertices.size());
				normals.resize(vertices.size());
			}
			// Process set of vertex data
			ProcessVertices(token, indices, tempTextures, textures, tempNormals, normals);
		}
	}
	fclose(file);

	printf("Load time: %dms\n", clock() - startTime);
	return loader.LoadToVAO(vertices, textures, normals, indices);
}
//Game loop
void GameManager::Start(){

	std::cout << "Game loop is now running.\n";

	Loader loader;
	StaticShader staticShader("basicShader");

	Renderer renderer(staticShader, m_displayManager->GetAspect());

	float vertices[] = {
		// Front face
		-1.0, -1.0, 1.0,
		1.0, -1.0, 1.0,
		1.0, 1.0, 1.0,
		-1.0, 1.0, 1.0,

		// Back face
		-1.0, -1.0, -1.0,
		-1.0, 1.0, -1.0,
		1.0, 1.0, -1.0,
		1.0, -1.0, -1.0,

		// Top face
		-1.0, 1.0, -1.0,
		-1.0, 1.0, 1.0,
		1.0, 1.0, 1.0,
		1.0, 1.0, -1.0,

		// Bottom face
		-1.0, -1.0, -1.0,
		1.0, -1.0, -1.0,
		1.0, -1.0, 1.0,
		-1.0, -1.0, 1.0,

		// Right face
		1.0, -1.0, -1.0,
		1.0, 1.0, -1.0,
		1.0, 1.0, 1.0,
		1.0, -1.0, 1.0,

		// Left face
		-1.0, -1.0, -1.0,
		-1.0, -1.0, 1.0,
		-1.0, 1.0, 1.0,
		-1.0, 1.0, -1.0
	};

	int indices[] = {
		0, 1, 2, 0, 2, 3,    // front
		4, 5, 6, 4, 6, 7,    // back
		8, 9, 10, 8, 10, 11,   // top
		12, 13, 14, 12, 14, 15,   // bottom
		16, 17, 18, 16, 18, 19,   // right
		20, 21, 22, 20, 22, 23    // left
	};

	float texCoords[] = {
		0, 0,
		0, 1,
		1, 1,
		1, 0,
		0, 0,
		0, 1,
		1, 1,
		1, 0,
		0, 0,
		0, 1,
		1, 1,
		1, 0,
		0, 0,
		0, 1,
		1, 1,
		1, 0,
		0, 0,
		0, 1,
		1, 1,
		1, 0,
		0, 0,
		0, 1,
		1, 1,
		1, 0
	};

	//RawModel model = loader.LoadToVAO(vertices, sizeof(vertices) / sizeof(vertices[0]));
	RawModel model = loader.LoadToVAO(vertices, indices, texCoords,
		sizeof(vertices) / sizeof(vertices[0]),
		sizeof(indices) / sizeof(indices[0]),
		sizeof(texCoords) / sizeof(texCoords[0]));

	ModelTexture texture(loader.LoadTexture("image"));
	TexturedModel texturedModel(model, texture);

	Entity entity(texturedModel,
		glm::vec3(0, 0, -5), glm::vec3(0, 0, 0), glm::vec3(1, 1, 1)
		);

	Camera camera;

	float x = -0.001f;
	//start the game loop
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	while (m_displayManager->IsWindowOpen()) {

		//Rotate Cube
		entity.ChangeRotation(glm::vec3(0.001f, 0.001f, 0.001f));
		camera.Move();
		renderer.Prepare();
		staticShader.Use();
		staticShader.LoadViewMatrix(camera);

		renderer.Render(entity, staticShader);
		staticShader.UnUse();

		m_displayManager->UpdateDisplay();
	}
}