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

0 Members and 1 Guest are viewing this topic.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
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.  

 
3D Knots Screenshot.jpg
« Last Edit: March 27, 2020, 05:57:00 am by Qwerkey »