Ejemplo n.º 1
0
void ComTerrain::LoadMap()
{
	vector<Vector3> vecP;
	vector<DWORD> vecAttbuf;
	CString mtlName;
	char token[TOKEN_SIZE];

	std::ifstream fin;

	fin.open(m_filePath + m_mapFilename);

	if (fin.is_open() == false)
		assert(false && "fin.is_open() == false");

	float fPosXMax = 0.0f;
	float fPosZMax = 0.0f;

	while (fin.eof() == false)
	{
		fin >> token;

		if (CompareStr(token, "mtllib"))
		{
			fin >> token;
			LoadMtlLib(m_filePath + token);
		}
		else if (CompareStr(token, "v"))
Ejemplo n.º 2
0
void cObjLoader::Load(std::vector<cGroup*>& vecGroup, std::string& sFolder, std::string& sFileName)
{
	m_mapMtlTex.clear();

	FILE* fp;
	fopen_s(&fp, (sFolder + sFileName).c_str(), "r");

	std::vector<D3DXVECTOR3> vecV;
	std::vector<D3DXVECTOR2> vecVT;
	std::vector<D3DXVECTOR3> vecVN;
	std::string sMtlName;
	std::vector<ST_PNT_VERTEX> vecVertex;

	while (!feof(fp))
	{
		char szLineBuf[1024] = { 0, };
		fgets(szLineBuf, 1024, fp);
		//OutputDebugString(szLineBuf);
		if (strlen(szLineBuf) == 0)
			continue;
		if (szLineBuf[0] == '#')
			continue;
		else if (szLineBuf[0] == 'm')
		{
			char szMtlLibFile[1024];
			sscanf(szLineBuf, "%*s %s", szMtlLibFile);
			LoadMtlLib(sFolder, std::string(szMtlLibFile));
		}
		else if (szLineBuf[0] == 'g')
		{
			if (!vecVertex.empty())
			{
				cGroup* pGroup = new cGroup;
				pGroup->m_vecVertex = vecVertex;
				pGroup->SetMtlTex(m_mapMtlTex[sMtlName]);
				vecGroup.push_back(pGroup);
				vecVertex.clear();
			}
		}
		else if (szLineBuf[0] == 'v')
		{
			if (szLineBuf[1] == ' ')
			{
				float x, y, z;
				sscanf(szLineBuf, "%*s %f %f %f", &x, &y, &z);
				vecV.push_back(D3DXVECTOR3(x, y, z));
			}
			else if (szLineBuf[1] == 't')
			{
				float u, v;
				sscanf(szLineBuf, "%*s %f %f %*f", &u, &v);
				vecVT.push_back(D3DXVECTOR2(u, v));
			}
			else if (szLineBuf[1] == 'n')
			{
				float x, y, z;
				sscanf(szLineBuf, "%*s %f %f %f", &x, &y, &z);
				vecVN.push_back(D3DXVECTOR3(x, y, z));
			}
		}
		else if (szLineBuf[0] == 'u')
		{
			char szMtlName[1024];
			sscanf(szLineBuf, "%*s %s", szMtlName);
			sMtlName = std::string(szMtlName);
		}
		else if (szLineBuf[0] == 'f')
		{
			int aIndex[3][3];
			sscanf(szLineBuf, "%*s %d/%d/%d %d/%d/%d %d/%d/%d",
				&aIndex[0][0], &aIndex[0][1], &aIndex[0][2],
				&aIndex[1][0], &aIndex[1][1], &aIndex[1][2],
				&aIndex[2][0], &aIndex[2][1], &aIndex[2][2]);
			for (int i = 0; i < 3; ++i)
			{
				ST_PNT_VERTEX v;
				v.p = vecV[aIndex[i][0] - 1];
				v.t = vecVT[aIndex[i][1] - 1];
				v.n = vecVN[aIndex[i][2] - 1];
				vecVertex.push_back(v);
			}
		}
	}

	for each(auto p in m_mapMtlTex)
	{
		SAFE_RELEASE(p.second);
	}
Ejemplo n.º 3
0
cGroup* cObjLoader::Load( char* szFullPath )
{
	std::vector<D3DXVECTOR3>	vecV;
	std::vector<D3DXVECTOR2>	vecVT;
	std::vector<D3DXVECTOR3>	vecVN;
	std::vector<ST_PNT_VERTEX>	vecVertex;
	std::string					sMtlName;
	

	FILE* fp = NULL;
	
	fopen_s(&fp, szFullPath, "r");

	while(true)
	{
		char szBuf[1024];
		fgets(szBuf, 1024, fp);
		
		if(szBuf[0] == '#')
		{
			continue;
		}
		else if(szBuf[0] == 'm')
		{
			char szMtlPath[1024] = {0, };
			sscanf_s(szBuf, "%*s %s", szMtlPath, sizeof(szMtlPath));
			LoadMtlLib(szMtlPath);
		}
		else if(szBuf[0] == 'g')
		{
			if(!vecVertex.empty())
			{
				cGroup* pGroup = new cGroup;
				pGroup->BuildVertexBuffer(vecVertex);
				vecVertex.clear();
				if(m_pHead)
				{
					pGroup->m_pNext = m_pHead;
				}
				pGroup->SetMtlTex(m_mapMtlTex[sMtlName]);
				m_pHead = pGroup;
			}
		}
		else if(szBuf[0] == 'v')
		{
			if(szBuf[1] == ' ')
			{
				float x, y, z;
				sscanf_s(szBuf, "%*s %f %f %f", &x, &y, &z);
				vecV.push_back(D3DXVECTOR3(x, y, z));
			}
			else if(szBuf[1] == 't')
			{
				float u, v;
				sscanf_s(szBuf, "%*s %f %f %*f", &u, &v);
				vecVT.push_back(D3DXVECTOR2(u, v));
			}
			else if(szBuf[1] == 'n')
			{
				float x, y, z;
				sscanf_s(szBuf, "%*s %f %f %f", &x, &y, &z);
				vecVN.push_back(D3DXVECTOR3(x, y, z));
			}
		}
		else if(szBuf[0] == 'f')
		{
			int idx[3][3];
			sscanf_s(szBuf, "%*s %d/%d/%d %d/%d/%d %d/%d/%d",
				&idx[0][0], &idx[0][1], &idx[0][2],
				&idx[1][0], &idx[1][1], &idx[1][2],
				&idx[2][0], &idx[2][1], &idx[2][2]);
			for (int i = 0; i < 3; ++i)
			{
				ST_PNT_VERTEX v;
				v.p = vecV[idx[i][0] - 1];
				v.t = vecVT[idx[i][1] - 1];
				v.n = vecVN[idx[i][2] - 1];
				vecVertex.push_back(v);
			}
		}
		else if(szBuf[0] == 'u')
		{
			char szMtlName[1024] = {0, };
			sscanf_s(szBuf, "%*s %s", szMtlName, sizeof(szMtlName));
			sMtlName = std::string(szMtlName);
		}
		if(feof(fp))
			break;
	}
	
	fclose(fp);

	for each(auto it in m_mapMtlTex)
	{
		SAFE_RELEASE(it.second);
	}