python实现3d建模

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

dX = 0.0
dY = 0.0
alphaX = 0.0
alphaY = 0.0
alphaZ = 0.0
sf = 0.0


def init():
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-4.0 * 64 / 48.0, 4.0 * 64 / 48, -4.0, 4.0, 0.0, 10)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(4.0, 2.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
    glViewport(0, 0, 640, 480)


def axis(length):
    glPushMatrix()
    glBegin(GL_LINES)
    glVertex3d(0, 0, 0)
    glVertex3d(0, 0, length)
    glEnd()
    glTranslated(0, 0, length - 0.2)
    glutWireCone(0.04, 0.2, 12, 9)
    glPopMatrix()


def displayWire():
    glClear(GL_COLOR_BUFFER_BIT)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glPushMatrix()
    glTranslated(0.0, 0.0, 0.0)
    glRotated(alphaY, 0.0, 1.0, 0.0)
    glRotated(alphaX, 1.0, 0.0, 0.0)
    glRotated(alphaZ, 0.0, 0.0, 1.0)
    glTranslated(dY, 0.0, 1.0)
    glTranslated(dX, 1.0, 0.0)
    glPushMatrix()
    glColor3d(0.0, 0.0, 1.0)
    axis(2.0)
    glRasterPos3f(0.0, 0.0, 2.3)

    glRotated(-90, 1.0, 0.0, 0.0)
    glColor3d(0.0, 1.0, 0.0)
    axis(2.0)
    glRasterPos3f(0.0, 0.0, 2.3)

    glColor3d(1.0, 0.0, 0.0)
    glRotated(90, 0.0, 1.0, 0.0)
    axis(2.0)
    glRasterPos3f(0.0, 0.0, 2.3)
    glPopMatrix()

    glColor3d(1.0, 1.0, 0.0)
    glScaled(1.5, 1.5, 1.5)


    glPushMatrix()
    glColor3d(1.0, 1.0, 1.0)
    glTranslated(0.0, 1.5, 0.0)
    glScaled(1 + sf, 1 + sf, 1 + sf)

    glRotated(360, 0, 1, 0)
    glutWireSphere(0.35, 30, 30)
    glPopMatrix()

    glPushMatrix()
    glColor3d(1.0, 1.0, 0.0)
    glRotated(120, 0, 1, 0)
    glScaled(1 + sf, 1 + sf, 1 + sf)
    glTranslated(0.0, 0.5, 1.0)
    glutWireCube(0.35)
    glPopMatrix()

    glPushMatrix()
    glColor3d(0.0, 1.0, 1.0)
    glRotated(290, 0, 1, 0)
    glScaled(1 + sf, 1 + sf, 1 + sf)
    glTranslated(0.0, 0.5, 1.0)
    glutWireCube(0.35)
    glPopMatrix()

    glPushMatrix()
    glColor3d(1.0, 0.0, 1.0)
    glTranslated(0.0, 0.0, 0.0)
    glScaled(1 + sf, 1 + sf, 1 + sf)
    glRotated(-90.0, 1, 0, 0)
    qobj = gluNewQuadric()
    gluQuadricDrawStyle(qobj, GLU_LINE)
    gluCylinder(qobj, 0.50, 0.50, 0.80, 30, 30)
    glPopMatrix()
    glPopMatrix()
    glutSwapBuffers()

def mySpecialKey(key, x, y):

    if (key == GLUT_KEY_RIGHT):
        global alphaX
        alphaX += 5.0
    if (key == GLUT_KEY_LEFT):
        global alphaZ
        alphaZ -= 5.0
    if (key == GLUT_KEY_UP):
        global alphaY
        alphaY += 5.0
    if (key == GLUT_KEY_DOWN):
        global alphaY
        alphaY -= 5.0
    glutPostRedisplay()

def keyboard(key, x, y):
    if key == 'a':
        global dY
        dY += 0.5
    glutPostRedisplay()


if __name__ == '__main__':
    glutInit()
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
    glutInitWindowSize(640, 640)
    glutInitWindowPosition(100, 100)
    glutCreateWindow("a")
    glutDisplayFunc(displayWire)
    glutSpecialFunc(mySpecialKey)
    glutKeyboardFunc(keyboard)
    init()
    glutMainLoop()

记得之前要

pip install PyOpenGL

安装完成后,您就可以在 Python 代码中导入 OpenGL.GL、OpenGL.GLU 和 OpenGL.GLUT 模块,并使用 OpenGL 相关的功能了

相关推荐

  1. python实现3d

    2024-04-08 22:48:05       36 阅读
  2. python实现3D玫瑰花

    2024-04-08 22:48:05       56 阅读
  3. 3D基础教程:模型UV讲解

    2024-04-08 22:48:05       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-08 22:48:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-08 22:48:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-08 22:48:05       82 阅读
  4. Python语言-面向对象

    2024-04-08 22:48:05       91 阅读

热门阅读

  1. 设计模式 工厂模式

    2024-04-08 22:48:05       33 阅读
  2. MySQL 中,常见的 JOIN 查询语句

    2024-04-08 22:48:05       34 阅读
  3. Docker安装Mysql

    2024-04-08 22:48:05       32 阅读
  4. 习题3-3 出租车计价

    2024-04-08 22:48:05       30 阅读
  5. 如何使用Python的内置函数和模块?

    2024-04-08 22:48:05       39 阅读
  6. Springboot 集成websocket

    2024-04-08 22:48:05       35 阅读
  7. Vue登陆鉴权方案(token)

    2024-04-08 22:48:05       25 阅读