一、目的
1、正弦函數的基本畫法;
2、GLSL方式實現練習。
二、程序運行結果
三、glDrawArrays 函數
??GLSL畫這些基本的類型使用的函數主要是glDraw*系列的函數:
void glDrawArrays (GLenum mode, GLint first, GLsizei count);
??mode有以下類型,畫點GL_POINTS,畫線 GL_LINES,順連線段GL_LINE_STRIP,回環線段GL_LINE_LOOP,三角形GL_TRIANGLES,GL_TRIANGLE_STRIP,GL_TRIANGLE_FAN,四邊形GL_QUADS,GL_QUAD_STRIP,多邊形GL_POLYGON。
??first為0即可,
??count表示要繪制頂點的個數。
四、正弦波繪制
??正弦波公式是y = sin(x ) ,通過描點畫出圖形。需確定了以下幾個參數來畫正弦波:
??1. sampleCnt:采樣點個數,openGL畫東西都采用逼近的方式,采樣點越多,正弦波就越精細。
??2. factor:用來控制正弦波的頻率,如sin(2x ),sin(3x ) 等。
??3. amplitude:振幅,用來控制正弦波的振幅,如3sin(2x )。
??4. rangeL:我們要把正弦波映射到[-1.0,1.0]的范圍內,否則畫出來的正弦波都看不到。
??5. rangeR:如傳的-pi~pi的范圍,會把這個范圍的正弦波映射到[-1.0,1.0]范圍內。
??注意:shader里面y坐標統一乘了0.9,主要是避免圖形頂到邊框。
五、源代碼
"""
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()
六、參考文獻
1、他山隨悟博客https://blog.csdn.net/t3swing/article/details/78471135
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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