Author Topic: Fire Effect!  (Read 4804 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Fire Effect!
« on: May 22, 2018, 09:33:00 am »
Hi everyone! :)
Just after coding water ripple effect, I started studing about simulating fire and this page helps me lot - http://lodev.org/cgtutor/fire.html
This simulation looks kinda real effect!
Just check the code below. :)

Code: QB64: [Select]
  1. 'Simple Fire Effect coded By Ashish. Just for Practice :D
  2. 'http://lodev.org/cgtutor/fire.html
  3. _TITLE "Fire Effect"
  4. SCREEN _NEWIMAGE(300, 300, 32)
  5.  
  6. DIM fireBuffer(_WIDTH + 2, _HEIGHT), colorPal(256) AS _UNSIGNED LONG
  7.  
  8. 'creating our palette for fire
  9. FOR i = 255 TO 0 STEP -1
  10.     IF i <= 255 AND i >= 200 THEN 'white to yellow
  11.         colorPal(i) = _RGB(map(i, 255, 200, 255, 255), map(i, 255, 200, 255, 242), map(i, 255, 200, 255, 0))
  12.     ELSEIF i <= 201 AND i >= 90 THEN 'yellow to orange
  13.         colorPal(i) = _RGB(map(i, 201, 90, 255, 221), map(i, 201, 90, 242, 140), map(i, 201, 90, 0, 0))
  14.     ELSEIF i <= 89 AND i >= 79 THEN 'orange to dark orange red
  15.         colorPal(i) = _RGB(map(i, 89, 79, 221, 183), map(i, 89, 79, 140, 45), map(i, 89, 79, 0, 0))
  16.     ELSEIF i <= 78 AND i >= 48 THEN 'dark orange red to dark grey
  17.         colorPal(i) = _RGB(map(i, 78, 48, 183, 10), map(i, 78, 48, 45, 10), map(i, 78, 48, 0, 10))
  18.     ELSEIF i <= 47 AND i >= 27 THEN 'dark grey to light grey
  19.         colorPal(i) = _RGBA(map(i, 47, 27, 10, 40), map(i, 47, 27, 10, 40), map(i, 47, 27, 10, 40), map(i, 47, 27, 255, 100))
  20.     ELSEIF i <= 26 AND i >= 0 THEN 'light grey to black
  21.         colorPal(i) = _RGBA(map(i, 26, 0, 40, 0), map(i, 26, 0, 40, 0), map(i, 26, 0, 40, 0), map(i, 26, 0, 100, 0))
  22.     END IF
  23. 'creating an image,  placing  a text, and storing the white pixel in an array
  24. tmp& = _NEWIMAGE(94, 20, 32)
  25. _DEST tmp&
  26. _PRINTSTRING (5, 0), "QB64 Rocks!"
  27. _PUTIMAGE (0, 100)-(282, 160), tmp&
  28. FOR i = 100 TO 160
  29.     FOR j = 0 TO 282
  30.         IF POINT(j, i) = _RGB(255, 255, 255) THEN n = n + 1
  31. NEXT j, i
  32. DIM textPixelPosX(n), textPixelPosY(n)
  33. n = 1
  34. FOR i = 100 TO 160
  35.     FOR j = 0 TO 282
  36.         IF POINT(j, i) = _RGB(255, 255, 255) THEN textPixelPosX(n) = j: textPixelPosY(n) = i: n = n + 1
  37. NEXT j, i
  38.  
  39. t# = TIMER
  40.     IF TIMER - t# > 1.8 THEN
  41.         xOff = p5random(-100, 100)
  42.         yOff = p5random(-70, 70)
  43.         'using stored pixel array to display fire text
  44.         FOR i = 0 TO n - 1
  45.             xx = textPixelPosX(i) + xOff
  46.             yy = textPixelPosY(i) + yOff
  47.             IF xx > 1 AND xx < _WIDTH - 1 AND yy > 1 AND yy < _HEIGHT - 1 THEN fireBuffer(xx, yy) = p5random(100, 255)
  48.         NEXT
  49.         t# = TIMER
  50.     END IF
  51.     CLS
  52.     FOR x = 0 TO _WIDTH
  53.         fireBuffer(x, _HEIGHT - 1) = p5random(10, 255)
  54.     NEXT
  55.     FOR y = 0 TO _HEIGHT - 2
  56.         FOR x = 1 TO _WIDTH - 1
  57.             fireBuffer(x, y) = (fireBuffer(x - 1, y + 1) + fireBuffer(x, y + 1) + fireBuffer(x + 1, y + 1) + fireBuffer(x, y + 2)) * .2441
  58.     NEXT x, y
  59.  
  60.     FOR x = 0 TO _WIDTH - 1
  61.         FOR y = 0 TO _HEIGHT - 1
  62.             PSET (x, y), colorPal(INT(fireBuffer(x, y)))
  63.     NEXT y, x
  64.     _DISPLAY
  65.     _LIMIT 60
  66.  
  67. 'taken from p5js.bas
  68. 'https://bit.ly/p5jsbas
  69. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  70.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  71.  
  72. FUNCTION p5random! (mn!, mx!)
  73.     IF mn! > mx! THEN
  74.         SWAP mn!, mx!
  75.     END IF
  76.     p5random! = RND * (mx! - mn!) + mn!
  77.  
  78.  
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: Fire Effect!
« Reply #1 on: May 22, 2018, 09:39:21 am »
Looks awesome, Ashish, great job

Coding Train for the win! :-)

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Fire Effect!
« Reply #2 on: May 22, 2018, 09:43:15 am »
Looks awesome, Ashish, great job

Coding Train for the win! :-)
Thanks! :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: Fire Effect!
« Reply #3 on: May 22, 2018, 02:50:17 pm »
Yeah another discoverer of fire!