溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么用Python?OpenGL的point?sprite技術(shù)繪制雪花

發(fā)布時(shí)間:2022-02-07 14:56:30 來源:億速云 閱讀:173 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“怎么用Python OpenGL的point sprite技術(shù)繪制雪花”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

在OpenGL中開啟和使用點(diǎn)精靈有一點(diǎn)點(diǎn)復(fù)雜,好在WxGL對(duì)此做了封裝,用起來非常簡單。在給出演示代碼前,先貼兩張雪花的紋理圖片。

snow_1.png

怎么用Python?OpenGL的point?sprite技術(shù)繪制雪花

snow_2.png

怎么用Python?OpenGL的point?sprite技術(shù)繪制雪花

熟悉GLSL語言的同學(xué),很容易讀懂著色器源碼。將著色器源碼、紋理圖片裝進(jìn)模型之后,只需要show一下,雪花就顯示出來了。如果想實(shí)現(xiàn)雪花飄飄的效果,請(qǐng)參考我的另一篇博文《用OpenGL導(dǎo)演一場煙花盛會(huì),迎接即將到來的新年》。

import numpy as np
import wxgl
from wxgl import wxplot as plt

vshader_src = """
    #version 330 core
    in vec4 a_Position;
    uniform mat4 u_MVPMatrix;
    void main() { 
        gl_Position = u_MVPMatrix * a_Position; 
        gl_PointSize = (a_Position.z + 1) * 30;
    }
"""

fshader_src = """
    #version 330 core
    uniform sampler2D u_Snow_1;
    in float idx;
    void main() { 
        gl_FragColor = texture2D(u_Snow_1, gl_PointCoord); 
    } 
"""

m = wxgl.Model(wxgl.POINTS, vshader_src, fshader_src, sprite=True) # 通過sprite=Treue開啟點(diǎn)精靈
m.set_vertex('a_Position', np.random.random((300, 3))*2-1) # 隨機(jī)生成300個(gè)點(diǎn)
m.add_texture('u_Snow_1', 'res/image/snow_1.png', wxgl.TEXTURE_2D) # 添加雪花紋理
m.set_mvp_matrix('u_MVPMatrix') # 設(shè)置模型矩陣、視點(diǎn)矩陣和投影矩陣

plt.model(m)
plt.show()

下面是使用snow_1.png做紋理的效果。

怎么用Python?OpenGL的point?sprite技術(shù)繪制雪花

下面是使用snow_2.png做紋理的效果。

怎么用Python?OpenGL的point?sprite技術(shù)繪制雪花

不過,這樣的雪花略顯單調(diào),畢竟,世界上沒有兩片完全相同的雪花。怎樣讓雪花看起來更逼真一點(diǎn)呢?下面的代碼嘗試在片元著色器中混用兩種紋理。

import numpy as np
import wxgl
from wxgl import wxplot as plt

vshader_src = """
    #version 330 core
    in vec4 a_Position;
    uniform mat4 u_MVPMatrix;
    void main() { 
        gl_Position = u_MVPMatrix * a_Position; 
        gl_PointSize = (a_Position.z + 1) * 30;
    }
"""

fshader_src = """
    #version 330 core
    uniform sampler2D u_Snow_1;
    uniform sampler2D u_Snow_2;
    in float idx;
    void main() { 
        if (fract(sin(dot(gl_PointCoord ,vec2(12.9898,78.233))) * 43758.5453) < 0.5) {
            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) # 通過sprite=Treue開啟點(diǎn)精靈
m.set_vertex('a_Position', np.random.random((300, 3))*2-1) # 隨機(jī)生成300個(gè)點(diǎn)
m.add_texture('u_Snow_1', 'res/image/snow_1.png', wxgl.TEXTURE_2D) # 添加雪花紋理1
m.add_texture('u_Snow_2', 'res/image/snow_2.png', wxgl.TEXTURE_2D) # 添加雪花紋理2
m.set_mvp_matrix('u_MVPMatrix') # 設(shè)置模型矩陣、視點(diǎn)矩陣和投影矩陣

plt.model(m)
plt.show()

這個(gè)雪花有點(diǎn)獨(dú)特吧?

怎么用Python?OpenGL的point?sprite技術(shù)繪制雪花

“怎么用Python OpenGL的point sprite技術(shù)繪制雪花”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI