Author Topic: Cube Plasma  (Read 12718 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Cube Plasma
« on: June 12, 2018, 05:45:28 am »
Hi everyone! Here's a simple effect of plasma on cube. I have learn about plasma
in detail from here http://lodev.org/cgtutor/plasma.html

Here's the code -
Code: QB64: [Select]
  1. 'Cube plasma, coded by Ashish  6/12/2018
  2. 'Twitter : @KingOfCoders
  3. 'http://lodev.org/cgtutor/plasma.html
  4.  
  5. _TITLE "Cube Plasma"
  6. SCREEN _NEWIMAGE(600, 600, 32)
  7.  
  8.     '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, textureImage&(258), tmp_buffer_image&
  12. tmp_buffer_image& = _NEWIMAGE(200, 200, 32)
  13.  
  14. _DEST tmp_buffer_image&
  15. 'storing calculation in memory for faster rendering
  16. DIM sin1(_WIDTH - 1, _HEIGHT - 1), sin2(_WIDTH - 1, _HEIGHT - 1), sin3(_WIDTH - 1, _HEIGHT - 1)
  17. FOR y = 0 TO _HEIGHT - 1
  18.     FOR x = 0 TO _WIDTH - 1
  19.         sin1(x, y) = SIN(SQR(x ^ 2 + y ^ 2) * .09)
  20.         sin2(x, y) = SIN(y * .03)
  21.         sin3(x, y) = COS(((_WIDTH / 2 - x) ^ 2 + (_HEIGHT / 2 - y) ^ 2) ^ .5 * .07)
  22. NEXT x, y
  23.  
  24.     _DEST 0
  25.     CLS
  26.     PRINT "Generating Textures "; f; "/"; UBOUND(textureImage&) - 1
  27.     _DEST tmp_buffer_image&
  28.     f = f + 1
  29.     FOR y = 0 TO _HEIGHT - 1
  30.         FOR x = 0 TO _WIDTH - 1
  31.             col = sin1(x, y) * 64 + 64 + sin2(x, y) * 64 + 64 + sin3(x, y) * 64 + 64 + f
  32.             col = col MOD 256
  33.             PSET (x, y), hsb(col, 255, 128, 255)
  34.     NEXT x, y
  35.     textureImage&(f) = _COPYIMAGE(tmp_buffer_image&)
  36. LOOP UNTIL f > UBOUND(textureImage&) - 1
  37. _FREEIMAGE tmp_buffer_image&
  38.  
  39. glAllow = -1
  40.     _LIMIT 30
  41.  
  42. SUB _GL ()
  43.     STATIC cubeTexture&(601), glSetup, aspect#, frame
  44.  
  45.     IF NOT glAllow THEN EXIT SUB
  46.  
  47.     IF NOT glSetup THEN
  48.         glSetup = -1
  49.         _glViewport 0, 0, _WIDTH, _HEIGHT
  50.         'Convert all images to GL textures
  51.         FOR i = 1 TO UBOUND(textureImage&) - 1
  52.             _glGenTextures 1, _OFFSET(cubeTexture&(i))
  53.             DIM m AS _MEM
  54.             m = _MEMIMAGE(textureImage&(i))
  55.  
  56.             _glBindTexture _GL_TEXTURE_2D, cubeTexture&(i)
  57.             _glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGB, _WIDTH(textureImage&(i)), _HEIGHT(textureImage&(i)), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
  58.  
  59.             _MEMFREE m
  60.  
  61.             _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  62.             _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST
  63.             _FREEIMAGE textureImage&(i)
  64.         NEXT
  65.         aspect# = _WIDTH / _HEIGHT
  66.     END IF
  67.  
  68.     _glEnable _GL_TEXTURE_2D
  69.     _glEnable _GL_DEPTH_TEST
  70.  
  71.     _glMatrixMode _GL_PROJECTION
  72.     _gluPerspective 45.0, aspect#, 1, 100
  73.  
  74.     _glMatrixMode _GL_MODELVIEW
  75.  
  76.     gluLookAt 0, 0, 5, 0, 0, 0, 0, 1, 0
  77.  
  78.     i = (frame MOD (UBOUND(textureImage&) - 1)) + 1
  79.     'select our texture
  80.     _glBindTexture _GL_TEXTURE_2D, cubeTexture&(i)
  81.  
  82.     'rotation
  83.     _glRotatef frame * .01 * 120, 1, .5, .3
  84.  
  85.     _glBegin _GL_QUADS
  86.     'front face
  87.     _glTexCoord2f 0, 1
  88.     _glVertex3f -1, 1, 1
  89.     _glTexCoord2f 1, 1
  90.     _glVertex3f 1, 1, 1
  91.     _glTexCoord2f 1, 0
  92.     _glVertex3f 1, -1, 1
  93.     _glTexCoord2f 0, 0
  94.     _glVertex3f -1, -1, 1
  95.     'rear face
  96.     _glTexCoord2f 0, 1
  97.     _glVertex3f -1, 1, -1
  98.     _glTexCoord2f 1, 1
  99.     _glVertex3f 1, 1, -1
  100.     _glTexCoord2f 1, 0
  101.     _glVertex3f 1, -1, -1
  102.     _glTexCoord2f 0, 0
  103.     _glVertex3f -1, -1, -1
  104.     'upward face
  105.     _glTexCoord2f 0, 1
  106.     _glVertex3f -1, 1, -1
  107.     _glTexCoord2f 1, 1
  108.     _glVertex3f 1, 1, -1
  109.     _glTexCoord2f 1, 0
  110.     _glVertex3f 1, 1, 1
  111.     _glTexCoord2f 0, 0
  112.     _glVertex3f -1, 1, 1
  113.     'downward face
  114.     _glTexCoord2f 0, 1
  115.     _glVertex3f -1, -1, -1
  116.     _glTexCoord2f 1, 1
  117.     _glVertex3f 1, -1, -1
  118.     _glTexCoord2f 1, 0
  119.     _glVertex3f 1, -1, 1
  120.     _glTexCoord2f 0, 0
  121.     _glVertex3f -1, -1, 1
  122.     'left face
  123.     _glTexCoord2f 0, 1
  124.     _glVertex3f -1, 1, -1
  125.     _glTexCoord2f 1, 1
  126.     _glVertex3f -1, 1, 1
  127.     _glTexCoord2f 1, 0
  128.     _glVertex3f -1, -1, 1
  129.     _glTexCoord2f 0, 0
  130.     _glVertex3f -1, -1, -1
  131.     'right face
  132.     _glTexCoord2f 0, 1
  133.     _glVertex3f 1, 1, -1
  134.     _glTexCoord2f 1, 1
  135.     _glVertex3f 1, 1, 1
  136.     _glTexCoord2f 1, 0
  137.     _glVertex3f 1, -1, 1
  138.     _glTexCoord2f 0, 0
  139.     _glVertex3f 1, -1, -1
  140.  
  141.     _glEnd
  142.  
  143.     frame = frame + 1
  144.  
  145.  
  146.  
  147. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  148.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  149.  
  150. 'method adapted form http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors
  151. FUNCTION hsb~& (__H AS _FLOAT, __S AS _FLOAT, __B AS _FLOAT, A AS _FLOAT)
  152.     DIM H AS _FLOAT, S AS _FLOAT, B AS _FLOAT
  153.  
  154.     H = map(__H, 0, 255, 0, 360)
  155.     S = map(__S, 0, 255, 0, 1)
  156.     B = map(__B, 0, 255, 0, 1)
  157.  
  158.     IF S = 0 THEN
  159.         hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
  160.         EXIT FUNCTION
  161.     END IF
  162.  
  163.     DIM fmx AS _FLOAT, fmn AS _FLOAT
  164.     DIM fmd AS _FLOAT, iSextant AS INTEGER
  165.     DIM imx AS INTEGER, imd AS INTEGER, imn AS INTEGER
  166.  
  167.     IF B > .5 THEN
  168.         fmx = B - (B * S) + S
  169.         fmn = B + (B * S) - S
  170.     ELSE
  171.         fmx = B + (B * S)
  172.         fmn = B - (B * S)
  173.     END IF
  174.  
  175.     iSextant = INT(H / 60)
  176.  
  177.     IF H >= 300 THEN
  178.         H = H - 360
  179.     END IF
  180.  
  181.     H = H / 60
  182.     H = H - (2 * INT(((iSextant + 1) MOD 6) / 2))
  183.  
  184.     IF iSextant MOD 2 = 0 THEN
  185.         fmd = (H * (fmx - fmn)) + fmn
  186.     ELSE
  187.         fmd = fmn - (H * (fmx - fmn))
  188.     END IF
  189.  
  190.     imx = _ROUND(fmx * 255)
  191.     imd = _ROUND(fmd * 255)
  192.     imn = _ROUND(fmn * 255)
  193.  
  194.     SELECT CASE INT(iSextant)
  195.         CASE 1
  196.             hsb~& = _RGBA32(imd, imx, imn, A)
  197.         CASE 2
  198.             hsb~& = _RGBA32(imn, imx, imd, A)
  199.         CASE 3
  200.             hsb~& = _RGBA32(imn, imd, imx, A)
  201.         CASE 4
  202.             hsb~& = _RGBA32(imd, imn, imx, A)
  203.         CASE 5
  204.             hsb~& = _RGBA32(imx, imn, imd, A)
  205.         CASE ELSE
  206.             hsb~& = _RGBA32(imx, imd, imn, A)
  207.     END SELECT
  208.  
  209.  
  210.  

EDIT : Code has been clean up.
« Last Edit: June 14, 2018, 04:52:38 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: Cube Plasma
« Reply #1 on: June 12, 2018, 10:32:27 am »
Wow! nice one Ashish!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Cube Plasma
« Reply #2 on: June 12, 2018, 10:44:43 am »
Very nice work, Ashish!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Cube Plasma
« Reply #3 on: June 12, 2018, 11:38:49 am »
Thank you, Bplus and Petr! :)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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

FellippeHeitor

  • Guest
Re: Cube Plasma
« Reply #4 on: June 12, 2018, 11:50:15 am »
Wow, dynamic texture! This is gorgeous, Ashish.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Cube Plasma
« Reply #5 on: June 12, 2018, 11:43:41 pm »
Very neat, it took me a while to notice the texture's animated and not an illusion due to the rotation.  A 3D plasma, instead of a 2D plasma on a surface, would be quite neat.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Cube Plasma
« Reply #6 on: June 13, 2018, 02:40:00 am »
Wow, dynamic texture! This is gorgeous, Ashish.
Thanks Fellippe! :D

Very neat, it took me a while to notice the texture's animated and not an illusion due to the rotation.  A 3D plasma, instead of a 2D plasma on a surface, would be quite neat.
Thanks Vince! A 3D plasma ? Interesting... never heard about it before... :)
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: Cube Plasma
« Reply #7 on: June 13, 2018, 09:24:47 am »
That is an interesting idea! I have to another look at how plasma effect works (in 2D).

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Cube Plasma
« Reply #8 on: June 13, 2018, 09:28:07 am »
 I just had to try do modification with video ...

Code: QB64: [Select]
  1. 'Cube plasma, coded by Ashish  6/12/2018
  2. 'Twitter : @KingOfCoders
  3. 'http://lodev.org/cgtutor/plasma.html
  4.  
  5.     'for camera
  6.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  7.  
  8.     SUB initQuadric ()
  9.     SUB drawCylinder ()
  10.     SUB drawDisk ()
  11.     SUB drawSphere ()
  12.     SUB drawCone ()
  13.     SUB drawPartialDisk (BYVAL a AS DOUBLE, BYVAL b AS DOUBLE)
  14.  
  15.  
  16.  
  17. _TITLE "Reply for Ashish"
  18. SCREEN _NEWIMAGE(600, 600, 32)
  19.  
  20. DIM SHARED glAllow AS _BYTE, textureImage&(11), tmp_buffer_image&
  21.  
  22. tmp_buffer_image& = _NEWIMAGE(200, 200, 32)
  23. horse& = _LOADIMAGE("horseU.jpg", 32)
  24.  
  25. TYPE FRAME ' generate to memory correct coordinates (no images!) to play video from source file
  26.     Source AS LONG
  27.     X_Start AS INTEGER
  28.     Y_Start AS INTEGER
  29.     X_End AS INTEGER
  30.     Y_End AS INTEGER
  31.     Index AS _UNSIGNED _BYTE
  32.  
  33. REDIM SHARED OhenVideo(0) AS FRAME
  34. REDIM SHARED Horse(0) AS FRAME
  35.  
  36. VideoLoad Horse(), horse&, 4, 3, 146, 95, 0, 0
  37.  
  38.     f = f + 1
  39.     _DEST tmp_buffer_image&
  40.     _SOURCE tmp_buffer_image&
  41.     CLS
  42.     _DEST tmp_buffer_image&
  43.     IF f < UBOUND(textureImage&) - 1 / 2 THEN VideoPlay OhenVideo(), 100, 100, 1 ELSE VideoPlay Horse(), 100, 100, 1
  44.     textureImage&(f) = _COPYIMAGE(tmp_buffer_image&) 'sem vlozit
  45. LOOP UNTIL f > UBOUND(textureImage&) - 1
  46. n:
  47.  
  48. _DEST tmp_buffer_image&
  49. CLS , _RGB32(22, 22, 249)
  50.  
  51. FOR f = 0 TO 11
  52.     CLS , _RGB32(22, 22, 249)
  53.     VideoPlay Horse(), -5, 25, 1.5 'I have only redirected function output to your virtual screen and it works great!
  54.     _DISPLAY
  55.     textureImage&(f) = _COPYIMAGE(tmp_buffer_image&)
  56.  
  57. _FREEIMAGE tmp_buffer_image&
  58.  
  59. glAllow = -1
  60.     _LIMIT 30 '?
  61.  
  62. SUB VideoLoad (Array() AS FRAME, Source AS LONG, FramesX AS INTEGER, FramesY AS INTEGER, ResFrameX AS INTEGER, ResFrameY AS INTEGER, CorrX AS _BYTE, CorrY AS _BYTE)
  63.     FOR T = 0 TO FramesX * FramesY
  64.         REDIM _PRESERVE Array(T) AS FRAME
  65.         Array(T).Source = Source&
  66.         Array(T).X_Start = X
  67.         Array(T).Y_Start = Y
  68.         Array(T).X_End = X + ResFrameX - CorrX
  69.         Array(T).Y_End = Y + ResFrameY - CorrY
  70.         X = X + ResFrameX + CorrX: IF X > _WIDTH(Source&) THEN X = 0: Y = Y + ResFrameY + CorrY
  71.     NEXT T
  72.  
  73. SUB VideoPlay (array() AS FRAME, X AS INTEGER, Y AS INTEGER, Zoom AS SINGLE)
  74.     IF array(0).Index + 1 > UBOUND(array) THEN array(0).Index = 0 ELSE array(0).Index = array(0).Index + 1
  75.     Frame = array(0).Index
  76.     PosXs = array(Frame).X_Start
  77.     PosXe = array(Frame).X_End
  78.     PosYs = array(Frame).Y_Start
  79.     PosYe = array(Frame).Y_End
  80.     ResX = PosXe - PosXs
  81.     ResY = PosYe - PosYs
  82.     S& = array(Frame).Source&
  83.     _PUTIMAGE (X, Y)-(Zoom * (X + ResX), Zoom * (Y + ResY)), S&, tmp_buffer_image&, (PosXs, PosYs)-(PosXe, PosYe)
  84.  
  85.  
  86. SUB _GL ()
  87.     STATIC cubeTexture&(11), glSetup, aspect#, frame
  88.  
  89.     IF NOT glAllow THEN EXIT SUB
  90.  
  91.     IF NOT glSetup THEN
  92.         glSetup = -1
  93.         _glViewport 0, 0, _WIDTH, _HEIGHT
  94.         'Convert all images to GL textures
  95.         FOR i = 1 TO UBOUND(textureImage&) - 1
  96.             _glGenTextures 1, _OFFSET(cubeTexture&(i))
  97.             DIM m AS _MEM
  98.             m = _MEMIMAGE(textureImage&(i))
  99.  
  100.             _glBindTexture _GL_TEXTURE_2D, cubeTexture&(i)
  101.             _glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGB, _WIDTH(textureImage&(i)), _HEIGHT(textureImage&(i)), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
  102.             _MEMFREE m
  103.             _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  104.             _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST
  105.             _FREEIMAGE textureImage&(i)
  106.         NEXT
  107.         aspect# = _WIDTH / _HEIGHT
  108.         initQuadric
  109.     END IF
  110.  
  111.     _glEnable _GL_TEXTURE_2D
  112.     _glEnable _GL_DEPTH_TEST
  113.  
  114.     _glMatrixMode _GL_PROJECTION
  115.     _gluPerspective 45.0, aspect#, 1, 100
  116.  
  117.     _glMatrixMode _GL_MODELVIEW
  118.  
  119.     gluLookAt 0, 0, 5, 0, 0, 0, 0, 1, 0
  120.     i = (frame MOD (UBOUND(textureImage&) - 1)) + 1
  121.     'select our texture
  122.     _LIMIT 25
  123.     _glBindTexture _GL_TEXTURE_2D, cubeTexture&(i)
  124.     'rotation
  125.     _glRotatef frame * .01 * 120, 1.1, .5, .3
  126.     drawDisk
  127.     frame = frame + 1
  128.  

and works perfectly!
* modifi.zip (Filesize: 609.7 KB, Downloads: 320)

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Cube Plasma
« Reply #9 on: June 13, 2018, 10:05:52 am »
+Petr
Amazing! Dynamic texturing is really cool thing to play with. :D
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: Cube Plasma
« Reply #10 on: June 13, 2018, 10:06:56 am »
Hi Petr,

Is the library "help" commonly found somewhere?
missing library.PNG
* missing library.PNG (Filesize: 18.84 KB, Dimensions: 450x689, Views: 386)

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Cube Plasma
« Reply #11 on: June 13, 2018, 10:09:07 am »
Hi Bplus, is in ZIP file. Copy it to your QB64 dir.

FellippeHeitor

  • Guest
Re: Cube Plasma
« Reply #12 on: June 13, 2018, 10:58:18 am »
Yeah, it was in the zip when I downloaded it too.

BTW, Petr, you can keep libraries in the same folder as your .bas file if you add them with a relative path:

DECLARE LIBRARY "./help"

Otherwise QB64 always looks for them in its own root folder.
« Last Edit: June 13, 2018, 11:00:39 am by FellippeHeitor »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cube Plasma
« Reply #13 on: June 13, 2018, 11:11:26 am »
Ah that helps! Thanks Fellippe

I was not expecting an help.h file. I saw the Horse.bas and wondered if I got a mod of Cube Plasma.

No, but with line change recommended by Fellippe I did get the code working:
That helps!.PNG
* That helps!.PNG (Filesize: 177.05 KB, Dimensions: 943x606, Views: 406)
« Last Edit: June 13, 2018, 11:13:44 am by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Cube Plasma
« Reply #14 on: June 13, 2018, 11:17:38 am »
Fellippe, thank you for the information. Bplus, sorry that the program is out of the Plasma theme. I'm returning to collisions.