黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

Python之OpenGL筆記(18):正弦波的繪制

系統(tǒng) 1944 0

一、目的

1、正弦函數(shù)的基本畫法;
2、GLSL方式實(shí)現(xiàn)練習(xí)。

二、程序運(yùn)行結(jié)果

Python之OpenGL筆記(18):正弦波的繪制_第1張圖片

三、glDrawArrays 函數(shù)

??GLSL畫這些基本的類型使用的函數(shù)主要是glDraw*系列的函數(shù):

            
              void  glDrawArrays (GLenum mode, GLint first, GLsizei count);

            
          

??mode有以下類型,畫點(diǎn)GL_POINTS,畫線 GL_LINES,順連線段GL_LINE_STRIP,回環(huán)線段GL_LINE_LOOP,三角形GL_TRIANGLES,GL_TRIANGLE_STRIP,GL_TRIANGLE_FAN,四邊形GL_QUADS,GL_QUAD_STRIP,多邊形GL_POLYGON。
??first為0即可,
??count表示要繪制頂點(diǎn)的個(gè)數(shù)。

四、正弦波繪制

??正弦波公式是y = sin(x ) ,通過描點(diǎn)畫出圖形。需確定了以下幾個(gè)參數(shù)來畫正弦波:
??1. sampleCnt:采樣點(diǎn)個(gè)數(shù),openGL畫東西都采用逼近的方式,采樣點(diǎn)越多,正弦波就越精細(xì)。
??2. factor:用來控制正弦波的頻率,如sin(2x ),sin(3x ) 等。
??3. amplitude:振幅,用來控制正弦波的振幅,如3sin(2x )。
??4. rangeL:我們要把正弦波映射到[-1.0,1.0]的范圍內(nèi),否則畫出來的正弦波都看不到。
??5. rangeR:如傳的-pi~pi的范圍,會把這個(gè)范圍的正弦波映射到[-1.0,1.0]范圍內(nèi)。
??注意:shader里面y坐標(biāo)統(tǒng)一乘了0.9,主要是避免圖形頂?shù)竭吙颉?

五、源代碼

            
              """
glfw_sin01.py
Author: dalong10
Description: Draw a Sine curve, learning OPENGL 
"""
import glutils    #Common OpenGL utilities,see glutils.py
import sys, random, math
import OpenGL
from OpenGL.GL import *
from OpenGL.GL.shaders import *
import numpy 
import numpy as np
import glfw

strVS = """
#version 330 core
layout(location = 0) in vec2 position;
void main(){
	gl_Position = vec4(position.x,position.y*0.9,0.0,1.0);
	}
"""

strFS = """
#version 330 core
out vec3 color;
void main(){
	color = vec3(1,1,0);
	}
"""

def createSinArray(sampleCnt, factor, amplitude, rangeL,rangeR):
    i = 0
    range0 = rangeR-rangeL
    if ((sampleCnt<=4) or (rangeR <= rangeL)):
        print("param error sampleCnt:%d rangeR:%f rangeL:%f\n",sampleCnt,rangeL,rangeR)
        return 
    array0 =np.zeros(sampleCnt*2, np.float32)
    for i in range(sampleCnt):
        array0[i*2] = (2.0*i-sampleCnt)/sampleCnt  # x
        array0[i*2+1] = amplitude * math.sin(factor*(rangeL+i*range0/sampleCnt)) # y
    return array0


class FirstSinCurve:
    def __init__(self, side):
        self.side = side

        # load shaders
        self.program = glutils.loadShaders(strVS, strFS)
        glUseProgram(self.program)             
        # set up VBOs
        vertexData = createSinArray(200, 1.0,1.0,-3*PI,3 * PI)
        self.vertexBuffer = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
        glBufferData(GL_ARRAY_BUFFER, 4*len(vertexData), vertexData, GL_STATIC_DRAW)
        # set up vertex array object (VAO)
        self.vao = glGenVertexArrays(1)
        glBindVertexArray(self.vao)
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, None)
        glEnableVertexAttribArray(0)
        # unbind VAO
        glBindVertexArray(0)
        glBindBuffer(GL_ARRAY_BUFFER, 0)    

    def render(self):       
        # use shader
        glUseProgram(self.program)
        # bind VAO
        glBindVertexArray(self.vao)
        # draw
        glDrawArrays(GL_LINE_STRIP, 0, 200)
        # unbind VAO
        glBindVertexArray(0)

if __name__ == '__main__':
    import sys
    import glfw
    import OpenGL.GL as gl
    def on_key(window, key, scancode, action, mods):
        if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
            glfw.set_window_should_close(window,1)

    # Initialize the library
    if not glfw.init():
        sys.exit()

    # Create a windowed mode window and its OpenGL context
    window = glfw.create_window(300, 300, "draw Sine Curve0 ", None, None)
    if not window:
        glfw.terminate()
        sys.exit()

    # Make the window's context current
    glfw.make_context_current(window)
    # Install a key handler
    glfw.set_key_callback(window, on_key)
    PI = 3.14159265358979323846264
    # Loop until the user closes the window
    firstSinCurve0 = FirstSinCurve(1.0)        
    while not glfw.window_should_close(window):
        # Render here
        width, height = glfw.get_framebuffer_size(window)
        ratio = width / float(height)
        gl.glViewport(0, 0, width, height)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        gl.glClearColor(0.0,0.0,4.0,0.0)
 
        firstSinCurve0.render()                 
        # Swap front and back buffers
        glfw.swap_buffers(window)       
        # Poll for and process events
        glfw.poll_events()

    glfw.terminate()


            
          

六、參考文獻(xiàn)

1、他山隨悟博客https://blog.csdn.net/t3swing/article/details/78471135


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論