### Author Topic: 3D : Sierpinski Cube by Ashish  (Read 354 times)

#### Qwerkey ##### 3D : Sierpinski Cube by Ashish
« on: March 14, 2020, 08:56:11 AM »
3D : Sierpinski Cube

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

Description:
This fractal is popularly known as Menger Sponge. It looks beautiful.

Controls:
Move mouse for rotation.

Source Code:
Code: QB64: [Select]
1. '@Author:Ashish Kushwaha
2. '28 Feb, 2020s
3. _TITLE "Menger Sponge"
4. SCREEN _NEWIMAGE(600, 600, 32)
5.
6. TYPE vec3
7.
8.     SUB glutSolidCube (BYVAL dsize AS DOUBLE) 'use to draw a solid cube by taking the side length as its arguement
9.
10. 'Algorithm
11. '1. We take a cube.
12. '2. We divide it into 27 equal cubical parts.
13. '3. Out of this 27 cubes, 7 cubes are removed.
14. '4. In the remaining 20 cubes, Step-1 is repeated for each cube.
15. iteration = 3 'no. of iteration. At each iteration, 7 cubes are removed from parent cube.
16. size = 0.5 'the size of our first cube
17. n = (20 ^ iteration) - 1
18.
19. DIM SHARED glAllow, cubeLoc(n) AS vec3, fundamentalCubeSize 'cubeLoc array store the location of cubes to be rendered. They are the smallest cube which are formed in the last iteration
20. fundamentalCubeSize = size / (3 ^ iteration) 'the size the smallest cube which is formed in the last iteration
21. initFractal 0, 0, 0, size, iteration 'this sub done all calculation for cube location & other stuff.
22.
23. PRINT (n + 1); " Cubes will rendered with total of "; 8 * (n + 1); " vertices"
24. PRINT "Hit a Key"
25. glAllow = 1 'to start rendering in the SUB _GL
26.     _LIMIT 40
27.
28. SUB _GL () STATIC
29.     DIM clr(3)
30.     IF glAllow = 0 THEN EXIT SUB 'So that rendering will start as soon as initialization is done.
31.     IF glInit = 0 THEN
32.         _GLVIEWPORT 0, 0, _WIDTH, _HEIGHT 'this defines the area in the screen where GL rendering will occur
33.         aspect# = _WIDTH / _HEIGHT
34.
35.         glInit = 1
36.
37.     _GLENABLE _GL_DEPTH_TEST 'this enable Z-buffer. So that we can do 3D things.
38.     _GLCLEAR _GL_DEPTH_BUFFER_BIT OR _GL_COLOR_BUFFER_BIT 'Not required unless we do softwre rendering as well.
39.
40.     'LIGHTS CONFIG
41.     _GLENABLE _GL_LIGHTING 'this enable us to use light. There are max of 8 lights in GL
42.     _GLENABLE _GL_LIGHT0
43.     clr(0) = 0.2: clr(1) = 0.2: clr(2) = 0.2: clr(3) = 1
44.     _GLLIGHTFV _GL_LIGHT0, _GL_AMBIENT, _OFFSET(clr()) 'this define the color of the material where light can hardly reach.
45.     clr(0) = 0.8: clr(1) = 0.8: clr(2) = 0.8: clr(3) = 1
46.     _GLLIGHTFV _GL_LIGHT0, _GL_SPECULAR, _OFFSET(clr()) 'this define the color of the material where light is directly reflected & reach your eye.
47.     _GLLIGHTFV _GL_LIGHT0, _GL_DIFFUSE, _OFFSET(clr()) 'this define the default/usual color of the light on the material.
48.     clr(0) = 0: clr(1) = 0: clr(2) = 0: clr(3) = 1
49.     _GLLIGHTFV _GL_LIGHT0, _GL_POSITION, _OFFSET(clr()) 'use to define the direction of light when 4th component is 0. When 4th component is 1, it defines the position of light. In this case, the light looses its intensity as distance increases.
50.
51.     _GLMATRIXMODE _GL_PROJECTION 'usually used for setting up perspective etc.
52.     _GLUPERSPECTIVE 60, aspect#, 0.1, 10 'first arguement tell angle for FOV (Field of View, for human it is round 70degree for one eye.LOL) next one aspect ratio, next 2 are near & far distance. Objects which are not between these distance are clipped. (or are not rendered.)
53.
54.     _GLMATRIXMODE _GL_MODELVIEW 'rendering takes place here
55.
56.     _GLTRANSLATEF 0, 0, -1 'move the origin forward by 1 unit
57.     _GLROTATEF _MOUSEX, 0, 1, 0 'these are for rotation by the movement of mouse.
58.     _GLROTATEF _MOUSEY, 1, 0, 0
59.
60.     drawFractal 'draws the fractal
61.     _GLFLUSH 'force all the GL command to complete in finite amount of time
62.
63. SUB initFractal (x, y, z, s, N) 'x-position, y-position, z-position, size, N-> iteration
64.     'As we divide the cube, value of N decreases.
65.     IF N = 0 THEN 'when the division is done N times (no. of iteration)
66.         cubeLoc(i).x = x 'store the coordinates of cube
67.         cubeLoc(i).y = y
68.         cubeLoc(i).z = z
69.         i = i + 1
71.         ' sleep
72.     'top section
73.     'front row, left to right
74.     initFractal (x - s / 3), (y + s / 3), (z + s / 3), s / 3, N - 1
75.     initFractal (x), (y + s / 3), (z + s / 3), s / 3, N - 1
76.     initFractal (x + s / 3), (y + s / 3), (z + s / 3), s / 3, N - 1
77.     'behind the previous row, left to right
78.     initFractal (x - s / 3), (y + s / 3), (z), s / 3, N - 1
79.     initFractal (x + s / 3), (y + s / 3), (z), s / 3, N - 1
80.     'behind the previous row, left to right
81.     initFractal (x - s / 3), (y + s / 3), (z - s / 3), s / 3, N - 1
82.     initFractal (x), (y + s / 3), (z - s / 3), s / 3, N - 1
83.     initFractal (x + s / 3), (y + s / 3), (z - s / 3), s / 3, N - 1
84.     'middle section
85.     'front row, left to right
86.     initFractal (x - s / 3), (y), (z + s / 3), s / 3, N - 1
87.     initFractal (x + s / 3), (y), (z + s / 3), s / 3, N - 1
88.     'behind the previous row (last one as middle one contain no cube ;) ), left to right
89.     initFractal (x - s / 3), (y), (z - s / 3), s / 3, N - 1
90.     initFractal (x + s / 3), (y), (z - s / 3), s / 3, N - 1
91.     'bottom section
92.     'front row, left to right
93.     initFractal (x - s / 3), (y - s / 3), (z + s / 3), s / 3, N - 1
94.     initFractal (x), (y - s / 3), (z + s / 3), s / 3, N - 1
95.     initFractal (x + s / 3), (y - s / 3), (z + s / 3), s / 3, N - 1
96.     'behind the previous row, left to right
97.     initFractal (x - s / 3), (y - s / 3), (z), s / 3, N - 1
98.     initFractal (x + s / 3), (y - s / 3), (z), s / 3, N - 1
99.     'behind the previous row, left to right
100.     initFractal (x - s / 3), (y - s / 3), (z - s / 3), s / 3, N - 1
101.     initFractal (x), (y - s / 3), (z - s / 3), s / 3, N - 1
102.     initFractal (x + s / 3), (y - s / 3), (z - s / 3), s / 3, N - 1 '20
103.
104.
105. SUB drawFractal ()
106.     FOR i = 0 TO UBOUND(cubeLoc)
107.         _GLPUSHMATRIX 'save the previous transformation configuration
108.         _GLTRANSLATEF cubeLoc(i).x, cubeLoc(i).y, cubeLoc(i).z 'move at given location
109.         glutSolidCube fundamentalCubeSize 'draws the solid cube of smallest size which is formed in the last iteration
110.         _GLPOPMATRIX 'restore the original transformation configuration
111.

« Last Edit: March 27, 2020, 05:56:30 AM by Qwerkey »