/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2012 I-maginer This program 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 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ----------------------------------------------------------------------------- */ struct ObjFollowStr = [ POBF_father : SO3_OBJECT, POBF_son : SO3_OBJECT, POBF_iFatherMode : I, POBF_iSonMode : I, POBF_bRotation : I, POBF_initpos : [[F F F] [F F F F]] ]mkObjFollowStr;; fun deleteOb(inst, objfstr)= setPluginInstanceCbPreRender inst nil; setPluginInstanceCbPostRender inst nil; setPluginInstanceCbCameraChange inst nil; let objfstr.POBF_initpos -> [pos quat] in ( SO3ObjectSetPosition objfstr.POBF_son pos; SO3ObjectSetOrientation objfstr.POBF_son quat; ); 0;; fun usePhysic(objfstr)= let SO3SceneNodeGetBody objfstr.POBF_son -> body in let SO3BodyGetMassMatrix body -> [mass _] in if ((objfstr.POBF_iFatherMode != 0) || (body == nil) || (SO3BodyGetFluid body) || !(V3DphysGetState c3dXsession) || (mass <=. 0.0)) then 0 else 1;; fun cbGetPreRender(inst, viewstr, objfstr)= let if (usePhysic objfstr) then SO3SceneNodeGetBody objfstr.POBF_son else nil -> body in let SO3ObjectGetGlobalPosition objfstr.POBF_father -> gpos in let SO3ObjectGetGlobalOrientation objfstr.POBF_father -> grot in if (body == nil) then ( SO3ObjectSetGlobalPosition objfstr.POBF_son gpos; if (!objfstr.POBF_bRotation) then nil else SO3ObjectSetGlobalOrientation objfstr.POBF_son grot; ) else ( SO3BodyMoveTo body gpos 0.5; if (!objfstr.POBF_bRotation) then nil else SO3BodyRotateTo body grot 0.5; ); 0;; fun cbFollowObject(inst, from, action, param, reply, objfstr)= if (usePhysic objfstr) || (objfstr.POBF_iSonMode) || ((V3DgetObjectByName c3dXsession "Current camera shell") == objfstr.POBF_father) || ((SO3ObjectGetType objfstr.POBF_father) == SO3_TYPE_BONE) || ((SO3ObjectGetType objfstr.POBF_son) == SO3_TYPE_CAMERA) || ((SO3ObjectGetType (hd SO3ObjectGetChildren objfstr.POBF_son)) == SO3_TYPE_CAMERA) then ( setPluginInstanceCbPreRender inst mkfun3 @cbGetPreRender objfstr; 0; ) else ( setPluginInstanceCbPostRender inst mkfun3 @cbGetPreRender objfstr; 0; ); 0;; fun cbUnFollowObject(inst, from, action, param, reply, objfstr)= setPluginInstanceCbPostRender inst nil; setPluginInstanceCbPreRender inst nil; 0;; fun cbChangeCamera(inst, viewstr, sessionstr, camera, objfstr)= if ((!objfstr.POBF_iSonMode) || (camera != V3DgetDefaultCamera c3dXsession)) then nil else ( let objfstr.POBF_son -> oldson in let V3DgetCameraByType sessionstr camera objfstr.POBF_iSonMode -> nson in let SO3ObjectGetPosition nson -> cpos in let SO3ObjectGetOrientation nson -> cquat in ( set objfstr.POBF_son = nson; let objfstr.POBF_initpos -> [pos quat] in ( SO3ObjectSetPosition oldson pos; SO3ObjectSetOrientation oldson quat; ); set objfstr.POBF_initpos = [cpos cquat]; ); ); if !objfstr.POBF_iFatherMode then nil else let V3DgetCameraByType sessionstr camera objfstr.POBF_iFatherMode -> nfather in ( set objfstr.POBF_father = nfather; ); 0;; fun cbSetSonObject(inst, from, action, param, reply, objfstr)= let V3DgetObjectByName c3dXsession param -> obj in if (obj == nil) then nil else let objfstr.POBF_son -> oldson in let objfstr.POBF_initpos -> [pos quat] in ( let SO3ObjectGetPosition obj -> cpos in let SO3ObjectGetOrientation obj -> cquat in ( set objfstr.POBF_initpos = [cpos cquat]; set objfstr.POBF_son = obj; ); SO3ObjectSetPosition oldson pos; SO3ObjectSetOrientation oldson quat; ); 0;; fun cbSetTargetObject(inst, from, action, param, reply, objfstr)= let V3DgetObjectByName c3dXsession param -> obj in if (obj == nil) then nil else ( set objfstr.POBF_father = obj; ); 0;; fun newOb(inst)= let (getPluginInstanceParam inst "object1") -> objname1 in let (getPluginInstanceParam inst "object2") -> objname2 in let (getPluginInstanceParam inst "rotation") -> rotation in let if rotation == nil then 0 else atoi rotation -> rotation in let atoi (getPluginInstanceParam inst "enable") -> enable in let if enable == nil then 1 else enable -> enable in // for compatibility let if ((atoi (getPluginInstanceParam inst "iscamera")) == 1) then "Current camera" else objname2 -> objname2 in let V3DgetObjectByName c3dXsession objname1 -> object1 in let V3DgetObjectTypeByName objname1 -> iobj1mode in let V3DgetObjectByName c3dXsession objname2 -> object2 in let V3DgetObjectTypeByName objname2 -> iobj2mode in let SO3ObjectGetPosition object2 -> cpos in let SO3ObjectGetOrientation object2 -> cquat in let mkObjFollowStr [object1 object2 iobj1mode iobj2mode rotation [cpos cquat]] -> objfstr in ( PluginRegisterAction inst "Enable" mkfun6 @cbFollowObject objfstr; PluginRegisterAction inst "Disable" mkfun6 @cbUnFollowObject objfstr; PluginRegisterAction inst "Set son object" mkfun6 @cbSetSonObject objfstr; PluginRegisterAction inst "Set target object" mkfun6 @cbSetTargetObject objfstr; if !iobj1mode && !iobj2mode then nil else setPluginInstanceCbCameraChange inst mkfun5 @cbChangeCamera objfstr; if (!enable) then nil else cbFollowObject inst nil nil nil nil objfstr; setPluginInstanceCbDel inst mkfun2 @deleteOb objfstr; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;