【Python OpenGL】【2】第一個三角形(Pyopengl)

來源:互聯網
上載者:User

標籤:href   main   ble   nump   span   profile   nbsp   class   opengl   

根據頂點緩衝來產生圖元

原文(英文連結)http://ogldev.atspace.co.uk/www/tutorial03/tutorial03.html

 1 __author__ = "WSX" 2  3 import numpy as np 4 from OpenGL.GLUT import * 5 from OpenGL.GL import * 6 import ctypes 7 def Draw(): 8     glClear(GL_COLOR_BUFFER_BIT) 9     glEnableVertexAttribArray(0)10     glBindBuffer(GL_ARRAY_BUFFER, VBO)11     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None) #這裡的None不能寫為012     glDrawArrays(GL_TRIANGLES, 0, 3)13     glDisableVertexAttribArray(0)  #解析資料 例如一個矩陣裡含有 位置 、顏色、多種資訊14     glutSwapBuffers()15 16 17 def CreateBuffer():  #建立頂點緩衝器18     global VBO   #設定為全域變數19     vertex = np.array([-1.0,-1.0,0.0,20                        1.0,-1.0,0.0,21                        0.0,1.0,0.0],dtype="float32")   #建立頂點數組22     VBO = glGenBuffers(1)  #建立緩衝23     glBindBuffer(GL_ARRAY_BUFFER , VBO)   #綁定24     glBufferData(GL_ARRAY_BUFFER , vertex.nbytes , vertex , GL_STATIC_DRAW)   #輸入資料25 26 27 def main():28     glutInit([])29     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)  # 顯示模式 雙緩衝30     glutInitWindowPosition(100, 100)  # 視窗位置31     glutInitWindowSize(500, 500)  # 視窗大小32     glutCreateWindow("sanjiao")  # 建立視窗33     glutInitContextVersion(4,3)   #為了相容34     glutInitContextProfile(GLUT_CORE_PROFILE)   #為了相容35     glutDisplayFunc(Draw)  # 回呼函數36     glClearColor(0.0, 0.0, 0.0, 0.0)37     CreateBuffer()38     glutMainLoop()39 40 main()

結果:

 

【Python OpenGL】【2】第一個三角形(Pyopengl)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.