python+opengl顯示三維模型小程序

1、安裝和初步使用

一、安裝PyOpenGL

已經安裝python的系統會自動安裝pip,因此只須要一句pip命令就能夠安裝opengl了,命令以下:html

pip install PyOpenGL PyOpenGL_accelerate

2.安裝報錯

在本地址找合適的版本下載:https://www.lfd.uci.edu/~gohlke/pythonlibs/python

安裝參考:http://www.javashuo.com/article/p-cywibfms-et.html小程序

————————————————————————————————————————————函數

暫未發現錯誤,如下未驗證:oop

而後在python 中import相關功能,運行後會出現錯誤ui

OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before callingspa

或者:.net

SyntaxError: multiple statements found while compiling a single statementcode

多是缺乏相關dll文件,能夠在這裏下載到     http://pan.baidu.com/s/1dFhC8G5htm

拷到你創建的工程目錄下,就是你寫的程序的目錄下就能夠了。

——————————————————————————————————————————————

3.一個demo

運行下面的程序

# -*- coding:utf-8 -*-
# Author:WYC
from OpenGL.GL import *

from OpenGL.GLU import *
from OpenGL.GLUT import *
def drawFunc():
#清楚以前畫面
    glClear(GL_COLOR_BUFFER_BIT)
    glRotatef(0.1, 0,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()


就應該可以顯示茶壺模型了

 

2、簡單使用例子

1.點線拋物線

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

def init():
    glClearColor(1.0,1.0,1.0,1.0)
    gluOrtho2D(-5.0,5.0,-5.0,5.0)

def plotfunc():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(1.0,0.2,0.6)
    glPointSize(3.0)

    glBegin(GL_POINTS)
    for x in arange(-5.0,5.0,0.1):#from -5.0 to 5.0 plus 0.1 every time
        y=x*x
        glVertex2f(x,y)
    glEnd()
    glFlush()

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
    glutInitWindowPosition(50,100)
    glutInitWindowSize(400,400)
    glutCreateWindow("Function Plotter")
    glutDisplayFunc(plotfunc)

    init()
    glutMainLoop()
main()

2.轉動時鐘

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

h = 0
m = 0
s = 0


def Draw():
    PI = 3.1415926
    R = 0.5
    TR = R - 0.05
    glClear(GL_COLOR_BUFFER_BIT)
    glLineWidth(5)
    glBegin(GL_LINE_LOOP)
    for i in range(100):
        glVertex2f(R * math.cos(2 * PI / 100 * i), R * math.sin(2 * PI / 100 * i))
    glEnd()
    glLineWidth(2)
    for i in range(100):
        glBegin(GL_LINES)
        glVertex2f(TR * math.sin(2 * PI / 12 * i), TR * math.cos(2 * PI / 12 * i))
        glVertex2f(R * math.sin(2 * PI / 12 * i), R * math.cos(2 * PI / 12 * i))
        glEnd()
    glLineWidth(1)

    h_Length = 0.2
    m_Length = 0.3
    s_Length = 0.4
    count = 60.0
    s_Angle = s / count
    count *= 60
    m_Angle = (m * 60 + s) / count
    count *= 12
    h_Angle = (h * 60 * 60 + m * 60 + s) / count
    glLineWidth(1)
    glBegin(GL_LINES)
    glVertex2f(0.0, 0.0)
    glVertex2f(s_Length * math.sin(2 * PI * s_Angle), s_Length * math.cos(2 * PI * s_Angle))
    glEnd()
    glLineWidth(5)
    glBegin(GL_LINES)
    glVertex2f(0.0, 0.0)
    glVertex2f(h_Length * math.sin(2 * PI * h_Angle), h_Length * math.cos(2 * PI * h_Angle))
    glEnd()
    glLineWidth(3)
    glBegin(GL_LINES)
    glVertex2f(0.0, 0.0)
    glVertex2f(m_Length * math.sin(2 * PI * m_Angle), m_Length * math.cos(2 * PI * m_Angle))
    glEnd()
    glLineWidth(1)
    glBegin(GL_POLYGON)
    for i in range(100):
        glVertex2f(0.03 * math.cos(2 * PI / 100 * i), 0.03 * math.sin(2 * PI / 100 * i));
    glEnd()
    glFlush()


def Update():
    global h, m, s
    t = time.localtime(time.time())
    h = int(time.strftime('%H', t))
    m = int(time.strftime('%M', t))
    s = int(time.strftime('%S', t))
    glutPostRedisplay()


glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow("My clock")
glutDisplayFunc(Draw)
glutIdleFunc(Update)
glutMainLoop()

 

 

 

參考文檔:

1.python+opengl顯示三維模型小程序:https://blog.csdn.net/renjiangui/article/details/76146160

2.初試PyOpenGL一 (Python+OpenGL):http://www.javashuo.com/article/p-xihqijly-c.html

3.Python 之 OpenGL程序環境:http://www.javashuo.com/article/p-avcwxmvx-na.html

相關文章
相關標籤/搜索