In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to draw a fireworks event with Python OpenGL". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to draw a fireworks event with Python OpenGL" can help you solve your doubts.
1. Install WxGL
WxGL is a three-dimensional data visualization library based on PyOpenGL, which uses wx as the display back end and provides Matplotlib-style interactive application. WxGL can also be seamlessly integrated with wxPython to draw 3D models on wx forms. Using the pip command, you can quickly install WxGL and other modules that it depends on.
Pip install wxgl2. Quick experienc
The following lines of code draw a solid-color sphere with a radius of 1 at the origin of the coordinate. Ignoring the module name, the code is exactly the same as the style of Matplotlib.
> import wxgl.wxplot as plt > plt.uvsphere ((0memo), 1, color='cyan') > plt.title ('Quick experience: $x ^ 2 + y ^ 2 = 1 $') > plt.show ()
It's so easy to generate a model of the earth.
> plt.uvsphere ((0pj0pc0), 1, texture='res/earth.jpg', xflip=True, yflip=False) > plt.show ()
It is even easier to make the earth rotate.
> plt.uvsphere, 1, texture='res/earth.jpg', xflip=True, yflip=False, transform = lambda tn,gms,tms: ((0,1,0, (0.01*tms)% 360),) > plt.show ()
Check "screen recording" and click the "play" button to save as a gif file or a video file in mp4/avi format.
This is the earth texture image used in the code, which can be downloaded directly.
3. Write your own shader
WxGL not only provides a series of drawing functions such as line segments, scatters, surfaces, 3D isosurfaces, but also supports user-customized shader programs to achieve more complex functions. In the following example, the process of fireworks lift off is simulated by particle technology.
#-coding: utf-8-*-import numpy as npimport wxglimport wxgl.wxplot as pltdef rise (n, pos, h, v, a Cycle): "" fireworks lift-off model n-number of particles pos-initial position h-ascending stroke v-initial velocity a-ascending acceleration cycle-cycle "" vshader_src = "" # version 330 core in vec4 a_Position In vec4 averse Colour; in float averse Delay; / / Particle emission delay time (s) uniform float uplink ts; / / duration (s) uniform float upright V; / / initial velocity uniform float upright A; / / rising acceleration uniform mat4 upright MVPMatrix; out vec4 vested Color; out float v_Ts Void main () {float t = u_Ts-await Delay; if (t
< 0) t = 0; float s = u_V * t + 0.5 * u_A * t * t; gl_Position = u_MVPMatrix * vec4(a_Position.x, a_Position.y+s, a_Position.z, a_Position.w); gl_PointSize = 1; v_Color = a_Color; v_Ts = u_Ts; } """ fshader_src = """ #version 330 core in vec4 v_Color; uniform float u_Tmax; in float v_Ts; void main() { if(v_Ts >U_Tmax) discard; vec2 temp = gl_PointCoord-vec2 (0.5); float f = dot (temp, temp); if (f > 0.25) discard; gl_FragColor = vec4 (v_Color.rgb, 1) } "vs = np.array (pos) + (np.random.random ((nmae3))-0.5) * hshock 100 color = np.tile (np.array ((1.0) (1.0)), (nMag1)) delay = np.float32 (np.absolute (np.random.randn (n) / 10 tmax = (pow (v*v+2*a*h)) 0. 5)-v) / a + delay.max () m = wxgl.Model (wxgl.POINTS, vshader_src, fshader_src, sprite=True) m.set_vertex ('Austin Positioning, vs) m.set_color (' ABA Collections, color) m.set_argument ('ABA Delayments, delay) m.set_argument (' upright Tslots, lambda tn,gms,tms: (tms/1000)% cycle) m.set_argument ('upright V') V) m.set_argument ('upright Achievement, a) m.set_argument (' upright Tmax, tmax) m.set_mvp_matrix ('upright MVPMatrix') # set the model matrix, viewpoint matrix and projection matrix return mvs = MVPMatrix. set up the model matrix, viewpoint matrix and projection matrix return mvs = MVPMatrix. set up the model matrix, viewpoint matrix and projection matrix. -1], [- 1.5 beacon 2, pos= 1], [1.5 vs = vs]) vs = vs [[1, 5, 1, 2, 2, 2, 5, 4, 4, 5, 7, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 7, 4] m = rise (nasty 500, pos=, cycle=5) plt.figure (zoom=0.7, elev=10) plt.line (vs, color=, method='isolate') # hexahedron wireframe Represents the space plt.model (m) plt.show () 4. Blooming fireworks
As long as you understand the code for fireworks to lift off, it is easy to write a shader program for fireworks to explode in the air. In addition to the shader for fireworks lift-off, the following code also provides two shaders for fireworks explosion, in which a texture image is used, which can be used directly by downloading the image below.
If you replace the above texture image with text, you can display the text at the moment of the fireworks explosion. WxGL provides a function for converting text to PIL graphics objects, which can be used directly as a texture.
#-coding: utf-8-*-import numpy as npimport wxglimport wxgl.wxplot as pltdef rise (n, pos, h, v, a Cycle): "" fireworks lift-off model n-number of particles pos-initial position h-ascending stroke v-initial velocity a-ascending acceleration cycle-cycle "" vshader_src = "" # version 330 core in vec4 a_Position In vec4 averse Colour; in float averse Delay; / / Particle emission delay time (s) uniform float uplink ts; / / duration (s) uniform float upright V; / / initial velocity uniform float upright A; / / rising acceleration uniform mat4 upright MVPMatrix; out vec4 vested Color; out float v_Ts Void main () {float t = u_Ts-await Delay; if (t
< 0) t = 0; float s = u_V * t + 0.5 * u_A * t * t; gl_Position = u_MVPMatrix * vec4(a_Position.x, a_Position.y+s, a_Position.z, a_Position.w); gl_PointSize = 1; v_Color = a_Color; v_Ts = u_Ts; } """ fshader_src = """ #version 330 core in vec4 v_Color; uniform float u_Tmax; in float v_Ts; void main() { if(v_Ts >U_Tmax) discard; vec2 temp = gl_PointCoord-vec2 (0.5); float f = dot (temp, temp); if (f > 0.25) discard; gl_FragColor = vec4 (v_Color.rgb, 1) } "vs = np.array (pos) + (np.random.random ((nmae3))-0.5) * hshock 100 color = np.tile (np.array ((1.0) (1.0)), (nMag1)) delay = np.float32 (np.absolute (np.random.randn (n) / 10 tmax = (pow (v*v+2*a*h)) 0. 5)-v) / a + delay.max () m = wxgl.Model (wxgl.POINTS, vshader_src, fshader_src, sprite=True) m.set_vertex ('Austin Positioning, vs) m.set_color (' ABA Collections, color) m.set_argument ('ABA Delayments, delay) m.set_argument (' upright Tslots, lambda tn,gms,tms: (tms/1000)% cycle) m.set_argument ('upright V') V) m.set_argument ('upright pos, a) m.set_argument (' upright TmaxMatrix, tmax) m.set_mvp_matrix ('upright MVPMatrix') # set the model matrix, viewpoint matrix and projection matrix return m, tmaxdef bomb_1 (n, MVPMatrix, a Cycle): fireworks explosion model n-number of particles pos-position start-time a-drop acceleration cycle-cycle "" vshader_src = "# version 330 core in vec4 a_Position" In vec3 compressed data; uniform float upright ts; uniform float upright start; uniform float upright A; uniform mat4 upright MVPMatrix; out vec4 vested colors; out float vested Tests; void main () {float t = u_Ts-upright start; if (t
< 0) t = 0; float lat = radians((a_Data.x - 0.5) * 90); float lon = radians(a_Data.y * 360); float r = (a_Data.z * 0.3 + 0.7) * 0.3 * t * (1 + 0.3 * a_Position.z); float y = r * sin(lat) + a_Position.y - 0.5*u_A*t*t; float xz = r * cos(lat); float x = xz * cos(lon) + a_Position.x; float z = xz * sin(lon) + a_Position.z; gl_Position = u_MVPMatrix * vec4(x,y,z,a_Position.w); gl_PointSize = 3 * t; v_Ts = t; int i = gl_VertexID % 6; if (i == 0) v_Color = vec4(1,0,0,1); else if (i == 1) v_Color = vec4(0,1,0,1); else if (i == 2) v_Color = vec4(0,0,1,1); else if (i == 3) v_Color = vec4(1,1,0,1); else if (i == 4) v_Color = vec4(0,1,1,1); else v_Color = vec4(1,0,1,1); } """ fshader_src = """ #version 330 core in vec4 v_Color; in float v_Ts; void main() { if(v_Ts 2) discard; vec2 temp = gl_PointCoord - vec2(0.5); float f = dot(temp, temp); if(f >Discard; / / float alpha = v_Color.a * exp (1-30 Secretf) * (4-v_Ts*v_Ts) / 2; float alpha = v_Color.a * (1-4 folf) * (4-v_Ts*v_Ts) / 2; gl_FragColor = vec4 (v_Color.rgb, alpha) } "vs = np.tile (np.array (pos), (nMagol 1)) data = np.float32 (np.random.random ((nMagol 3) m = wxgl.Model (wxgl.POINTS, vshader_src, fshader_src, sprite=True, opacity=False) m.set_vertex ('ajar Positionality, vs) m.set_argument (' axiom Datagram, data) m.set_argument ('upright Start' Start) m.set_argument ('upright Aids, a) m.set_argument (' upright Tslots, lambda tn,gms,tms: (tms/1000)% cycle) m.set_mvp_matrix ('upright MVPMatrix') # set the model matrix, viewpoint matrix and projection matrix return mdef bomb_2 (pos, start, texture, a, size Cycle): "" fireworks explosion model pos-position start-time texture-texture a-drop acceleration cycle-cycle "" vshader_src = "" # version 330 core in vec4 a_Position Uniform float upright ts; uniform float upright Start; uniform float upright start; uniform float upright Size; uniform mat4 upright MVPMatrix; out float vested TS; void main () {float t = u_Ts-upright start; if (t < 0) t = 0 If (t < 2) gl_PointSize = t * u_Size/2 * (1 + 0.3 * a_Position.z); else gl_PointSize = u_Size * (1 + 0.3 * a_Position.z) Gl_Position = u_MVPMatrix * vec4 (a_Position.x, a_Position.y-0.5*u_A*t*t, a_Position.z, a_Position.w); v_Ts = t;} "" fshader_src = "" # version 330 core uniform sampler2D upright fireworks; in float v_Ts Void main () {if (v_Ts 2) discard; vec4 color = texture2D (u_Fireworks, gl_PointCoord); gl_FragColor = vec4 (color.rgb, color.a* (4-v_Ts*v_Ts) / 2) } "vs = np.array (pos). Reshape (- 1p3) m = wxgl.Model (wxgl.POINTS, vshader_src, fshader_src, sprite=True) m.set_vertex ('Austin Positioning, vs) m.set_argument (' upright Aids, a) m.set_argument ('upright SizeSize) m.set_argument (' upright Startweights, start) m.set_argument ('upright Ts'' Lambda tn,gms,tms: (tms/1000)% cycle) m.add_texture ('upright fireworks, texture, wxgl.TEXTURE_2D, yflip=False) m.set_mvp_matrix (' upright MVPMatrix') # set the model matrix, viewpoint matrix and projection matrix return mif _ _ name__ = ='_ _ main__': vs = np.array ([[- 1.5 main__': vs 2 np.array 1], [- 1.5 memo 0mem1], [1.5 MVPMatrix] Vs = vs [1, 5, 2, 5, 4, 4, 6, 7, 4, 5, 7, 4, 5, 7, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 7, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 7, 4, 5, 5, 5, 5, 7, 4, 4, 4, 4, 4, 5, 5, 7, 4, 4, 4, 4, 4, 5, 5, 5, 7, 4, 4, 4, 4, 4, 5, 5, 7, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, Represents the space for fireworks display #-h, v, a, cycle = 1.7,2.2,-1.2,4 for I, ch in enumerate ('Happy New year'): X =-1.5 + IM1, start = rise (nasty 300, pos= (xpen0jue 1), hindh, venerv, aplasa Cycle=cycle) m2 = bomb_1, start, axiom 0.1, cycle=cycle) m3 = bomb_2, start, wxgl.text2image (ch, 96, (1meme 0)), axiom 0.1, size=100 Cycle=cycle) plt.model (M1) plt.model (m2) plt.model (m3) #-for i in range (20): X, z = (np.random.random ()-0.5) * 4, (np.random.random ()-0.5) * 2 h, v A = 1.5 + (np.random.random ()-0.5) * 0.4,2.2,-1.2 cycle= np.random.randint (4,7) M1, start = rise (nasty 300, pos= (xmem0pime z), hagh, viciv, aura, cycle=cycle) m2 = bomb_1 (200,( xmeme hlagz), start, aplomb 0.1 Cycle=cycle) plt.model (M1) plt.model (m2) #-for i in range (20): X, z = (np.random.random ()-0.5) * 4, (np.random.random ()-0.5) * 2 h, v A = 1.5 + (np.random.random ()-0.5) * 0.4,2.3,-1.2 cycle= np.random.randint (4,7) M1, start = rise (nasty 300, pos= (xmem0pime z), hagh, viciv, ahuma, cycle=cycle) m2 = bomb_2 ((xmemhencez), start, 'res/fw.png', axie 0.1, size=300) Cycle=cycle) plt.model (M1) plt.model (m2) plt.show ()
The final effect is shown in gif below.
After reading this, the article "how to draw a fireworks event with Python OpenGL" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.