#include "stdafx.h"
#include "ObjLoader.h"

#include <LibQtGeoViewerCore/SceneMain.h>

#include "../FileUtil.h"

#include "ObjMesh.h"

#include <QFileInfo>

#include <LibTest/DbgTimer.h>



GeomObject* ObjLoader::LoadGeom(SceneMain& scene, const std::string& filename)
{
	lib_geo::ObjMesh obj_mesh;
	if (!obj_mesh.Load(filename))
		return NULL;

	GeomObject* geom = new GeomObject();

	ObjSplitMode splitMode = ObjSplitMode::None;
	if (scene.m_IOConfig.ObjSplit)
		splitMode = obj_mesh.GetSeparateMode();

	if (splitMode == ObjSplitMode::None)
	{
		MeshBuf* mbuf = geom->CreateNewMeshBuf();

		obj_mesh.ConvertToBaseMesh(mbuf->m_Mesh);

		if (scene.m_IOConfig.ObjMergeLine)
			mbuf->m_Mesh.MergeLinkedPolylines();

		if (!mbuf->m_Mesh.HasNormal())
			mbuf->m_Mesh.CreateNormalsEachVerts(true);

		InitTextureFromObjMeshBuf(scene, *mbuf, obj_mesh);
	}
	else
	{
		std::vector<lib_geo::BaseMesh> mesh_ary;
		obj_mesh.ConvertToBaseMeshSeparated(splitMode, mesh_ary);
		if (mesh_ary.empty())
			return geom;

		for (size_t i = 0; i < mesh_ary.size(); ++i)
		{
			MeshBuf* mbuf = geom->CreateNewMeshBuf();

			mbuf->m_Mesh.CopySwap(mesh_ary[i]);

			if (!mbuf->m_Mesh.HasNormal())
				mbuf->m_Mesh.CreateNormalsEachVerts(true);

			InitTextureFromObjMeshBuf(scene, *mbuf, obj_mesh);

			std::string object_name;
			if (splitMode == ObjSplitMode::Object)
				object_name = obj_mesh.m_Objects[i].m_Name;
			else if (splitMode == ObjSplitMode::Group)
				object_name = obj_mesh.m_Groups[i].m_Name;

			if (object_name.empty())
				mbuf->m_Name = FileUtil::GetFileTitle(filename);
			else
				mbuf->m_Name = object_name;
		}
	}

	geom->m_Name       = FileUtil::GetFileTitle(filename);
	geom->m_FileFormat = GeomFileFormat::Obj;
	geom->m_FilePath   = filename;

	geom->InitializeBufferCommon();

	return geom;
}

void ObjLoader::InitTextureFromObjMeshBuf(SceneMain& scene, geom::MeshBuf& mbuf, const lib_geo::ObjMesh& obj_mesh)
{
	// テクスチャの設定
	if (obj_mesh.m_MaterialGroups.empty())
		return;

	// 複数 material group には未対応
	const lib_geo::ObjMaterialGroup& mat_grp = obj_mesh.m_MaterialGroups[0];

	mbuf.CreateTextureBuf(mat_grp.m_Materials.size());
	for (size_t i = 0; i < mat_grp.m_Materials.size() ; ++i)
	{
		const lib_geo::ObjMaterial& mat_source = mat_grp.m_Materials[i];

		const std::string tex_col = mat_source.GetTextureFilePath();
		const std::string name_col = GetTextureNameFromFilePath(tex_col);
		mbuf.InitColorTexture(i, tex_col, name_col, scene.m_TexConfig);

		const std::string tex_n = mat_source.GetNormalMapFilePath();
		const std::string name_norm = GetTextureNameFromFilePath(tex_n);
		mbuf.InitNormalTexture(i, tex_n, name_norm, scene.m_TexConfig);
	}
}

std::string ObjLoader::GetTextureNameFromFilePath(const std::string& filepath) const
{
	QString path_qs = QString::fromLocal8Bit(filepath.c_str());
	QString name_qs = QFileInfo(path_qs).fileName();
	return name_qs.toLocal8Bit().data();
}
