/* ----------------------------------------------------------------------------- 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 ObjPosStr = [ POBP_father : SO3_OBJECT, POBP_iFatherMode : I, POBP_pos : [[F F F] [F F F F]], POBP_initpos : [[F F F] [F F F F] [F F F]], POBP_lastPos : [F F F], POBP_bLocal : I, POBP_bScreenPos : I, POBP_bTranslation : I ]mkObjPosStr;; fun deleteOb(inst, objpstr)= setPluginInstanceCbCameraChange inst nil; setPluginInstanceCbScenePreRender2 inst nil; let objpstr.POBP_initpos -> [pos quat scale] in ( SO3ObjectSetScale objpstr.POBP_father scale; SO3ObjectSetPosition objpstr.POBP_father pos; SO3ObjectSetOrientation objpstr.POBP_father quat; ); 0;; fun cbSetPosition(inst, from, action, param, reply, objpstr)= let if (!strcmp "" (strtrim param)) || param == nil then ( let objpstr.POBP_pos -> [pos quat] in [pos quat] ) else ( let strextr param -> lp in [[(atof (nth_list (hd lp) 0)) (atof (nth_list (hd lp) 1)) (atof (nth_list (hd lp) 2))] nil] ) -> [pos quat] in ( if objpstr.POBP_bLocal then SO3ObjectSetPosition objpstr.POBP_father pos else SO3ObjectSetGlobalPosition objpstr.POBP_father pos; set objpstr.POBP_lastPos = pos; ); 0;; fun cbSetOrientation(inst, from, action, param, reply, objpstr)= let if (!strcmp "" (strtrim param)) || param == nil then ( let objpstr.POBP_pos -> [pos quat] in [pos quat] ) else ( let strextr param -> lp in [nil (SO3MathsEulerYXZToQuat [SO3MathsDegreeToRadian (atof (nth_list (hd lp) 0)) SO3MathsDegreeToRadian(atof (nth_list (hd lp) 1)) SO3MathsDegreeToRadian(atof (nth_list (hd lp) 2))])] ) -> [pos quat] in ( if objpstr.POBP_bLocal then SO3ObjectSetOrientation objpstr.POBP_father quat else SO3ObjectSetGlobalOrientation objpstr.POBP_father quat; ); 0;; fun cbSetScale(inst, from, action, param, reply, objpstr)= if (!strcmp "" (strtrim param)) || (param == nil) then nil else let strextr param -> lp in let [(atof (nth_list (hd lp) 0)) (atof (nth_list (hd lp) 1)) (atof (nth_list (hd lp) 2))] -> [sx sy sz] in let if (sy == nil) then [sx sx sx] else [sx sy sz] -> scale in ( if objpstr.POBP_bLocal then SO3ObjectSetScale objpstr.POBP_father scale else SO3ObjectSetGlobalScale objpstr.POBP_father scale; ); 0;; fun cbSetCoords(inst, from, action, param, reply, objpstr)= let if (!strcmp "" (strtrim param)) || param == nil then ( let objpstr.POBP_pos -> [pos quat] in [pos quat] ) else ( let strextr param -> lp in [[(atof (nth_list (hd lp) 0)) (atof (nth_list (hd lp) 1)) (atof (nth_list (hd lp) 2))] (SO3MathsEulerYXZToQuat [SO3MathsDegreeToRadian (atof (nth_list (hd tl lp) 0)) SO3MathsDegreeToRadian(atof (nth_list (hd tl lp) 1)) SO3MathsDegreeToRadian(atof (nth_list (hd tl lp) 2))])] ) -> [pos quat] in ( if objpstr.POBP_bLocal then ( SO3ObjectSetOrientation objpstr.POBP_father quat; SO3ObjectSetPosition objpstr.POBP_father pos; ) else ( SO3ObjectSetGlobalOrientation objpstr.POBP_father quat; SO3ObjectSetGlobalPosition objpstr.POBP_father pos; ); set objpstr.POBP_lastPos = pos; ); 0;; fun cbSetDirection(inst, from, action, param, reply, objpstr)= if (!strcmp "" (strtrim param)) || param == nil then nil else let strextr param -> lp in let [(atof (nth_list (hd lp) 0)) (atof (nth_list (hd lp) 1)) (atof (nth_list (hd lp) 2))] -> [dx dy dz] in //get destination quat to direction let SO3ObjectGetGlobalPosition objpstr.POBP_father -> vec in let SO3ObjectGetGlobalOrientation objpstr.POBP_father -> ang in let vec -> [vx vy vz] in let SO3MathsQuatGetDirection ang [0.0 0.0 (-.1.0)] -> src in let subVectorF [dx vy dz] vec -> dir in let SO3MathsGetRotationTo src dir -> qrot in let SO3MathsQuatAdd ang qrot -> quat in SO3ObjectSetOrientation objpstr.POBP_father quat; 0;; fun cbGetPosition(inst, from, action, param, reply, objpstr)= let if objpstr.POBP_bLocal then SO3ObjectGetPosition objpstr.POBP_father else SO3ObjectGetGlobalPosition objpstr.POBP_father -> [x y z] in SendPluginEvent inst "Position" strcatnSep (ftoa x)::(ftoa y)::(ftoa z)::nil " " nil; 0;; fun cbGetOrientation(inst, from, action, param, reply, objpstr)= let if objpstr.POBP_bLocal then SO3ObjectGetOrientation objpstr.POBP_father else SO3ObjectGetGlobalOrientation objpstr.POBP_father -> quat in let SO3MathsQuatToEulerYXZ quat -> [x y z] in SendPluginEvent inst "Orientation" strcatnSep (ftoa (SO3MathsRadianToDegree x))::(ftoa (SO3MathsRadianToDegree y))::(ftoa (SO3MathsRadianToDegree z))::nil " " nil; 0;; fun cbGetScale(inst, from, action, param, reply, objpstr)= let if objpstr.POBP_bLocal then SO3ObjectGetScale objpstr.POBP_father else SO3ObjectGetGlobalScale objpstr.POBP_father -> [x y z] in SendPluginEvent inst "Scale" strcatnSep (ftoa x)::(ftoa y)::(ftoa z)::nil " " nil; 0;; fun cbGetCoords(inst, from, action, param, reply, objpstr)= let if objpstr.POBP_bLocal then SO3ObjectGetPosition objpstr.POBP_father else SO3ObjectGetGlobalPosition objpstr.POBP_father -> [x y z] in let if objpstr.POBP_bLocal then SO3ObjectGetOrientation objpstr.POBP_father else SO3ObjectGetGlobalOrientation objpstr.POBP_father -> quat in let SO3MathsQuatToEulerYXZ quat -> [qx qy qz] in SendPluginEvent inst "Coords" strcatn (strcatnSep (ftoa x)::(ftoa y)::(ftoa z)::nil " ")::"\n"::(strcatnSep (ftoa (SO3MathsRadianToDegree qx))::(ftoa (SO3MathsRadianToDegree qy))::(ftoa (SO3MathsRadianToDegree qz))::nil " ")::nil nil; 0;; fun cbGetDirectionTo(inst, from, action, param, reply, objpstr)= if param == nil then nil else let strextr param -> lp in let if (atof (nth_list (hd lp) 0)) == nil then 0.0 else (atof (nth_list (hd lp) 0)) -> px in let if (atof (nth_list (hd lp) 1)) == nil then 0.0 else (atof (nth_list (hd lp) 1)) -> py in let if (atof (nth_list (hd lp) 2)) == nil then 0.0 else (atof (nth_list (hd lp) 2)) -> pz in let if objpstr.POBP_bLocal then SO3ObjectGetOrientation objpstr.POBP_father else SO3ObjectGetGlobalOrientation objpstr.POBP_father -> quat in let SO3MathsQuatGetDirection quat [px py pz] -> [dx dy dz] in SendPluginEvent inst "Direction" strcatnSep (ftoa dx)::(ftoa dy)::(ftoa dz)::nil " " nil; 0;; fun cbGetPreRender(inst, sessionstr, etime, objpstr)= let (V3DgetSessionView c3dXsession) -> viewstr in let (V3DgetDefaultViewport viewstr) -> viewportstr in let (SO3ObjectGetGlobalPosition objpstr.POBP_father) -> gpos in let if objpstr.POBP_bLocal then SO3ObjectGetPosition objpstr.POBP_father else gpos -> rpos in ( if (!objpstr.POBP_bScreenPos) then nil else ( let SO3ViewportGetPixelPositionFromWorldPos viewportstr.V3D_viewport gpos -> spos in let spos -> [x y] in if (spos == nil) then nil else SendPluginEvent inst "Screen position" strcatnSep (itoa x)::(itoa y)::nil " " nil; ); if (!objpstr.POBP_bTranslation) then nil else let subVectorF rpos objpstr.POBP_lastPos -> [tx ty tz] in SendPluginEvent inst "Translation" strcatnSep (ftoa tx)::(ftoa ty)::(ftoa tz)::nil " " nil; set objpstr.POBP_lastPos = rpos; ); 0;; fun cbChangeCamera(inst, viewstr, sessionstr, camera, objpstr)= let V3DgetCameraByType sessionstr camera objpstr.POBP_iFatherMode -> nfather in let SO3ObjectGetPosition nfather -> cpos in let SO3ObjectGetOrientation nfather -> cquat in ( let objpstr.POBP_initpos -> [pos quat scale] in ( //SO3ObjectSetPosition objpstr.POBP_father pos; //SO3ObjectSetOrientation objpstr.POBP_father quat; set objpstr.POBP_father = nfather; set objpstr.POBP_initpos = [cpos cquat scale]; ); ); 0;; fun cbSetObject(inst, from, action, param, reply, objpstr)= if param == nil then nil else let V3DgetObjectByName c3dXsession param -> father in let V3DgetObjectTypeByName param -> iobjmode in let SO3ObjectGetPosition father -> cpos in let SO3ObjectGetOrientation father -> cquat in let SO3ObjectGetScale father -> cscale in let if objpstr.POBP_bLocal then cpos else SO3ObjectGetGlobalPosition father -> rpos in ( set objpstr.POBP_father = father; set objpstr.POBP_iFatherMode = iobjmode; set objpstr.POBP_initpos = [cpos cquat cscale]; set objpstr.POBP_lastPos = rpos; if !iobjmode then nil else setPluginInstanceCbCameraChange inst mkfun5 @cbChangeCamera objpstr; ); 0;; fun cbResetObject(inst, from, action, param, reply, objpstr)= let objpstr.POBP_initpos -> [pos quat scale] in ( SO3ObjectSetScale objpstr.POBP_father scale; SO3ObjectSetPosition objpstr.POBP_father pos; SO3ObjectSetOrientation objpstr.POBP_father quat; ); 0;; fun newOb(inst)= let (getPluginInstanceParam inst "object") -> objname in // for compatibility let if ((atoi (getPluginInstanceParam inst "iscamera")) == 1) then "Current camera" else objname -> objname in let atoi (getPluginInstanceParam inst "local") -> local in let if (local == nil) then 1 else local -> local in let atof (getPluginInstanceParam inst "posx") -> posx in let atof (getPluginInstanceParam inst "posy") -> posy in let atof (getPluginInstanceParam inst "posz") -> posz in let atof (getPluginInstanceParam inst "angx") -> angx in let atof (getPluginInstanceParam inst "angy") -> angy in let atof (getPluginInstanceParam inst "angz") -> angz in let SO3MathsEulerYXZToQuat [(SO3MathsDegreeToRadian angx) (SO3MathsDegreeToRadian angy) (SO3MathsDegreeToRadian angz)] -> quat in let atoi (getPluginInstanceParam inst "init") -> init in let if (init == nil) then 1 else init -> init in let V3DgetObjectByName c3dXsession objname -> father in let V3DgetObjectTypeByName objname -> iobjmode in let SO3ObjectGetPosition father -> cpos in let SO3ObjectGetOrientation father -> cquat in let SO3ObjectGetScale father -> cscale in let if local then cpos else SO3ObjectGetGlobalPosition father -> rpos in let (IsInEditor inst) || IsEventLinked inst "Screen position" -> bscreenpos in let (IsInEditor inst) || IsEventLinked inst "Translation" -> btranslation in let mkObjPosStr [father iobjmode [[posx posy posz] quat] [cpos cquat cscale] rpos local bscreenpos btranslation] -> objpstr in ( PluginRegisterAction inst "Set position" mkfun6 @cbSetPosition objpstr; PluginRegisterAction inst "Set orientation" mkfun6 @cbSetOrientation objpstr; PluginRegisterAction inst "Set scale" mkfun6 @cbSetScale objpstr; PluginRegisterAction inst "Set direction" mkfun6 @cbSetDirection objpstr; PluginRegisterAction inst "Set coords" mkfun6 @cbSetCoords objpstr; PluginRegisterAction inst "Get position" mkfun6 @cbGetPosition objpstr; PluginRegisterAction inst "Get orientation" mkfun6 @cbGetOrientation objpstr; PluginRegisterAction inst "Get scale" mkfun6 @cbGetScale objpstr; PluginRegisterAction inst "Get coords" mkfun6 @cbGetCoords objpstr; PluginRegisterAction inst "Get direction to" mkfun6 @cbGetDirectionTo objpstr; PluginRegisterAction inst "Set object" mkfun6 @cbSetObject objpstr; PluginRegisterAction inst "Reset object" mkfun6 @cbResetObject objpstr; if !iobjmode then nil else setPluginInstanceCbCameraChange inst mkfun5 @cbChangeCamera objpstr; if (!init) then nil else ( if local then ( SO3ObjectSetPosition father [posx posy posz]; SO3ObjectSetOrientation father quat; ) else ( SO3ObjectSetGlobalPosition father [posx posy posz]; SO3ObjectSetGlobalOrientation father quat; ); ); setPluginInstanceCbScenePreRender2 inst mkfun4 @cbGetPreRender objpstr; setPluginInstanceCbDel inst mkfun2 @deleteOb objpstr; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;