void MeshEntry::LoadMesh(FbxMesh * mesh, QVector<quint32> &master_indices, QVector<float> &master_vertices, QVector<float> &master_normals, QVector<float> &master_uvs, QVector<float> &master_tangents, bool load_normals, bool load_uvs, bool load_tangents, qint32 & current_control_point_offset, qint32 & current_polygon_offset) { /** *Load vertices and indices of the mesh entry */ LoadIndices(mesh, master_indices, current_polygon_offset, current_control_point_offset); LoadVertices(mesh, master_vertices, current_control_point_offset); /** *Load the entry's layers */ if (load_normals) LoadNormals(mesh, master_normals); if (load_uvs) LoadUVs(mesh, master_uvs); if (load_tangents) LoadTangents(mesh, master_tangents); /** *Compute the transform and bounding box and load them */ LoadTransform(mesh); LoadBoundingBox(mesh); }
TextureMap* ReadTexture(TiXmlElement *element) { const char* texName = element->Attribute("texture"); if ( texName == NULL ) return NULL; Texture *tex = NULL; if ( COMPARE(texName,"checkerboard") ) { TextureChecker *ctex = new TextureChecker; tex = ctex; printf(" Texture: Checker Board\n"); for ( TiXmlElement *child = element->FirstChildElement(); child!=NULL; child = child->NextSiblingElement() ) { if ( COMPARE( child->Value(), "color1" ) ) { Color c(0,0,0); ReadColor( child, c ); ctex->SetColor1(c); printf(" color1 %f %f %f\n",c.r,c.g,c.b); } else if ( COMPARE( child->Value(), "color2" ) ) { Color c(0,0,0); ReadColor( child, c ); ctex->SetColor2(c); printf(" color2 %f %f %f\n",c.r,c.g,c.b); } } textureList.Append( tex, texName ); } else { printf(" Texture: File \"%s\"",texName); tex = textureList.Find( texName ); if ( tex == NULL ) { TextureFile *ftex = new TextureFile; tex = ftex; ftex->SetName(texName); if ( ! ftex->Load() ) { printf(" -- Error loading file!"); delete tex; tex = NULL; } else { textureList.Append( tex, texName ); } } printf("\n"); } TextureMap *map = new TextureMap(tex); LoadTransform(map,element,1); return map; }
void LoadNode(Node *parent, TiXmlElement *element, int level) { Node *node = new Node; parent->AppendChild(node); // name const char* name = element->Attribute("name"); node->SetName(name); PrintIndent(level); printf("object ["); if ( name ) printf("%s",name); printf("]"); // type const char* type = element->Attribute("type"); if ( type ) { if ( COMPARE(type,"sphere") ) { node->SetObject( &theSphere ); printf(" - Sphere"); } else { printf(" - UNKNOWN TYPE"); } } // type const char* mtlName = element->Attribute("material"); if ( mtlName ) { printf(" <%s>", mtlName); NodeMtl nm; nm.node = node; nm.mtlName = mtlName; nodeMtlList.push_back(nm); } printf("\n"); for ( TiXmlElement *child = element->FirstChildElement(); child!=NULL; child = child->NextSiblingElement() ) { if ( COMPARE( child->Value(), "object" ) ) { LoadNode(node,child,level+1); } } LoadTransform( node, element, level ); }
bool LoadLight(SECore::Scene* scene, const Json::Value& lightRoot) { const char* type = lightRoot["Type"].asCString(); SECore::Light* light = nullptr; if (strcmp(type, "Point") == 0) { light = scene->AddPointLight(); } else if (strcmp(type, "Directional") == 0) { light = scene->AddDirectionalLight(); } if (lightRoot.isMember("Enable")) { light->Enable(lightRoot["Enable"].asBool()); } if (lightRoot.isMember("Name")) { light->SetName(lightRoot["Name"].asCString()); } if (lightRoot.isMember("Transform")) LoadTransform(light->GetTransform(), lightRoot["Transform"]); if (lightRoot.isMember("Color")) light->SetColor(Json2Color(lightRoot["Color"])); if (lightRoot.isMember("Intensity")) light->SetIntensity((float)(lightRoot["Intensity"].asDouble())); return true; }
Geometry* XMLReader::LoadGeometry(QXmlStreamReader &xml_reader, QMap<QString, QList<Geometry*>> &map, const QStringRef &local_path) { Geometry* result = NULL; //First check what type of geometry we're supposed to load QXmlStreamAttributes attribs(xml_reader.attributes()); QStringRef type = attribs.value(QString(), QString("type")); bool is_mesh = false; if(QStringRef::compare(type, QString("obj")) == 0) { result = new Mesh(); is_mesh = true; } else if(QStringRef::compare(type, QString("sphere")) == 0) { result = new Sphere(); } else if(QStringRef::compare(type, QString("square")) == 0) { result = new SquarePlane(); } else if(QStringRef::compare(type, QString("cube")) == 0) { result = new Cube(); } else { std::cout << "Could not parse the geometry!" << std::endl; return NULL; } QStringRef name = attribs.value(QString(), QString("name")); result->name = name.toString(); while(!xml_reader.isEndElement() || QStringRef::compare(xml_reader.name(), QString("geometry")) != 0) { xml_reader.readNext();// xml_reader.readNext(); QString tag(xml_reader.name().toString()); if(is_mesh && QString::compare(tag, QString("filename")) == 0) { xml_reader.readNext(); if(xml_reader.isCharacters()) { ((Mesh*)result)->LoadOBJ(xml_reader.text(), local_path); } xml_reader.readNext(); } else if(QString::compare(tag, QString("transform")) == 0) { result->transform = LoadTransform(xml_reader); xml_reader.readNext(); } else if(QString::compare(tag, QString("material")) == 0) { //Add the geometry to the map of material names to geometries so that we can assign it a material later xml_reader.readNext(); if(xml_reader.isCharacters()) { QString material_name = xml_reader.text().toString(); QList<Geometry*> list = map.value(material_name); list.append(result); map.insert(material_name, list); xml_reader.readNext(); } // attribs = QXmlStreamAttributes(xml_reader.attributes()); // QString name = attribs.value(QString(), QString("value")).toString(); // QList<Geometry*> list = map.value(name); // list.append(result); // map.insert(name, list); // xml_reader.readNext(); } } return result; }
bool LoadEntity(SECore::Core* core, SECore::Scene::Entity* entity, const Json::Value& entityRoot) { bool ret = false; if (entityRoot.isMember("Name")) { entity->SetName(entityRoot["Name"].asCString()); } if (entityRoot.isMember("Transform")) LoadTransform(entity->GetTransform(), entityRoot["Transform"]); if (entityRoot.isMember("Animation")) { const Json::Value& animationRoot = entityRoot["Animation"]; SECore::Animation* animation = entity->GetAnimation(); if (!animation) animation = entity->CreateAnimation(); SECore::Skeleton* skeleton = core->LoadSkeleton(animationRoot["Skeleton"].asCString()); animation->SetSkeleton(skeleton); skeleton->Release(); if (animationRoot.isMember("SavedBones")) { const Json::Value& savedBonesRoot = animationRoot["SavedBones"]; size_t count = savedBonesRoot.size(); for (size_t i = 0; i < count; i++) { animation->AddSavedBoneTM(savedBonesRoot[i].asCString()); } } const Json::Value& jsonClips = animationRoot["Clips"]; std::vector<std::string> memberNames = jsonClips.getMemberNames(); for (size_t i = 0; i < memberNames.size(); ++i) { SECore::AnimationClip* clip = core->LoadClip(jsonClips[memberNames[i]].asCString()); clip->SetName(memberNames[i].c_str()); animation->AddClip(clip); clip->Release(); } } if (entityRoot.isMember("Renderer")) { const Json::Value& rendererRoot = entityRoot["Renderer"]; CHECK(rendererRoot.isArray()); size_t count = rendererRoot.size(); SECore::Renderer* renderer = entity->GetRenderer(); if (!renderer) { renderer = entity->CreateRenderer(); } for (size_t i = 0; i < count; ++i) { SECore::Renderer::Entity* re = renderer->CreateEntity(); const Json::Value& reRoot = rendererRoot[i]; if (reRoot.isMember("CastShadow")) { re->SetCastShadow(reRoot["CastShadow"].asBool()); } if (reRoot.isMember("Mesh")) { SECore::Mesh* mesh = core->LoadMesh(reRoot["Mesh"].asCString()); re->SetMesh(mesh); mesh->Release(); } if (reRoot.isMember("Material")) { SECore::Material* material = re->GetMaterial(); const Json::Value& mtlRoot = reRoot["Material"]; SECore::Shader* shader = nullptr; if (mtlRoot.isMember("Shader")) { shader = core->CreateShader(mtlRoot["Shader"].asCString()); } else { shader = core->CreateShader("AlphaTest-Diffuse"); } CHECK(shader); material->SetShader(shader); std::vector<std::string> propNames = mtlRoot.getMemberNames(); size_t propCount = propNames.size(); for (size_t pi = 0; pi < propCount; pi++) { const std::string& propName = propNames[pi]; SECore::Shader::PropertyType propType = shader->GetPropertyType(propName.c_str()); switch (propType) { case SECore::Shader::eFloat: { material->SetFloat(propName.c_str(), (float)mtlRoot[propName].asDouble()); } break; case SECore::Shader::eColor: { const char* str = mtlRoot[propName].asCString(); DWORD dwColor = strtoul(str, NULL, 16); Color color = ConvertDwordToColor(dwColor); material->SetColor(propName.c_str(), color); } break; case SECore::Shader::eTexture: { SECore::Texture* texture = core->LoadTexture(mtlRoot[propName].asCString()); material->SetTexture(propName.c_str(), texture); texture->Release(); } break; default: CHECK(false); break; } } } } entity->CalcBound(); } if (entityRoot.isMember("Collider")) { const Json::Value& colliderRoot = entityRoot["Collider"]; bool isDynamic = false; if (colliderRoot.isMember("Dynamic")) { isDynamic = colliderRoot["Dynamic"].asBool(); } SECore::Collider* collider = nullptr; if (strcmp(colliderRoot["Shape"].asCString(), "Box") == 0) { Vector3 center, size; SECore::Bound bound = entity->GetBound(); bound.max = bound.max * entity->GetTransform().scaling; bound.min = bound.min * entity->GetTransform().scaling; size = (bound.max - bound.min) / 2; center = bound.min + size; if (colliderRoot.isMember("Size")) size = Json2Vec3(colliderRoot["Size"]); if (colliderRoot.isMember("Offset")) size = Json2Vec3(colliderRoot["Offset"]); collider = entity->CreateBoxCollider(isDynamic, size); collider->SetLocalPose(center, Quat()); } if (isDynamic) { SECore::RigidBody* rigidBody = collider->GetRigidBody(); CHECK(rigidBody); bool enableGravity = false; if (colliderRoot.isMember("Gravity")) { enableGravity = colliderRoot["Gravity"].asBool(); } rigidBody->EnableGravity(enableGravity); if (colliderRoot.isMember("Mass")) { rigidBody->SetMass((float)colliderRoot["Mass"].asDouble()); } } } if (entityRoot.isMember("CharacterController")) { const Json::Value& cctRoot = entityRoot["CharacterController"]; Vector3 offset, size; float height, radius; SECore::Bound bound = entity->GetBound(); bound.max = bound.max * entity->GetTransform().scaling; bound.min = bound.min * entity->GetTransform().scaling; size = (bound.max - bound.min) / 2; offset = bound.min + size; height = size.y; radius = (size.x + size.z) / 2; if (cctRoot.isMember("Height")) height = (float)cctRoot["Height"].asDouble(); if (cctRoot.isMember("Radius")) radius = (float)cctRoot["Radius"].asDouble(); if (cctRoot.isMember("Offset")) offset = Json2Vec3(cctRoot["Offset"]); entity->CreateCCT(height, radius, offset); } ret = true; Exit0: return ret; }
void LoadNode(Node *parent, TiXmlElement *element, int level) { Node *node = new Node; parent->AppendChild(node); // name const char* name = element->Attribute("name"); node->SetName(name); PrintIndent(level); printf("object ["); if ( name ) printf("%s",name); printf("]"); // type const char* type = element->Attribute("type"); if ( type ) { if ( COMPARE(type,"sphere") ) { node->SetObject( &theSphere ); printf(" - Sphere"); } else if ( COMPARE(type,"plane") ) { node->SetObject( &thePlane ); printf(" - Plane"); } else if ( COMPARE(type,"obj") ) { printf(" - OBJ"); Object *obj = objList.Find(name); if ( obj == NULL ) { // object is not on the list, so we should load it now TriObj *tobj = new TriObj; if ( ! tobj->Load( name ) ) { printf(" -- ERROR: Cannot load file \"%s.\"", name); delete tobj; } else { objList.Append(tobj,name); // add to the list obj = tobj; } } node->SetObject( obj ); } else { printf(" - UNKNOWN TYPE"); } } // type const char* mtlName = element->Attribute("material"); if ( mtlName ) { printf(" <%s>", mtlName); NodeMtl nm; nm.node = node; nm.mtlName = mtlName; nodeMtlList.push_back(nm); } printf("\n"); for ( TiXmlElement *child = element->FirstChildElement(); child!=NULL; child = child->NextSiblingElement() ) { if ( COMPARE( child->Value(), "object" ) ) { LoadNode(node,child,level+1); } } LoadTransform( node, element, level ); }