Author Topic: 3D Knots by Ashish  (Read 429 times)

Offline Qwerkey

  • Moderator
  • Forum Resident
  • Posts: 736
3D Knots by Ashish
« on: March 14, 2020, 11:59:21 AM »
3D Knots

Author: @Ashish
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=169.msg902#msg902
Version: 1
Tags: [3D], [Graphics], [Open_GL]

Description:
Some mathematical knots or geometry, effected by SUB _GL

Source Code:
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. DECLARE LIBRARY 'used for camera.
  12.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  13.  
  14. DIM SHARED glAllow AS _BYTE, knot_type, ma 'knot_type store the type knot being drawn. ma store percentage of knot which is being drawn.
  15. knot_type = 1
  16. glAllow = -1
  17.  
  18.     k& = _KEYHIT
  19.     IF k& = ASC(" ") THEN
  20.         knot_type = knot_type + 1
  21.         ma = 0
  22.         IF knot_type > 7 THEN knot_type = 1 '7 knots are there.
  23.     END IF
  24.     _LIMIT 60
  25.  
  26. SUB _GL ()
  27.     STATIC glInit, clock
  28.     ' static r, pos, theta, phi, beta
  29.  
  30.     IF NOT glAllow THEN EXIT SUB
  31.  
  32.     IF NOT glInit THEN
  33.         glInit = -1
  34.         aspect# = _WIDTH / _HEIGHT
  35.         _GLVIEWPORT 0, 0, _WIDTH, _HEIGHT
  36.     END IF
  37.  
  38.     _GLENABLE _GL_DEPTH_TEST 'We are doing 3D. This enables Z-Buffer.
  39.  
  40.     'set perspective configuration
  41.     _GLMATRIXMODE _GL_PROJECTION
  42.     _GLUPERSPECTIVE 45.0, aspect#, 1.0, 100.0
  43.  
  44.     _GLMATRIXMODE _GL_MODELVIEW
  45.     ' gluLookAt 0,0,-1,0,0,0,0,1,0
  46.  
  47.     _GLCOLOR3F 1, 1, 1 'set color
  48.     _GLTRANSLATEF 0, 0, 0 'not require. becoz, origin is already at 0,0,0
  49.     _GLROTATEF clock * 90, 0, 1, 0 'rotation along Y-axis
  50.     _GLLINEWIDTH 3.0 'width of the line.
  51.  
  52.     SELECT CASE knot_type
  53.         CASE 7 'equations are knots are taken from paulbourke site
  54.             _GLBEGIN _GL_LINE_STRIP
  55.             'for animation, value of ma is gradually increased till a certain constant. In this case, it is pi.
  56.             FOR beta = 0 TO ma STEP .005
  57.                 r = .3 + .6 * SIN(6 * beta)
  58.                 theta = 2 * beta
  59.                 phi = _PI(.6) * SIN(12 * beta)
  60.                 x = r * COS(phi) * COS(theta)
  61.                 y = r * COS(phi) * SIN(theta)
  62.                 z = r * SIN(phi)
  63.                 _GLCOLOR3F map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  64.                 _GLVERTEX3F x, y, z 'draws it.
  65.             NEXT
  66.             _GLEND
  67.             IF ma <= _PI THEN ma = ma + .005
  68.             'others are made to be rendered in the same way.
  69.         CASE 6
  70.             _GLBEGIN _GL_LINE_STRIP
  71.             FOR beta = 0 TO ma STEP .005
  72.                 r = 1.2 * 0.6 * SIN(_PI(.5) * 6 * beta)
  73.                 theta = 4 * beta
  74.                 phi = _PI(.2) * SIN(6 * beta)
  75.                 x = r * COS(phi) * COS(theta)
  76.                 y = r * COS(phi) * SIN(theta)
  77.                 z = r * SIN(phi)
  78.                 _GLCOLOR3F map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  79.                 _GLVERTEX3F x, y, z
  80.             NEXT
  81.             _GLEND
  82.             IF ma <= _PI(2) THEN ma = ma + .005
  83.         CASE 5
  84.             k = 1
  85.             _GLBEGIN _GL_LINE_STRIP
  86.             FOR u = 0 TO ma STEP .005
  87.                 x = COS(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  88.                 y = SIN(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  89.                 z = -SIN(2 * u / (2 * k + 1)) / 5
  90.                 _GLCOLOR3F map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  91.                 _GLVERTEX3F x, y, z
  92.             NEXT
  93.             _GLEND
  94.             IF ma < _PI(4 * k + 2) THEN ma = ma + .045
  95.         CASE 4
  96.             k = 2
  97.             _GLBEGIN _GL_LINE_STRIP
  98.             FOR u = 0 TO ma STEP .005
  99.                 x = COS(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  100.                 y = SIN(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  101.                 z = -SIN(2 * u / (2 * k + 1)) / 5
  102.                 _GLCOLOR3F map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  103.                 _GLVERTEX3F x, y, z
  104.             NEXT
  105.             _GLEND
  106.             IF ma < _PI(4 * k + 2) THEN ma = ma + .045
  107.         CASE 3
  108.             k = 3
  109.             _GLBEGIN _GL_LINE_STRIP
  110.             FOR u = 0 TO ma STEP .005
  111.                 x = COS(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  112.                 y = SIN(u) * (2 - COS(2 * u / (2 * k + 1))) / 5
  113.                 z = -SIN(2 * u / (2 * k + 1)) / 5
  114.                 _GLCOLOR3F map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  115.                 _GLVERTEX3F x, y, z
  116.             NEXT
  117.             _GLEND
  118.             IF ma < _PI(4 * k + 2) THEN ma = ma + .045
  119.         CASE 2
  120.             _GLBEGIN _GL_LINE_STRIP
  121.             FOR u = 0 TO ma STEP .005
  122.                 x = (41 * COS(u) - 18 * SIN(u) - 83 * COS(2 * u) - 83 * SIN(2 * u) - 11 * COS(3 * u) + 27 * SIN(3 * u)) / 200
  123.                 y = (36 * COS(u) + 27 * SIN(u) - 113 * COS(2 * u) + 30 * SIN(2 * u) + 11 * COS(3 * u) - 27 * SIN(3 * u)) / 200
  124.                 z = (45 * SIN(u) - 30 * COS(2 * u) + 113 * SIN(2 * u) - 11 * COS(3 * u) + 27 * SIN(3 * u)) / 200
  125.                 _GLCOLOR3F map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  126.                 _GLVERTEX3F x, y, z
  127.             NEXT
  128.             _GLEND
  129.             IF ma < _PI(2) THEN ma = ma + .005
  130.         CASE 1
  131.             _GLBEGIN _GL_LINE_STRIP
  132.             FOR u = 0 TO ma STEP .005
  133.                 x = (-22 * COS(u) - 128 * SIN(u) - 44 * COS(3 * u) - 78 * SIN(3 * u)) / 200
  134.                 y = (-10 * COS(2 * u) - 27 * SIN(2 * u) + 38 * COS(4 * u) + 46 * SIN(4 * u)) / 200
  135.                 z = (70 * COS(3 * u) - 40 * SIN(3 * u)) / 200
  136.                 _GLCOLOR3F map(x, -1, 1, 0, 1), map(y, -1, 1, 0, 1), map(z, -1, 1, 0, 1)
  137.                 _GLVERTEX3F x, y, z
  138.             NEXT
  139.             _GLEND
  140.             IF ma < _PI(2) THEN ma = ma + .005
  141.     END SELECT
  142.     _GLFLUSH
  143.  
  144.     clock = clock + .01
  145.  
  146. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  147.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  148.  

 
« Last Edit: March 27, 2020, 05:57:00 AM by Qwerkey »