Author Topic: Spherical Rainbow!  (Read 3648 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Spherical Rainbow!
« on: February 26, 2020, 12:21:19 pm »
Hi everyone! Yet, another cool OpenGL demo... I try to create a rainbow with the strips of sphere. Thanks to @STxAxTIC for helping in sphere rendering..
Enjoy the program. ;)
Code: QB64: [Select]
  1. 'By Ashish
  2. '26 Feb, 2020
  3. 'Thanks to STxAxTIC for helping in sphere rendering
  4. _title "Spherical Rainbow"
  5. screen _newimage(600,600,32)
  6.  
  7. dim shared allowGL
  8. allowGL = 1
  9.  
  10.         _limit 40
  11.  
  12. sub _gl() static
  13.         if allowGL=0 then exit sub
  14.         if glInit = 0 then
  15.                 _glViewport 0,0,_width,_height
  16.                 aspect#= _width/_height
  17.                 r = 1
  18.                
  19.                 glInit = 1
  20.         end if
  21.         _glEnable _GL_DEPTH_TEST
  22.        
  23.         _glMatrixMode _GL_PROJECTION
  24.         _glLoadIdentity
  25.         _gluPerspective 50,aspect#,0.1,10.0
  26.        
  27.         _glMatrixMode _GL_MODELVIEW
  28.         _glLoadIdentity
  29.         _glRotatef c*60,1,1,1
  30.         c=c+0.01
  31.        
  32.         s = 0.07
  33.         a = 0
  34.         for theta = 0 to _pi step s
  35.                 a~& = hsb(map(theta,0,_pi,0,255),255,128,255)
  36.                 _glColor3f _red(a~&)/255, _green(a~&)/255, _blue(a~&)/255
  37.                 _glBegin _gl_triangle_strip
  38.                 for phi = 0 to _pi(2)+s step s
  39.                         x0 = r*sin(theta)*cos(phi)
  40.                         y0 = r*sin(theta)*sin(phi)
  41.                         z0 = r*cos(theta)
  42.                        
  43.                         x1 = r*sin(theta+s)*cos(phi)
  44.                         y1 = r*sin(theta+s)*sin(phi)
  45.                         z1 = r*cos(theta+s)
  46.                        
  47.                         _glVertex3f x0,y0,z0
  48.                         _glVertex3f x1,y1,z1
  49.                 next
  50.                 _glEnd
  51.                 a=a+1
  52.         next
  53.        
  54.         _glFlush
  55.  
  56. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  57.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  58.  
  59. 'method adapted form http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors
  60. FUNCTION hsb~& (__H AS _FLOAT, __S AS _FLOAT, __B AS _FLOAT, A AS _FLOAT)
  61.     DIM H AS _FLOAT, S AS _FLOAT, B AS _FLOAT
  62.  
  63.     H = map(__H, 0, 255, 0, 360)
  64.     S = map(__S, 0, 255, 0, 1)
  65.     B = map(__B, 0, 255, 0, 1)
  66.  
  67.     IF S = 0 THEN
  68.         hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
  69.         EXIT FUNCTION
  70.     END IF
  71.  
  72.     DIM fmx AS _FLOAT, fmn AS _FLOAT
  73.     DIM fmd AS _FLOAT, iSextant AS INTEGER
  74.     DIM imx AS INTEGER, imd AS INTEGER, imn AS INTEGER
  75.  
  76.     IF B > .5 THEN
  77.         fmx = B - (B * S) + S
  78.         fmn = B + (B * S) - S
  79.     ELSE
  80.         fmx = B + (B * S)
  81.         fmn = B - (B * S)
  82.     END IF
  83.  
  84.     iSextant = INT(H / 60)
  85.  
  86.     IF H >= 300 THEN
  87.         H = H - 360
  88.     END IF
  89.  
  90.     H = H / 60
  91.     H = H - (2 * INT(((iSextant + 1) MOD 6) / 2))
  92.  
  93.     IF iSextant MOD 2 = 0 THEN
  94.         fmd = (H * (fmx - fmn)) + fmn
  95.     ELSE
  96.         fmd = fmn - (H * (fmx - fmn))
  97.     END IF
  98.  
  99.     imx = _ROUND(fmx * 255)
  100.     imd = _ROUND(fmd * 255)
  101.     imn = _ROUND(fmn * 255)
  102.  
  103.     SELECT CASE INT(iSextant)
  104.         CASE 1
  105.             hsb~& = _RGBA32(imd, imx, imn, A)
  106.         CASE 2
  107.             hsb~& = _RGBA32(imn, imx, imd, A)
  108.         CASE 3
  109.             hsb~& = _RGBA32(imn, imd, imx, A)
  110.         CASE 4
  111.             hsb~& = _RGBA32(imd, imn, imx, A)
  112.         CASE 5
  113.             hsb~& = _RGBA32(imx, imn, imd, A)
  114.         CASE ELSE
  115.             hsb~& = _RGBA32(imx, imd, imn, A)
  116.     END SELECT
  117.  
  118.  
  119.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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