TrackObjectPresentationInstancing::TrackObjectPresentationInstancing(const XMLNode& xml_node,
    scene::ISceneNode* parent,
    ModelDefinitionLoader& model_def_loader) : TrackObjectPresentationSceneNode(xml_node)
{
    m_instancing_group = NULL;
    m_fallback_scene_node = NULL;

    std::string instancing_model;
    xml_node.get("instancing_model", &instancing_model);

    m_node = irr_driver->getSceneManager()->addEmptySceneNode(parent);
    m_node->setPosition(m_init_xyz);
    m_node->setRotation(m_init_hpr);
    m_node->setScale(m_init_scale);
    m_node->updateAbsolutePosition();
    if (irr_driver->isGLSL())
    {
        m_instancing_group = model_def_loader.instanciate(m_node->getAbsolutePosition(),
            m_node->getAbsoluteTransformation().getRotationDegrees(), m_node->getAbsoluteTransformation().getScale(),
            instancing_model);
    }
    else
    {
        scene::IMesh* mesh = model_def_loader.getFirstMeshFor(instancing_model);
        scene::IMeshSceneNode* node = irr_driver->addMesh(mesh, m_node);
        node->grab();
        irr_driver->grabAllTextures(mesh);
        mesh->grab();
        World::getWorld()->getTrack()->addNode(node);

        m_fallback_scene_node = node;
    }
}
TrackObjectPresentationInstancing::TrackObjectPresentationInstancing(const XMLNode& xml_node,
    scene::ISceneNode* parent,
    ModelDefinitionLoader& model_def_loader) : TrackObjectPresentationSceneNode(xml_node)
{
    m_instancing_group = NULL;

    std::string instancing_model;
    xml_node.get("instancing_model", &instancing_model);

    m_node = irr_driver->getSceneManager()->addEmptySceneNode(parent);
    m_node->setPosition(m_init_xyz);
    m_node->setRotation(m_init_hpr);
    m_node->setScale(m_init_scale);
    m_node->updateAbsolutePosition();
    if (irr_driver->isGLSL())
    {
        m_instancing_group = model_def_loader.instanciate(m_node->getAbsolutePosition(),
            m_node->getAbsoluteTransformation().getRotationDegrees(), m_node->getAbsoluteTransformation().getScale(),
            instancing_model);
    }
    else
    {
        m_instancing_group = new STKInstancedSceneNode(model_def_loader.getFirstMeshFor(instancing_model),
            m_node, irr_driver->getSceneManager(), -1);
    }
}
/** Initialises the track object based on the specified XML data.
 *  \param xml_node The XML data.
 *  \param parent The parent scene node.
 *  \param model_def_loader Used to load level-of-detail nodes.
 */
void TrackObject::init(const XMLNode &xml_node, scene::ISceneNode* parent,
                       ModelDefinitionLoader& model_def_loader,
                       TrackObject* parent_library)
{
    m_init_xyz   = core::vector3df(0,0,0);
    m_init_hpr   = core::vector3df(0,0,0);
    m_init_scale = core::vector3df(1,1,1);
    m_enabled    = true;
    m_initially_visible = false;
    m_presentation = NULL;
    m_animator = NULL;
    m_parent_library = parent_library;
    m_physical_object = NULL;

    xml_node.get("id",      &m_id        );
    xml_node.get("model",   &m_name      );
    xml_node.get("xyz",     &m_init_xyz  );
    xml_node.get("hpr",     &m_init_hpr  );
    xml_node.get("scale",   &m_init_scale);
    xml_node.get("enabled", &m_enabled   );

    m_interaction = "static";
    xml_node.get("interaction", &m_interaction);
    xml_node.get("lod_group", &m_lod_group);

    m_is_driveable = false;
    xml_node.get("driveable", &m_is_driveable);

    bool lod_instance = false;
    xml_node.get("lod_instance", &lod_instance);

    m_soccer_ball = false;
    xml_node.get("soccer_ball", &m_soccer_ball);
    
    std::string type;
    xml_node.get("type",    &type );

    m_type = type;

    m_initially_visible = true;
    xml_node.get("if", &m_visibility_condition);
    if (m_visibility_condition == "false")
    {
        m_initially_visible = false;
    }
    if (!m_initially_visible)
        setEnabled(false);

    if (xml_node.getName() == "particle-emitter")
    {
        m_type = "particle-emitter";
        m_presentation = new TrackObjectPresentationParticles(xml_node, parent);
    }
    else if (xml_node.getName() == "light")
    {
        m_type = "light";
        m_presentation = new TrackObjectPresentationLight(xml_node, parent);
    }
    else if (xml_node.getName() == "library")
    {
        xml_node.get("name", &m_name);
        m_presentation = new TrackObjectPresentationLibraryNode(this, xml_node, model_def_loader);
        if (parent_library != NULL)
        {
            Track::getCurrentTrack()->addMetaLibrary(parent_library, this);
        }
    }
    else if (type == "sfx-emitter")
    {
        // FIXME: at this time sound emitters are just disabled in multiplayer
        //        otherwise the sounds would be constantly heard
        if (race_manager->getNumLocalPlayers() < 2)
            m_presentation = new TrackObjectPresentationSound(xml_node, parent);
    }
    else if (type == "action-trigger")
    {
        std::string action;
        xml_node.get("action", &action);
        m_name = action; //adds action as name so that it can be found by using getName()
        m_presentation = new TrackObjectPresentationActionTrigger(xml_node, parent_library);
    }
    else if (type == "billboard")
    {
        m_presentation = new TrackObjectPresentationBillboard(xml_node, parent);
    }
    else if (type=="cutscene_camera")
    {
        m_presentation = new TrackObjectPresentationEmpty(xml_node);
    }
    else
    {
        // Colorization settings
        std::string model_name;
        xml_node.get("model", &model_name);
#ifndef SERVER_ONLY
        if (CVS->isGLSL())
        {
            scene::IMesh* mesh = NULL;
            // Use the first material in mesh to determine hue
            Material* colorized = NULL;
            if (model_name.size() > 0)
            {
                mesh = irr_driver->getMesh(model_name);
                if (mesh != NULL)
                {
                    for (u32 j = 0; j < mesh->getMeshBufferCount(); j++)
                    {
                        SP::SPMeshBuffer* mb = static_cast<SP::SPMeshBuffer*>
                            (mesh->getMeshBuffer(j));
                        std::vector<Material*> mbs = mb->getAllSTKMaterials();
                        for (Material* m : mbs)
                        {
                            if (m->isColorizable() && m->hasRandomHue())
                            {
                                colorized = m;
                                break;
                            }
                        }
                        if (colorized != NULL)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                std::string group_name = "";
                xml_node.get("lod_group", &group_name);
                // Try to get the first mesh from lod groups
                mesh = model_def_loader.getFirstMeshFor(group_name);
                if (mesh != NULL)
                {
                    for (u32 j = 0; j < mesh->getMeshBufferCount(); j++)
                    {
                        SP::SPMeshBuffer* mb = static_cast<SP::SPMeshBuffer*>
                            (mesh->getMeshBuffer(j));
                        std::vector<Material*> mbs = mb->getAllSTKMaterials();
                        for (Material* m : mbs)
                        {
                            if (m->isColorizable() && m->hasRandomHue())
                            {
                                colorized = m;
                                break;
                            }
                        }
                        if (colorized != NULL)
                        {
                            break;
                        }
                    }
                }
            }

            // If at least one material is colorizable, add RenderInfo for it
            if (colorized != NULL)
            {
                const float hue = colorized->getRandomHue();
                if (hue > 0.0f)
                {
                    m_render_info = std::make_shared<RenderInfo>(hue);
                }
            }
        }
#endif
        scene::ISceneNode *glownode = NULL;
        bool is_movable = false;
        if (lod_instance)
        {
            m_type = "lod";
            TrackObjectPresentationLOD* lod_node =
                new TrackObjectPresentationLOD(xml_node, parent, model_def_loader, m_render_info);
            m_presentation = lod_node;

            LODNode* node = (LODNode*)lod_node->getNode();
            if (type == "movable" && parent != NULL)
            {
                // HACK: unparent movables from their parent library object if any,
                // because bullet provides absolute transforms, not transforms relative
                // to the parent object
                node->updateAbsolutePosition();
                core::matrix4 absTransform = node->getAbsoluteTransformation();
                node->setParent(irr_driver->getSceneManager()->getRootSceneNode());
                node->setPosition(absTransform.getTranslation());
                node->setRotation(absTransform.getRotationDegrees());
                node->setScale(absTransform.getScale());
            }

            glownode = node->getAllNodes()[0];
        }
        else
        {
            m_type = "mesh";
            m_presentation = new TrackObjectPresentationMesh(xml_node,
                                                             m_enabled,
                                                             parent,
                                                             m_render_info);
            scene::ISceneNode* node = ((TrackObjectPresentationMesh *)m_presentation)->getNode();
            if (type == "movable" && parent != NULL)
            {
                // HACK: unparent movables from their parent library object if any,
                // because bullet provides absolute transforms, not transforms relative
                // to the parent object
                node->updateAbsolutePosition();
                core::matrix4 absTransform = node->getAbsoluteTransformation();
                node->setParent(irr_driver->getSceneManager()->getRootSceneNode());
                node->setPosition(absTransform.getTranslation());
                // Doesn't seem necessary to set rotation here, TODO: not sure why
                //node->setRotation(absTransform.getRotationDegrees());
                node->setScale(absTransform.getScale());
                is_movable = true;
            }

            glownode = node;
        }

        std::string render_pass;
        xml_node.get("renderpass", &render_pass);

        if (m_interaction != "ghost" && m_interaction != "none" &&
            render_pass != "skybox"                                     )
        {
            m_physical_object = PhysicalObject::fromXML(type == "movable",
                                                   xml_node,
                                                   this);
        }

        if (parent_library != NULL)
        {
            if (is_movable)
                parent_library->addMovableChild(this);
            else
                parent_library->addChild(this);
        }

        video::SColor glow;
        if (xml_node.get("glow", &glow) && glownode)
        {
            float r, g, b;
            r = glow.getRed() / 255.0f;
            g = glow.getGreen() / 255.0f;
            b = glow.getBlue() / 255.0f;
            SP::SPMeshNode* spmn = dynamic_cast<SP::SPMeshNode*>(glownode);
            if (spmn)
            {
                spmn->setGlowColor(video::SColorf(r, g, b));
            }
        }

        bool is_in_shadowpass = true;
        if (xml_node.get("shadow-pass", &is_in_shadowpass) && glownode)
        {
            SP::SPMeshNode* spmn = dynamic_cast<SP::SPMeshNode*>(glownode);
            if (spmn)
            {
                spmn->setInShadowPass(is_in_shadowpass);
            }
        }

        bool forcedbloom = false;
        if (xml_node.get("forcedbloom", &forcedbloom) && forcedbloom && glownode)
        {
            float power = 1;
            xml_node.get("bloompower", &power);
            btClamp(power, 0.5f, 10.0f);
            irr_driver->addForcedBloomNode(glownode, power);
        }
    }


    if (type == "animation" || xml_node.hasChildNamed("curve"))
    {
        m_animator = new ThreeDAnimation(xml_node, this);
    }

    reset();

    if (!m_initially_visible)
        setEnabled(false);
    if (parent_library != NULL && !parent_library->isEnabled())
        setEnabled(false);
}   // TrackObject