相关文章推荐
从容的排球  ·  日期和时间函数_日志服务(SLS)-阿里云帮助中心·  1 年前    · 
英勇无比的仙人掌  ·  SuppressLint Class ...·  2 年前    · 
有情有义的汉堡包  ·  matlab画图时怎样设置x轴显示日期时间- ...·  2 年前    · 
坚韧的椅子  ·  小鹏汽车窃密特斯拉实锤?前员工:确实上传过源 ...·  2 年前    · 
Code  ›  python+opengl显示三维模型小程序 原开发者社区
三维模型
https://cloud.tencent.com/developer/article/1186191
老实的小摩托
2 年前
作者头像
晓歌
0 篇文章

python+opengl显示三维模型小程序 原

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 破晓之歌 > python+opengl显示三维模型小程序 原

python+opengl显示三维模型小程序 原

作者头像
晓歌
发布 于 2018-08-15 15:15:46
3.5K 0
发布 于 2018-08-15 15:15:46
举报

一、安装和初步使用

1、安装PyOpenGL

已经安装python的系统会自动安装pip,所以只需要一句pip命令就可以安装opengl了,命令如下:

pip install PyOpenGL PyOpenGL_accelerate

2.安装报错

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

安装参考: https://my.oschina.net/u/3018050/blog/1793640

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

暂未发现错误,以下未验证:

然后在python 中import相关功能,运行后会出现错误

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

或者:

SyntaxError: multiple statements found while compiling a single statement

可能是缺少相关dll文件,可以在这里下载到     http://pan.baidu.com/s/1dFhC8G5

拷到你建立的工程目录下,就是你写的程序的目录下就可以了。

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

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()

就应该能够显示茶壶模型了

二、简单使用例子

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()
 
推荐文章
从容的排球  ·  日期和时间函数_日志服务(SLS)-阿里云帮助中心
1 年前
英勇无比的仙人掌  ·  SuppressLint Class (Android.Annotation) | Microsoft Learn
2 年前
有情有义的汉堡包  ·  matlab画图时怎样设置x轴显示日期时间-百度经验
2 年前
坚韧的椅子  ·  小鹏汽车窃密特斯拉实锤?前员工:确实上传过源码_手机新浪网
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号