3D Spirograph
#1
Rainbow 
I've added one more dimension to my previous Spirograph demo. Big Grin

Code:
'Coded By Ashish on 7 March, 2018

_TITLE "3D Spirograph"

SCREEN _NEWIMAGE(800, 600, 32)

TYPE vec3
   x AS SINGLE
   y AS SINGLE
   z AS SINGLE
END TYPE

TYPE sphere
   pos AS vec3
   r AS SINGLE
   angX AS DOUBLE
    angY AS DOUBLE
    angZ AS DOUBLE
   angStp AS DOUBLE
END TYPE

DECLARE LIBRARY
   SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
   SUB glutWireSphere (BYVAL radius AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
END DECLARE

DIM SHARED __sphere(6) AS sphere, glAllow AS _BYTE
DIM SHARED tracer(12000) AS vec3, f

init:
ERASE tracer
v = 0
f = 0
__sphere(0).pos.x = 0
__sphere(0).pos.y = 0
__sphere(0).pos.z = 0
__sphere(0).r = 1
__sphere(0).angStp = p5random(.001, .1)
__sphere(0).angX = p5random(0,_pi(2))
__sphere(0).angY = p5random(0,_pi(2))
__sphere(0).angZ = p5random(0,_pi(2))
FOR i = 1 TO UBOUND(__sphere)
   __sphere(i).pos.x = COS(__sphere(i - 1).angX) * __sphere(i - 1).r + __sphere(i - 1).pos.x
   __sphere(i).pos.y = SIN(__sphere(i - 1).angY) * __sphere(i - 1).r + __sphere(i - 1).pos.y
   __sphere(i).pos.z = SIN(__sphere(i - 1).angZ) * __sphere(i - 1).r + __sphere(i - 1).pos.z

   __sphere(i).r = __sphere(i - 1).r / 2
   __sphere(i).angStp = p5random(-.1, .1)
    __sphere(i).angX = p5random(0,_pi(2))
    __sphere(i).angY = p5random(0,_pi(2))
    __sphere(i).angZ = p5random(0,_pi(2))
NEXT

_GLRENDER _BEHIND
glAllow = -1
DO
   k& = _KEYHIT
   IF k& = ASC(" ") THEN GOTO init
   FOR i = 0 TO UBOUND(__sphere)
       __sphere(i).angX = __sphere(i).angX + __sphere(i).angStp
        __sphere(i).angY = __sphere(i).angY + __sphere(i).angStp
        __sphere(i).angZ = __sphere(i).angZ + __sphere(i).angStp
   NEXT
   f = f + 1
   IF f > 4 THEN
       v = v + 1
       tracer(v) = __sphere(UBOUND(__sphere)).pos
   END IF
   _LIMIT 60
   _DISPLAY
LOOP

SUB _GL ()
   STATIC clock!, glInit AS _BYTE, aspect#

   IF NOT glAllow THEN EXIT SUB
   IF NOT glInit THEN
       glInit = -1
       _glViewport 0, 0, _WIDTH, _HEIGHT
       aspect# = _WIDTH / _HEIGHT
   END IF

   _glEnable _GL_DEPTH_TEST
   '_glEnable _GL_BLEND

   _glEnable _GL_LIGHTING
   _glEnable _GL_LIGHT0

   _glShadeModel _GL_SMOOTH

   _glMatrixMode _GL_PROJECTION
   _glLoadIdentity
   _gluPerspective 45.0, aspect#, 1.0, 100.0

   _glMatrixMode _GL_MODELVIEW
   _glLoadIdentity

   gluLookAt 0, 0, -8, 0, 0, 0, 0, -1, 0

   _glTranslatef 0, 0, 0
  _glRotatef clock! * 90, 0, 1, 0
   _glColor4f 1, 1, 1, .3
   _glLineWidth 1.0
   glutWireSphere __sphere(0).r, UBOUND(__sphere) * 20, UBOUND(__sphere) * 20

   FOR i = 1 TO UBOUND(__sphere)
       __sphere(i).pos.x = SIN(__sphere(i - 1).angX) * (__sphere(i - 1).r) + (__sphere(i - 1).pos.x)
       __sphere(i).pos.y = COS(__sphere(i - 1).angY) * (__sphere(i - 1).r + __sphere(i).r) + (__sphere(i - 1).pos.y)
       __sphere(i).pos.z = SIN(__sphere(i - 1).angZ) * (__sphere(i - 1).r) + (__sphere(i - 1).pos.z)
       _glPushMatrix
       _glTranslatef __sphere(i).pos.x, __sphere(i).pos.y, __sphere(i).pos.z
       glutWireSphere __sphere(i).r, (UBOUND(__sphere) + 1 - i) * 20, (UBOUND(__sphere) + 1 - i) * 20
       _glPopMatrix
   NEXT
   IF f > 6 THEN
       _glDisable _GL_LIGHTING
       _glDisable _GL_LIGHT0
       _glTranslatef 0, 0, 0
       _glLineWidth 2.0
       _glPushMatrix
       _glColor3f 1, 0, 0
       FOR i = 1 TO f - 2
           _glBegin _GL_LINES
           _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)
           _glVertex3f tracer(i).x, tracer(i).y, tracer(i).z
           _glVertex3f tracer(i + 1).x, tracer(i + 1).y, tracer(i + 1).z
           _glEnd
       NEXT
       _glPopMatrix
   END IF

   clock! = clock! + .01

   _glFlush
END SUB


'taken from p5js.bas
'https://bit.ly/p5jsbas
FUNCTION p5random! (mn!, mx!)
   IF mn! > mx! THEN
       SWAP mn!, mx!
   END IF
   p5random! = RND * (mx! - mn!) + mn!
END FUNCTION

FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
   map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
END FUNCTION


Attached Files Thumbnail(s)

Reply
#2
Very nicely done! Smile

J
May your journey be free of incident.

Live long and prosper.
Reply
#3
Quote:Very nicely done! [Image: smile.gif]

J
Thanks Smile
Reply
#4
I've updated the code in my first post. Now, it perfectly do the rotation in 3D space.
Reply
#5
Ah. Much better. Good job.

J
May your journey be free of incident.

Live long and prosper.
Reply
#6
@ Johnno56: I was kidnapped once, and taken to a rock concert against my will. To that end, please change your signature line to:

"May your incident be free of Journey!"

In other news,

@ Ashish: Why stop there? With a little work, that could produce a solar system model. That would rock. It's very cool as is, but I'm just sayin'.

Pete Big Grin

- 3rd rock from the sun proving being first isn't always the best outcome.
Reply
#7
@Pete
http://www.qb64.net/forum/index.php?topi...#msg123998
Reply
#8
Pete,
You may have been 'taken' there, but nothing was said about being 'held' there... unless 'you' were not the driver... I am not a 'Journey' fan. Sorry, the signature stays. To change it now would be highly illogical.

KingAshish,
Please tell me you are not one of 'those' eight planet solar system supporters? I agree with Pete. A little extra work would make a nice planetary simulation. But, with a little extra work, a 'real time' accurate model system could be made... nudge, nudge, wink, wink...

J
May your journey be free of incident.

Live long and prosper.
Reply
#9
I concur. Nine planets. Pluto rocks! Ask any astrologer, yes, I wrote astrologer. Now if you will excuse me, I cant get, ""Who's Crying Now" out of my head, which means I'm back on my meds. Momma don't take my Thorizine away!

Pete Big Grin
Reply