Author Topic: Body Tree  (Read 3399 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Body Tree
« on: August 14, 2020, 02:22:48 am »
Has anyone seen this before (besides Aurel)? We played with it for awhile at BP.org 2015 or 2016.

Code: QB64: [Select]
  1. _TITLE "Body Tree recursive fill"
  2. 'BodyTree recur fill.bas for SmallBASIC [B+=MGA] 2016-06-20
  3. 'copied and translated from body tree by PeterMaria
  4. '2016-06-20 even more color mods including using triangle fills
  5. ' 2020-08-11 trans to QB64
  6.  
  7. CONST xmax = 1200, ymax = 700
  8.  
  9. SCREEN _NEWIMAGE(xmax, ymax, 32)
  10. _DELAY .25
  11.  
  12.  
  13. DIM SHARED limit
  14. limit = 14
  15.  
  16. 'sky
  17. COLOR 0, _RGB32(40, 100, 180): CLS
  18.  
  19. 'the hill for tree
  20. ax0 = xmax / 2 - ymax / 10
  21. ay0 = ymax - 40
  22. bx0 = xmax / 2 + ymax / 10
  23.  
  24. 'hill
  25. FOR r = xmax / 1.5 TO 2 * xmax / 15 STEP -10
  26.     EllipseFill xmax / 2, ay0, r, .15 * r, _RGB32(0, r / 5, 0)
  27.     'circle , .1, rgb(0, r / 5, 0) filled
  28.  
  29. 'tree
  30. BodyTree ax0, ay0, bx0, ay0, 0
  31.  
  32.  
  33. SUB BodyTree (ax, ay, bx, by, rc)
  34.     cx = ax - ay + by
  35.     cy = ax + ay - bx
  36.     dx = bx + by - ay
  37.     dy = ax - bx + by
  38.     ex = 0.5 * (cx - cy + dx + dy)
  39.     ey = 0.5 * (cx + cy - dx + dy)
  40.     k = _RGB32((15 - rc) * 8, 64 + rc * 8, .25 * (15 - rc) * 12)
  41.     ftri cx, cy, ax, ay, bx, by, k
  42.     ftri bx, by, dx, dy, cx, cy, k
  43.     ftri cx, cy, dx, dy, ex, ey, k
  44.     IF rc < limit THEN
  45.         BodyTree cx, cy, ex, ey, rc + 1
  46.         BodyTree ex, ey, dx, dy, rc + 1
  47.     END IF
  48.  
  49.  
  50. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  51.     DIM D AS LONG
  52.     STATIC a&
  53.     D = _DEST
  54.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  55.     _DEST a&
  56.     _DONTBLEND a& '  '<<<< new 2019-12-16 fix
  57.     PSET (0, 0), K
  58.     _BLEND a& '<<<< new 2019-12-16 fix
  59.     _DEST D
  60.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  61.  
  62. SUB EllipseFill (CX AS INTEGER, CY AS INTEGER, a AS INTEGER, b AS INTEGER, C AS _UNSIGNED LONG)
  63.     ' CX = center x coordinate
  64.     ' CY = center y coordinate
  65.     '  a = semimajor axis
  66.     '  b = semiminor axis
  67.     '  C = fill color
  68.     IF a = 0 OR b = 0 THEN EXIT SUB
  69.     DIM h2 AS _INTEGER64
  70.     DIM w2 AS _INTEGER64
  71.     DIM h2w2 AS _INTEGER64
  72.     DIM x AS INTEGER
  73.     DIM y AS INTEGER
  74.     w2 = a * a
  75.     h2 = b * b
  76.     h2w2 = h2 * w2
  77.     LINE (CX - a, CY)-(CX + a, CY), C, BF
  78.     DO WHILE y < b
  79.         y = y + 1
  80.         x = SQR((h2w2 - y * y * w2) \ h2)
  81.         LINE (CX - x, CY + y)-(CX + x, CY + y), C, BF
  82.         LINE (CX - x, CY - y)-(CX + x, CY - y), C, BF
  83.     LOOP
  84.  
  85.  
  86.  
  87.  


Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: Body Tree
« Reply #1 on: August 14, 2020, 04:57:17 am »
Maybe ...i doubt that people from here visit bp.org forum.
Looking into Ashish algos i figured that is possible to make some fractals without recursion and without
subroutines/functions ...but this one hmmm..i can try only...
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Body Tree
« Reply #2 on: August 14, 2020, 12:07:02 pm »
I have a bunch of versions including non recursive and your (Aurel's) Cherry Tree one.

I reworked the code from scratch and can draw an animation now. Paul Dunn was first to show one of those, blew my mind at time:
Code: QB64: [Select]
  1. _TITLE "BodyTree Rework 2020-08" 'b+ 2020-08-14
  2. 'BodyTree recur fill.bas for SmallBASIC [B+=MGA] 2016-06-20
  3. 'copied and translated from body tree by PeterMaria
  4. '2016-06-20 even more color mods including using triangle fills
  5. ' 2020-08-11 trans to QB64
  6. ' 2020-08-14 rework the recursive sub so can flex the body tree
  7.  
  8. CONST xmax = 1200, ymax = 700
  9.  
  10. SCREEN _NEWIMAGE(xmax, ymax, 32)
  11. _DELAY .25
  12.  
  13.  
  14. DIM SHARED limit, ra
  15. limit = 14
  16. ra = _PI / -4 '45 degrees is standard angle for roof
  17.  
  18. 'sky
  19. COLOR 0, _RGB32(40, 100, 180): CLS
  20.  
  21. 'the hill for tree
  22. ax0 = xmax / 2 - ymax / 10
  23. ay0 = ymax - 40
  24. bx0 = xmax / 2 + ymax / 10
  25.  
  26.  
  27. FOR ra = 0 TO _PI / 3.7 STEP _PI / 64
  28.     CLS
  29.     level = 0
  30.     'hill
  31.     FOR r = xmax / 1.5 TO 2 * xmax / 15 STEP -10
  32.         EllipseFill xmax / 2, ay0, r, .15 * r, _RGB32(0, r / 5, 0)
  33.         'circle , .1, rgb(0, r / 5, 0) filled
  34.     NEXT
  35.  
  36.     'tree
  37.     BodyTree ax0, ay0, bx0, ay0, level
  38.     _DISPLAY
  39.     _LIMIT 5
  40.  
  41. SUB BodyTree (x1, y1, x2, y2, level)
  42.     'dim shared ra = roof angle 0 to PI/4
  43.     L = _HYPOT(x2 - x1, y2 - y1)
  44.     pa = _ATAN2(y2 - y1, x2 - x1) - _PI / 2 'perpendicular angle to base line
  45.     x3 = x1 + L * COS(pa)
  46.     y3 = y1 + L * SIN(pa)
  47.     x4 = x2 + L * COS(pa)
  48.     y4 = y2 + L * SIN(pa)
  49.     'build roof to square
  50.     mx = (x3 + x4) / 2
  51.     my = (y3 + y4) / 2
  52.     adj = _HYPOT(x3 - mx, y3 - my)
  53.     raise = TAN(ra) * adj
  54.     x5 = mx + raise * COS(pa)
  55.     y5 = my + raise * SIN(pa)
  56.  
  57.     'now that we have our drawing points draw our house
  58.     'LINE (x2, y2)-(x1, y1), &HFFFFFFFF 'house base
  59.     'LINE (x1, y1)-(x3, y3), &HFFFFFFFF ' left wall
  60.     'LINE (x3, y3)-(x5, y5), &HFFFFFFFF 'left peak
  61.     'LINE (x5, y5)-(x4, y4), &HFFFFFFFF ' right peak
  62.     'LINE (x4, y4)-(x2, y2), &HFFFFFFFF ' right wall completes house
  63.  
  64.     k = _RGB32((15 - level) * 8, 64 + level * 8, .25 * (15 - level) * 12, 255 - level * 18)
  65.     ftri x1, y1, x2, y2, x3, y3, k
  66.     ftri x2, y2, x3, y3, x4, y4, k
  67.     ftri x3, y3, x4, y4, x5, y5, k
  68.  
  69.     IF level < limit THEN
  70.         BodyTree x3, y3, x5, y5, level + 1
  71.         BodyTree x5, y5, x4, y4, level + 1
  72.     END IF
  73.  
  74.  
  75. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  76.     DIM D AS LONG
  77.     STATIC a&
  78.     D = _DEST
  79.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  80.     _DEST a&
  81.     _DONTBLEND a& '  '<<<< new 2019-12-16 fix
  82.     PSET (0, 0), K
  83.     _BLEND a& '<<<< new 2019-12-16 fix
  84.     _DEST D
  85.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  86.  
  87. SUB EllipseFill (CX AS INTEGER, CY AS INTEGER, a AS INTEGER, b AS INTEGER, C AS _UNSIGNED LONG)
  88.     ' CX = center x coordinate
  89.     ' CY = center y coordinate
  90.     '  a = semimajor axis
  91.     '  b = semiminor axis
  92.     '  C = fill color
  93.     IF a = 0 OR b = 0 THEN EXIT SUB
  94.     DIM h2 AS _INTEGER64
  95.     DIM w2 AS _INTEGER64
  96.     DIM h2w2 AS _INTEGER64
  97.     DIM x AS INTEGER
  98.     DIM y AS INTEGER
  99.     w2 = a * a
  100.     h2 = b * b
  101.     h2w2 = h2 * w2
  102.     LINE (CX - a, CY)-(CX + a, CY), C, BF
  103.     DO WHILE y < b
  104.         y = y + 1
  105.         x = SQR((h2w2 - y * y * w2) \ h2)
  106.         LINE (CX - x, CY + y)-(CX + x, CY + y), C, BF
  107.         LINE (CX - x, CY - y)-(CX + x, CY - y), C, BF
  108.     LOOP
  109.  
  110.  
  111.  
  112.  


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Body Tree
« Reply #3 on: August 14, 2020, 12:54:58 pm »
Hey Aurel, I sweetened the cherries of your Tree a bit!

Code: QB64: [Select]
  1. _TITLE "Mod Aurel Mod of Java Cherry Tree" ' b+ 2020-08-14
  2. 'bodytree by Aurel /java
  3. 'mods 2016-06-26 for SmallBASIC 0.12.6 [B+=MGA]
  4. 'float limit
  5. ' 2020-08-14 bring to QB64 trans as is leave original code lines not using commented
  6.  
  7. DIM SHARED limit
  8. limit = 7 'wcolor 0,0,0  frontpen 220,200,0
  9. 'print 10,10,"Java Cherry Tree"
  10. SCREEN _NEWIMAGE(800, 600, 32)
  11. _DELAY .25
  12. drawtree 800 / 2 - 800 / 12, 600 - 40, 800 / 2 + 800 / 12, 600 - 40, 1
  13. 'pause
  14.  
  15. SUB drawtree (x1, y1, x2, y2, depth)
  16.     'float dx,dy,x3,y3,x4,y4,x5,y5
  17.     'local dx,dy, x3,y3, x4,y4, x5,y5
  18.     dx = x2 - x1
  19.     dy = y1 - y2
  20.  
  21.     x3 = x2 - dy
  22.     y3 = y2 - dx
  23.     x4 = x1 - dy
  24.     y4 = y1 - dx
  25.     x5 = x4 + 0.5 * (dx - dy)
  26.     y5 = y4 - 0.5 * (dx + dy)
  27.  
  28.     'square
  29.     'frontpen 220,200,0
  30.     IF depth = 1 THEN LINE (x1, y1)-(x2, y2)
  31.     LINE (x2, y2)-(x3, y3)
  32.     'line x3,y3,x4,y4
  33.     LINE (x4, y4)-(x1, y1)
  34.     'trinagle
  35.     'frontpen 0,160,0
  36.     'line x3,y3,x4,y4 '<<<<<<<<<<<< just leave out
  37.     'line x4,y4,x5,y5
  38.     'frontpen 220,120,0
  39.     ' just at the end
  40.     IF depth = limit THEN
  41.         LINE (x4, y4)-(x5, y5)
  42.         LINE (x3, y3)-(x4, y4)
  43.         'circle x5, y5, 4, 1, 12 filled
  44.         FOR r = 0 TO 6 STEP .2
  45.             CIRCLE (x5, y5), r, _RGB32(255 - 25 * r, 0, 0)
  46.         NEXT
  47.     END IF
  48.  
  49.     IF depth < limit THEN
  50.         drawtree x4, y4, x5, y5, depth + 1
  51.         drawtree x5, y5, x3, y3, depth + 1
  52.  
  53.     END IF
  54.  
  55.  
  56.  
  57.  

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Body Tree
« Reply #4 on: August 14, 2020, 01:06:04 pm »
These are incredible.

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: Body Tree
« Reply #5 on: August 15, 2020, 02:55:32 am »
That is really nice Mark !!!
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Body Tree
« Reply #6 on: August 16, 2020, 10:31:28 am »
Some great fractal tree there...

Here's my version which I coded sometime ago.

1. Simple tree with changing base-angle
Code: QB64: [Select]
  1. 'Coded By Ashish with <3
  2. _TITLE "Fractal_Trees"
  3. SCREEN _NEWIMAGE(800, 600, 32)
  4. radius = 130
  5.     CLS
  6.     drawTree 400, 400, radius, _PI(3 / 2), s
  7.     internalp5line 400, 600, 400, 400, radius / 10, _RGB(160, 10, 10)
  8.     _DISPLAY
  9.     _LIMIT 40
  10.     s = ABS(SIN(v#)) * 0.25 + 0.2
  11.     v# = v# + 0.01
  12.  
  13. SUB drawTree (x, y, r, a, s)
  14.     IF r < 18 AND r > 3 THEN c~& = _RGB(10, 200, 10) ELSE c~& = _RGB(160, 10, 10)
  15.     IF r < 3 THEN c~& = _RGB(200, 0, 200)
  16.     internalp5line x, y, x + r * COS(a - s), y + r * SIN(a - s), r / 10, c~&
  17.     internalp5line x, y, x + r * COS(a + s * 3), y + r * SIN(a + s * 3), r / 10, c~&
  18.  
  19.     IF r > 2 THEN
  20.         drawTree x + r * COS(a - s), y + r * SIN(a - s), r * 0.67, a - s, s
  21.         drawTree x + r * COS(a + s * 3), y + r * SIN(a + s * 3), r * 0.67, a + s * 3, s
  22.     END IF
  23.  
  24.  
  25. SUB CircleFill (CX AS LONG, CY AS LONG, R AS LONG, C AS _UNSIGNED LONG)
  26.     'This sub from here: http://www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=1848.msg17254#msg17254
  27.     DIM Radius AS LONG
  28.     DIM RadiusError AS LONG
  29.     DIM X AS LONG
  30.     DIM Y AS LONG
  31.  
  32.     Radius = ABS(R)
  33.     RadiusError = -Radius
  34.     X = Radius
  35.     Y = 0
  36.  
  37.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  38.  
  39.     ' Draw the middle span here so we don't draw it twice in the main loop,
  40.     ' which would be a problem with blending turned on.
  41.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  42.  
  43.     WHILE X > Y
  44.  
  45.         RadiusError = RadiusError + Y * 2 + 1
  46.  
  47.         IF RadiusError >= 0 THEN
  48.  
  49.             IF X <> Y + 1 THEN
  50.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  51.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  52.             END IF
  53.  
  54.             X = X - 1
  55.             RadiusError = RadiusError - X * 2
  56.  
  57.         END IF
  58.  
  59.         Y = Y + 1
  60.  
  61.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  62.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  63.  
  64.     WEND
  65.  
  66.  
  67. 'taken from QB64's p5.js
  68. 'http://bit.ly/p5jsbas
  69.  
  70. SUB internalp5line (x0!, y0!, x1!, y1!, s!, col~&)
  71.     dx! = x1! - x0!
  72.     dy! = y1! - y0!
  73.     d! = SQR(dx! * dx! + dy! * dy!)
  74.     FOR i = 0 TO d!
  75.         CircleFill x0! + dxx!, y0! + dyy!, s!, col~&
  76.         dxx! = dxx! + dx! / d!
  77.         dyy! = dyy! + dy! / d!
  78.     NEXT
  79.  
  80. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  81.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  82.  


2. 3D Fractal Tree with cube leaves (was made especially for @bplus )

Code: QB64: [Select]
  1. '3D Fall Foilage or Fractal Tree with cubed leaves
  2. 'Made by Ashish, especially for Bplus. :)
  3.  
  4.  
  5. _TITLE "3D Fall Foilage"
  6. SCREEN _NEWIMAGE(800, 600, 32)
  7.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  8.     SUB glutSolidCube (BYVAL dsize AS DOUBLE)
  9.  
  10. TYPE vec3
  11.     x AS SINGLE
  12.     y AS SINGLE
  13.     z AS SINGLE
  14.  
  15. FOR y = 0 TO _HEIGHT
  16.     COLOR _RGB(map(y, 0, _WIDTH, 0, 10), map(y, 0, _HEIGHT, 0, 180), 128)
  17.     LINE (0, y)-(_WIDTH, y)
  18.  
  19. DIM SHARED glAllow AS _BYTE, VertexCount, ZoomFactor
  20. COLOR _rgb(255,255,255), 1
  21. ZoomFactor = 2.0
  22. glAllow = -1
  23.     IF NOT me THEN
  24.         me = -1
  25.         PRINT VertexCount; " Vertices are being rendered."
  26.     END IF
  27.         if _keydown(asc("w")) then ZoomFactor = ZoomFactor - 0.05
  28.         if _keydown(asc("s")) then ZoomFactor = ZoomFactor + 0.05
  29.     _LIMIT 40
  30.  
  31. SUB _GL () STATIC
  32.     IF NOT glAllow THEN EXIT SUB
  33.     IF NOT glSetup THEN
  34.         _glViewport 0, 0, _WIDTH, _HEIGHT
  35.         aspect# = _WIDTH / _HEIGHT
  36.     END IF
  37.    
  38.     _glMatrixMode _GL_PROJECTION
  39.     _gluPerspective 45.0, aspect#, 1, 1000
  40.    
  41.     _glMatrixMode _GL_MODELVIEW
  42.    
  43.     _glEnable _GL_DEPTH_TEST
  44.     _glEnable _GL_BLEND
  45.    
  46.     gluLookAt 0, 1.1, 2+ZoomFactor, 0, 0, 0, 0, 1, 0
  47.     _glRotatef -clock# * 60, 0, 1, 0
  48.    
  49.     IF NOT glListGenerated THEN
  50.         fractal_tree = _glGenLists(1)
  51.         _glNewList fractal_tree, _GL_COMPILE
  52.         _glTranslatef 0, 0, 0
  53.         _glRotatef 180, 0, 0, 1
  54.        
  55.         _glLineWidth 10
  56.         _glColor3f .6, .3, 0
  57.         _glBegin _GL_LINES
  58.         _glVertex3f 0, 0, 0
  59.         _glVertex3f 0, 1.4, 0
  60.         _glEnd
  61.         drawTree3D 0, 0, 0, .76, 35, 35
  62.        
  63.         _glTranslatef 0, 1.4, 0
  64.         _glColor3f 1, 1, 1
  65.         drawGround3D
  66.         _glEndList
  67.         glListGenerated = -1
  68.     END IF
  69.     _glCallList fractal_tree
  70.    
  71.     _glFlush
  72.     clock# = clock# + .01
  73.  
  74. SUB drawTree3D (x, y, z, r, t1, t2)
  75.     ' IF r < 0.4 THEN _glColor4f 0, .8, 0, 0.7 ELSE _glColor3f .6, .3, 0
  76.     _glColor4f map(r, 0, 1, .8, .5), map(r, 0, 1, .5, .3), 0, .7
  77.  
  78.     _glLineWidth map(r, 0.8, 0.06, 10, 1)
  79.  
  80.     _glRotatef t1, 0, 0, 1
  81.     _glBegin _GL_LINES
  82.     _glVertex3f x, y, z
  83.     _glVertex3f x, y - r, z
  84.     _glEnd
  85.     if rnd > 0 and rnd < 0.25 then drawLeaf3D x, y - r, z
  86.    
  87.     _glColor4f map(r, 0, 1, .8, .5), map(r, 0, 1, .5, .3), 0, .7
  88.  
  89.     _glRotatef -t1, 0, 0, 1
  90.     _glBegin _GL_LINES
  91.     _glVertex3f x, y, z
  92.     _glVertex3f x, y - r, z
  93.     _glEnd
  94.     if rnd > 0.25 and rnd < 0.5 then drawLeaf3D x, y - r, z
  95.  
  96.     _glColor4f map(r, 0, 1, .8, .5), map(r, 0, 1, .5, .3), 0, .7
  97.    
  98.     _glRotatef t2, 1, 0, 0
  99.     _glBegin _GL_LINES
  100.     _glVertex3f x, y, z
  101.     _glVertex3f x, y - r, z
  102.     _glEnd
  103.     if rnd > 0.5 and rnd < 0.75 then drawLeaf3D x, y - r, z
  104.  
  105.     _glColor4f map(r, 0, 1, .8, .5), map(r, 0, 1, .5, .3), 0, .7
  106.    
  107.     _glRotatef -t2, 1, 0, 0
  108.     _glBegin _GL_LINES
  109.     _glVertex3f x, y, z
  110.     _glVertex3f x, y - r, z
  111.     _glEnd
  112.     if rnd > .75 then drawLeaf3D x, y - r, z
  113.  
  114.     VertexCount = VertexCount + 8
  115.  
  116.     IF r > 0.06 THEN
  117.         _glPushMatrix
  118.         _glRotatef t1, 0, 0, 1
  119.         _glTranslatef x, y - r, z
  120.         drawTree3D 0, 0, 0, r * 0.61, t1, t2
  121.         _glPopMatrix
  122.  
  123.         _glPushMatrix
  124.         _glRotatef -t1, 0, 0, 1
  125.         _glTranslatef x, y - r, z
  126.         drawTree3D 0, 0, 0, r * 0.61, t1, t2
  127.         _glPopMatrix
  128.  
  129.         _glPushMatrix
  130.         _glRotatef t2, 1, 0, 0
  131.         _glTranslatef x, y - r, z
  132.         drawTree3D 0, 0, 0, r * 0.61, t1, t2
  133.         _glPopMatrix
  134.  
  135.         _glPushMatrix
  136.         _glRotatef -t2, 1, 0, 0
  137.         _glTranslatef x, y - r, z
  138.         drawTree3D 0, 0, 0, r * 0.61, t1, t2
  139.         _glPopMatrix
  140.  
  141.     END IF
  142.  
  143.  
  144. SUB drawLeaf3D (x, y, z)
  145.     area = 0.2
  146.     ' FOR n = 1 TO leafs
  147.         _glColor4f p5random(0.19, 0.98), p5random(0.1, 1), p5random(0, 0.16), 0.6
  148.         _glPushMatrix
  149.         _glTranslatef x + RND * area - RND * area, y + RND * area - RND * area, z + RND * area - RND * area
  150.         glutSolidCube 0.01 + RND * .02
  151.         _glPopMatrix
  152.         VertexCount = VertexCount + 8
  153.     ' NEXT
  154.  
  155. SUB drawGround3D ()
  156.     _glBegin _GL_TRIANGLES
  157.     _glColor3f 0.27, 0.42, 0.11
  158.     _glVertex3f -2, 0, -2
  159.     _glColor3f 0.03, 0.31, 0.02
  160.     _glVertex3f -2, 0, 2
  161.     _glVertex3f 2, 0, -2
  162.    
  163.     _glColor3f 0.03, 0.31, 0.02
  164.     _glVertex3f 2, 0, -2
  165.     _glColor3f 0.27, 0.42, 0.11
  166.     _glVertex3f 2, 0, 2
  167.     _glColor3f 0.03, 0.31, 0.02
  168.     _glVertex3f -2, 0, 2
  169.     _glEnd
  170.  
  171. 'taken from a 2D rendering library, p5js.bas
  172. 'http://bit.ly/p5jsbas
  173. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  174.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  175.  
  176. FUNCTION p5random! (mn!, mx!)
  177.     IF mn! > mx! THEN
  178.         SWAP mn!, mx!
  179.     END IF
  180.     p5random! = RND * (mx! - mn!) + mn!
  181.  
  182.  

3. 3D Fractal tree with changing angles with Mouse

Code: QB64: [Select]
  1. '3D Tree Fractal!
  2. 'By Ashish Kushwaha
  3. 'Move mouse for new pattern
  4.  
  5.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  6.  
  7. _TITLE "3D Fractal Tree"
  8. SCREEN _NEWIMAGE(800, 600, 32)
  9.  
  10. DIM SHARED glAllow AS _BYTE, branchAngle1,branchAngle2
  11.  
  12. glAllow = -1
  13.     branchAngle1 = map(_MOUSEX, 0, _WIDTH, 15, 55)
  14.     branchAngle2 = map(_MOUSEY, 0, _HEIGHT, 15, 55)
  15.         _LIMIT 40
  16.  
  17. SUB _GL () STATIC
  18.     IF NOT glAllow THEN EXIT SUB
  19.  
  20.     IF NOT glSetup THEN
  21.         _glViewport 0, 0, _WIDTH, _HEIGHT
  22.         aspect# = _WIDTH / _HEIGHT
  23.         glSetup = -1
  24.     END IF
  25.  
  26.     _glEnable _GL_DEPTH_TEST
  27.     _glEnable _GL_BLEND
  28.  
  29.     _glMatrixMode _GL_PROJECTION
  30.     _gluPerspective 45.0, aspect#, 1.0, 1000.0
  31.  
  32.     _glMatrixMode _GL_MODELVIEW
  33.  
  34.     gluLookAt 0, -3, 3, 0, -.6, 0, 0, -1, 0
  35.  
  36.     _glRotatef clock# * 60, 0, 1, 0
  37.     _glLineWidth 10.0
  38.  
  39.     _glColor3f .6, .3, 0
  40.     _glBegin _GL_LINES
  41.     _glVertex3f 0, 0.0, 0
  42.     _glVertex3f 0, 1.8, 0
  43.     _glEnd
  44.  
  45.     drawTree3D 0, 0, 0, .8, branchAngle1, branchAngle2
  46.     clock# = clock# + 0.01
  47.     _glFlush
  48.  
  49. SUB drawTree3D (x, y, z, r, t1, t2)
  50.     IF r < 0.4 THEN _glColor4f 0, .8, 0, 0.7 ELSE _glColor3f .6, .3, 0
  51.  
  52.     _glLineWidth map(r, 0.8, 0.06, 10, 1)
  53.  
  54.     _glRotatef t1, 0, 0, 1
  55.     _glBegin _GL_LINES
  56.     _glVertex3f x, y, z
  57.     _glVertex3f x, y - r, z
  58.     _glEnd
  59.  
  60.     _glRotatef -t1, 0, 0, 1
  61.     _glBegin _GL_LINES
  62.     _glVertex3f x, y, z
  63.     _glVertex3f x, y - r, z
  64.     _glEnd
  65.  
  66.     _glRotatef t2, 1, 0, 0
  67.     _glBegin _GL_LINES
  68.     _glVertex3f x, y, z
  69.     _glVertex3f x, y - r, z
  70.     _glEnd
  71.  
  72.     _glRotatef -t2, 1, 0, 0
  73.     _glBegin _GL_LINES
  74.     _glVertex3f x, y, z
  75.     _glVertex3f x, y - r, z
  76.     _glEnd
  77.  
  78.  
  79.  
  80.     IF r > 0.06 THEN
  81.         _glPushMatrix
  82.         _glRotatef t1, 0, 0, 1
  83.         _glTranslatef x, y - r, z
  84.         drawTree3D 0, 0, 0, r * 0.61, t1, t2
  85.         _glPopMatrix
  86.  
  87.         _glPushMatrix
  88.         _glRotatef -t1, 0, 0, 1
  89.         _glTranslatef x, y - r, z
  90.         drawTree3D 0, 0, 0, r * 0.61, t1, t2
  91.         _glPopMatrix
  92.  
  93.         _glPushMatrix
  94.         _glRotatef t2, 1, 0, 0
  95.         _glTranslatef x, y - r, z
  96.         drawTree3D 0, 0, 0, r * 0.61, t1, t2
  97.         _glPopMatrix
  98.  
  99.         _glPushMatrix
  100.         _glRotatef -t2, 1, 0, 0
  101.         _glTranslatef x, y - r, z
  102.         drawTree3D 0, 0, 0, r * 0.61, t1, t2
  103.         _glPopMatrix
  104.  
  105.     END IF
  106.  
  107.  
  108. 'taken from a 2D rendering library, p5js.bas
  109. 'http://bit.ly/p5jsbas
  110. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  111.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  112.  
  113.  
  114.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Body Tree
« Reply #7 on: August 16, 2020, 11:41:35 am »
Hey @Ashish

New avatar I see.

Great trees! I like last one best can even assume the same pose as Body Tree when it is turned just right. You are a GL master! :-))