// Wild Magic Source Code
// David Eberly
// http://www.geometrictools.com
// Copyright (c) 1998-2007
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or (at
// your option) any later version.  The license is available for reading at
// either of the locations:
//     http://www.gnu.org/copyleft/lgpl.html
//     http://www.geometrictools.com/License/WildMagicLicense.pdf
//
// Version: 4.0.0 (2006/06/28)

#include "WSceneBuilder.h"
#include "iparamb2.h"
#include "iskin.h"
#include "modstack.h"

//----------------------------------------------------------------------------
void WSceneBuilder::ApplyModifiers ()
{
    // Apply the modifier info that was collected during node traversal.  The
    // only modifier currently supported is skin.

    std::vector<ModifierInfo*>::iterator pkI;
    for (pkI = m_kModList.begin(); pkI < m_kModList.end(); pkI++)
    {
        ModifierInfo* pkInfo = *pkI;
        std::vector<Modifier*>::iterator pkJ = pkInfo->Modifiers.begin();
        for (/**/; pkJ < pkInfo->Modifiers.end(); pkJ++)
        {
            Modifier* pkModifier = *pkJ;
            Class_ID kID = pkModifier->ClassID();
            if (kID == SKIN_CLASSID)
            {
                ProcessSkin(pkInfo->Node,pkModifier);
            }
        }       
    }
}
//----------------------------------------------------------------------------
void WSceneBuilder::FindModifiers (INode* pkMaxNode,
    std::vector<Modifier*>& rkModifiers)
{
    // Determine whether a node has any modifiers.  If so, store them for
    // later use.
    //
    // pkNode:
    //     Pointer to a Max node.
    // rkModifiers:
    //     Vector in which to store modifier pointers.

    // Get object from node. Abort if no object.
    Object* pkObj = pkMaxNode->GetObjectRef();
    if (!pkObj) 
    {
        return;
    }

    // Is derived object?
    while (pkObj->SuperClassID() == GEN_DERIVOB_CLASS_ID)
    {
        // Yes -> Cast.
        IDerivedObject* pkDerObj = static_cast<IDerivedObject*>(pkObj);

        // iterate over all entries of the modifier stack
        int iModStackIndex = 0;
        while (iModStackIndex < pkDerObj->NumModifiers())
        {
            // get current modifier
            Modifier* pkMod = pkDerObj->GetModifier(iModStackIndex);
            if (pkMod) 
            {
                rkModifiers.push_back(pkMod);
            }
            iModStackIndex++;
        }
        pkObj = pkDerObj->GetObjRef();
    }
}
//----------------------------------------------------------------------------
