📄 objimporter.cpp
字号:
posArray[faceCount * 9 + i * 3 + 1] = pos[j].y(); posArray[faceCount * 9 + i * 3 + 2] = pos[j].z(); // cache this vertex index indeces->Cache(faceCount * 3 + i); if (tex.size() != 0) { // indices in the OBJ file start with 1 so subtract 1 here j = faceIter->vertex[i].vt - 1; // for every face, we have 9 values (3 x <x,y,z>) texArray[faceCount * 9 + i * 3] = tex[j].x(); texArray[faceCount * 9 + i * 3 + 1] = tex[j].y(); texArray[faceCount * 9 + i * 3 + 2] = tex[j].z(); } // indices in the OBJ file start with 1 so subtract 1 here j = faceIter->vertex[i].vn - 1; // for every face, we have 9 values (3 x <x,y,z>) normArray[faceCount * 9 + i * 3] = norm[j].x(); normArray[faceCount * 9 + i * 3 + 1] = norm[j].y(); normArray[faceCount * 9 + i * 3 + 2] = norm[j].z(); } // increase face counter ++faceCount; } // add face with all the indices for the current material triMesh->AddFace(indeces, matIter->first); } triMesh->SetPos(posArray, faceCount * 3); if (tex.size() != 0) { triMesh->SetTexCoords(texArray); } triMesh->SetNormals(normArray); GetLog()->Normal() << "(ObjImporter) Done importing .obj file " << name << "\n"; return triMesh;}void ObjImporter::ExplodeString(string & input, vector<string> & output, string & del){ size_t pos1 = 0; size_t pos2 = 0; while ((pos2 = input.find(del, pos1)) != string::npos) { output.push_back(input.substr(pos1, pos2 - pos1)); pos1 = pos2 + 1; //pos2 = input.find(del, pos1); } output.push_back(input.substr(pos1, input.length() - pos1));}bool ObjImporter::SetupMaterials(string & matLibName, TObjMatValueVector & matVector){ // open file string fileName; if (! GetFile()->LocateResource("materials/" + matLibName, fileName)) { GetLog()->Error() << "(ObjImporter) WARNING: cannot locate file '" << matLibName << "'\n"; return false; } ifstream ifs; ifs.open(fileName.c_str()); if (! ifs) { GetLog()->Error() << "(ObjImporter) WARNING: cannot open file '" << fileName << "'\n"; return false; } // buffers to hold lines from the file string buffer, templine; // skip first 2 lines (comment) getline(ifs, buffer); getline(ifs, buffer); string matName, texName; float x1,x2,x3; ObjMatValues* currentMat = 0; while(! ifs.eof()) { getline(ifs, templine); stringstream str(templine); str >> buffer; if (buffer == "newmtl") { // whenever we find the "newmtl" keyword, we create a // new struct and store it in the vector; we fill in // the values as we read the following lines str >> matName; ObjMatValues temp; temp.name = matName; matVector.push_back(temp); currentMat = &(matVector.back()); } if (currentMat == 0) { continue; } if (buffer == "Ka") { str >> x1 >> x2 >> x3; currentMat->ambient[0] = x1; currentMat->ambient[1] = x2; currentMat->ambient[2] = x3; } else if (buffer == "Kd") { str >> x1 >> x2 >> x3; currentMat->diffuse[0] = x1; currentMat->diffuse[1] = x2; currentMat->diffuse[2] = x3; } else if (buffer == "Ks") { str >> x1 >> x2 >> x3; currentMat->specular[0] = x1; currentMat->specular[1] = x2; currentMat->specular[2] = x3; } else if (buffer == "Ns") { str >> x1; currentMat->shininess = x1; } else if (buffer == "d") { str >> x1; currentMat->alpha = x1; } else if (buffer == "map_Kd") { str >> texName; if (! GetFile()->LocateResource("textures/" + texName, currentMat->diffuseTexture)) { GetLog()->Error() << "(ObjImporter) ERROR: cannot locate texture file '" << "textures/" << texName << "'\n"; } } } GetLog()->Debug() << "(ObjImporter) read " << matVector.size() << " material definitions.\n"; // now we read all the materials values, so create the materials and // register them. // get reference to the MaterialServer shared_ptr<MaterialServer> materialServer = shared_dynamic_cast<MaterialServer> (GetCore()->Get("/sys/server/material")); if (materialServer.get() == 0) { GetLog()->Error() << "(ObjImporter) ERROR: cannot find MaterialServer\n"; return false; } RGBA rgba; shared_ptr<MaterialSolid> material; for ( TObjMatValueVector::iterator iter = matVector.begin(); iter != matVector.end(); ++iter ) { // decide whether we need to setup a Material2DTexture or whether // a MaterialSolid is sufficient (we only support diffuse textures for now) if (iter->diffuseTexture.size()) { material = shared_dynamic_cast<Material2DTexture> (GetCore()->New("/kerosin/Material2DTexture")); if (material.get() == 0) { GetLog()->Error() << "(ObjImporter) ERROR: could not create new Material2DTexture\n"; return false; } GetLog()->Debug() << "(ObjImporter) defining Material2DTexture " << iter->name << "\n"; //we only support diffuse textures for now (shared_dynamic_cast<Material2DTexture>(material))->SetDiffuseTexture(iter->diffuseTexture); } else { material = shared_dynamic_cast<MaterialSolid> (GetCore()->New("/kerosin/MaterialSolid")); if (material.get() == 0) { GetLog()->Error() << "(ObjImporter) ERROR: could not create new MaterialSolid\n"; return false; } GetLog()->Debug() << "(ObjImporter) defining MaterialSolid " << iter->name << "\n"; } material->SetName(iter->name); rgba = RGBA( iter->ambient[0], iter->ambient[1], iter->ambient[2], iter->alpha ); material->SetAmbient(rgba); rgba = RGBA( iter->diffuse[0], iter->diffuse[1], iter->diffuse[2], iter->alpha ); material->SetDiffuse(rgba); rgba = RGBA( iter->specular[0], iter->specular[1], iter->specular[2], iter->alpha ); material->SetSpecular(rgba); material->SetShininess(iter->shininess); materialServer->RegisterMaterial(material); GetLog()->Debug() << "(ObjImporter) Material " << iter->name << " set up and successfully registered.\n"; } GetLog()->Debug() << "(ObjImporter) Material setup finished for material library " << matLibName << ".\n"; return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -