Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw snowflakes with Python OpenGL's point sprite technology

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to draw snowflakes with Python OpenGL's point sprite technology". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Opening and using point sprites in OpenGL is a little complicated, but WxGL encapsulates it and is very easy to use. Before giving the demo code, post two texture pictures of snowflakes.

Snow_1.png

Snow_2.png

Students who are familiar with the GLSL language can easily read the shader source code. After loading the shader source code and texture image into the model, you only need to show, and the snowflake will be displayed. If you want to achieve the effect of snowflakes, please refer to my other blog post "directing a fireworks event with OpenGL to greet the coming New year".

Import numpy as npimport wxglfrom wxgl import wxplot as pltvshader_src = "" # version 330 core in vec4 Awareness Positioning; uniform mat4 upright MVPMatrix; void main () {gl_Position = u_MVPMatrix * afigurative Positioning; gl_PointSize = (a_Position.z + 1) * 30;} "fshader_src =" # version 330 core uniform sampler2D utilisation Snow1; in float idx Void main () {gl_FragColor = texture2D (u_Snow_1, gl_PointCoord) } "" m = wxgl.Model (wxgl.POINTS, vshader_src, fshader_src, sprite=True) # using sprite=Treue to open the point wizard m.set_vertex ('axiom Positioning, np.random.random ((300,3)) * 2-1) # randomly generate 300 points m.add_texture (' uplink Snowdays 1, 'res/image/snow_1.png') Wxgl.TEXTURE_2D) # add snowflake texture m.set_mvp_matrix ('upright MVPMatrix') # set model matrix, viewpoint matrix and projection matrix plt.model (m) plt.show ()

Here is the effect of texturing with snow_1.png.

Here is the effect of texturing with snow_2.png.

However, such snowflakes are a little monotonous. After all, there are no two identical snowflakes in the world. How to make snowflakes look more realistic? The following code attempts to mix two textures in a slice element shader.

Import numpy as npimport wxglfrom wxgl import wxplot as pltvshader_src = "" # version 330 core in vec4 Awareness Positioning; uniform mat4 utilising MVPMatrix; void main () {gl_Position = u_MVPMatrix * afigurative Positioning; gl_PointSize = (a_Position.z + 1) * 30;} "fshader_src =" # version 330 core uniform sampler2D utilising Snowdays 1; uniform sampler2D utilising Snowdays 2; in float idx Void main () {if (fract (sin (dot (gl_PointCoord, vec2 (12.9898) 78.233)) * 43758.5453) {gl_FragColor = texture2D (u_Snow_1, gl_PointCoord);} else {gl_FragColor = texture2D (u_Snow_2, gl_PointCoord) } "m = wxgl.Model (wxgl.POINTS, vshader_src, fshader_src, sprite=True) # Open Point Wizard m.set_vertex ('axioms Positioning, np.random.random ((300,3)) * 2-1) # randomly generate 300 points m.add_texture (' utensils Snowdays 1, 'res/image/snow_1.png') via sprite=Treue Wxgl.TEXTURE_2D) # add snowflake texture 1m.add_texture ('upright Snowflake 2 texture,' res/image/snow_2.png', wxgl.TEXTURE_2D) # add snowflake texture 2m.set_mvp_matrix ('uplink MVPMatrix') # set model matrix, viewpoint matrix and projection matrix plt.model (m) plt.show ()

Isn't this snowflake a little unique?

This is the end of the content of "how to draw snowflakes with Python OpenGL's point sprite technology". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report