/* ShuffleTexture Plugin - DMS - March 99 - by Sylvain HUET */


typeof class=S;;
var Pinitab =64;; /* valeur par defaut de la taille du tableau */



fun initab2(i, k, param) =

 let param -> [ ray ray2] in
 let itof ray -> ray in
 let itof ray2 -> ray2 in
 
 let (itof i)*.k ->a in
 let (itof i+1)*.k->b in
 let [ray*.cos a ray*.sin a]->[a1 b1] in
 let [ray2*.cos b ray2*.sin b]->[a2 b2] in
 [ftoi a2-.a1 ftoi b2-.b1]
;;


fun initab(n,ray,ray2) =
  
  create_tab n mkfun3 @initab2 [ray ray2] (itof 2)*.PIf/.(itof n) 
;;



fun rotTexture2(l, param) =
 
 let param -> [ base rottab] in
 if l==nil then 
   nil
 else 
   let l->[[vrt u v] nxt] in
 let rottab.((5*vrt+base)&63) ->[du dv] in
 [vrt u+du v+dv]::rotTexture2 nxt param
;;


fun rotTexture(l, param) =
 
 let param -> [ base rottab] in 
 if l==nil then 
   nil
 else 
   let l->[[hmat poly] nxt] in
   [hmat rotTexture2 poly param]::rotTexture nxt param
;;


fun GetPolygonsByMaterial (polyList, targetMat, session) =
  
  if polyList == nil then
    nil
  else if targetMat == nil then /* all materials */
    polyList
  else
    let hd polyList -> [currMat _] in
      (if currMat == targetMat then (hd polyList) else nil)::
      (GetPolygonsByMaterial tl polyList targetMat session)
;;


fun shuffleobj2(a, v) =

  match a with
  (objAnchor [h m _ _] -> M3setPolygons session h rotTexture (GetPolygonsByMaterial (M3listOfPolygons session h) m session) v)
  |(_->nil)
;;

 
fun shuffleobj(o, v) =

  let v-> [base rottab] in
  (
    apply_on_list ObAnchor o @shuffleobj2 v;
    mutate v<-[base+1 _]
  );

  0
;;

 
fun newOb(o)=

  let atoi hd UgetParam ObUi o "ray1" -> Pray in
  let atoi hd UgetParam ObUi o "ray2" -> Pray2 in
  /*let atoi hd UgetParam ObUi o "initab" -> Pinitab in*/
  let initab Pinitab Pray Pray2 -> rottab in
    
  ObCbAnim o mkfun2 @shuffleobj [-1 rottab];
  0
;;  

fun IniPlug(file)=

  set class=getInfo strextr _getpack _checkpack file "name";
  PlugRegister class @newOb nil;
  0
;;