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

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

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