Author Topic: Moving into the Matrix Rain by Ashish  (Read 7533 times)

0 Members and 1 Guest are viewing this topic.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Moving into the Matrix Rain by Ashish
« on: March 07, 2020, 10:11:51 am »
Author: @Ashish
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=1453
Version: 1
Tags: [3D], [Graphics]

Description:
I was just inspired by Bplus's Matrix rain on this topic - https://www.qb64.org/forum/index.php?topic=1152.msg106362
So, I decided to create a similar matrix rain having depth effect.


Source Code:
Code: QB64: [Select]
  1. 'Moving into the Matrix Rain
  2. 'By Ashish Kushwaha
  3. '23 Jun, 2019
  4. '
  5. 'Inspire by B+ Matrix Rain.
  6. _TITLE "Moving into the Matrix Rain"
  7.  
  8.  
  9. SCREEN _NEWIMAGE(800, 600, 32)
  10.  
  11. TYPE matRain
  12.     x AS SINGLE 'x location
  13.     y AS SINGLE 'y location
  14.     z AS SINGLE 'z location
  15.     ay AS SINGLE 'rain velocity
  16.     strData AS STRING 'string data of each matrix rain
  17.  
  18. DIM SHARED charImg(74) AS LONG, matRainWidth, matRainHeight 'ascii char from 48 to 122, i.e, total 75 type of chars
  19. DIM SHARED glAllow AS _BYTE, matrixRain(700) AS matRain, matrixRainTex(74) AS LONG, mov
  20. matRainWidth = _FONTWIDTH * 0.005
  21. matRainHeight = _FONTHEIGHT * 0.005
  22. CLS , _RGB32(255)
  23. FOR i = 0 TO 74
  24.     charImg(i) = _NEWIMAGE(_FONTWIDTH * 5, _FONTHEIGHT * 5, 32)
  25.     _DEST tmp&
  26.     CLS , _RGBA(0, 0, 0, 0)
  27.     COLOR _RGB32(0, 255, 0), 1
  28.     _PRINTSTRING (0, 0), CHR$(i + 48)
  29.     _DEST charImg(i)
  30.     _PUTIMAGE , tmp&
  31.     _DEST 0
  32.  
  33.  
  34. glAllow = -1
  35.     FOR i = 0 TO UBOUND(matrixRain)
  36.         matrixRain(i).y = matrixRain(i).y - matrixRain(i).ay
  37.         IF RND > 0.9 THEN
  38.             d$ = ""
  39.             FOR k = 1 TO LEN(matrixRain(i).strData)
  40.                 d$ = d$ + CHR$(48 + p5random(0, 74)) 'change the character of rain randomly by a chance of 10%
  41.             NEXT
  42.             matrixRain(i).strData = d$
  43.         END IF
  44.         matrixRain(i).z = matrixRain(i).z + 0.00566 'move into the rain
  45.         IF matrixRain(i).z > 0.1 THEN 'when behind screen
  46.             matrixRain(i).x = p5random(-2, 2)
  47.             matrixRain(i).y = p5random(2, 3.7)
  48.             matrixRain(i).z = map((i / UBOUND(matrixRain)), 0, 1, -8, -0.2)
  49.             matrixRain(i).ay = p5random(0.006, 0.02)
  50.         END IF
  51.     NEXT
  52.     _LIMIT 60
  53.  
  54. SUB _GL ()
  55.     STATIC glInit
  56.     mov = mov + 0.01
  57.     IF NOT glAllow THEN EXIT SUB
  58.  
  59.     IF glInit = 0 THEN
  60.         glInit = 1
  61.  
  62.         FOR i = 0 TO UBOUND(matrixRainTex) 'create texture for each ascii character
  63.             _glGenTextures 1, _OFFSET(matrixRainTex(i))
  64.  
  65.             DIM m AS _MEM
  66.             m = _MEMIMAGE(charImg(i))
  67.  
  68.             _glBindTexture _GL_TEXTURE_2D, matrixRainTex(i)
  69.             _glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGBA, _WIDTH(charImg(i)), _HEIGHT(charImg(i)), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
  70.  
  71.             _MEMFREE m
  72.  
  73.             _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_NEAREST
  74.             _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST
  75.             _FREEIMAGE charImg(i)
  76.         NEXT
  77.  
  78.         FOR i = 0 TO UBOUND(matrixRain) 'initialization
  79.             n = p5random(1, 15)
  80.             FOR j = 1 TO n
  81.                 v$ = CHR$(p5random(48, 122))
  82.                 matrixRain(i).strData = matrixRain(i).strData + v$
  83.             NEXT
  84.             matrixRain(i).x = p5random(-2, 2)
  85.             matrixRain(i).y = p5random(2, 3.7)
  86.             matrixRain(i).z = map((i / UBOUND(matrixRain)), 0, 1, -8, -0.2)
  87.             matrixRain(i).ay = p5random(0.006, 0.02)
  88.         NEXT
  89.  
  90.         _glViewport 0, 0, _WIDTH, _HEIGHT
  91.     END IF
  92.  
  93.     _glEnable _GL_BLEND 'enabling necessary stuff
  94.     _glEnable _GL_DEPTH_TEST
  95.     _glEnable _GL_TEXTURE_2D
  96.  
  97.  
  98.     _glClearColor 0, 0, 0, 1
  99.     _glClear _GL_COLOR_BUFFER_BIT OR _GL_DEPTH_BUFFER_BIT
  100.  
  101.  
  102.     _glMatrixMode _GL_PROJECTION
  103.     _gluPerspective 60, _WIDTH / _HEIGHT, 0.01, 10.0
  104.  
  105.     _glRotatef SIN(mov) * 20, 1, 0, 0 'rotating x-axis a bit, just to get Depth effect.
  106.  
  107.  
  108.     _glMatrixMode _GL_MODELVIEW
  109.  
  110.     'rendering the rain
  111.     FOR i = 0 TO UBOUND(matrixRain)
  112.         n = LEN(matrixRain(i).strData)
  113.         FOR j = 1 TO n
  114.             ca$ = MID$(matrixRain(i).strData, j, 1)
  115.             'selecting texture on the basis of ascii code.
  116.             _glBindTexture _GL_TEXTURE_2D, matrixRainTex(ASC(ca$) - 48)
  117.             _glBegin _GL_QUADS
  118.             _glTexCoord2f 0, 1
  119.             _glVertex3f matrixRain(i).x - matRainWidth, matrixRain(i).y - matRainHeight + 2 * (j - 1) * matRainHeight, matrixRain(i).z
  120.             _glTexCoord2f 0, 0
  121.             _glVertex3f matrixRain(i).x - matRainWidth, matrixRain(i).y + matRainHeight + 2 * (j - 1) * matRainHeight, matrixRain(i).z
  122.             _glTexCoord2f 1, 0
  123.             _glVertex3f matrixRain(i).x + matRainWidth, matrixRain(i).y + matRainHeight + 2 * (j - 1) * matRainHeight, matrixRain(i).z
  124.             _glTexCoord2f 1, 1
  125.             _glVertex3f matrixRain(i).x + matRainWidth, matrixRain(i).y - matRainHeight + 2 * (j - 1) * matRainHeight, matrixRain(i).z
  126.             _glEnd
  127.         NEXT
  128.     NEXT
  129.  
  130.     _glFlush
  131.  
  132. 'taken from p5js.bas
  133. 'https://bit.ly/p5jsbas
  134. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  135.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  136.  
  137.  
  138. FUNCTION p5random! (mn!, mx!)
  139.     IF mn! > mx! THEN
  140.         SWAP mn!, mx!
  141.     END IF
  142.     p5random! = RND * (mx! - mn!) + mn!
  143.  

 
Moving into the Matrix Rain Screenshot.jpg
« Last Edit: March 27, 2020, 05:54:52 am by Qwerkey »