bool ccMaterialSet::addMaterial(const ccMaterial& mat) { if (findMaterial(mat.name) >= 0) return false; try { push_back(mat); } catch(std::bad_alloc) { //not enough memory return false; } return true; }
bool SceneLoader::aplicaMaterials(Object * o, Material * mat) { Material * mat2; if(o->mat_id=="null") o->mat=mat; else { mat2=findMaterial(o->mat_id); if(mat2==NULL) return false; o->mat=mat2; } if(o->type=="compound") { for(unsigned int i=0; i<o->getObjs()->size(); i++) { if(!aplicaMaterials(o->getObjs()->at(i), o->mat)) return false; } } return true; }
bool SceneLoader::loadScene() { sgxElement = doc.FirstChildElement( "sgx" ); globalsElement = sgxElement->FirstChildElement( "globals" ); viewElement = sgxElement->FirstChildElement( "view" ); illuminationElement = sgxElement->FirstChildElement("illumination"); texturesElement = sgxElement->FirstChildElement("textures"); objectsElement = sgxElement->FirstChildElement("objects"); materialsElement = sgxElement->FirstChildElement("materials"); Material * mat; Texture * tex; // Inicialização // Um exemplo de um conjunto de nós bem conhecidos e obrigatórios if(sgxElement == NULL) { cout << "Bloco sgx nao encontrado\n"; system("pause"); return false; } if(globalsElement != NULL) { if(!loadGlobals()) return false; } else { cout<<"Bloco globals nao encontrado\n"; system("pause"); return false; } if (viewElement != NULL) { if(!loadView()) return false; } else { cout << "Bloco view nao encontrado\n"; system("pause"); return false; } if(illuminationElement != NULL) { if(!loadIllumination()) return false; } else { cout<<"Bloco illumination nao econtrado\n"; system("pause"); return false; } if(texturesElement != NULL) { if(!loadTextures()) return false; } else { cout<<"Bloco textures nao econtrado\n"; system("pause"); return false; } if(materialsElement != NULL) { if(!loadMaterials()) return false; } else { cout<<"Bloco materials nao econtrado\n"; system("pause"); return false; } if(objectsElement!=NULL) { if(!loadObjects()) return false; if(!loadCompound()) return false; root_object=findObject(global.root); if(root_object==NULL) return false; if(root_object->mat_id=="null") mat=this->mat_base; else { mat=findMaterial(root_object->mat_id); if(mat==NULL) return false; } if(!aplicaMaterials(root_object, mat)) return false; if(root_object->tex_id=="null"||root_object->tex_id=="clear") tex=this->no_tex; else { tex=findTexture(root_object->tex_id); if(tex==NULL) return false; } if(!aplicaTextures(root_object, tex)) return false; } else { cout<<"Bloco objects nao econtrado\n"; system("pause"); return false; } return true; }
zenic::ModelData* ExporterBackend::buildModelData(CSLModel* model, Model* modelNode) { ModelData* target = zenic_new ModelData; CSLXSIMesh* mesh = static_cast<CSLXSIMesh*>(model->Primitive()); CSLXSIShape* shape = mesh->XSIShape(); DegenerateMesh degMesh; degMesh.process(mesh); TriangleStrip triangleStripper(TriangleStrip::Actc); const TriangleStrip::StripInfo& stripInfo = triangleStripper.process(°Mesh); if (stripInfo.remapTable == 0) { ZENIC_ERROR("Failed to trianglestrip " << mesh->GetName() << " mesh will be skipped"); zenic_delete target; return 0; } // Set the correct material to the modelData if (model->GlobalMaterial()) target->setMaterial((opengl::Material*)findMaterial(model->GlobalMaterial()->GetMaterial())); SkinInfo* skinInfo = 0; if (model->GetEnvelopeCount() != 0) { skinInfo = buildSkinInfo(model, modelNode, stripInfo.remapTable); target->setType(ModelData::Skinned); } static float colors[] = { 0, 1.0f, 1.0f, 0, 1.0f, 0, 1.0f, 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 0, 0 }; target->polygons().allocate(u32(stripInfo.stripList.size())); ModelData::PolygonList* polygonLists = target->polygons().objects(); const std::vector<DegenerateMesh::Vertex>& vertices = degMesh.vertexList(); uint d = 0; for (std::vector<DataPtr<u32>*>::const_iterator i = stripInfo.stripList.begin(); i != stripInfo.stripList.end(); ++i, ++d) { u32 colorSelection = (d & 3) << 2; DataPtr<u32>* triStripList = (*i); // process strip u32* stripList = triStripList->objects(); u32 stripCount = triStripList->count(); ModelData::PolygonList& polygonList = polygonLists[d]; polygonList.setPolygonType(ModelData::TriStrips); if (skinInfo) polygonList.setVertexFormat(ModelData::Postion | ModelData::Color | ModelData::Weight); else polygonList.setVertexFormat(ModelData::Postion | ModelData::Color | ModelData::Uv1); if (skinInfo) polygonList.vertexStream().allocate((stripCount * 14)); // coords and colors else polygonList.vertexStream().allocate((stripCount * 8)); // 3 coords, 3 colors, 2 uv f32* tempStream = polygonList.vertexStream().objects(); for (uint d = 0; d < stripCount; ++d) { const DegenerateMesh::Vertex& vertex = vertices[stripList[d]]; *tempStream++ = vertex.position.x; *tempStream++ = vertex.position.y; *tempStream++ = vertex.position.z; *tempStream++ = colors[colorSelection + 0]; *tempStream++ = colors[colorSelection + 1]; *tempStream++ = colors[colorSelection + 2]; *tempStream++ = vertex.uv.x; *tempStream++ = vertex.uv.y; if (skinInfo) { const SkinInfo& currInfo = skinInfo[stripList[d]]; for (uint d = 0; d < 4; ++d) { *tempStream++ = float(currInfo.indices[d]); *tempStream++ = float(currInfo.weights[d]); } } } } delete [] skinInfo; return target; }
void OBJFileReader::parseLine(const char*& current, const char* endOfLine) { if(matchCommand(current, "v")) positions.push_back(parseVec3(current)); else if(matchCommand(current, "vn")) normals.push_back(parseVec3(current)); else if(matchCommand(current, "vt")) texcoords.push_back(parseVec2(current)); else if(matchCommand(current, "f")) // face { int count = 0; while(!errThisLine && *current) { VertInfo nfo; nfo.pos = parseIndex(current, positions.size()); if(*current == '/') nfo.tex = parseIndex(++current, texcoords.size()); else nfo.tex = -1; if(*current == '/') nfo.norm = parseIndex(++current, normals.size()); else nfo.norm = -1; if(*current && !isspace(*current)) diagnostic(false, current, "unexpected character after vertex indices"); vertices.push_back(nfo); count++; } if(count < 3) diagnostic(false, current, "need at least 3 vertices for a face"); else if(count > 4) diagnostic(true, current, "non-tri/quad face"); FaceInfo nfo; nfo.start = vertices.size() - count; nfo.count = count; nfo.mtrl = currentMaterial; nfo.smoothing_group = currentSmoothingGroup; faces.push_back(nfo); } else if(matchCommand(current, "g")) // group { current = endOfLine; } else if(matchCommand(current, "o")) // object { current = endOfLine; } else if(matchCommand(current, "s")) // smooth shading { if(matchCommand(current, "off")) currentSmoothingGroup = -1; else currentSmoothingGroup = parseInt(current) - 1; } else if(matchCommand(current, "usemtl")) { currentMaterial = findMaterial(current); current = endOfLine; } else if(matchCommand(current, "mtllib")) { MTLFileReader mtlreader; if(!mtlreader.read((basepath + current).c_str())) diagnostic(false, current, "error reading material library"); else { materials.swap(mtlreader.materials); current = endOfLine; defaultMaterial = -1; defaultMaterial = findMaterial("default"); sVERIFY(defaultMaterial != -1); currentMaterial = defaultMaterial; } } else diagnostic(false, current, "unknown command"); }