/* ----------------------------------------------------------------------------- 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 ----------------------------------------------------------------------------- */ /******************************************************************************* Plugin Kinect Capture Player permettant de lire des media reconnus par Kinect Client part Version: 4.0 Author: Bastien BOURINEAU Date: 21/01/2007 Last update: 02/04/2009 *******************************************************************************/ var lAvailableModes = "None"::"RGB"::"Grayscale"::"Depth"::"Users"::"Hands"::"RGB Users"::nil;; struct PlugKinect = [ KIN_instance : PInstance, KIN_Device : ObjKinect, KIN_CamParam : ObjArCameraParam, KIN_lastCamera : SO3_OBJECT, KIN_bOnBackground : I, KIN_bOnMaterial : I, KIN_Material : SO3_MATERIAL, KIN_iTechnique : I, KIN_iPass : I, KIN_iTexture : I, KIN_win : SO3_WIDGET, KIN_iPosX : I, KIN_iPosY : I, KIN_iWidth : I, KIN_iHeight : I, KIN_tPercent : [I I I I I I I I], KIN_tAlign : [I I], KIN_bTransparency : I, KIN_iOpacity : I, KIN_Buffer : ObjBitmap, KIN_Buffer8 : ObjBitmap8, KIN_AlphaBuffer : AlphaBitmap, KIN_bMirror : I, KIN_fSmooth : F, KIN_tCutoff : [I F], KIN_bArEnabled : I, KIN_bArDebug : I, KIN_iMode : I, KIN_bFlipPicture : I, KIN_bShow : I ] MkPlugKinect;; fun restoreTexture(kinstr)= setPluginInstanceCbPreRender kinstr.KIN_instance nil; V3DremoveWidgetControl (V3DgetDefaultViewport (V3DgetSessionView c3dXsession)) kinstr.KIN_win; SO3WidgetDestroy kinstr.KIN_win; set kinstr.KIN_win = nil; _DSbitmap kinstr.KIN_Buffer; _DSbitmap8 kinstr.KIN_Buffer8; _DSalphaBitmap kinstr.KIN_AlphaBuffer; set kinstr.KIN_Buffer = nil; set kinstr.KIN_Buffer8 = nil; set kinstr.KIN_AlphaBuffer = nil; 0;; fun computePosSize(kinstr, vw, vh)= let kinstr.KIN_tPercent -> [px py pw ph xo yo wo ho] in let kinstr.KIN_tAlign -> [ax ay] in let if pw then (ftoi (((itof kinstr.KIN_iWidth) /. 100.0) *. (itof vw))) + wo else kinstr.KIN_iWidth -> pw in let if ph then (ftoi (((itof kinstr.KIN_iHeight) /. 100.0) *. (itof vh))) + ho else kinstr.KIN_iHeight -> ph in let if px then (ftoi (((itof kinstr.KIN_iPosX) /. 100.0) *. (itof vw))) + xo else kinstr.KIN_iPosX -> px in let if (ax == 1) then ((vw / 2) - (pw / 2)) + px else if (ax == 2) then (vw - pw) - px else px -> px in let if py then (ftoi (((itof kinstr.KIN_iPosY) /. 100.0) *. (itof vh))) + yo else kinstr.KIN_iPosY -> py in let if (ay == 1) then ((vh / 2) - (ph / 2)) + py else if (ay == 2) then (vh - ph) - py else py -> py in [px py pw ph];; /*! \brief Callback on "connected" dms action * * Get event when Kinect is connected * * Prototype: fun [PInstance DMI S S I [F F F I]] I * * \param OBJKinect : Kinect object * \param PInstance : plugIT instance * * \return I : 0 **/ fun cbConnected(kinect, kinstr)= let _GETKinectDeviceSize kinstr.KIN_Device -> [width height] in let V3DgetSessionView c3dXsession -> viewstr in let V3DgetDefaultViewport viewstr -> viewportstr in ( set kinstr.KIN_Buffer = _FILLbitmap _CRbitmap _channel width height 0; set kinstr.KIN_Buffer8 = _FILLbitmap8 _CRbitmap8 _channel width height 0; set kinstr.KIN_AlphaBuffer = _CRalphaBitmap _channel kinstr.KIN_Buffer kinstr.KIN_Buffer8 nil nil; if (!kinstr.KIN_bArEnabled) then nil else ( set kinstr.KIN_CamParam = _CRarCameraParam _channel width height nil nil nil /*(_checkpack strcat (getPluginDirectory (getInstancePlugin inst)) "/rgb.yaml")*/; if ((!kinstr.KIN_bArEnabled) || (!kinstr.KIN_bOnBackground)) then nil else let V3DgetViewportSize viewstr viewportstr -> [_ _ w h] in let SO3ViewportGetCamera viewportstr.V3D_viewport -> camera in ( set kinstr.KIN_lastCamera = camera; //TODO set KIN_CamParam clipping SO3CameraSetProjectionMatrix kinstr.KIN_lastCamera _GETarProjectionMatrix kinstr.KIN_CamParam w h; ); ); if (kinstr.KIN_iMode == 0) then nil else ( if kinstr.KIN_win != nil then nil else ( V3DremoveWidgetControl viewportstr kinstr.KIN_win; SO3WidgetDestroy kinstr.KIN_win; if kinstr.KIN_bOnMaterial then ( set kinstr.KIN_win = SO3BitmapWidgetCreateOnMaterial (V3DgetSession c3dXsession) kinstr.KIN_Material (strcat (getPluginInstanceName kinstr.KIN_instance) "_kinectCtrl") kinstr.KIN_iWidth kinstr.KIN_iHeight kinstr.KIN_iTechnique kinstr.KIN_iPass kinstr.KIN_iTexture; 0; ) else if kinstr.KIN_bOnBackground then ( let V3DgetViewportSize viewstr viewportstr -> [_ _ vw vh] in let computePosSize kinstr vw vh -> [px py pw ph] in set kinstr.KIN_win = SO3BitmapWidgetCreateBackground (V3DgetSession c3dXsession) viewportstr.V3D_viewport (strcat (getPluginInstanceName kinstr.KIN_instance) "_kinectCtrl") px py pw ph; SO3WidgetSetTopOnFocus kinstr.KIN_win 0; ) else ( let V3DgetViewportSize viewstr viewportstr -> [_ _ vw vh] in let computePosSize kinstr vw vh -> [px py pw ph] in set kinstr.KIN_win = SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport (strcat (getPluginInstanceName kinstr.KIN_instance) "_kinectCtrl") px py pw ph 100; SO3WidgetSetTopOnFocus kinstr.KIN_win 0; SO3WidgetSetForeground kinstr.KIN_win 1; ); V3DaddWidgetControl viewportstr kinstr.KIN_win; SO3WidgetSetTransparency kinstr.KIN_win kinstr.KIN_bTransparency; SO3WidgetSetKeyboardEnable kinstr.KIN_win 0; SO3WidgetSetMouseEnable kinstr.KIN_win 0; SO3WidgetSetOpacity kinstr.KIN_win (itof kinstr.KIN_iOpacity) *. 0.01; ); ); // manage AR capture size for marker plugIT if (!kinstr.KIN_bArEnabled) then nil else ( set c3dxCameraSize = [width height]; set c3dxCameraFlip = !kinstr.KIN_bFlipPicture; ); SendPluginEvent kinstr.KIN_instance "Connected" (itoa _GETKinectDeviceId kinect) nil; ); 0;; /*! \brief Callback on "disconnected" dms action * * Get event when Kinect is disconnected * * Prototype: fun [PInstance DMI S S I [F F F I]] I * * \param OBJKinect : Kinect object * \param PInstance : plugIT instance * * \return I : 0 **/ fun cbDisconnected(kinect, kinstr)= restoreTexture kinstr; SendPluginEvent kinstr.KIN_instance "Disconnected" nil nil; 0;; fun cbRenderKinect(inst, viewstr, kinstr)= // RGB mode if (((kinstr.KIN_iMode == 1) && SO3WidgetGetVisibility kinstr.KIN_win) || kinstr.KIN_bArEnabled) then ( _BLTKinectDeviceRGB kinstr.KIN_Device kinstr.KIN_Buffer; ) else nil; // Greyscale mode if (kinstr.KIN_iMode == 2) && SO3WidgetGetVisibility kinstr.KIN_win then ( _BLTKinectDeviceGrayscale kinstr.KIN_Device kinstr.KIN_Buffer; 0; ) // Depth mode else if (kinstr.KIN_iMode == 3) && SO3WidgetGetVisibility kinstr.KIN_win then ( _BLTKinectDeviceDepth kinstr.KIN_Device kinstr.KIN_Buffer; 0; ) //users mode else if (kinstr.KIN_iMode == 4) && SO3WidgetGetVisibility kinstr.KIN_win then ( _BLTKinectDeviceUsers kinstr.KIN_Device kinstr.KIN_AlphaBuffer; 0; ) //hands mode else if (kinstr.KIN_iMode == 5) && SO3WidgetGetVisibility kinstr.KIN_win then ( _BLTKinectDeviceHands kinstr.KIN_Device kinstr.KIN_AlphaBuffer; 0; ) //user RGB mode else if (kinstr.KIN_iMode == 6) && SO3WidgetGetVisibility kinstr.KIN_win then ( _BLTKinectDeviceUsersRGB kinstr.KIN_Device kinstr.KIN_AlphaBuffer; 0; ) else nil; //show asked picture even if AR is enable if (((SO3WidgetGetVisibility kinstr.KIN_win) && (kinstr.KIN_iMode != 0)) || (kinstr.KIN_bArEnabled)) then ( if (!kinstr.KIN_bFlipPicture) then nil else ( if (kinstr.KIN_iMode >= 4) then let _GETalphaBitmaps kinstr.KIN_AlphaBuffer -> [rgb alpha] in ( _BTFLIPbitmap rgb; _BTFLIPbitmap8 alpha; 0; ) else ( _BTFLIPbitmap kinstr.KIN_Buffer; 0; ); ); if (!kinstr.KIN_bArEnabled) then nil else ( //update camera setting on current camera change if (!kinstr.KIN_bOnBackground) then nil else let V3DgetDefaultViewport viewstr -> viewportstr in let V3DgetViewportSize viewstr viewportstr -> [_ _ w h] in let SO3ViewportGetCamera viewportstr.V3D_viewport -> camera in ( if (camera == kinstr.KIN_lastCamera) then nil else ( //reset camera matrix SO3CameraSetProjectionMatrix kinstr.KIN_lastCamera nil; set kinstr.KIN_lastCamera = camera; //TODO set KIN_CamParam clipping SO3CameraSetProjectionMatrix kinstr.KIN_lastCamera _GETarProjectionMatrix kinstr.KIN_CamParam w h; ); ); // kinect is mirrored by default if (kinstr.KIN_bFlipPicture) then nil else _BTFLIPbitmap kinstr.KIN_Buffer; _UPDATEarMarkers kinstr.KIN_Buffer kinstr.KIN_CamParam !kinstr.KIN_bFlipPicture kinstr.KIN_bArDebug; // replace mirror mode if (kinstr.KIN_bFlipPicture) then nil else _BTFLIPbitmap kinstr.KIN_Buffer; ); if (kinstr.KIN_iMode >= 4) then ( SO3BitmapWidgetBlitAlpha kinstr.KIN_win kinstr.KIN_AlphaBuffer; 0; ) else ( SO3BitmapWidgetBlit kinstr.KIN_win kinstr.KIN_Buffer; 0; ); ) else nil; 0;; fun cbShow(inst, from, action, param, reply, kinstr)= SO3WidgetSetVisibility kinstr.KIN_win 1; set kinstr.KIN_bShow = 1; 0;; fun cbHide(inst, from, action, param, reply, kinstr)= SO3WidgetSetVisibility kinstr.KIN_win 0; set kinstr.KIN_bShow = 0; 0;; fun cbResizeCtrl(inst, viewstr, vw, vh, kinstr)= if kinstr.KIN_win == nil then nil else ( //update camera setting on current camera change if ((!kinstr.KIN_bOnBackground) || (!kinstr.KIN_bArEnabled) || (kinstr.KIN_iMode == 0)) then nil else let V3DgetDefaultViewport viewstr -> viewportstr in let V3DgetViewportSize viewstr viewportstr -> [_ _ w h] in let SO3ViewportGetCamera viewportstr.V3D_viewport -> camera in ( if (camera == kinstr.KIN_lastCamera) then nil else ( //reset camera matrix SO3CameraSetProjectionMatrix kinstr.KIN_lastCamera nil; set kinstr.KIN_lastCamera = camera; ); //TODO set KIN_CamParam clipping SO3CameraSetProjectionMatrix kinstr.KIN_lastCamera _GETarProjectionMatrix kinstr.KIN_CamParam w h; ); if (kinstr.KIN_bOnMaterial) then nil else let computePosSize kinstr vw vh -> [px py pw ph] in ( SO3WidgetSetPosition kinstr.KIN_win px py; SO3WidgetSetSize kinstr.KIN_win pw ph; ); ); 0;; fun cbSetPosition(inst, from, action, param, reply, kinstr)= if param == nil then nil else let strextr param -> lp in let (nth_list (hd lp) 0) -> sx in let (nth_list (hd lp) 1) -> sy in let V3DgetViewSize (V3DgetSessionView c3dXsession) -> [vw vh] in ( let 0 -> xpercent in let if (!strcmp "%" (substr sx ((strlen sx) - 1) 1)) then ( set xpercent = 1; atoi sx; ) else atoi sx -> px in let 0 -> ypercent in let if (!strcmp "%" (substr sy ((strlen sy) - 1) 1)) then ( set ypercent = 1; atoi sy; ) else atoi sy -> py in let [(if px == nil then 0 else px) (if py == nil then 0 else py)] -> [px py] in ( mutate kinstr.KIN_tPercent <- [xpercent ypercent _ _ _ _ _ _]; set kinstr.KIN_iPosX = px; set kinstr.KIN_iPosY = py; let computePosSize kinstr vw vh -> [px py pw ph] in ( SO3WidgetSetPosition kinstr.KIN_win px py; SO3WidgetSetSize kinstr.KIN_win pw ph; ); ); ); 0;; fun cbSetSize(inst, from, action, param, reply, kinstr)= if param == nil then nil else let strextr param -> lp in let (nth_list (hd lp) 0) -> sx in let (nth_list (hd lp) 1) -> sy in let V3DgetViewSize (V3DgetSessionView c3dXsession) -> [vw vh] in ( let 0 -> wpercent in let if (!strcmp "%" (substr sx ((strlen sx) - 1) 1)) then ( set wpercent = 1; atoi sx; ) else atoi sx -> px in let 0 -> hpercent in let if (!strcmp "%" (substr sy ((strlen sy) - 1) 1)) then ( set hpercent = 1; atoi sy; ) else atoi sy -> py in let [(if px == nil then 0 else px) (if py == nil then 0 else py)] -> [px py] in ( mutate kinstr.KIN_tPercent <- [_ _ wpercent hpercent _ _ _ _]; set kinstr.KIN_iWidth = px; set kinstr.KIN_iHeight = py; let computePosSize kinstr vw vh -> [px py pw ph] in ( SO3WidgetSetPosition kinstr.KIN_win px py; SO3WidgetSetSize kinstr.KIN_win pw ph; ); ); ); 0;; fun connectKinect(inst, from, action, param, rep, kinstr)= if (kinstr.KIN_Device != nil) then nil else ( set kinstr.KIN_Device = _CRKinectDevice _channel; _SETKinectDeviceMirror kinstr.KIN_Device kinstr.KIN_bMirror; let kinstr.KIN_tCutoff -> [dist ang] in ( _SETKinectDeviceSkeletonSmoothing kinstr.KIN_Device kinstr.KIN_fSmooth; _SETKinectDeviceDetectionDistance kinstr.KIN_Device dist; _SETKinectDeviceDetectionAngle kinstr.KIN_Device ang; ); _CBKinectConnected kinstr.KIN_Device @cbConnected kinstr; _CBKinectDisconnected kinstr.KIN_Device @cbDisconnected kinstr; if (!kinstr.KIN_bArEnabled) && (kinstr.KIN_iMode == 0) then nil else setPluginInstanceCbPreRender inst mkfun3 @cbRenderKinect kinstr; ); 0;; fun disconnectKinect(inst, from, action, param, rep, kinstr)= restoreTexture kinstr; if (kinstr.KIN_Device == nil) then nil else ( _DSKinectDevice kinstr.KIN_Device; set kinstr.KIN_Device = nil; _DSarCameraParam kinstr.KIN_CamParam; set kinstr.KIN_CamParam = nil; //reset camera matrix if (kinstr.KIN_lastCamera == nil) then nil else ( SO3CameraSetProjectionMatrix kinstr.KIN_lastCamera nil; set kinstr.KIN_lastCamera = nil; ); ); 0;; fun cbSetOutputMode(inst, from, action, param, rep, kinstr)= let getStringPosInListi lAvailableModes param -> pos in if (pos == nil) then nil else ( set kinstr.KIN_iMode = pos; if (pos == 0) then SO3WidgetSetVisibility kinstr.KIN_win 0 else SO3WidgetSetVisibility kinstr.KIN_win kinstr.KIN_bShow; ); 0;; // Destroy instance fun deleteOb(inst, kinstr) = disconnectKinect inst nil nil nil nil kinstr; SendPluginEvent inst "Disconnected" nil nil; 0;; fun cbNewOb(inst)= let atoi (getPluginInstanceParam inst "istexture") -> istexture in let if istexture == nil then 0 else istexture -> istexture in let (getPluginInstanceParam inst "object") -> objname in let (getPluginInstanceParam inst "material") -> matname in let atoi (getPluginInstanceParam inst "technique") -> technique in let if technique == nil then 0 else technique -> technique in let atoi (getPluginInstanceParam inst "pass") -> pass in let if pass == nil then 0 else pass -> pass in let atoi (getPluginInstanceParam inst "texture") -> texture in let if texture == nil then 0 else texture -> texture in let atoi (getPluginInstanceParam inst "background") -> background in let if background == nil then 0 else background -> background in let atoi (getPluginInstanceParam inst "posx") -> posx in let atoi (getPluginInstanceParam inst "posy") -> posy in let atoi (getPluginInstanceParam inst "width") -> width in let atoi (getPluginInstanceParam inst "height") -> height in let atoi (getPluginInstanceParam inst "xpercent") -> xpercent in let atoi (getPluginInstanceParam inst "ypercent") -> ypercent in let atoi (getPluginInstanceParam inst "wpercent") -> wpercent in let atoi (getPluginInstanceParam inst "hpercent") -> hpercent in let atoi (getPluginInstanceParam inst "xoffset") -> xoffset in let if xoffset == nil then 0 else xoffset -> xoffset in let atoi (getPluginInstanceParam inst "yoffset") -> yoffset in let if yoffset == nil then 0 else yoffset -> yoffset in let atoi (getPluginInstanceParam inst "woffset") -> woffset in let if woffset == nil then 0 else woffset -> woffset in let atoi (getPluginInstanceParam inst "hoffset") -> hoffset in let if hoffset == nil then 0 else hoffset -> hoffset in let atoi (getPluginInstanceParam inst "xalign") -> xalign in let atoi (getPluginInstanceParam inst "yalign") -> yalign in let atoi (getPluginInstanceParam inst "transparency") -> transparency in let atoi (getPluginInstanceParam inst "opacity") -> opacity in let atoi (getPluginInstanceParam inst "mode") -> mode in let atoi (getPluginInstanceParam inst "mirror") -> mirror in let atoi (getPluginInstanceParam inst "flippicture") -> flippicture in let atof (getPluginInstanceParam inst "smooth") -> smooth in let if (smooth == nil) then 0.6 else smooth -> smooth in let atoi (getPluginInstanceParam inst "distcutoff") -> distcutoff in let if (distcutoff == nil) then 1000 else distcutoff -> distcutoff in let atof (getPluginInstanceParam inst "angcutoff") -> angcutoff in let if (angcutoff == nil) then 0.0 else angcutoff -> angcutoff in let atoi (getPluginInstanceParam inst "arenabled") -> arenabled in let if (arenabled == nil) then 0 else arenabled -> arenabled in let atoi (getPluginInstanceParam inst "ardebug") -> ardebug in let if (ardebug == nil) then 0 else ardebug -> ardebug in let atoi (getPluginInstanceParam inst "init") -> oninit in let SO3SceneGetObject (V3DgetSession c3dXsession) objname -> obj in let if !istexture then nil else SO3SceneGetMaterial (V3DgetSession c3dXsession) (SO3EntityGetResourceGroup obj) matname -> mat in let MkPlugKinect [inst nil nil nil background istexture mat technique pass texture nil posx posy width height [xpercent ypercent wpercent hpercent xoffset yoffset woffset hoffset] [xalign yalign] transparency opacity nil nil nil mirror smooth [distcutoff angcutoff] arenabled ardebug mode flippicture 1] -> kinstr in ( if kinstr.KIN_bOnMaterial then nil else setPluginInstanceCbResizeView inst mkfun5 @cbResizeCtrl kinstr; if !oninit then nil else connectKinect inst nil nil nil nil kinstr; PluginRegisterAction inst "Connect" mkfun6 @connectKinect kinstr; PluginRegisterAction inst "Disconnect" mkfun6 @disconnectKinect kinstr; PluginRegisterAction inst "Show" mkfun6 @cbShow kinstr; PluginRegisterAction inst "Hide" mkfun6 @cbHide kinstr; PluginRegisterAction inst "Set output mode" mkfun6 @cbSetOutputMode kinstr; PluginRegisterAction inst "Set position" mkfun6 @cbSetPosition kinstr; PluginRegisterAction inst "Set size" mkfun6 @cbSetSize kinstr; setPluginInstanceCbDel inst mkfun2 @deleteOb kinstr; ); 0;; fun IniPlug(file) = PlugRegister @cbNewOb nil; setPluginEditor @dynamicedit; 0;;