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 相关的功能了

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值