Texture::Specification::Specification(const Any& any, bool assumesRGBForAuto, Dimension defaultDimension) { *this = Specification(); assumeSRGBSpaceForAuto = assumesRGBForAuto; dimension = defaultDimension; if (any.type() == Any::STRING) { filename = any.string(); if (filename == "<whiteCube>") { filename = "<white>"; dimension = Texture::DIM_CUBE_MAP; } if (! beginsWith(filename, "<")) { filename = any.resolveStringAsFilename(); if (FilePath::containsWildcards(filename)) { // Assume this is a cube map dimension = Texture::DIM_CUBE_MAP; } } } else if ((any.type() == Any::NUMBER) || any.nameBeginsWith("Color4") || anyNameIsColor3Variant(any)) { filename = "<white>"; encoding.readMultiplyFirst = Color4(any); } else { any.verifyNameBeginsWith("Texture::Specification"); AnyTableReader r(any); r.getFilenameIfPresent("filename", filename); r.getFilenameIfPresent("alphaFilename", alphaFilename); r.getIfPresent("encoding", encoding); r.getIfPresent("assumeSRGBSpaceForAuto", assumeSRGBSpaceForAuto); { Any a; if (r.getIfPresent("dimension", a)) { dimension = toDimension(a); } } r.getIfPresent("generateMipMaps", generateMipMaps); r.getIfPresent("preprocess", preprocess); r.getIfPresent("visualization", visualization); r.getIfPresent("cachable", cachable); r.verifyDone(); if (! any.containsKey("dimension") && FilePath::containsWildcards(filename)) { // Assume this is a cube map dimension = Texture::DIM_CUBE_MAP; } } }
Any Scene::load(const std::string& scene) { std::string filename; clear(); m_modelTable.clear(); m_name = scene; bool isFilename = endsWith(toLower(scene), ".scn.any") || endsWith(toLower(scene), ".Scene.Any"); if (isFilename) { filename = scene; } else { const std::string* f = filenameTable().getPointer(scene); if (f == NULL) { throw "No scene with name '" + scene + "' found in (" + stringJoin(filenameTable().getKeys(), ", ") + ")"; } filename = *f; } Any any; any.load(filename); { const std::string& n = any.get("name", filename); // Ensure that this name appears in the filename table if it does not already, // so that it can be loaded by name in the future. if (! filenameTable().containsKey(n)) { filenameTable().set(n, filename); } } m_sourceAny = any; // Load the lighting environment (do this before loading entities, since some of them may // be lights that will enter this array) bool hasEnvironmentMap = false; if (any.containsKey("localLightingEnvironment")) { m_localLightingEnvironment = any["localLightingEnvironment"]; hasEnvironmentMap = any["localLightingEnvironment"].containsKey("environmentMap"); } // Load the models if (any.containsKey("models")) { Any models = any["models"]; if (models.size() > 0) { for (Any::AnyTable::Iterator it = models.table().begin(); it.isValid(); ++it) { const std::string name = it->key; Any v = it->value; createModel(v, name); } } } // Instance the models if (any.containsKey("entities")) { Any entities = any["entities"]; if (entities.size() > 0) { for (Table<std::string, Any>::Iterator it = entities.table().begin(); it.isValid(); ++it) { const std::string& name = it->key; const std::string& entityType = it->value.name(); createEntity(entityType, name, it->value); } } } shared_ptr<Texture> skyboxTexture = Texture::whiteCube(); Any skyAny; // Use the environment map as a skybox if there isn't one already, and vice versa Array<shared_ptr<Skybox> > skyboxes; getTypedEntityArray<Skybox>(skyboxes); if ( skyboxes.size() == 0 ) { if (any.containsKey("skybox")) { createEntity("Skybox", "skybox", any["skybox"]); m_skybox = typedEntity<Skybox>("skybox"); } else if (hasEnvironmentMap) { m_skybox = Skybox::create("skybox", this, Array<ScaledTexture>(m_localLightingEnvironment.environmentMapArray[0]), Array<SimTime>(0.0), 0, SplineExtrapolationMode::CLAMP, false, false); insert(m_skybox); } else { m_skybox = Skybox::create("skybox", this, Array<ScaledTexture>(ScaledTexture(Texture::whiteCube(), 1.0f)), Array<SimTime>(0.0), 0, SplineExtrapolationMode::CLAMP, false, false); insert(m_skybox); } } if (any.containsKey("environmentMap")) { throw std::string("environmentMap field has been replaced with localLightingEnvironment"); } // Default to using the skybox as an environment map if none is specified. if (! hasEnvironmentMap) { m_localLightingEnvironment.environmentMapArray.append(ScaledTexture(m_skybox->keyframeArray()[0].texture, m_skybox->keyframeArray()[0].constant)); } ////////////////////////////////////////////////////// if (m_cameraArray.size() == 0) { // Create a default camera, back it up from the origin m_cameraArray.append(Camera::create("camera")); m_cameraArray.last()->setFrame(CFrame::fromXYZYPRDegrees(0,1,-5,0,-5)); } setTime(any.get("time", 0.0)); m_lastVisibleChangeTime = m_lastLightChangeTime = m_lastStructuralChangeTime = System::time(); m_defaultCameraName = (std::string)any.get("defaultCamera", ""); // Set the initial positions, repeating a few times to allow // objects defined relative to others to reach a fixed point for (int i = 0; i < 3; ++i) { for (int e = 0; e < m_entityArray.size(); ++e) { m_entityArray[e]->onSimulation(m_time, nan()); } } // Pose objects so that they have bounds. { Array< shared_ptr<Surface> > ignore; onPose(ignore); } return any; }
Scene::Ref Scene::create(const std::string& scene, GCamera& camera) { if (scene == "") { return NULL; } Scene::Ref s = new Scene(); const std::string* f = filenameTable().getPointer(scene); if (f == NULL) { throw "No scene with name '" + scene + "' found in (" + stringJoin(filenameTable().getKeys(), ", ") + ")"; } const std::string& filename = *f; Any any; any.load(filename); // Load the lighting s->m_lighting = Lighting::create(any.get("lighting", Lighting::Specification())); // Load the models Any models = any["models"]; typedef ReferenceCountedPointer<ReferenceCountedObject> ModelRef; Table< std::string, ModelRef > modelTable; for (Any::AnyTable::Iterator it = models.table().begin(); it.hasMore(); ++it) { ModelRef m; Any v = it->value; if (v.nameBeginsWith("ArticulatedModel")) { m = ArticulatedModel::create(v); } else if (v.nameBeginsWith("MD2Model")) { m = MD2Model::create(v); } else if (v.nameBeginsWith("MD3Model")) { m = MD3Model::create(v); } else { debugAssertM(false, "Unrecognized model type: " + v.name()); } modelTable.set(it->key, m); } // Instance the models Any entities = any["entities"]; for (Table<std::string, Any>::Iterator it = entities.table().begin(); it.hasMore(); ++it) { const std::string& name = it->key; const Any& modelArgs = it->value; modelArgs.verifyType(Any::ARRAY); const ModelRef* model = modelTable.getPointer(modelArgs.name()); modelArgs.verify((model != NULL), "Can't instantiate undefined model named " + modelArgs.name() + "."); PhysicsFrameSpline frameSpline; ArticulatedModel::PoseSpline poseSpline; if (modelArgs.size() >= 1) { frameSpline = modelArgs[0]; if (modelArgs.size() >= 2) { // Poses poseSpline = modelArgs[1]; } } ArticulatedModel::Ref artModel = model->downcast<ArticulatedModel>(); MD2Model::Ref md2Model = model->downcast<MD2Model>(); MD3Model::Ref md3Model = model->downcast<MD3Model>(); if (artModel.notNull()) { s->m_entityArray.append(Entity::create(name, frameSpline, artModel, poseSpline)); } else if (md2Model.notNull()) { s->m_entityArray.append(Entity::create(name, frameSpline, md2Model)); } else if (md3Model.notNull()) { s->m_entityArray.append(Entity::create(name, frameSpline, md3Model)); } } // Load the camera camera = any["camera"]; if (any.containsKey("skybox")) { Any sky = any["skyBox"]; s->m_skyBoxConstant = sky.get("constant", 1.0f); if (sky.containsKey("texture")) { s->m_skyBoxTexture = Texture::create(sky["texture"]); } } else { s->m_skyBoxTexture = s->m_lighting->environmentMapTexture; s->m_skyBoxConstant = s->m_lighting->environmentMapConstant; } // Default to using the skybox as an environment map if none is specified. if (s->m_skyBoxTexture.notNull() && s->m_lighting->environmentMapTexture.isNull()) { s->m_lighting->environmentMapTexture = s->m_skyBoxTexture; s->m_lighting->environmentMapConstant = s->m_skyBoxConstant; } return s; }