Python之OpenGL笔记(34):采用了顶点常量属性方法画多彩六角星
生活随笔
收集整理的這篇文章主要介紹了
Python之OpenGL笔记(34):采用了顶点常量属性方法画多彩六角星
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、目的
1、采用了頂點常量屬性方法畫多彩六角星;
二、程序運行結果
三、頂點常量屬性
?? 吳亞峰《OpenGL ES 3.x游戲開發》(上卷)內容
?? 前面的很多案例中,給每一個頂點都單獨指定了顏色值,這在很多情況下是很好的一種選擇。但是,如果一個物體中所有的頂點顏色是一樣的,這樣做就顯得效率不高了。因為這樣不但更多地占用了內存空間,還會無謂增加數據 IO 的時間。本節將向讀者介紹頂點常量屬性技術,通過其可以很好地解決上述效率不夠高的問題,具體內容如下:
##1、頂點常量基本知識
?? 頂點常量屬性是指給一個需繪制物體中的所有頂點指定同樣的某方面(如顏色)屬性值,在繪制時可以減小內存的占用以及 IO 傳輸的時間,以提高繪制效率。
##2、glVertexAttribNf 方法
?? 將 N 個浮點數傳入管線,以備管線傳遞給由 N 個浮點數分量組成的屬性變量, N 可能的取值為 1、 2、 3 或 4。
##3、glVertexAttribNfv 方法
?? 將長度為 N 的浮點數組或者長度為 N 的數據緩沖傳入管線,以備管線傳遞給由 N 個浮點數分量組成的屬性變量, N 可能的取值為 1、 2、 3 或 4。
四、源代碼
""" 程序名稱:GL_VertexAttrib4f.py 編程: dalong10 功能: 頂點常量屬性應用的實現 參考資料: 《OpenGL ES 3.x游戲開發》(上卷)吳亞峰 """ import myGL_Funcs #Common OpenGL utilities,see myGL_Funcs.py import glfw from OpenGL.GL import * import numpy import numpy as np import pyrr from pyrr import Quaternion, matrix44, Vector3StarVS = """ # version 330 layout(location = 0) in vec3 a_position; //頂點位置 layout(location = 1) in vec3 a_color; //頂點顏色 uniform mat4 rotation; //總變換矩陣 out vec3 v_color; //用于傳遞給片元著色器的變量 void main() {gl_Position = rotation * vec4(a_position, 1.0); //根據總變換矩陣計算此次繪制此頂點位置 v_color = a_color; //將接收的顏色傳遞給片元著色器 } """StarFS = """ # version 330 in vec3 v_color; //接收從頂點著色器過來的參數 out vec4 out_color; //輸出到的片元顏色 void main() {out_color = vec4(v_color, 1.0f); //給此片元顏色值 } """class SixPointedStar:def initVertexData(self,R,r,z,color): # 初始化頂點數據的initVertexData方法self.vertexs = np.array([], np.float32) # 位置FloatArray(numPoint * 3)self.color = color # 顏色# 把矩形平鋪在一個平面上PI = np.pitempAngle=int(360/6)count=0for angle in range(0,360,tempAngle): # 循環生成構成六角形各三角形的頂點坐標x1=0.0 #第一個三角形,三個點y1=0.0z1=zx2=R*np.cos(PI*angle/180) y2=R*np.sin(PI*angle/180)z2=zx3=r*np.cos(PI*(angle+tempAngle/2)/180) y3=r*np.sin(PI*(angle+tempAngle/2)/180) z3=zx4=0y4=0z4=z x5=r*np.cos(PI*(angle+tempAngle/2)/180) y5=r*np.sin(PI*(angle+tempAngle/2)/180)z5=zx6=R*np.cos(PI*(angle+tempAngle)/180) y6=R*np.sin(PI*(angle+tempAngle)/180)z6=zself.vertexs=np.hstack((self.vertexs, np.array([x1,y1,z1], np.float32) )) #每個頂點xyz三個坐標,6個頂點 self.vertexs=np.hstack((self.vertexs, np.array([x2,y2,z2], np.float32) )) self.vertexs=np.hstack((self.vertexs, np.array([x3,y3,z3], np.float32) )) self.vertexs=np.hstack((self.vertexs, np.array([x4,y4,z4], np.float32) )) self.vertexs=np.hstack((self.vertexs, np.array([x5,y5,z5], np.float32) )) self.vertexs=np.hstack((self.vertexs, np.array([x6,y6,z6], np.float32) )) def __init__(self,R,r,z,color):self.R= Rself.r= rself.z = z self.color=colorself.initVertexData(R,r,z,color)# load shadersself.program = myGL_Funcs.loadShaders(StarVS, StarFS)glUseProgram(self.program)self.vertIndex = glGetAttribLocation(self.program, b"a_position")self.colorIndex = glGetAttribLocation(self.program, b"a_color")# set up vertex array object (VAO)self.vao = glGenVertexArrays(1)glBindVertexArray(self.vao) # Step2: 創建并綁定VBO 對象 傳送數據#self.vertexs= verticesvertexData = numpy.array(self.vertexs, numpy.float32)self.vertexBuffer = glGenBuffers(1)glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)glBufferData(GL_ARRAY_BUFFER, 4*len(vertexData), vertexData, GL_STATIC_DRAW) # enable arrays# 頂點位置屬性glEnableVertexAttribArray(self.vertIndex)glVertexAttribPointer(self.vertIndex, 3, GL_FLOAT, GL_FALSE, vertexData.itemsize * 3, ctypes.c_void_p(0)) # unbind VAOglBindVertexArray(0)glBindBuffer(GL_ARRAY_BUFFER, 0) def render(self, model): # use shaderglUseProgram(self.program)# set modelview matrixglUniformMatrix4fv(glGetUniformLocation(self.program, 'rotation'), 1, GL_FALSE, model) glUniform3f(glGetUniformLocation(self.program, "a_color"), self.color[0],self.color[1],self.color[2]) # bind VAOglBindVertexArray(self.vao)# 頂點顏色屬性#print( self.color[0],self.color[1],self.color[2])glVertexAttrib3f(self.colorIndex, self.color[0],self.color[1],self.color[2]) glDrawArrays(GL_TRIANGLES,0,len(self.vertexs) )# unbind VAOglBindVertexArray(0) # glfw callback functions def window_resize(window, width, height):glViewport(0, 0, width, height)if __name__ == '__main__':import sysimport glfwimport OpenGL.GL as glcameraPos=np.array([2.0, 0.0, 3]) # 眼睛的位置(默認z軸的正方向)cameraFront=np.array([0.0, 0.0, 0.0]) # 瞄準方向的參考點(默認在坐標原點)cameraUp=np.array([0.0, 1.0, 0.0]) # 定義對觀察者而言的上方(默認y軸的正方向)# Initialize the libraryif not glfw.init():sys.exit()# Create a windowed mode window and its OpenGL contextwindow = glfw.create_window(400, 300, "My OpenGL window", None, None)if not window:glfw.terminate()sys.exit()# set window's positionglfw.set_window_pos(window, 100, 100)# set the callback function for window resizeglfw.set_window_size_callback(window, window_resize)# make the context currentglfw.make_context_current(window)glClearColor(0, 0.1, 0.1, 1)glEnable(GL_DEPTH_TEST)glEnable(GL_BLEND)glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)scale1 = matrix44.create_from_scale(Vector3([1, 1, 1]))cube1 = matrix44.create_from_translation(Vector3([-0.2, 0, 0]))board= [None]*6 #創建對象數組color= [None]*3for i in range(6):if i==0:color=[1,0.0,0.1] #紅elif i==1:color=[0.98,0.49,0.04] #橙elif i==2:color=[1,1.0,0.04] # 黃elif i==3:color=[0.67,1.0,0] #綠elif i==4:color=[0.27,0.4,1] #藍else :color=[0.88,0.43,0.9] #紫board[i]=SixPointedStar(0.2,0.5,-1*i,color)# the main application loopwhile not glfw.window_should_close(window):width, height = glfw.get_framebuffer_size(window)ratio = width / float(height)currentFrame = 1.0*glfw.get_time()glfw.poll_events() gl.glViewport(0, 0, width, height) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)gl.glMatrixMode(gl.GL_PROJECTION)gl.glLoadIdentity()gl.glOrtho(-ratio, ratio, -1, 1, 1, -1)gl.glMatrixMode(gl.GL_MODELVIEW)gl.glLoadIdentity()gl.glClearColor(0.0,0.0,4.0,0.0) pMatrix = matrix44.create_perspective_projection_from_bounds(-ratio*0.4, ratio*0.4, -0.4, 0.4,1,50,None)# modelview matrixmvMatrix = matrix44.create_look_at(cameraPos, cameraFront, cameraUp,None)for i in range(6): model1 = matrix44.multiply(scale1, cube1) model2 = matrix44.multiply(pMatrix,model1) model3 = matrix44.multiply(mvMatrix,model2) board[i].render( model3)glfw.swap_buffers(window)# terminate glfw, free up allocated resourcesglfw.terminate()五、參考資料
1、大龍10的簡書:https://www.jianshu.com/p/49dec482a291
2、吳亞峰《OpenGL ES 3.x游戲開發》(上卷)
總結
以上是生活随笔為你收集整理的Python之OpenGL笔记(34):采用了顶点常量属性方法画多彩六角星的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 儿童编程软件python征服_少儿编程P
- 下一篇: jquery实现淘宝精品案例