Python環境搭建之OpenGL

如下內容爲我python OpenGl 環境搭建歷程:python

  win7 64位操做系統,python3.5.3 ,無其餘相關。函數

  直接cmd或PowerShell輸入如下命令:oop

pip install  PyOpenGL PyOpenGL_accelerate

  安裝失敗,提示需安裝Microsoft Visual C++ 14.0,讓我使用Microsoft Visual C++ build tools。而且後面給出了下載連接http://landinghub.visualstudio.com/visual-cpp-build-tools,在該連接下載獲得文件visualcppbuildtools_full.exe,進行安裝。。。測試

  失敗,提示須要.net framework4.5.1以上ui

  一開始我下載了.net framework 4.5 發現還不能安裝,因而從新搜索,終於在https://www.microsoft.com/zh-CN/download/details.aspx?id=48130找到4.6版本,下載得文件NDP46-KB3045560-Web.exe安裝之。spa

  終於能夠安Microsoft Visual C++ build tools(visualcppbuildtools_full.exe)了。操作系統

  通過漫長的等待VC++也搞定,重啓後繼續執行命令:.net

pip install  PyOpenGL PyOpenGL_accelerate

  一次性成功,興奮之餘在網上找了段測試代碼,並稍做調整( glutCreateWindow(b"first")處,原文爲glutCreateWindow("first"),運行會報錯:Python glutCreateWindow error 'wrong type',詳情參見https://codeyarns.com/2012/04/27/pyopengl-glut-ctypes-error/ code

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

def drawFunc():
    #清楚以前畫面
    glClear(GL_COLOR_BUFFER_BIT)
    glRotatef(0.1, 5, 5, 0)   #(角度,x,y,z)
    glutWireTeapot(0.5)
    #刷新顯示
    glFlush()
    
#使用glut初始化OpenGL
glutInit()
#顯示模式:GLUT_SINGLE無緩衝直接顯示|GLUT_RGBA採用RGB(A非alpha)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
#窗口位置及大小-生成
glutInitWindowPosition(0,0)
glutInitWindowSize(400,400)
glutCreateWindow(b"first")
#調用函數繪製圖像
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
#主循環
glutMainLoop()

  運行,結果提示OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling。原來我沒搭建glut,又開始漫長的搜尋路程,終於在http://download.csdn.net/detail/knownall/6799947找到須要的(其實就須要glut.h、glut64.dll、glut64.lib三個文件,32位同理)。blog

  下載解壓後將文件夾內 glut.h 放在 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\ 下;

  將 .\Release\glut64.lib 放在 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib\ 下;

  將 .\Release\glut64.dll 放在 C:\Windows\System32 下。

  再次運行,終於大功告成,能夠看到一個旋轉的茶壺。

相關文章
相關標籤/搜索