Exemple #1
0
ObjParser::ObjParser(const char* baseDir, const char* objFilename) {
	m_baseDir = (char*)malloc(sizeof(char) * (strlen(baseDir) + 1));
	strcpy(m_baseDir, baseDir);
	m_objFilename = (char*)malloc(sizeof(char) * (strlen(m_baseDir) + strlen(objFilename) + 1));
	strcpy(m_objFilename, m_baseDir);
	strcat(m_objFilename, objFilename);
	
	parseObj();
	parseMtl();
}
void
ObjModelLoader::firstPass()
{
	string lastType = "";

	fs::path my_path(mPriFName);
	fs::ifstream objFile;
	objFile.open(my_path, ios::in);

	char line[200];
	vector<string> tokens;
	while (objFile.getline(line, 200)) {
		tokens = splitSpace(string(line));
		if (tokens[0] == ("o")) {
			//++grpCount;
			lastType = "o";
		}
		else if (tokens[0] == ("g")) {
//			removeSpecialCharsFromName(tokens[1]);
			string longname("");
			for (unsigned int i = 1; i < tokens.size(); ++i) {
				longname.append(tokens[i]);
				longname.append("_");
			}
			longname.erase(longname.end() - 1);
			mPriModelPtr->addGrpPtr(new MetaGroup(longname));
			lastType = "g";
		}
		else if (tokens[0] == ("mtllib")) {
			string longname("");
			for (unsigned int i = 1; i < tokens.size(); ++i) {
				longname.append(tokens[i]);
				longname.append(" ");
			}
			longname.erase(longname.end() - 1);
			parseMtl(fs::path(my_path.parent_path() / longname));
			lastType = "mtllib";
		}
		else if (tokens[0] == ("v")) {
			V3f* v = new V3f(atof(tokens[1].c_str()), atof(tokens[2].c_str()),
					atof(tokens[3].c_str()));
			mPriModelPtr->addVPtr(v);
			lastType = "v";
		}
		else if (tokens[0] == ("vt")) {
			V3f* t = new V3f(atof(tokens[1].c_str()), atof(tokens[2].c_str()),
					atof(tokens[3].c_str()));
			mPriModelPtr->addTPtr(t);
			lastType = "vt";
		}
		else if (tokens[0] == ("vn")) {
			V3b *n = new V3b((char) 127.0f * atof(tokens[1].c_str()),
					(char) 127.0f * atof(tokens[2].c_str()), (char) 127.0f * atof(tokens[3].c_str()));
			mPriModelPtr->addNPtr(n);
			lastType = "vn";
		}
		else if (tokens[0] == ("f")) {
			lastType = "f";
		}

	}
	objFile.close();

	// if we have not read any groups in, create a default group
	if (mPriModelPtr->getGrpCount() < 1) {
		mPriModelPtr->addGrpPtr(new MetaGroup());
	}
}
Exemple #3
0
bool OBJParser::parseLine(YFile* file, const wchar_t* lineContent)
{
	Assert(NULL != file);
	Assert(NULL != lineContent);

	OBJ_SPECIFIER specifier = OBJ_OTEHR;
	getOBJSpecifier(lineContent, &specifier);

	switch(specifier)
	{
	case OBJ_COMMENT:
		break;
	case MTLLIB:
		{
			wchar_t pathStr[MAX_PATH_LEN];
			YString::Scan(lineContent, L"%*s %s", pathStr);

			wchar_t mtlFilePath[MAX_PATH_LEN];
			YString::GetParentDirPath(mtlFilePath, _countof(mtlFilePath), mFilePath);
			YString::Concat(mtlFilePath, _countof(mtlFilePath), L"/", pathStr);

			//YString::GetFullPath(mtlFilePath, _countof(mtlFilePath));
			parseMtl(mtlFilePath);

			break;
		}
	case VERT_POS:
		{
			wchar_t lineStrs[3][MAX_STR_LEN];
			YString::Scan(lineContent, L"%*c %s %s %s", &lineStrs[0], &lineStrs[1], &lineStrs[2]);
			mPosData.push_back(Vector3((float)_wtof(lineStrs[0]), (float)_wtof(lineStrs[1]), (float)_wtof(lineStrs[2])));
			break;
		}
	case VERT_UV:
		{
			wchar_t lineStrs[2][MAX_STR_LEN];
			YString::Scan(lineContent, L"%*s %s %s", &lineStrs[0], &lineStrs[1]);
			//uvData.push_back(Vector2(1.0f - (float)_wtof(lineStrs[0]), 1.0f - (float)_wtof(lineStrs[1])));
			uvData.push_back(Vector2((float)_wtof(lineStrs[0]), 1.0f - (float)_wtof(lineStrs[1])));
			break;
		}
	case VERT_NORMAL: 
		{
			wchar_t lineStrs[3][MAX_STR_LEN];
			YString::Scan(lineContent, L"%*c %s %s %s", &lineStrs[0], &lineStrs[1], &lineStrs[2]);
			normalData.push_back(Vector3((float)_wtof(lineStrs[0]), (float)_wtof(lineStrs[1]), (float)_wtof(lineStrs[2])));
			break;
		}
	case GROUP:
		{
			break;
		}
	case MESH_MTL:
		{
			determineDataContentType();

			std::vector<std::wstring> blockContent;
			blockContent.push_back(lineContent);

			wchar_t blockLineContent[MAX_STR_LEN];
			while(file->ReadLine(blockLineContent, _countof(blockLineContent)) != NULL)
			{
				OBJ_SPECIFIER specifier = OBJ_OTEHR;
				getOBJSpecifier(blockLineContent, &specifier);

				if(specifier == MESH_MTL || specifier == VERT_POS || specifier == VERT_UV || specifier == VERT_NORMAL)
					break;

				blockContent.push_back(blockLineContent);
			}

			parseTrianglesBlock(blockContent);

			if(!file->ReachEnd())
				parseLine(file, blockLineContent);

			break;
		}
	case OBJ_OTEHR:
		{
			break;
		}
	default:
		_Assert(false);
	}

Exit:
	return false;
}