home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / gfx / 3d / irit / scripts / trees.irt < prev    next >
Encoding:
Text File  |  1994-10-14  |  4.1 KB  |  145 lines

  1. #
  2. # A virtual tree generator.
  3. #
  4. #                Gershon Elber, June 1994.
  5. #
  6.  
  7. #
  8. # Factor of width reduction as we traverse the tree from its root to its
  9. # leaves, SFactor, and length reduction factor, LFactor.
  10. WFactor = 0.6;
  11. LFactor = 0.8;
  12.  
  13. #
  14. # Relative factor of branch rotation.
  15. RFactor = 1.0;
  16.  
  17. #
  18. # Colors of tree branches and leaves.
  19. BRGB = "244,164,96";
  20. LRGB = "50,255,50";
  21. BColor = 8;
  22. LColor = green;
  23.  
  24. #
  25. # A function to compute a unit vector perpendicular to the given vector.
  26. #
  27. PerpVector = function( V ):V1:
  28.     V1 = vector( coord( V, 1 ),
  29.          coord( V, 2 ),
  30.          coord( V, 0 ) ):
  31.     return = normalize( V1 ^ V );
  32.  
  33. #
  34. # Functions to change direction of V using a perpendicular to V.
  35. #
  36. RotateVector = function( V, Amount ):V1:
  37.     V1 = normalize( PerpVector( V ) ^ V ) * sqrt( V * V ):
  38.     if ( Amount > 0,
  39.      return = V + V1 * random( 0, Amount ),
  40.      return = V + V1 * random( Amount, 0 ) );
  41. RotateVector2 = function( V, Amount ):V1:V2:
  42.     V1 = normalize( PerpVector( V ) ):
  43.     V2 = normalize( V1 ^ V ) * sqrt( V * V ):
  44.     if ( Amount > 0,
  45.      return = V + V1 * random( 0, Amount ) + V2 * random( 0, Amount ),
  46.      return = V + V1 * random( Amount, 0 ) + V2 * random( Amount, 0 ) );
  47.  
  48. #
  49. # This function should define a branch from P1 to P2 with an approximated
  50. # radius of R.
  51. #
  52. TreeBranch = function(Pt1, Pt2, R):
  53.     return = SwpSclSrf( circle( vector( 0, 0, 0 ), 1 ),
  54.             coerce(Pt1, E3) + coerce(Pt2, E3),
  55.             ctlpt(E2, 0, R) + ctlpt(E2, 1, R * WFactor),
  56.             off,
  57.             1.0);
  58.  
  59. #
  60. # A recursive constructor of the tree2. Gets position of root of tree,
  61. # Direction of branch, Size of branch, Level of Branches, and recursion Level.
  62. #
  63. VirtTree2 = function(): return = 0; # Dummy function for recursive def.
  64. VirtTree2 = function( Pos, Dir, Size, BLevel, Level ) : NewPos:Tr1:Tr2:Tr:
  65. #   printf("%pf, %vf, %f, %f\n", list( Pos, Dir, Size, Level ) ):
  66.     return = nil():
  67.     NewPos = Pos + Dir:
  68.     if ( Level > 0,
  69.          Tr = treeBranch( Pos, NewPos, Size ):
  70.          if ( Level >= BLevel,
  71.           attrib( Tr, "COLOR", BColor ),
  72.           attrib( Tr, "COLOR", LColor ) ):
  73.      snoc( Tr, return ) ):
  74.     if ( Level > 1,
  75.      Tr1 = VirtTree2( NewPos,
  76.                   rotateVector( Dir, RFactor ) * LFactor,
  77.               Size * WFactor,
  78.               BLevel,
  79.               Level - 1 ):
  80.      Tr2 = VirtTree2( NewPos,
  81.                   rotateVector( Dir, -RFactor ) * LFactor,
  82.               Size * WFactor,
  83.               BLevel,
  84.               Level - 1 ):
  85.          return = return + Tr1 + Tr2 );
  86. #
  87. # A recursive constructor of the tree3. Gets position of root of tree,
  88. # Direction of branch, Size of branch,, Level of Branches and recursion Level.
  89. #
  90. VirtTree3 = function(): return = 0; # Dummy function for recursive def.
  91. VirtTree3 = function( Pos, Dir, Size, BLevel, Level ) : NewPos:Tr1:Tr2:Tr3:Tr:
  92. #   printf("%pf, %vf, %f, %f, %f\n", list( Pos, Dir, Size, BLevel, Level ) ):
  93.     return = nil():
  94.     NewPos = Pos + Dir:
  95.     if ( Level > 0,
  96.          Tr = treeBranch( Pos, NewPos, Size ):
  97.          if ( Level >= BLevel,
  98.           attrib( Tr, "COLOR", BColor ),
  99.           attrib( Tr, "COLOR", LColor ) ):
  100.      snoc( Tr, return ) ):
  101.     if ( Level > 1,
  102.      Tr1 = VirtTree3( NewPos,
  103.                   rotateVector2( Dir, RFactor ) * LFactor,
  104.               Size * WFactor,
  105.               BLevel,
  106.               Level - 1 ):
  107.      Tr2 = VirtTree3( NewPos,
  108.                   rotateVector2( Dir, RFactor * random( -1, 1 ) ) * LFactor,
  109.               Size * WFactor,
  110.               BLevel,
  111.               Level - 1 ):
  112.      Tr3 = VirtTree3( NewPos,
  113.                   rotateVector2( Dir, -RFactor ) * LFactor,
  114.               Size * WFactor,
  115.               BLevel,
  116.               Level - 1 ):
  117.          return = return + Tr1 + Tr2 + Tr3 );
  118.  
  119. save_mat = view_mat;
  120. view_mat = rotx( -90 ) * roty( 135 ) * rotx( -30 )
  121.     * scale( vector( 0.2, 0.2, 0.2 ) )
  122.     * trans( vector( 0, -0.5, 0 ) );
  123.  
  124. tree1 = VirtTree2( point( 0, 0, 0 ), vector( 0, 0, 1 ), 0.3, 4, 7);
  125. interact( list( view_mat, tree1 ) );
  126.  
  127. WFactor = 0.5;
  128. RFactor = 1.5;
  129. tree2 = VirtTree3( point( 0, 0, 0 ), vector( 0, 0, 1 ), 0.3, 3, 5);
  130. interact(tree2);
  131.  
  132. forest3 = function( n, m, BLevel, Level ):i:j:
  133.     return = nil():
  134.     for ( i = 0, 1, n,
  135.     for ( j = 0, 1, m,
  136.         snoc( VirtTree3( point( i * 5, j * 5, 0 ), vector( 0, 0, 1 ),
  137.                  0.3, BLevel, Level ),
  138.           return ) ) );
  139.  
  140. frst = forest3( 1, 1, 2, 4 );
  141. interact( frst );
  142.  
  143. # frst = forest3( 4, 4, 3, 5 );
  144. # save( "forest", frst );
  145.