Author Topic: 3D Spirograph  (Read 7135 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
3D Spirograph
« on: March 07, 2018, 05:26:43 am »
Hi There! :) You might have probably seen my 2D Spirograph here - www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=14376.msg124179
Now, it comes with 3D Version. :)

Code: QB64: [Select]
  1. 'Coded By Ashish on 7 March, 2018
  2.  
  3. _TITLE "3D Spirograph"
  4.  
  5. SCREEN _NEWIMAGE(800, 600, 32)
  6.  
  7. TYPE vec3
  8.     x AS SINGLE
  9.     y AS SINGLE
  10.     z AS SINGLE
  11.  
  12. TYPE sphere
  13.     pos AS vec3
  14.     r AS SINGLE
  15.     angX AS DOUBLE
  16.         angY AS DOUBLE
  17.         angZ AS DOUBLE
  18.     angStp AS DOUBLE
  19.  
  20.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  21.     SUB glutWireSphere (BYVAL radius AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
  22.  
  23. DIM SHARED __sphere(6) AS sphere, glAllow AS _BYTE
  24. DIM SHARED tracer(12000) AS vec3, f
  25.  
  26. init:
  27. ERASE tracer
  28. v = 0
  29. f = 0
  30. __sphere(0).pos.x = 0
  31. __sphere(0).pos.y = 0
  32. __sphere(0).pos.z = 0
  33. __sphere(0).r = 1
  34. __sphere(0).angStp = p5random(.001, .1)
  35. __sphere(0).angX = p5random(0,_pi(2))
  36. __sphere(0).angY = p5random(0,_pi(2))
  37. __sphere(0).angZ = p5random(0,_pi(2))
  38. FOR i = 1 TO UBOUND(__sphere)
  39.     __sphere(i).pos.x = COS(__sphere(i - 1).angX) * __sphere(i - 1).r + __sphere(i - 1).pos.x
  40.     __sphere(i).pos.y = SIN(__sphere(i - 1).angY) * __sphere(i - 1).r + __sphere(i - 1).pos.y
  41.     __sphere(i).pos.z = SIN(__sphere(i - 1).angZ) * __sphere(i - 1).r + __sphere(i - 1).pos.z
  42.  
  43.     __sphere(i).r = __sphere(i - 1).r / 2
  44.     __sphere(i).angStp = p5random(-.1, .1)
  45.         __sphere(i).angX = p5random(0,_pi(2))
  46.         __sphere(i).angY = p5random(0,_pi(2))
  47.         __sphere(i).angZ = p5random(0,_pi(2))
  48.  
  49. glAllow = -1
  50.     k& = _KEYHIT
  51.     IF k& = ASC(" ") THEN GOTO init
  52.     FOR i = 0 TO UBOUND(__sphere)
  53.         __sphere(i).angX = __sphere(i).angX + __sphere(i).angStp
  54.                 __sphere(i).angY = __sphere(i).angY + __sphere(i).angStp
  55.                 __sphere(i).angZ = __sphere(i).angZ + __sphere(i).angStp
  56.     NEXT
  57.     f = f + 1
  58.     IF f > 4 THEN
  59.         v = v + 1
  60.         tracer(v) = __sphere(UBOUND(__sphere)).pos
  61.     END IF
  62.     _LIMIT 60
  63.     _DISPLAY
  64.  
  65. SUB _GL ()
  66.     STATIC clock!, glInit AS _BYTE, aspect#
  67.  
  68.     IF NOT glAllow THEN EXIT SUB
  69.     IF NOT glInit THEN
  70.         glInit = -1
  71.         _glViewport 0, 0, _WIDTH, _HEIGHT
  72.         aspect# = _WIDTH / _HEIGHT
  73.     END IF
  74.  
  75.     _glEnable _GL_DEPTH_TEST
  76.     '_glEnable _GL_BLEND
  77.  
  78.     _glEnable _GL_LIGHTING
  79.     _glEnable _GL_LIGHT0
  80.  
  81.     _glShadeModel _GL_SMOOTH
  82.  
  83.     _glMatrixMode _GL_PROJECTION
  84.     _gluPerspective 45.0, aspect#, 1.0, 100.0
  85.  
  86.     _glMatrixMode _GL_MODELVIEW
  87.  
  88.     gluLookAt 0, 0, -8, 0, 0, 0, 0, -1, 0
  89.  
  90.     _glTranslatef 0, 0, 0
  91.    _glRotatef clock! * 90, 0, 1, 0
  92.     _glColor4f 1, 1, 1, .3
  93.     _glLineWidth 1.0
  94.     glutWireSphere __sphere(0).r, UBOUND(__sphere) * 20, UBOUND(__sphere) * 20
  95.  
  96.     FOR i = 1 TO UBOUND(__sphere)
  97.         __sphere(i).pos.x = SIN(__sphere(i - 1).angX) * (__sphere(i - 1).r) + (__sphere(i - 1).pos.x)
  98.         __sphere(i).pos.y = COS(__sphere(i - 1).angY) * (__sphere(i - 1).r + __sphere(i).r) + (__sphere(i - 1).pos.y)
  99.         __sphere(i).pos.z = SIN(__sphere(i - 1).angZ) * (__sphere(i - 1).r) + (__sphere(i - 1).pos.z)
  100.         _glPushMatrix
  101.         _glTranslatef __sphere(i).pos.x, __sphere(i).pos.y, __sphere(i).pos.z
  102.         glutWireSphere __sphere(i).r, (UBOUND(__sphere) + 1 - i) * 20, (UBOUND(__sphere) + 1 - i) * 20
  103.         _glPopMatrix
  104.     NEXT
  105.     IF f > 6 THEN
  106.         _glDisable _GL_LIGHTING
  107.         _glDisable _GL_LIGHT0
  108.         _glTranslatef 0, 0, 0
  109.         _glLineWidth 2.0
  110.         _glPushMatrix
  111.         _glColor3f 1, 0, 0
  112.         FOR i = 1 TO f - 2
  113.             _glBegin _GL_LINES
  114.             _glColor3f map(tracer(i).x * tracer(i).y, -9, 9, .4, .1), map(tracer(i).x, -3, 3, .2, 1), map(tracer(i).y, -3, 3, 1, .2)
  115.             _glVertex3f tracer(i).x, tracer(i).y, tracer(i).z
  116.             _glVertex3f tracer(i + 1).x, tracer(i + 1).y, tracer(i + 1).z
  117.             _glEnd
  118.         NEXT
  119.         _glPopMatrix
  120.     END IF
  121.  
  122.     clock! = clock! + .01
  123.  
  124.     _glFlush
  125.  
  126.  
  127. 'taken from p5js.bas
  128. 'https://bit.ly/p5jsbas
  129. FUNCTION p5random! (mn!, mx!)
  130.     IF mn! > mx! THEN
  131.         SWAP mn!, mx!
  132.     END IF
  133.     p5random! = RND * (mx! - mn!) + mn!
  134.  
  135. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  136.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  137.  
  138.  
  139.  
  140.  
  141.  
Screenshot_1.png
* Screenshot_1.png (Filesize: 62.51 KB, Dimensions: 806x633, Views: 648)
« Last Edit: March 08, 2018, 06:10:09 am by Ashish »
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 Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: 3D Spirograph
« Reply #1 on: March 08, 2018, 06:10:46 am »
I've updated the code in the first post. Now it perfectly do the rotation in 3D Space.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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

FellippeHeitor

  • Guest
Re: 3D Spirograph
« Reply #2 on: March 08, 2018, 11:52:13 am »
Now you're talking!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: 3D Spirograph
« Reply #4 on: March 09, 2018, 03:11:41 am »
Now you're talking!
what  you want to say?
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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

FellippeHeitor

  • Guest