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

0 Members and 1 Guest are viewing this topic.

#### Qwerkey

• Forum Resident
• Posts: 755
##### 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.
32.
33. glAllow = -1
34.     FOR i = 0 TO UBOUND(matrixRain)
35.         matrixRain(i).y = matrixRain(i).y - matrixRain(i).ay
36.         IF RND > 0.9 THEN
37.             d\$ = ""
38.             FOR k = 1 TO LEN(matrixRain(i).strData)
39.                 d\$ = d\$ + CHR\$(48 + p5random(0, 74)) 'change the character of rain randomly by a chance of 10%
40.             matrixRain(i).strData = d\$
41.         matrixRain(i).z = matrixRain(i).z + 0.00566 'move into the rain
42.         IF matrixRain(i).z > 0.1 THEN 'when behind screen
43.             matrixRain(i).x = p5random(-2, 2)
44.             matrixRain(i).y = p5random(2, 3.7)
45.             matrixRain(i).z = map((i / UBOUND(matrixRain)), 0, 1, -8, -0.2)
46.             matrixRain(i).ay = p5random(0.006, 0.02)
47.     _LIMIT 60
48.
49. SUB _GL ()
50.     STATIC glInit
51.     mov = mov + 0.01
52.     IF NOT glAllow THEN EXIT SUB
53.
54.     IF glInit = 0 THEN
55.         glInit = 1
56.
57.         FOR i = 0 TO UBOUND(matrixRainTex) 'create texture for each ascii character
58.             _glGenTextures 1, _OFFSET(matrixRainTex(i))
59.
60.             m = _MEMIMAGE(charImg(i))
61.
62.             _glBindTexture _GL_TEXTURE_2D, matrixRainTex(i)
63.             _glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGBA, _WIDTH(charImg(i)), _HEIGHT(charImg(i)), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
64.
65.
66.             _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_NEAREST
67.             _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST
68.             _FREEIMAGE charImg(i)
69.
70.         FOR i = 0 TO UBOUND(matrixRain) 'initialization
71.             n = p5random(1, 15)
72.             FOR j = 1 TO n
73.                 v\$ = CHR\$(p5random(48, 122))
74.                 matrixRain(i).strData = matrixRain(i).strData + v\$
75.             matrixRain(i).x = p5random(-2, 2)
76.             matrixRain(i).y = p5random(2, 3.7)
77.             matrixRain(i).z = map((i / UBOUND(matrixRain)), 0, 1, -8, -0.2)
78.             matrixRain(i).ay = p5random(0.006, 0.02)
79.
80.
81.     _glEnable _GL_BLEND 'enabling necessary stuff
82.     _glEnable _GL_DEPTH_TEST
83.     _glEnable _GL_TEXTURE_2D
84.
85.
86.     _glClearColor 0, 0, 0, 1
87.     _glClear _GL_COLOR_BUFFER_BIT OR _GL_DEPTH_BUFFER_BIT
88.
89.
90.     _glMatrixMode _GL_PROJECTION
91.     _gluPerspective 60, _WIDTH / _HEIGHT, 0.01, 10.0
92.
93.     _glRotatef SIN(mov) * 20, 1, 0, 0 'rotating x-axis a bit, just to get Depth effect.
94.
95.
96.     _glMatrixMode _GL_MODELVIEW
97.
98.     'rendering the rain
99.     FOR i = 0 TO UBOUND(matrixRain)
100.         n = LEN(matrixRain(i).strData)
101.         FOR j = 1 TO n
102.             ca\$ = MID\$(matrixRain(i).strData, j, 1)
103.             'selecting texture on the basis of ascii code.
104.             _glBindTexture _GL_TEXTURE_2D, matrixRainTex(ASC(ca\$) - 48)
106.             _glVertex3f matrixRain(i).x - matRainWidth, matrixRain(i).y - matRainHeight + 2 * (j - 1) * matRainHeight, matrixRain(i).z
107.             _glVertex3f matrixRain(i).x - matRainWidth, matrixRain(i).y + matRainHeight + 2 * (j - 1) * matRainHeight, matrixRain(i).z
108.             _glVertex3f matrixRain(i).x + matRainWidth, matrixRain(i).y + matRainHeight + 2 * (j - 1) * matRainHeight, matrixRain(i).z
109.             _glVertex3f matrixRain(i).x + matRainWidth, matrixRain(i).y - matRainHeight + 2 * (j - 1) * matRainHeight, matrixRain(i).z
110.
111.
112. 'taken from p5js.bas
113. 'https://bit.ly/p5jsbas
114. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
115.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
116.
117.
118. FUNCTION p5random! (mn!, mx!)
119.     IF mn! > mx! THEN
120.         SWAP mn!, mx!
121.     p5random! = RND * (mx! - mn!) + mn!
122.

« Last Edit: March 27, 2020, 05:54:52 am by Qwerkey »