Author Topic: 3D : Sierpinski Cube  (Read 6889 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
3D : Sierpinski Cube
« on: February 28, 2020, 07:39:34 am »
Hi everyone! :) I think this fractal is populary known as Menger Sponge. It looks beautiful..
Try to change the value of iteration to 4 or 3. (GO ABOVE 4 AT YOUR OWN RISK). It makes my CPU fan on at value of 4.
Default value is 3.

Controls :-
  • Move mouse for rotation
EDIT: Last update 29 Feb '20
Code: QB64: [Select]
  1. 'Ashish Kushwaha
  2. '28 Feb, 2020s
  3. _TITLE "Menger Sponge"
  4. SCREEN _NEWIMAGE(600, 600, 32)
  5.  
  6. TYPE vec3
  7.     x AS SINGLE
  8.     y AS SINGLE
  9.     z AS SINGLE
  10.  
  11.     SUB glutSolidCube (BYVAL dsize AS DOUBLE)
  12.  
  13. iteration = 3
  14. size = 0.5
  15. n = (20 ^ iteration) - 1
  16.  
  17. DIM SHARED glAllow, cubeLoc(n) AS vec3, fundamentalCubeSize
  18. fundamentalCubeSize = size / (3 ^ iteration)
  19. initFractal 0, 0, 0, size, iteration
  20.  
  21. PRINT (n + 1); " Cubes will rendered with total of "; 8 * (n + 1); " vertices"
  22. PRINT "Hit a Key"
  23. glAllow = 1
  24.     _LIMIT 40
  25.  
  26. SUB _GL () STATIC
  27.     DIM clr(2)
  28.     IF glAllow = 0 THEN EXIT SUB
  29.     IF glInit = 0 THEN
  30.         _glViewport 0, 0, _WIDTH, _HEIGHT
  31.         aspect# = _WIDTH / _HEIGHT
  32.  
  33.         glInit = 1
  34.     END IF
  35.  
  36.     _glEnable _GL_DEPTH_TEST
  37.     _glClear _GL_DEPTH_BUFFER_BIT OR _GL_COLOR_BUFFER_BIT
  38.  
  39.     'LIGHTS CONFIG
  40.     _glEnable _GL_LIGHTING
  41.     _glEnable _GL_LIGHT0
  42.     clr(0) = 0.2: clr(1) = 0.2: clr(2) = 0.0
  43.     _glLightfv _GL_LIGHT0, _GL_AMBIENT, _OFFSET(clr())
  44.     clr(0) = 0.8: clr(1) = 0.8: clr(2) = 0
  45.     _glLightfv _GL_LIGHT0, _GL_SPECULAR, _OFFSET(clr())
  46.     _glLightfv _GL_LIGHT0, _GL_DIFFUSE, _OFFSET(clr())
  47.  
  48.     _glMatrixMode _GL_PROJECTION
  49.     _gluPerspective 50, aspect#, 0.1, 10
  50.  
  51.     _glMatrixMode _GL_MODELVIEW
  52.  
  53.     _glTranslatef 0, 0, -1
  54.     _glRotatef _MOUSEX, 0, 1, 0
  55.     _glRotatef _MOUSEY, 1, 0, 0
  56.  
  57.     drawFractal
  58.     _glFlush
  59.  
  60. SUB initFractal (x, y, z, s, N) 'x-position, y-position, z-position, size, N-> iteration
  61.     STATIC i
  62.     IF N = 0 THEN
  63.         cubeLoc(i).x = x
  64.         cubeLoc(i).y = y
  65.         cubeLoc(i).z = z
  66.         i = i + 1
  67.         ' ? "Added #",i
  68.         ' sleep
  69.         EXIT SUB
  70.     END IF
  71.     'top section
  72.     'sabse samne wali row, left to right
  73.     initFractal (x - s / 3), (y + s / 3), (z + s / 3), s / 3, N - 1
  74.     initFractal (x), (y + s / 3), (z + s / 3), s / 3, N - 1
  75.     initFractal (x + s / 3), (y + s / 3), (z + s / 3), s / 3, N - 1
  76.     'uske peeche wali row, left to right
  77.     initFractal (x - s / 3), (y + s / 3), (z), s / 3, N - 1
  78.     initFractal (x + s / 3), (y + s / 3), (z), s / 3, N - 1
  79.     'sabse peeche wali row, left to right
  80.     initFractal (x - s / 3), (y + s / 3), (z - s / 3), s / 3, N - 1
  81.     initFractal (x), (y + s / 3), (z - s / 3), s / 3, N - 1
  82.     initFractal (x + s / 3), (y + s / 3), (z - s / 3), s / 3, N - 1
  83.     'middle section
  84.     'sabse samne wali row, left to right
  85.     initFractal (x - s / 3), (y), (z + s / 3), s / 3, N - 1
  86.     initFractal (x + s / 3), (y), (z + s / 3), s / 3, N - 1
  87.     'sabse peeche wali row, left to right
  88.     initFractal (x - s / 3), (y), (z - s / 3), s / 3, N - 1
  89.     initFractal (x + s / 3), (y), (z - s / 3), s / 3, N - 1
  90.     'bottom section
  91.     'sabse samne wali row, left to right
  92.     initFractal (x - s / 3), (y - s / 3), (z + s / 3), s / 3, N - 1
  93.     initFractal (x), (y - s / 3), (z + s / 3), s / 3, N - 1
  94.     initFractal (x + s / 3), (y - s / 3), (z + s / 3), s / 3, N - 1
  95.     'uske peeche wali row, left to right
  96.     initFractal (x - s / 3), (y - s / 3), (z), s / 3, N - 1
  97.     initFractal (x + s / 3), (y - s / 3), (z), s / 3, N - 1
  98.     'sabse peeche wali row, left to right
  99.     initFractal (x - s / 3), (y - s / 3), (z - s / 3), s / 3, N - 1
  100.     initFractal (x), (y - s / 3), (z - s / 3), s / 3, N - 1
  101.     initFractal (x + s / 3), (y - s / 3), (z - s / 3), s / 3, N - 1
  102.  
  103.  
  104. SUB drawFractal ()
  105.     FOR i = 0 TO UBOUND(cubeLoc)
  106.         _glPushMatrix
  107.         _glTranslatef cubeLoc(i).x, cubeLoc(i).y, cubeLoc(i).z
  108.         glutSolidCube fundamentalCubeSize
  109.         _glPopMatrix
  110.     NEXT
  111.  

Screenshot_1.png

« Last Edit: February 29, 2020, 12:38:08 am by Ashish »
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 + ...
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #1 on: February 28, 2020, 10:13:07 am »
Another great demo of Open_GL Ashish!

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #2 on: February 28, 2020, 01:05:48 pm »
Mighty cool.  It was easy to modify it to rotate by itself too. 

How is the color changed?
It works better if you plug it in.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #3 on: February 28, 2020, 02:31:17 pm »
Very nice work, Ashish!

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #4 on: February 28, 2020, 05:21:03 pm »
Ashish, have you made any tutorials on the use of OpenGL within QB64? I need to migrate toward OpenGL but it feels like a monster in the closet to me. I have no idea where to even begin. Your OpenGL work amazes me.
In order to understand recursion, one must first understand recursion.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #5 on: February 29, 2020, 12:44:41 am »
Thank you @bplus, @Richard Frost , @Petr and @TerryRitchie
@Richard Frost
You can change color by changing color of the light. Go to line 51, and change value of clr(0), clr(1) and clr(2). These clr(0), clr(1), clr(2)
are basically the red, green & blue component of light. Value must be between 0 and 1. SO for color yellow, set clr(0) = 1, clr(1) = 1 and clr(2) = 0

@TerryRitchie
Learning OpenGL is easy... for basics please visit -> https://ashishkingdom.github.io/OpenGL-Tutorials
Please solve the exercise before moving to next section.
Site is in development.
« Last Edit: February 29, 2020, 12:46:29 am by Ashish »
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 TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #6 on: February 29, 2020, 01:51:40 am »
Awesome! You are making tutorials. Yes, please do finish them, looking forward to the 3D sections.
In order to understand recursion, one must first understand recursion.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #7 on: February 29, 2020, 08:36:25 am »
Thanks for the color info, Ashish.  It's a bit nicer with the colors slowly cycling, IMO.

I made the rotation automatic with:
tx = (tx + 1) MOD _WIDTH
ty = (ty + 1) MOD _HEIGHT
_glRotatef tx, 0, 1, 0
_glRotatef ty, 1, 0, 0

Color cycling, auto rotation, and killing that initial "press a key" intro is great for lazy people like me.

I hope you'll simulate this cube next:
&t=1s

It works better if you plug it in.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #8 on: March 14, 2020, 09:12:56 am »
Ashish, your graphical output of 27 cubes assembled into a larger cube is probably just what is needed in my Rubik's Cube Program.

https://www.qb64.org/forum/index.php?topic=894.0

There (because I have no skill with Open_GL and at the time I did not even know about _MAPTRIANGLE(3D)) I created all the mathematics for the graphics from scratch.  When (at some far-off time in the future) you have completed your Open_GL tutorials I should imagine that Open_GL would be ideal for the Rubik's Cube program.



Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #9 on: March 14, 2020, 09:59:01 am »
@Richard Frost  That's crazy. I'm sure you are joking with me.
@Qwerkey
Yeah. I thought about that once. It will be not be much harder to code one. Yeah, I'm working on the site as well as many other upcoming projects.
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 Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #10 on: March 22, 2020, 12:36:39 pm »
Why have you brought the Borg to QB64?!

We will all be assimilated!

Resistance is futile!


5 works fine... 6 might work someday(currently Not Responding).. 7 goes Out Of Memory, and 10 just looks at me like I'm Out Of My Mind!
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #11 on: March 22, 2020, 04:54:07 pm »
Hey imagine playing snake Game on this surface!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #12 on: March 23, 2020, 05:15:56 am »
Don't tempt me...
You're not done when it works, you're done when it's right.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #13 on: March 23, 2020, 05:45:29 am »
Everybody: Do tempt him!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: 3D : Sierpinski Cube
« Reply #14 on: March 23, 2020, 09:21:20 am »
Thanks for trying it @Cobalt
Ok, I know here are some Star Trek/Star Wars fans.
It took a long time to understand what Cobalt mean in his reply until [banned user] helped me to understand Cobalt's message at Discord.

@bplus
I imagine snake moving on its surface and then suddenly it gets fall into the empty area on the surface. And then its head
appears from opposite face. ;)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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