Ogre::ParticleSystem的Abstract Factory模式

Edit on Github

最近在看Ogre的粒子系统的绘制方式,这里小记下。

从上面的类图可以看出,Ogre::ParticleSystem是一个MovableObject,也就是说Ogre把它看成就一个可移动的场景物体。所以不可以直接创建,而是应该通过SceneManager来创建,并且只有attached到SceneNode后,才会显示。

为了便于ScenManager统一管理各种MovableObject,所有MovableObject使用了 Abstract Factory 模式,并且每个子类的Factory要在Ogre::Root的mMovableObjectFactoryMap中注册,而mMovableObjectCollectionMap中存放的是各种MovableObject类的实例集合。下面就粒子系统的创建函数,常规的Abstract Factory

ParticleSystem* SceneManager::createParticleSystem(const String&name,
    const String&templateName)
{
    NameValuePairList params;
    params["templateName"] = templateName;
    
    return static_cast<ParticleSystem*>(
        createMovableObject(name, ParticleSystemFactory::FACTORY_TYPE_NAME,&params));
}
MovableObject* SceneManager::createMovableObject(const String&name, 
    const String&typeName, const NameValuePairList* params)
{
    // Nasty hack to make generalised Camera functions work without breaking add-on SMs
    if (typeName =="Camera")
    {
        return createCamera(name);
    }
    MovableObjectFactory* factory = 
        Root::getSingleton().getMovableObjectFactory(typeName);
    // Check for duplicate names
    MovableObjectCollection* objectMap = getMovableObjectCollection(typeName);

    {
        OGRE_LOCK_MUTEX(objectMap->mutex)

        if (objectMap->map.find(name) != objectMap->map.end())
        {
            OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM,"An object of type '"+ typeName +"' with name '"+ name
                +"' already exists.","SceneManager::createMovableObject");
        }

        MovableObject* newObj = factory->createInstance(name, this, params);
        objectMap->map[name] = newObj;
        return newObj;
    }
}