Author Topic: Deformed Sphere (3D)  (Read 6341 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Deformed Sphere (3D)
« on: July 06, 2019, 07:40:07 am »
Hi guys. I muss go out, but have problem. It is most for 3D programmers. Why my attempt for sphere draws something als baseball ball but not sphere? Is used MAPTRINGLE 3D again:
I look back in the evening, or maybe tomorrow, I'm go to celebrate friends daughter's birthday with our kids. If someone reveals the reason for the error, I will be very happy. Thank you.

Program idea is - draw circles in space, from zero radius for first circle in foreground to full radius in middle and then from middle with full radius to zero radius in background.
I know about _gluSphere, but i try solving it with _MAPTRIANGLE.

Code: QB64: [Select]
  1. 'cilem programu je rotujici koule pomoci maptriangle.
  2.     cX AS SINGLE
  3.     cY AS SINGLE
  4.     cZ AS SINGLE
  5.     X AS SINGLE
  6.     Y AS SINGLE
  7.     Z AS SINGLE
  8.     R AS SINGLE
  9.     Pi AS SINGLE
  10.     Pi2 AS SINGLE
  11. REDIM SHARED v(0) AS V
  12.  
  13.  
  14. texture& = Textur&
  15. init_Sphere 0.1, 0.1, -15, 10
  16.  
  17.  
  18. 'test
  19.     RotoXZ
  20.     draw_Sphere texture&
  21.     _DISPLAY
  22.  
  23.  
  24.  
  25. SUB test
  26.     FOR t = LBOUND(v) TO UBOUND(v)
  27.         PRINT v(t).Z; v(t).R; t
  28.         SLEEP
  29.     NEXT
  30.  
  31.  
  32.  
  33.  
  34.  
  35. SUB init_Sphere (cx AS SINGLE, cy AS SINGLE, cz AS SINGLE, radius AS SINGLE)
  36.  
  37.     'one circle circuit is set here as 8 steps (6.28/8)
  38.  
  39.     O = radius / 8 'step on circle circuit
  40.     Hl = radius / 16 'depth step in Z axis
  41.     nz = cz + radius 'nz = new Z = new depth
  42.  
  43.     FOR r1 = 0 TO radius STEP O 'udelej 8 kruznic
  44.         FOR c1 = 0 TO _PI(2) STEP _PI(2) / 8
  45.             nX = cx + (SIN(c1) * r1)
  46.             nY = cy + (COS(c1) * r1)
  47.             tz = cz + COS(c1) * r1
  48.             '            pi = JK(cy, cz, nY, nz, r1)
  49.             '           pi2 = JK(cx, cz, nX, nz, r1)
  50.             v(i).X = nX
  51.             v(i).Y = nY
  52.             v(i).Z = nz
  53.             v(i).cZ = cz
  54.             v(i).cY = cy
  55.             v(i).cX = cx
  56.             '    v(i).Pi = pi
  57.             '    v(i).Pi2 = pi2
  58.  
  59.             radius3D = SQR((cx - nX) ^ 2 + (cy - nY) ^ 2 + (cz - nz) ^ 2)
  60.  
  61.             v(i).R = radius3D
  62.  
  63.             pi = JK(cy, cz, nY, nz, radius3D)
  64.             pi2 = JK(cx, cz, nX, nz, radius3D)
  65.  
  66.             v(i).Pi = pi
  67.             v(i).Pi2 = pi2
  68.  
  69.             i = i + 1
  70.             REDIM _PRESERVE v(i) AS V
  71.  
  72.         NEXT c1
  73.  
  74.         nz = nz - Hl
  75.  
  76.     NEXT r1
  77.  
  78.  
  79.     FOR r1 = radius - O TO 0 STEP -O
  80.         FOR c1 = 0 TO _PI(2) STEP _PI(2) / 8
  81.             nX = cx + (SIN(c1) * r1)
  82.             nY = cy + (COS(c1) * r1)
  83.             tz = cz + COS(c1) * r1
  84.             ' pi = JK(cy, cz, nY, nz, r1)
  85.             'pi2 = JK(cx, cz, nX, nz, r1)
  86.             v(i).X = nX
  87.             v(i).Y = nY
  88.             v(i).Z = nz
  89.             v(i).cZ = cz
  90.             v(i).cY = cy
  91.             v(i).cX = cx
  92.             '            v(i).Pi = pi
  93.             '            v(i).Pi2 = pi2
  94.  
  95.             radius3D = SQR((cx - nX) ^ 2 + (cy - nY) ^ 2 + (cz - nz) ^ 2)
  96.             v(i).R = radius3D
  97.  
  98.             pi = JK(cy, cz, nY, nz, radius3D)
  99.             pi2 = JK(cx, cz, nX, nz, radius3D)
  100.  
  101.             v(i).Pi = pi
  102.             v(i).Pi2 = pi2
  103.             i = i + 1
  104.             REDIM _PRESERVE v(i) AS V
  105.         NEXT c1
  106.         nz = nz - Hl
  107.     NEXT r1
  108.     i = i - 1
  109.     REDIM _PRESERVE v(i) AS V
  110.  
  111.  
  112. SUB draw_Sphere (texture AS LONG)
  113.     'je to po osmi zaznamech na jeden obvod   'every one circle circuit contains 8 steps (so one radius is not circle, but 8-angle)
  114.     W = _WIDTH(texture)
  115.     H = _HEIGHT(texture)
  116.  
  117.     c = 1
  118.     DO UNTIL c >= UBOUND(v) - 8
  119.         FOR i = c TO c + 7
  120.             i2 = 9 + i
  121.             _MAPTRIANGLE (0, 0)-(W, 0)-(0, H), texture TO(v(i - 1).X, v(i - 1).Y, v(i - 1).Z)-(v(i).X, v(i).Y, v(i).Z)-(v(i2 - 1).X, v(i2 - 1).Y, v(i2 - 1).Z)
  122.             _MAPTRIANGLE (W, 0)-(0, H)-(W, H), texture TO(v(i).X, v(i).Y, v(i).Z)-(v(i2 - 1).X, v(i2 - 1).Y, v(i2 - 1).Z)-(v(i2).X, v(i2).Y, v(i2).Z)
  123.         NEXT i
  124.         c = c + 9
  125.     LOOP
  126.  
  127.  
  128.  
  129.  
  130. SUB RotoXZ
  131.     SHARED angle
  132.     angle = angle + .0001
  133.     FOR r = LBOUND(v) TO UBOUND(v)
  134.         cx = v(r).cX
  135.         cz = v(r).cZ
  136.         pi = v(r).Pi2
  137.         radius = v(r).R
  138.         nx = cx + SIN(pi + angle) * radius
  139.         nz = cz + COS(pi + angle) * radius
  140.         v(r).Z = nz
  141.         v(r).X = nx
  142.         REM        PRINT nx, ny, nz
  143.     NEXT r
  144.  
  145.  
  146. SUB RotoYZ
  147.     SHARED angle
  148.     angle = angle + .001
  149.     FOR r = LBOUND(v) TO UBOUND(v)
  150.         cy = v(r).cY
  151.         cz = v(r).cZ
  152.         pi = v(r).Pi
  153.         radius = v(r).R
  154.         nz = cz + SIN(pi + angle) * radius
  155.         ny = cy + COS(pi + angle) * radius
  156.         v(r).Z = nz
  157.         v(r).Y = ny
  158.         REM        PRINT nx, ny, nz
  159.     NEXT r
  160.  
  161.  
  162. FUNCTION JK! (cx, cy, px, py, R!) 'return vector angle
  163.     LenX! = cx - px
  164.     LenY! = cy - py
  165.     jR! = 1 / R!
  166.  
  167.     jX! = LenX! * jR!
  168.     jY! = LenY! * jR!
  169.  
  170.     sinusAlfa! = jX!
  171.     Alfa! = ABS(_ASIN(sinusAlfa!))
  172.  
  173.     Q = 1
  174.     IF px >= cx AND py >= cy THEN Q = 1 ' select angle to quadrant
  175.     IF px >= cx AND py <= cy THEN Q = 2
  176.     IF px <= cx AND py <= cy THEN Q = 3
  177.     IF px <= cx AND py >= cy THEN Q = 4
  178.     SELECT CASE Q
  179.         CASE 1: alfaB! = Alfa!
  180.         CASE 2: alfaB! = _PI / 2 + (_PI / 2 - Alfa!)
  181.         CASE 3: alfaB! = _PI + Alfa!
  182.         CASE 4: alfaB! = _PI(1.5) + (_PI / 2 - Alfa!)
  183.     END SELECT
  184.     JK! = alfaB!
  185.     IF JK! = 0 THEN BEEP
  186.  
  187.  
  188. FUNCTION Textur&
  189.     text = _NEWIMAGE(100, 100, 32)
  190.     a = _DEST
  191.     _DEST text
  192.     FOR f = 5 TO 0 STEP -1
  193.         LINE (f, f)-(100 - f, 100 - f), &HFFFFFFFF, B
  194.     NEXT f
  195.     LINE (5, 5)-(95, 95), &H00000000, BF
  196.     Textur& = _COPYIMAGE(text, 33)
  197.     _DEST a
  198.     _FREEIMAGE text
  199.  
  200.  

preview.JPG
* preview.JPG (Filesize: 83.21 KB, Dimensions: 655x438, Views: 178)

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Deformed Sphere (3D)
« Reply #1 on: July 06, 2019, 11:51:45 am »
It looks like you're trying to build a golf net... Can I help?

All that 3-D effect in under 200 lines of code, wow! I hope you get some non-SCREEN 0 guru to tweak the math so you get  that  sphere shape you want out of it. Apparently _maptriangle likes to create geometric shapes in it's own image?

Oh well, sorry I am of no use at all when it comes to graphics, but I did run the program and posted this reply because I'm impressed with the 3-D effects in general. Back in the old QB days, it took more lines to rotate a simple 3-D cube outline on one axis.

EDIT: Well, I can't just sit around on my ASCII and do nothing, so I fiddled with one line. This change does improve the shape, making it have a more spherical appearance, but I have no idea if it is close enough to what you are looking for or if other areas of the code need to be modified to further improve the effect.

So, try: init_Sphere 0, 0, -6, 6

Pete
« Last Edit: July 06, 2019, 12:11:09 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Deformed Sphere (3D)
« Reply #2 on: July 07, 2019, 09:56:43 am »
Hi Petr! You are doing everything fine, but I see that you are not using correct equation for sphere in init_sphere() subroutine.
This is the actual equation for the sphere which I found according to wikipedia -

Screenshot_1.png


I write a little demo using these equation. It is in OpenGL (I'm not good at _maptriangle)
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2. TYPE vec3
  3.     x AS SINGLE
  4.     y AS SINGLE
  5.     z AS SINGLE
  6.     _DELAY 0.05
  7.  
  8. SUB _GL ()
  9.     DIM center AS vec3, vertex AS vec3, radius
  10.     center.x = 0
  11.     center.y = 0
  12.     center.z = 0
  13.     radius = 1
  14.     _glPointSize 5.0
  15.     _glRotatef TIMER * 30, 1, 2, 0
  16.     _glBegin _GL_POINTS
  17.     FOR theta = 0 TO _PI STEP _PI / 50
  18.         FOR phi = 0 TO _PI(2) STEP _PI(2) / 100
  19.             vertex.x = center.x + SIN(theta) * COS(phi) * radius
  20.             vertex.y = center.y + SIN(theta) * SIN(phi) * radius
  21.             vertex.z = center.z + COS(theta) * radius
  22.  
  23.             _glVertex3f vertex.x, vertex.y, vertex.z
  24.         NEXT phi
  25.     NEXT theta
  26.     _glEnd
  27.  
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: Deformed Sphere (3D)
« Reply #3 on: July 07, 2019, 10:04:33 am »
Hi Ashish,

WOW OpenGL does it again!

Is there a way to differentiate one hemisphere and pole from the other, like make the dots of closer one larger and/or farther one darker in shade?

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Deformed Sphere (3D)
« Reply #4 on: July 07, 2019, 10:16:31 am »
Yes, why not?
I used shading method for differentiating hemisphere and poles.
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2. TYPE vec3
  3.     x AS SINGLE
  4.     y AS SINGLE
  5.     z AS SINGLE
  6.     _DELAY 0.05
  7.  
  8. SUB _GL ()
  9.     DIM center AS vec3, vertex AS vec3, radius
  10.     center.x = 0
  11.     center.y = 0
  12.     center.z = 0
  13.     radius = 1
  14.     _glPointSize 5.0
  15.     _glRotatef TIMER * 30, 1, 2, 0
  16.     _glBegin _GL_POINTS
  17.     FOR theta = 0 TO _PI STEP _PI / 50
  18.         FOR phi = 0 TO _PI(2) STEP _PI(2) / 100
  19.             vertex.x = center.x + SIN(theta) * COS(phi) * radius
  20.             vertex.y = center.y + SIN(theta) * SIN(phi) * radius
  21.             vertex.z = center.z + COS(theta) * radius
  22.             c = ABS(COS(theta))
  23.             _glColor3f c, c, c
  24.             _glVertex3f vertex.x, vertex.y, vertex.z
  25.         NEXT phi
  26.     NEXT theta
  27.     _glEnd
  28.  
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 Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Deformed Sphere (3D)
« Reply #5 on: July 07, 2019, 12:39:11 pm »
So. Guys. Thank you very much for your interest and your patince with me.

What I did. Flat disk. Something that can be called a 3D satellite with a little imagination. Shape that can be used on a 3D submarine (cigar shape). Something that can be used as a cannon when properly textured. I have achieved everything possible. With the idea that the regular round circuit will be supplemented by another sum of SIN or COS to achieve an oval (and irregular) shape, I can texture the meteorites. BUT PERFECT BALLS I CAN NOT DO! I attach the source. Play with the variables O, Hl, Nz, PIN in the init_sphere sub, to get a variety of 3D things. This is fun!
Ashish, thank you for the ball equation, when I try to implant it, I achieved some sort of thing that looked like - a funny statement - as gatehouse from Chernobyl,  that flew into space after the Chernobyl explosion. :-D I'm sure it's due my implantation that was bad. I realized that increasing the distance in the Z-axis simply couldn't be linear, because then I would get some shape of a dragonfly or how to call it better. Your OpenGL version is a fantastic example of math and speed. 30 lines and you're done. What? Unreal. Absolutely great work. I can't imagine how I would connect the individual points of the surface for maptriangle in this way.
Pete thank you for your interest and effort to find a problem. I appreciate it.

almost ball source:

Code: QB64: [Select]
  1.  
  2.     cX AS SINGLE
  3.     cY AS SINGLE
  4.     cZ AS SINGLE
  5.     X AS SINGLE
  6.     Y AS SINGLE
  7.     Z AS SINGLE
  8.     R AS SINGLE
  9.     Pi AS SINGLE
  10.     Pi2 AS SINGLE
  11.     pi3 AS SINGLE
  12. REDIM SHARED v(0) AS V
  13.  
  14.  
  15. texture& = Textur&
  16. init_Sphere 0, 0, -6, 2, 24 'this last number muss be the same!
  17.  
  18.     RotoXZ
  19.     draw_Sphere texture&, 24 'this last number muss be the same!
  20.     _DISPLAY
  21.  
  22.  
  23. SUB init_Sphere (cx AS SINGLE, cy AS SINGLE, cz AS SINGLE, radius AS SINGLE, N)
  24.  
  25.     O = radius / 8 'step on circle circuit
  26.     hl = radius / (_PI / .9)
  27.     nz = cz - radius
  28.     PIN = N * 2
  29.  
  30.  
  31.     t = 0
  32.     FOR r1 = 0 TO radius STEP O
  33.         t = t + _PI / PIN
  34.         FOR c1 = 0 TO _PI(2) + .001 STEP _PI(2) / N
  35.             nX = cx + (SIN(c1) * r1)
  36.             nY = cy + (COS(c1) * r1)
  37.  
  38.             v(i).X = nX
  39.             v(i).Y = nY
  40.             v(i).Z = nz
  41.             v(i).cZ = cz
  42.             v(i).cY = cy
  43.             v(i).cX = cx
  44.             radius3d = SQR((cx - nX) ^ 2 + (cz - nz) ^ 2)
  45.  
  46.             v(i).R = radius3d
  47.  
  48.             PI = JK(cy, cz, nY, nz, radius3d)
  49.             pi2 = JK(cx, cz, nX, nz, radius3d)
  50.             v(i).pi3 = JK(cx, cy, nX, nY, radius3d)
  51.             v(i).Pi = PI
  52.             v(i).Pi2 = pi2
  53.  
  54.             i = i + 1
  55.             REDIM _PRESERVE v(i) AS V
  56.  
  57.         NEXT c1
  58.         nz = nz + hl * SIN(t)
  59.     NEXT r1
  60.  
  61.     ' t = 0
  62.     FOR r1 = radius TO 0 STEP -O
  63.         t = t - _PI / PIN
  64.         FOR c1 = -.001 TO _PI(2) STEP _PI(2) / N
  65.             nX = cx + SIN(c1) * r1
  66.             nY = cy + COS(c1) * r1
  67.  
  68.  
  69.             v(i).X = nX
  70.             v(i).Y = nY
  71.             v(i).Z = nz
  72.             v(i).cZ = cz
  73.             v(i).cY = cy
  74.             v(i).cX = cx
  75.             radius3d = SQR((cx - nX) ^ 2 + (cz - nz) ^ 2)
  76.             v(i).R = radius3d
  77.  
  78.             PI = JK(cy, cz, nY, nz, radius3d)
  79.             pi2 = JK(cx, cz, nX, nz, radius3d)
  80.             v(i).pi3 = JK(cx, cy, nX, nY, radius3d)
  81.             v(i).Pi = PI
  82.             v(i).Pi2 = pi2
  83.             i = i + 1
  84.             REDIM _PRESERVE v(i) AS V
  85.         NEXT c1
  86.         nz = nz + hl * SIN(t)
  87.  
  88.     NEXT r1
  89.     i = i - 1
  90.     REDIM _PRESERVE v(i) AS V
  91.  
  92.  
  93. SUB draw_Sphere (texture AS LONG, N)
  94.     W = _WIDTH(texture)
  95.     H = _HEIGHT(texture)
  96.  
  97.     c = 1
  98.     DO UNTIL c >= UBOUND(v) - N
  99.         FOR i = c TO c + (N - 1)
  100.             i2 = (N + 1) + i
  101.             _MAPTRIANGLE (0, 0)-(W, 0)-(0, H), texture TO(v(i - 1).X, v(i - 1).Y, v(i - 1).Z)-(v(i).X, v(i).Y, v(i).Z)-(v(i2 - 1).X, v(i2 - 1).Y, v(i2 - 1).Z)
  102.             _MAPTRIANGLE (W, 0)-(0, H)-(W, H), texture TO(v(i).X, v(i).Y, v(i).Z)-(v(i2 - 1).X, v(i2 - 1).Y, v(i2 - 1).Z)-(v(i2).X, v(i2).Y, v(i2).Z)
  103.         NEXT i
  104.         c = c + (N + 1)
  105.     LOOP
  106.  
  107.  
  108.  
  109. SUB RotoXZ
  110.     SHARED angle
  111.     angle = angle + .0001
  112.     FOR r = LBOUND(v) TO UBOUND(v)
  113.         cx = v(r).cX
  114.         cz = v(r).cZ
  115.         pi = v(r).Pi2
  116.         pi3 = v(r).pi3
  117.         radius = v(r).R
  118.         nx = cx + SIN(pi + angle) * radius
  119.         nz = cz + COS(pi + angle) * radius
  120.         v(r).Z = nz
  121.         v(r).X = nx
  122.         REM        PRINT nx, ny, nz
  123.     NEXT r
  124.  
  125.  
  126. SUB RotoYZ
  127.     SHARED angle
  128.     angle = angle + .0001
  129.     FOR r = LBOUND(v) TO UBOUND(v)
  130.         cy = v(r).cY
  131.         cz = v(r).cZ
  132.         pi = v(r).Pi
  133.         radius = v(r).R
  134.         nz = cz + SIN(pi + angle) * radius
  135.         ny = cy + COS(pi + angle) * radius
  136.         v(r).Z = nz
  137.         v(r).Y = ny
  138.         REM        PRINT nx, ny, nz
  139.     NEXT r
  140.  
  141.  
  142. FUNCTION JK! (cx, cy, px, py, R!) 'return vector angle
  143.     LenX! = cx - px
  144.     LenY! = cy - py
  145.     jR! = 1 / R!
  146.  
  147.     jX! = LenX! * jR!
  148.     jY! = LenY! * jR!
  149.  
  150.     sinusAlfa! = jX!
  151.     Alfa! = ABS(_ASIN(sinusAlfa!))
  152.  
  153.     Q = 1
  154.     IF px >= cx AND py >= cy THEN Q = 1 ' select angle to quadrant
  155.     IF px >= cx AND py <= cy THEN Q = 2
  156.     IF px <= cx AND py <= cy THEN Q = 3
  157.     IF px <= cx AND py >= cy THEN Q = 4
  158.     SELECT CASE Q
  159.         CASE 1: alfaB! = Alfa!
  160.         CASE 2: alfaB! = _PI / 2 + (_PI / 2 - Alfa!)
  161.         CASE 3: alfaB! = _PI + Alfa!
  162.         CASE 4: alfaB! = _PI(1.5) + (_PI / 2 - Alfa!)
  163.     END SELECT
  164.     JK! = alfaB!
  165.     IF JK! = 0 THEN BEEP
  166.  
  167.  
  168. FUNCTION Textur&
  169.     text = _NEWIMAGE(100, 100, 32)
  170.     a = _DEST
  171.     _DEST text
  172.     FOR f = 5 TO 0 STEP -1
  173.         LINE (f, f)-(100 - f, 100 - f), &HFFFFFFFF, B
  174.     NEXT f
  175.     LINE (5, 5)-(95, 95), &H00000000, BF
  176.     Textur& = _COPYIMAGE(text, 33)
  177.     _DEST a
  178.     _FREEIMAGE text
  179.  

"Sattelite"

Code: QB64: [Select]
  1.  
  2.     cX AS SINGLE
  3.     cY AS SINGLE
  4.     cZ AS SINGLE
  5.     X AS SINGLE
  6.     Y AS SINGLE
  7.     Z AS SINGLE
  8.     R AS SINGLE
  9.     Pi AS SINGLE
  10.     Pi2 AS SINGLE
  11.     pi3 AS SINGLE
  12. REDIM SHARED v(0) AS V
  13.  
  14.  
  15. texture& = Textur&
  16. init_Sphere 0, 0, -6, 2
  17.  
  18.  
  19. 'test
  20.     RotoXZ
  21.     draw_Sphere texture&
  22.     _DISPLAY
  23.  
  24.  
  25.  
  26. SUB test
  27.     FOR t = LBOUND(v) TO UBOUND(v)
  28.         PRINT v(t).Z; v(t).R; t
  29.         SLEEP
  30.     NEXT
  31.  
  32.  
  33.  
  34.  
  35.  
  36. SUB init_Sphere (cx AS SINGLE, cy AS SINGLE, cz AS SINGLE, radius AS SINGLE)
  37.  
  38.     'one circle circuit is set here as 8 steps (6.28/8)
  39.  
  40.     O = radius / 8 'step on circle circuit
  41.     Hl = _PI / 16 '16 'depth step in Z axis
  42.     nz = cz + radius
  43.  
  44.     FOR r1 = 0 TO radius STEP O 'udelej 8 kruznic
  45.         FOR c1 = 0 TO _PI(2) STEP _PI(2) / 8
  46.             nX = cx + (SIN(c1) * r1)
  47.             nY = cy + (COS(c1) * r1)
  48.             v(i).X = nX
  49.             v(i).Y = nY
  50.             v(i).Z = nz
  51.             v(i).cZ = cz
  52.             v(i).cY = cy
  53.             v(i).cX = cx
  54.             radius3D = SQR((cx - nX) ^ 2 + (cz - nz) ^ 2) '+ (cz - nz) ^ 2)
  55.  
  56.             v(i).R = radius3D
  57.  
  58.             pi = JK(cy, cz, nY, nz, radius3D)
  59.             pi2 = JK(cx, cz, nX, nz, radius3D)
  60.             v(i).pi3 = JK(cx, cy, nX, nY, radius3D)
  61.             v(i).Pi = pi
  62.             v(i).Pi2 = pi2
  63.  
  64.             i = i + 1
  65.             REDIM _PRESERVE v(i) AS V
  66.  
  67.         NEXT c1
  68.  
  69.         nz = cz + SIN(Hl) * r1
  70.         Hl = Hl + Hl
  71.  
  72.  
  73.     NEXT r1
  74.  
  75.  
  76.     FOR r1 = radius - O TO 0 STEP -O
  77.         FOR c1 = 0 TO _PI(2) STEP _PI(2) / 8
  78.             nX = cx + (SIN(c1) * r1)
  79.             nY = cy + (COS(c1) * r1)
  80.  
  81.             v(i).X = nX
  82.             v(i).Y = nY
  83.             v(i).Z = nz
  84.             v(i).cZ = cz
  85.             v(i).cY = cy
  86.             v(i).cX = cx
  87.             radius3D = SQR((cx - nX) ^ 2 + (cz - nz) ^ 2) ' + (cz - nz) ^ 2)
  88.             v(i).R = radius3D
  89.  
  90.             pi = JK(cy, cz, nY, nz, radius3D)
  91.             pi2 = JK(cx, cz, nX, nz, radius3D)
  92.             v(i).pi3 = JK(cx, cy, nX, nY, radius3D)
  93.             v(i).Pi = pi
  94.             v(i).Pi2 = pi2
  95.             i = i + 1
  96.             REDIM _PRESERVE v(i) AS V
  97.         NEXT c1
  98.  
  99.         nz = cz + SIN(Hl) * r1
  100.         Hl = Hl + Hl
  101.  
  102.     NEXT r1
  103.     i = i - 1
  104.     REDIM _PRESERVE v(i) AS V
  105.  
  106.  
  107. SUB draw_Sphere (texture AS LONG)
  108.     'je to po osmi zaznamech na jeden obvod   'every one circle circuit contains 8 steps (so one radius is not circle, but 8-angle)
  109.     W = _WIDTH(texture)
  110.     H = _HEIGHT(texture)
  111.  
  112.     c = 1
  113.     DO UNTIL c >= UBOUND(v) - 8
  114.         FOR i = c TO c + 7
  115.             i2 = 9 + i
  116.             _MAPTRIANGLE (0, 0)-(W, 0)-(0, H), texture TO(v(i - 1).X, v(i - 1).Y, v(i - 1).Z)-(v(i).X, v(i).Y, v(i).Z)-(v(i2 - 1).X, v(i2 - 1).Y, v(i2 - 1).Z)
  117.             _MAPTRIANGLE (W, 0)-(0, H)-(W, H), texture TO(v(i).X, v(i).Y, v(i).Z)-(v(i2 - 1).X, v(i2 - 1).Y, v(i2 - 1).Z)-(v(i2).X, v(i2).Y, v(i2).Z)
  118.         NEXT i
  119.         c = c + 9
  120.     LOOP
  121.  
  122.  
  123.  
  124.  
  125. SUB RotoXZ
  126.     SHARED angle
  127.     angle = angle + .0001
  128.     FOR r = LBOUND(v) TO UBOUND(v)
  129.         cx = v(r).cX
  130.         cz = v(r).cZ
  131.         pi = v(r).Pi2
  132.         pi3 = v(r).pi3
  133.         radius = v(r).R
  134.         nx = cx + SIN(pi + angle) * radius
  135.         nz = cz + COS(pi + angle) * radius
  136.         v(r).Z = nz
  137.         v(r).X = nx
  138.         REM        PRINT nx, ny, nz
  139.     NEXT r
  140.  
  141.  
  142. SUB RotoYZ
  143.     SHARED angle
  144.     angle = angle + .0001
  145.     FOR r = LBOUND(v) TO UBOUND(v)
  146.         cy = v(r).cY
  147.         cz = v(r).cZ
  148.         pi = v(r).Pi
  149.         radius = v(r).R
  150.         nz = cz + SIN(pi + angle) * radius
  151.         ny = cy + COS(pi + angle) * radius
  152.         v(r).Z = nz
  153.         v(r).Y = ny
  154.         REM        PRINT nx, ny, nz
  155.     NEXT r
  156.  
  157.  
  158. FUNCTION JK! (cx, cy, px, py, R!) 'return vector angle
  159.     LenX! = cx - px
  160.     LenY! = cy - py
  161.     jR! = 1 / R!
  162.  
  163.     jX! = LenX! * jR!
  164.     jY! = LenY! * jR!
  165.  
  166.     sinusAlfa! = jX!
  167.     Alfa! = ABS(_ASIN(sinusAlfa!))
  168.  
  169.     Q = 1
  170.     IF px >= cx AND py >= cy THEN Q = 1 ' select angle to quadrant
  171.     IF px >= cx AND py <= cy THEN Q = 2
  172.     IF px <= cx AND py <= cy THEN Q = 3
  173.     IF px <= cx AND py >= cy THEN Q = 4
  174.     SELECT CASE Q
  175.         CASE 1: alfaB! = Alfa!
  176.         CASE 2: alfaB! = _PI / 2 + (_PI / 2 - Alfa!)
  177.         CASE 3: alfaB! = _PI + Alfa!
  178.         CASE 4: alfaB! = _PI(1.5) + (_PI / 2 - Alfa!)
  179.     END SELECT
  180.     JK! = alfaB!
  181.     IF JK! = 0 THEN BEEP
  182.  
  183.  
  184. FUNCTION Textur&
  185.     text = _NEWIMAGE(100, 100, 32)
  186.     a = _DEST
  187.     _DEST text
  188.     FOR f = 5 TO 0 STEP -1
  189.         LINE (f, f)-(100 - f, 100 - f), &HFFFFFFFF, B
  190.     NEXT f
  191.     LINE (5, 5)-(95, 95), &H00000000, BF
  192.     Textur& = _COPYIMAGE(text, 33)
  193.     _DEST a
  194.     _FREEIMAGE text
  195.  

"satellite 2"

Code: QB64: [Select]
  1. 'cilem programu je rotujici koule pomoci maptriangle.
  2.     cX AS SINGLE
  3.     cY AS SINGLE
  4.     cZ AS SINGLE
  5.     X AS SINGLE
  6.     Y AS SINGLE
  7.     Z AS SINGLE
  8.     R AS SINGLE
  9.     Pi AS SINGLE
  10.     Pi2 AS SINGLE
  11.     pi3 AS SINGLE
  12. REDIM SHARED v(0) AS V
  13.  
  14.  
  15. texture& = Textur&
  16. init_Sphere 0, 0, -6, 2
  17.  
  18.  
  19. 'test
  20.     RotoXZ
  21.     draw_Sphere texture&
  22.     _DISPLAY
  23.  
  24.  
  25.  
  26. SUB test
  27.     FOR t = LBOUND(v) TO UBOUND(v)
  28.         PRINT v(t).Z; v(t).R; t
  29.         SLEEP
  30.     NEXT
  31.  
  32.  
  33.  
  34.  
  35.  
  36. SUB init_Sphere (cx AS SINGLE, cy AS SINGLE, cz AS SINGLE, radius AS SINGLE)
  37.  
  38.     'one circle circuit is set here as 8 steps (6.28/8)
  39.  
  40.     O = radius / 16 'step on circle circuit
  41.     Hl = _PI / 32 '16 'depth step in Z axis
  42.     nz = cz + radius
  43.  
  44.     FOR r1 = 0 TO radius STEP O 'udelej 8 kruznic
  45.         FOR c1 = 0 TO _PI(2) STEP _PI(2) / 8
  46.             nX = cx + (SIN(c1) * r1)
  47.             nY = cy + (COS(c1) * r1)
  48.             v(i).X = nX
  49.             v(i).Y = nY
  50.             v(i).Z = nz
  51.             v(i).cZ = cz
  52.             v(i).cY = cy
  53.             v(i).cX = cx
  54.             radius3D = SQR((cx - nX) ^ 2 + (cz - nz) ^ 2) '+ (cz - nz) ^ 2)
  55.  
  56.             v(i).R = radius3D
  57.  
  58.             pi = JK(cy, cz, nY, nz, radius3D)
  59.             pi2 = JK(cx, cz, nX, nz, radius3D)
  60.             v(i).pi3 = JK(cx, cy, nX, nY, radius3D)
  61.             v(i).Pi = pi
  62.             v(i).Pi2 = pi2
  63.  
  64.             i = i + 1
  65.             REDIM _PRESERVE v(i) AS V
  66.  
  67.         NEXT c1
  68.  
  69.         nz = cz + SIN(Hl) * r1
  70.         Hl = Hl + Hl
  71.  
  72.  
  73.     NEXT r1
  74.  
  75.  
  76.     FOR r1 = radius - O TO 0 STEP -O
  77.         FOR c1 = 0 TO _PI(2) STEP _PI(2) / 8
  78.             nX = cx + (SIN(c1) * r1)
  79.             nY = cy + (COS(c1) * r1)
  80.  
  81.             v(i).X = nX
  82.             v(i).Y = nY
  83.             v(i).Z = nz
  84.             v(i).cZ = cz
  85.             v(i).cY = cy
  86.             v(i).cX = cx
  87.             radius3D = SQR((cx - nX) ^ 2 + (cz - nz) ^ 2) ' + (cz - nz) ^ 2)
  88.             v(i).R = radius3D
  89.  
  90.             pi = JK(cy, cz, nY, nz, radius3D)
  91.             pi2 = JK(cx, cz, nX, nz, radius3D)
  92.             v(i).pi3 = JK(cx, cy, nX, nY, radius3D)
  93.             v(i).Pi = pi
  94.             v(i).Pi2 = pi2
  95.             i = i + 1
  96.             REDIM _PRESERVE v(i) AS V
  97.         NEXT c1
  98.  
  99.         nz = cz + SIN(Hl) * r1
  100.         Hl = Hl + Hl
  101.  
  102.     NEXT r1
  103.     i = i - 1
  104.     REDIM _PRESERVE v(i) AS V
  105.  
  106.  
  107. SUB draw_Sphere (texture AS LONG)
  108.     'je to po osmi zaznamech na jeden obvod   'every one circle circuit contains 8 steps (so one radius is not circle, but 8-angle)
  109.     W = _WIDTH(texture)
  110.     H = _HEIGHT(texture)
  111.  
  112.     c = 1
  113.     DO UNTIL c >= UBOUND(v) - 8
  114.         FOR i = c TO c + 7
  115.             i2 = 9 + i
  116.             _MAPTRIANGLE (0, 0)-(W, 0)-(0, H), texture TO(v(i - 1).X, v(i - 1).Y, v(i - 1).Z)-(v(i).X, v(i).Y, v(i).Z)-(v(i2 - 1).X, v(i2 - 1).Y, v(i2 - 1).Z)
  117.             _MAPTRIANGLE (W, 0)-(0, H)-(W, H), texture TO(v(i).X, v(i).Y, v(i).Z)-(v(i2 - 1).X, v(i2 - 1).Y, v(i2 - 1).Z)-(v(i2).X, v(i2).Y, v(i2).Z)
  118.         NEXT i
  119.         c = c + 9
  120.     LOOP
  121.  
  122.  
  123.  
  124.  
  125. SUB RotoXZ
  126.     SHARED angle
  127.     angle = angle + .0001
  128.     FOR r = LBOUND(v) TO UBOUND(v)
  129.         cx = v(r).cX
  130.         cz = v(r).cZ
  131.         pi = v(r).Pi2
  132.         pi3 = v(r).pi3
  133.         radius = v(r).R
  134.         nx = cx + SIN(pi + angle) * radius
  135.         nz = cz + COS(pi + angle) * radius
  136.         v(r).Z = nz
  137.         v(r).X = nx
  138.         REM        PRINT nx, ny, nz
  139.     NEXT r
  140.  
  141.  
  142. SUB RotoYZ
  143.     SHARED angle
  144.     angle = angle + .0001
  145.     FOR r = LBOUND(v) TO UBOUND(v)
  146.         cy = v(r).cY
  147.         cz = v(r).cZ
  148.         pi = v(r).Pi
  149.         radius = v(r).R
  150.         nz = cz + SIN(pi + angle) * radius
  151.         ny = cy + COS(pi + angle) * radius
  152.         v(r).Z = nz
  153.         v(r).Y = ny
  154.         REM        PRINT nx, ny, nz
  155.     NEXT r
  156.  
  157.  
  158. FUNCTION JK! (cx, cy, px, py, R!) 'return vector angle
  159.     LenX! = cx - px
  160.     LenY! = cy - py
  161.     jR! = 1 / R!
  162.  
  163.     jX! = LenX! * jR!
  164.     jY! = LenY! * jR!
  165.  
  166.     sinusAlfa! = jX!
  167.     Alfa! = ABS(_ASIN(sinusAlfa!))
  168.  
  169.     Q = 1
  170.     IF px >= cx AND py >= cy THEN Q = 1 ' select angle to quadrant
  171.     IF px >= cx AND py <= cy THEN Q = 2
  172.     IF px <= cx AND py <= cy THEN Q = 3
  173.     IF px <= cx AND py >= cy THEN Q = 4
  174.     SELECT CASE Q
  175.         CASE 1: alfaB! = Alfa!
  176.         CASE 2: alfaB! = _PI / 2 + (_PI / 2 - Alfa!)
  177.         CASE 3: alfaB! = _PI + Alfa!
  178.         CASE 4: alfaB! = _PI(1.5) + (_PI / 2 - Alfa!)
  179.     END SELECT
  180.     JK! = alfaB!
  181.     IF JK! = 0 THEN BEEP
  182.  
  183.  
  184. FUNCTION Textur&
  185.     text = _NEWIMAGE(100, 100, 32)
  186.     a = _DEST
  187.     _DEST text
  188.     FOR f = 5 TO 0 STEP -1
  189.         LINE (f, f)-(100 - f, 100 - f), &HFFFFFFFF, B
  190.     NEXT f
  191.     LINE (5, 5)-(95, 95), &H00000000, BF
  192.     Textur& = _COPYIMAGE(text, 33)
  193.     _DEST a
  194.     _FREEIMAGE text
  195.  

and more and more....


I would like to add one thing. I just wanted to try. I thought it might be like this, I wasn't looking for formulas, nothing. I just sat down and started writing the program. I'm excited about a lot of new things I've discovered.
« Last Edit: July 07, 2019, 12:44:53 pm by Petr »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Deformed Sphere (3D)
« Reply #6 on: July 07, 2019, 01:20:00 pm »
I'm the candidate who will bring hemispheres and poles together!

@Amish: OpenGL seems well suited for this. I wonder if _maptriangle could make a sphere without noticeable linear components?

I wasn't sure what Petr meant by "baseball" in his original post, So I thought he was just looking for a way to make one hemisphere match the other, but if he wants the image perfectly rounded, you nailed it, just not with _maptriangle!

@Petr: I half finished my post, did some stuff in the yard, and saw you came up with a _maptriangle solution. I tried it, and yes, it evens things out between the two hemispheres better than just adjusting init_Sphere.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Deformed Sphere (3D)
« Reply #7 on: July 07, 2019, 01:44:04 pm »
Hi Pete. Certainly it can be done. I just chose the wrong way to do it. Those lines around the perimeter - that's the intention. It is actually the texture of a white rectangle with transparency set inside to see where the texture is mapped. To texturize the surface, simply take photos, split them into the same number of parts, and place them on a specific surface location. This achieves the effect of the textured body. For example, for a cylinder rotating in X / Z axes. If the cylinder is made up of ten segments, you divide the texture into 10 bars - the texture width / 10 and place it on the surface of the cylinder. Each segment is one tenth of the circumference., so one tenth from texture width.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Deformed Sphere (3D)
« Reply #8 on: July 07, 2019, 01:58:28 pm »
Oh, it's a math thing...

I tried math once, but it didn't add up!

Code: QB64: [Select]
  1. DEFINT A-Z
  2. var1 = 3
  3. up! = 2
  4.  
  5. main sum, var1, up
  6. PRINT sum
  7.  
  8. SUB main (sum, var1, up)
  9.     sum = var1 + up
  10.  

Pete :D
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Deformed Sphere (3D)
« Reply #9 on: July 07, 2019, 03:35:24 pm »
yes, Pete... :-D use DEFSNG.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Deformed Sphere (3D)
« Reply #10 on: July 07, 2019, 11:41:16 pm »
Hi Petr,

Those are cool looking satellites.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Deformed Sphere (3D)
« Reply #11 on: July 08, 2019, 12:02:20 am »
Saddle lights? Oh right. What cowboys use for night riding.

Pete :D
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Deformed Sphere (3D)
« Reply #12 on: July 08, 2019, 12:52:40 am »
Oh, it's a math thing...

I tried math once, but it didn't add up!

Code: QB64: [Select]
  1. DEFINT A-Z
  2. var1 = 3
  3. up! = 2
  4.  
  5. main sum, var1, up
  6. PRINT sum
  7.  
  8. SUB main (sum, var1, up)
  9.     sum = var1 + up
  10.  

Pete :D
You are forgetting "!" on line 5.
This add up the thing.
Code: QB64: [Select]
  1. DEFINT A-Z
  2. var1 = 3
  3. up! = 2
  4.  
  5. main sum, var1, up!
  6. PRINT sum
  7.  
  8. SUB main (sum, var1, up)
  9.     sum = var1 + up
  10.  
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 Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Deformed Sphere (3D)
« Reply #13 on: July 08, 2019, 01:37:23 am »
@Petr
Why not use OpenGL for this?
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 Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Deformed Sphere (3D)
« Reply #14 on: July 08, 2019, 08:12:23 am »
Hi Ashish. Why? 90 percent of my program use hardware images. Is problem with _DISPLAYORDER  _HARDWARE, _GLRENDER. Hardware layer is not visible if i use quadrics and external H file for call it. I just tried it. With _GLRENDER _BEHIND is visible hardware layer, but not OpenGL layer.... better way is maptriangle if is most used in existing program.