Author Topic: 3D Knots  (Read 4761 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
3D Knots
« on: March 09, 2018, 05:57:55 am »
Some mathematical knots or geometry....
http://paulbourke.net/geometry/knots/
Code: QB64: [Select]
  1. 'Coded in QB64 by Ashish on 9 March, 2018
  2. 'http://paulbourke.net/geometry/knots/
  3. _TITLE "3D Knot [Press space for next knot]"
  4.  
  5. SCREEN _NEWIMAGE(700, 700, 32)
  6.  
  7. TYPE vec3
  8.     x AS SINGLE
  9.     y AS SINGLE
  10.     z AS SINGLE
  11.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  12.  
  13. DIM SHARED glAllow AS _BYTE, knot_type, ma
  14. knot_type = 1
  15. glAllow = -1
  16.  
  17.     k& = _KEYHIT
  18.     IF k& = ASC(" ") THEN
  19.         knot_type = knot_type + 1
  20.         ma = 0
  21.         IF knot_type > 7 THEN knot_type = 1
  22.     END IF
  23.     _LIMIT 60
  24.  
  25. SUB _GL ()
  26.     STATIC glInit, clock
  27.     ' static r, pos, theta, phi, beta
  28.  
  29.     IF NOT glAllow THEN EXIT SUB
  30.  
  31.     IF NOT glInit THEN
  32.         glInit = -1
  33.         aspect# = _WIDTH / _HEIGHT
  34.         _glViewport 0, 0, _WIDTH, _HEIGHT
  35.     END IF
  36.    
  37.     _glEnable _GL_DEPTH_TEST
  38.     _glDepthMask _GL_TRUE
  39.    
  40.     _glMatrixMode _GL_PROJECTION
  41.     _gluPerspective 45.0, aspect#, 1.0, 100.0
  42.    
  43.     _glMatrixMode _GL_MODELVIEW
  44.     ' gluLookAt 0,0,-1,0,0,0,0,1,0
  45.    
  46.     _glColor3f 1, 1, 1
  47.     _glTranslatef 0, 0, 0
  48.     _glRotatef clock * 90, 0, 1, 0
  49.     _glLineWidth 3.0
  50.    
  51.     SELECT CASE knot_type
  52.         CASE 7
  53.             _glBegin _GL_LINE_STRIP
  54.             FOR beta = 0 TO ma STEP .005
  55.                 r = .3 + .6 * SIN(6 * beta)
  56.                 theta = 2 * beta
  57.                 phi = _PI(.6) * SIN(12 * beta)
  58.                 x = r * COS(phi) * COS(theta)
  59.                 y = r * COS(phi) * SIN(theta)
  60.                 z = r * SIN(phi)
  61.                 _glColor3f map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  62.                 _glVertex3f x, y, z
  63.             NEXT
  64.             _glEnd
  65.             IF ma <= _PI THEN ma = ma + .005
  66.         CASE 6
  67.             _glBegin _GL_LINE_STRIP
  68.             FOR beta = 0 TO ma STEP .005
  69.                 r = 1.2 * 0.6 * SIN(_PI(.5) * 6 * beta)
  70.                 theta = 4 * beta
  71.                 phi = _PI(.2) * SIN(6 * beta)
  72.                 x = r * COS(phi) * COS(theta)
  73.                 y = r * COS(phi) * SIN(theta)
  74.                 z = r * SIN(phi)
  75.                 _glColor3f map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  76.                 _glVertex3f x, y, z
  77.             NEXT
  78.             _glEnd
  79.             IF ma <= _PI(2) THEN ma = ma + .005
  80.         CASE 5
  81.             k = 1
  82.             _glBegin _GL_LINE_STRIP
  83.             FOR u = 0 TO ma STEP .005
  84.                 x = COS(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  85.                 y = SIN(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  86.                 z = -SIN(2 * u / (2 * k + 1)) / 5
  87.                 _glColor3f map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  88.                 _glVertex3f x, y, z
  89.             NEXT
  90.             _glEnd
  91.             IF ma < _PI(4 * k + 2) THEN ma = ma + .045
  92.         CASE 4
  93.             k = 2
  94.             _glBegin _GL_LINE_STRIP
  95.             FOR u = 0 TO ma STEP .005
  96.                 x = COS(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  97.                 y = SIN(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  98.                 z = -SIN(2 * u / (2 * k + 1)) / 5
  99.                 _glColor3f map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  100.                 _glVertex3f x, y, z
  101.             NEXT
  102.             _glEnd
  103.             IF ma < _PI(4 * k + 2) THEN ma = ma + .045
  104.         CASE 3
  105.             k = 3
  106.             _glBegin _GL_LINE_STRIP
  107.             FOR u = 0 TO ma STEP .005
  108.                 x = COS(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  109.                 y = SIN(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  110.                 z = -SIN(2 * u / (2 * k + 1)) / 5
  111.                 _glColor3f map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  112.                 _glVertex3f x, y, z
  113.             NEXT
  114.             _glEnd
  115.             IF ma < _PI(4 * k + 2) THEN ma = ma + .045
  116.         CASE 2
  117.             _glBegin _GL_LINE_STRIP
  118.             FOR u = 0 TO ma STEP .005
  119.                 x = (41 * COS(u) - 18 * SIN(u) - 83 * COS(2 * u) - 83 * SIN(2 * u) - 11 * COS(3 * u) + 27 * SIN(3 * u)) / 200
  120.                 y = (36 * COS(u) + 27 * SIN(u) - 113 * COS(2 * u) + 30 * SIN(2 * u) + 11 * COS(3 * u) - 27 * SIN(3 * u)) / 200
  121.                 z = (45 * SIN(u) - 30 * COS(2 * u) + 113 * SIN(2 * u) - 11 * COS(3 * u) + 27 * SIN(3 * u)) / 200
  122.                 _glColor3f map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  123.                 _glVertex3f x, y, z
  124.             NEXT
  125.             _glEnd
  126.             IF ma < _PI(2) THEN ma = ma + .005
  127.         CASE 1
  128.             _glBegin _GL_LINE_STRIP
  129.             FOR u = 0 TO ma STEP .005
  130.                 x = (-22 * COS(u) - 128 * SIN(u) - 44 * COS(3 * u) - 78 * SIN(3 * u)) / 200
  131.                 y = (-10 * COS(2 * u) - 27 * SIN(2 * u) + 38 * COS(4 * u) + 46 * SIN(4 * u)) / 200
  132.                 z = (70 * COS(3 * u) - 40 * SIN(3 * u)) / 200
  133.                 _glColor3f map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  134.                 _glVertex3f x, y, z
  135.             NEXT
  136.             _glEnd
  137.             IF ma < _PI(2) THEN ma = ma + .005
  138.     END SELECT
  139.     _glFlush
  140.    
  141.     clock = clock + .01
  142.  
  143. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  144.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  145.  
  146.  
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 + ...
Re: 3D Knots
« Reply #1 on: March 09, 2018, 07:25:28 am »
WoW! very nice!

Bravo!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
Re: 3D Knots
« Reply #2 on: March 09, 2018, 07:58:25 am »
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 Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: 3D Knots
« Reply #3 on: March 09, 2018, 11:32:39 am »
Knot quite what I expected, but it did tie up my computer for awhile.

Funny how our minds work. It creates an optical illusion of the line going under itself. If you concentrate on a 2-D level, you can change that image, but you really have to stay focused. Neat.

Pete :D
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!
Re: 3D Knots
« Reply #4 on: March 10, 2018, 01:23:24 am »
Knot quite what I expected, but it did tie up my computer for awhile.

Funny how our minds work. It creates an optical illusion of the line going under itself. If you concentrate on a 2-D level, you can change that image, but you really have to stay focused. Neat.

Pete :D
Thanks Pete! I appreciate your thought.
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 Knots
« Reply #5 on: March 18, 2018, 08:38:21 pm »
SUB _GL is treated differently by QB64. You don't call it, but it is run at ~60fps (or whatever the refresh rate on your system is).

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
Re: 3D Knots
« Reply #6 on: March 19, 2018, 03:09:36 am »
Quite impressive, but can you please explain to me how it is working when there is no call to the _GL subroutine?

As Fellippe said, SUB _GL is special subroutine which when detected by QB64, allows you to use OpenGL commands in it.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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