/* ----------------------------------------------------------------------------- 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 LightDirection = [ PLD_light : SO3_OBJECT, PLD_iObjMode : I, PLD_tOrientation : [F F F F], PLD_fAngle : F, PLD_bEnabled : I ]mkLightDirection;; fun deleteOb(inst, obstr)= setPluginInstanceCbPostRender inst nil; V3DenableNavigateControl c3dXsession 1; 0;; fun setDirectionFromMouse(viewstr, obstr)= let (V3DWidgetCtrlHasFocus viewstr) -> widgetctrl in let if (widgetctrl != nil) && (SO3WidgetGetMouseEnable widgetctrl) && ((SO3WidgetGetMode widgetctrl) != 3) then 1 else 0 -> disable in if disable then nil else let V3DgetLastMouseMove viewstr 0 -> [x y] in let [viewstr.V3D_iWinW viewstr.V3D_iWinH] -> [w h] in let if (x > w) then w else if (x < 0) then 0 else x -> x in let if (y > h) then h else if (y < 0) then 0 else y -> y in let V3DgetDefaultCamera c3dXsession -> cam in let SO3ObjectGetGlobalOrientation cam -> camquat in let [(x - (w / 2)) (y - (h / 2))] -> [ix iy] in let [((itof ix) /. (itof (w / 2))) ((itof iy) /. (itof (h / 2)))] -> [fx fy] in let [SO3MathsDegreeToRadian (obstr.PLD_fAngle *. fx) SO3MathsDegreeToRadian (obstr.PLD_fAngle *. fy)] -> [dx dy] in let SO3MathsQuatAdd camquat [0.0 1.0 0.0 0.0] -> camquat in ( SO3ObjectSetGlobalOrientation obstr.PLD_light SO3MathsQuatAdd camquat (SO3MathsEulerPYRToQuat [(-.dy) dx 0.0]); ); // old way //let V3DgetDefaultCamera c3dXsession -> cam in //let SO3ObjectGetGlobalPosition cam -> campos in //let V3DgetDefaultViewport viewstr -> viewportstr in //let SO3ViewportGetWorldPosFromPixelPosition viewportstr.V3D_viewport (w - x) (h - y) 0.01 -> [vpos _] in //let normalizeVectorF (subVectorF campos vpos) -> dir in //( // SO3ObjectSetDirection obstr.PLD_light dir SO3_WORLD_TS; //); 0;; fun cbPreRender(inst, viewstr, obstr)= if !(viewstr.V3D_iMoveClickStatus & V3DCLICK_LEFT) then nil else setDirectionFromMouse viewstr obstr; 0;; fun cbClick(inst, viewstr, x, y, btn, obstr)= setDirectionFromMouse viewstr obstr; 0;; fun cbSetLight(inst, from, action, param, rep, obstr)= if (param == nil) then nil else ( let V3DgetObjectByName c3dXsession param -> object in let V3DgetObjectTypeByName param -> iobjmode in ( set obstr.PLD_light = object; set obstr.PLD_iObjMode = iobjmode; ); ); 0;; fun cbEnable(inst, from, action, param, rep, obstr)= setPluginInstanceCbPreRender inst mkfun3 @cbPreRender obstr; setPluginInstanceCbClick inst mkfun6 @cbClick obstr; V3DenableNavigateControl c3dXsession 0; set obstr.PLD_bEnabled = 1; SendPluginEvent inst "Enabled" nil nil; 0;; fun cbDisable(inst, from, action, param, rep, obstr)= setPluginInstanceCbPreRender inst nil; setPluginInstanceCbClick inst nil; V3DenableNavigateControl c3dXsession 1; set obstr.PLD_bEnabled = 0; SendPluginEvent inst "Disabled" nil nil; 0;; fun newOb(inst)= let (getPluginInstanceParam inst "object") -> objname in let (atof (getPluginInstanceParam inst "angle")) -> angle in let if (angle == nil) then 90.0 else angle -> angle in let (atoi (getPluginInstanceParam inst "init")) -> init in let if (init == nil) then 0 else init -> init in let V3DgetObjectByName c3dXsession objname -> object in let V3DgetObjectTypeByName objname -> iobjmode in let if (object == nil) then (V3DgetObjectByName c3dXsession "default_light") else object -> object in let SO3ObjectGetOrientation object -> quat in let mkLightDirection[object iobjmode quat angle 0] -> obstr in ( if (init == 0) then nil else cbEnable inst nil nil nil nil obstr; PluginRegisterAction inst "Enable" mkfun6 @cbEnable obstr; PluginRegisterAction inst "Disable" mkfun6 @cbDisable obstr; PluginRegisterAction inst "Set light" mkfun6 @cbSetLight obstr; setPluginInstanceCbDel inst mkfun2 @deleteOb obstr; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;