1 / 15

Module - pyopengl

Module - pyopengl. 應用於 2d,3d 圖形繪製. Opengl 簡介. 提供繪製 2D,3D 圖形的函式庫及模組 支援語言 :C++ C# HTML Python 本次課程主要應用 opengl 支援 python 語言的模組 -Pyopengl. Pyopengl 下載點及安裝. http ://www.lfd.uci.edu/~gohlke/pythonlibs/ -64 位元電腦選擇 PyOpenGL-3.1.0a1.win-amd64-py3.3.exe -32 位元電腦選擇 PyOpenGL-3.1.0a1.win32-py3.3.exe

Download Presentation

Module - pyopengl

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module - pyopengl 應用於2d,3d圖形繪製

  2. Opengl簡介 • 提供繪製2D,3D圖形的函式庫及模組 • 支援語言:C++ C# HTML Python • 本次課程主要應用opengl支援python語言的模組-Pyopengl

  3. Pyopengl 下載點及安裝 • http://www.lfd.uci.edu/~gohlke/pythonlibs/ -64位元電腦選擇PyOpenGL-3.1.0a1.win-amd64-py3.3.exe -32位元電腦選擇PyOpenGL-3.1.0a1.win32-py3.3.exe 下載完後一直選擇下一步便可安裝成功

  4. 基本程式架構-範例 def init(): glClearColor(1.0, 1.0, 1.0, 1.0)//定義背景為白色 gluOrtho2D(-10.0, 10.0, -10.0, 10.0)//定義xy軸範圍 def exampleDraw()://畫圖函式 //開始寫要畫甚麼畫甚麼 glutInit() glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA) glutInitWindowSize(400, 400)//視窗大小設定 glutCreateWindow(b“First” )//視窗名字設定 glutDisplayFunc(exampleDraw) //載入我們寫好的畫圖函式 init()//做背景處理 glutMainLoop() *紅色部分是我們可以做更改的部分

  5. 繪圖函式及建立方式

  6. 繪圖函式及建立方式-範例 glBegin(GL_LINES) glVertex2f(-1.0, 0.0) glVertex2f(1.0, 0.0) glEnd() 說明: glBegin內選擇要用的繪圖 如範例可看到選擇的是畫線 利用glVertex2f 載入起點(-1,0) 及終點(1,0) 便會畫出從(-1,0) 到(1,0)的連線

  7. 程式碼範例(1)-圖

  8. 程式碼範例說明及結果-1 from OpenGL.GLU import *//先import需要用到的opengl模組 from OpenGL.GL import * from OpenGL.GLUT import * def init(): //定義背景顏色為白色跟XY軸 glClearColor(1.0, 1.0, 1.0, 1.0) gluOrtho2D(-10.0, 10.0, -10.0, 10.0)

  9. 程式碼範例說明及結果-2 • 接續上一頁 def drawFunc()://定義畫圖函式 glClear(GL_COLOR_BUFFER_BIT) glColor3f(1.0, 0.0, 0.0)//設定顏色為紅色 glBegin(GL_QUADS)//畫的是四邊形 glVertex2f(-2, 2)//給予四個點 glVertex2f(-2, 5) glVertex2f(-5, 5) glVertex2f(-5, 2) glEnd() glFlush()// 執行繪圖

  10. 程式碼範例說明及結果-3 • 接續上一頁 glutInit()//啟動glut glutInitDisplayMode(GLUT_RGBA|GLUT_SINGLE) glutInitWindowSize(400, 400) glutCreateWindow(b“First”)//設定名稱為First(前面可有bopengl判斷字串用) 有時候不加b會出錯 glutDisplayFunc(drawFunc)//載入我們上一頁的drawFunc來繪圖 init()//設定背景 glutMainLoop()

  11. 程式碼範例說明及結果-4 • 結果圖

  12. 旋轉/放大縮小/平移-函式介紹 Opengl提供glTranslated(x,y,z), glRotatef(角度,x,y,z), glScalef(x,y,z) 三個現成函是讓我們對圖形做旋轉/放大縮小/平移Example: • glTranslated(6,2,0) • glRotatef(90,0,0,1) • glScalef(0.5,0.5,1) • 以上三個function代表了 • 平移x軸6,y軸2,以第一個頂點旋轉90度,x,y軸縮小0.5倍

  13. 旋轉/放大縮小/平移 –順序 • 旋轉/放大縮小/平移 三個動作對電腦來說其實是針對”點”做數學運算,也就是矩陣相乘 • 在矩陣相乘不滿足交換律的條件下 ABC 不會等於BAC 也就是說旋轉後再平移跟平移後再旋轉得出來的圖形會不同

  14. 旋轉/放大縮小/平移 –進階(1)問題 OPENGL的平移旋轉指令下過之後 會對接下來所有的指令賦予同樣的內容 原因:最初始狀態的矩陣被拿去做運算之後消失了 例如: 原本物件在點(0,0) 平移(2,0)之後物件中心點變在(2,0) 想對原本物件再從(0,0)平移(-1,0) 實際上會是從(2,0)變到(1,0)

  15. 旋轉/放大縮小/平移 –進階(1)解決方法 • opengl提供pushMatrix() popMatrix()兩個方法幫我們保存尚未做平移旋轉的原始矩陣 • 以剛剛那狀況當作例子:Step1: pushMatrix() //保存中心點在(0,0)狀態 Step2:平移到(2,0)//此時中心點在(2,0) Step3:popMatrix()//將矩陣還原成中心點在(0,0)狀態 Step4:平移到(-1,0) //此時中心點在(-1,0) 這樣就可以提高繪圖過程中做平移旋轉的正確性

More Related