安裝環境:win10 64位作業系統,python3.7
一.安裝py庫
需要用pip 安裝
pip install PyOpenGL PyOpenGL_accelerate
可能會報錯,
是因為沒有安裝對應的c++庫
開啟網站https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl
找到pyopengl
下載對應的版本,我的是python3.7+win64
下載完後放到python目錄的Scripts中然後在該路徑下執行命令列
pip install PyOpenGL_accelerate-3.1.-cp37-cp37m-win_amd64.whl
成功安裝
再次安裝
pip install PyOpenGL PyOpenGL_accelerate
應該就不會報錯了!
二.搭建glut環境(需要glut.h、glut64.dll、glut64.lib三個檔案)
連結:https://pan.baidu.com/s/10ksiGaJMslHk9VMxOhFeXg
提取碼:2o5o
或者在此下載:https://download.csdn.net/download/bigboysunshine/10396268
或者:https://www.opengl.org/resources/libraries/glut/
下載解壓後
將資料夾內 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 下。
執行一下例項:
# -*- coding: utf-8 -*-
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
import numpy as np # 畫圓 def circle(x, y, r, n):
theta = np.linspace(0, 2*np.pi, n)
x = x + r * np.cos(theta)
y = y + r * np.sin(theta)
return x, y def plotfunc():
glClear(GL_COLOR_BUFFER_BIT) # 清除之前快取
glPointSize(3.0) # 設定點大小
glColor3f(1.0, 0.0, 0.0) # 設定點顏色
glBegin(GL_POINTS) # 此次開始,設定此次畫的幾何圖形
x, y = circle(0, 0, 1, 100)
for x_, y_ in zip(x, y):
glVertex2f(x_, y_)
glEnd() # 此次結束
glFlush() # 重新整理螢幕 if __name__ == '__main__':
glutInit(sys.argv) #初始化
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) #設定顯示模式
glutInitWindowPosition(100, 100) #視窗開啟的位置,左上角座標在螢幕座標
glutInitWindowSize(900, 600) #視窗大小
glutCreateWindow(b"Function Plotter") #視窗名字,二進位制
glutDisplayFunc(plotfunc) #設定當前視窗的顯示回撥
glClearColor(1.0, 1.0, 1.0, 1.0) # 設定背景顏色
gluOrtho2D(-5.0, 5.0, -5.0, 5.0) # 設定顯示範圍
glutMainLoop() # 啟動迴圈
此時,應該可以顯示一個橢圓
至此,OpenGL環境搭建完成!
opencv的學習,推薦網站www.opencv.org.cn
參考:
https://blog.csdn.net/BigBoySunshine/article/details/80218245?utm_source=blogkpcl4
https://blog.csdn.net/qq_15602569/article/details/79670880
http://pyopengl.sourceforge.net/