计算机图形学作业 - 运用Python-OpenGL设计一个带键盘控制的三维模型

设计一个三维模型,模型可以旋转、缩放、平移和反射变换。 要求: (1)模型有三个以上的基本体素; (2)模型具有多种颜色; (3)以键、菜单、鼠标方式来控制各种变换;(至少采用一种方式) (4)鼓励有创意的模型。

#encoding=utf8
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import time

xaxis = 0.0
yaxis = 0.0
zaxis = 0.0


def cube():
    glBegin(GL_QUADS)
    glColor3f(0.3, 0.3, 0.0)
    glVertex3f(0.3, 0.3, -0.3)
    glColor3f(0.0, 0.3, 0.0)
    glVertex3f(-0.3, 0.3, -0.3)
    glColor3f(0.0, 0.3, 0.3)
    glVertex3f(-0.3, 0.3, 0.3)
    glColor3f(0.3, 0.3, 0.3)
    glVertex3f(0.3, 0.3, 0.3)

    glColor3f(0.3, 0.3, 0.3)
    glVertex3f(0.3, 0.3, 0.3)
    glColor3f(0.0, 0.3, 0.3)
    glVertex3f(-0.3, 0.3, 0.3)
    glColor3f(0.0, 0.0, 0.3)
    glVertex3f(-0.3, -0.3, 0.3)
    glColor3f(0.3, 0.0, 0.3)
    glVertex3f(0.3, -0.3, 0.3)

    glColor3f(0.3, 0.0, 0.0)
    glVertex3f(0.3, -0.3, -0.3)
    glColor3f(0.0, 0.0, 0.0)
    glVertex3f(-0.3, -0.3, -0.3)
    glColor3f(0.0, 0.3, 0.0)
    glVertex3f(-0.3, 0.3, -0.3)
    glColor3f(0.3, 0.3, 0.0)
    glVertex3f(0.3, 0.3, -0.3)

    glColor3f(0.0, 0.3, 0.3)
    glVertex3f(-0.3, 0.3, 0.3)
    glColor3f(0.0, 0.3, 0.0)
    glVertex3f(-0.3, 0.3, -0.3)
    glColor3f(0.0, 0.0, 0.0)
    glVertex3f(-0.3, -0.3, -0.3)
    glColor3f(0.0, 0.0, 0.3)
    glVertex3f(-0.3, -0.3, 0.3)

    glColor3f(0.3, 0.3, 0.0)
    glVertex3f(0.3, 0.3, -0.3)
    glColor3f(0.3, 0.3, 0.3)
    glVertex3f(0.3, 0.3, 0.3)
    glColor3f(0.3, 0.0, 0.3)
    glVertex3f(0.3, -0.3, 0.3)
    glColor3f(0.3, 0.0, 0.0)
    glVertex3f(0.3, -0.3, -0.3)

    glColor3f(0.5, 0.5, 0.0)
    glVertex3f(0.5, 0.5, -0.5)
    glColor3f(0.0, 0.5, 0.0)
    glVertex3f(-0.5, 0.5, -0.5)
    glColor3f(0.0, 0.5, 0.5)
    glVertex3f(-0.5, 0.5, 0.5)
    glColor3f(0.5, 0.5, 0.5)
    glVertex3f(0.5, 0.5, 0.5)

    glColor3f(0.5, 0.5, 0.5)
    glVertex3f(0.5, 0.5, 0.5)
    glColor3f(0.0, 0.5, 0.5)
    glVertex3f(-0.5, 0.5, 0.5)
    glColor3f(0.0, 0.0, 0.5)
    glVertex3f(-0.5, -0.5, 0.5)
    glColor3f(0.5, 0.0, 0.5)
    glVertex3f(0.5, -0.5, 0.5)

    glColor3f(0.5, 0.0, 0.0)
    glVertex3f(0.5, -0.5, -0.5)
    glColor3f(0.0, 0.0, 0.0)
    glVertex3f(-0.5, -0.5, -0.5)
    glColor3f(0.0, 0.5, 0.0)
    glVertex3f(-0.5, 0.5, -0.5)
    glColor3f(0.5, 0.5, 0.0)
    glVertex3f(0.5, 0.5, -0.5)

    glColor3f(0.0, 0.5, 0.5)
    glVertex3f(-0.5, 0.5, 0.5)
    glColor3f(0.0, 0.5, 0.0)
    glVertex3f(-0.5, 0.5, -0.5)
    glColor3f(0.0, 0.0, 0.0)
    glVertex3f(-0.5, -0.5, -0.5)
    glColor3f(0.0, 0.0, 0.5)
    glVertex3f(-0.5, -0.5, 0.5)

    glColor3f(0.5, 0.5, 0.0)
    glVertex3f(0.5, 0.5, -0.5)
    glColor3f(0.5, 0.5, 0.5)
    glVertex3f(0.5, 0.5, 0.5)
    glColor3f(0.5, 0.0, 0.5)
    glVertex3f(0.5, -0.5, 0.5)
    glColor3f(0.5, 0.0, 0.0)
    glVertex3f(0.5, -0.5, -0.5)

    glColor3f(1.0, 1.0, 0.0)
    glVertex3f(1.0, 1.0, -1.0)
    glColor3f(0.0, 1.0, 0.0)
    glVertex3f(-1.0, 1.0, -1.0)
    glColor3f(0.0, 1.0, 1.0)
    glVertex3f(-1.0, 1.0, 1.0)
    glColor3f(1.0, 1.0, 1.0)
    glVertex3f(1.0, 1.0, 1.0)

    glColor3f(1.0, 1.0, 1.0)
    glVertex3f(1.0, 1.0, 1.0)
    glColor3f(0.0, 1.0, 1.0)
    glVertex3f(-1.0, 1.0, 1.0)
    glColor3f(0.0, 0.0, 1.0)
    glVertex3f(-1.0, -1.0, 1.0)
    glColor3f(1.0, 0.0, 1.0)
    glVertex3f(1.0, -1.0, 1.0)

    glColor3f(1.0, 0.0, 0.0)
    glVertex3f(1.0, -1.0, -1.0)
    glColor3f(0.0, 0.0, 0.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(0.0, 1.0, 0.0)
    glVertex3f(-1.0, 1.0, -1.0)
    glColor3f(1.0, 1.0, 0.0)
    glVertex3f(1.0, 1.0, -1.0)

    glColor3f(0.0, 1.0, 1.0)
    glVertex3f(-1.0, 1.0, 1.0)
    glColor3f(0.0, 1.0, 0.0)
    glVertex3f(-1.0, 1.0, -1.0)
    glColor3f(0.0, 0.0, 0.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(0.0, 0.0, 1.0)
    glVertex3f(-1.0, -1.0, 1.0)

    glColor3f(1.0, 1.0, 0.0)
    glVertex3f(1.0, 1.0, -1.0)
    glColor3f(1.0, 1.0, 1.0)
    glVertex3f(1.0, 1.0, 1.0)
    glColor3f(1.0, 0.0, 1.0)
    glVertex3f(1.0, -1.0, 1.0)
    glColor3f(1.0, 0.0, 0.0)
    glVertex3f(1.0, -1.0, -1.0)
    glEnd()


def display():
    global xaxis,yaxis,zaxis
    time.sleep(0.1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glTranslatef(0, 0, -5)
    glRotatef(xaxis, 1, 0, 0)
    glRotatef(yaxis, 0, 1, 0)
    glRotatef(zaxis, 0, 0, 1)
    cube()
    xaxis = xaxis + 1
    yaxis = yaxis + 1
    zaxis = zaxis + 1
    glutSwapBuffers()


def reshape(w, h):
    if (h == 0):
        h = 1
    glViewport(0, 0, w,h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0,w /h, 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)


def init(width, height):
    if (height == 0):
        height = 1
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0)
    glDepthFunc(GL_LESS)
    glEnable(GL_DEPTH_TEST)
    glShadeModel(GL_SMOOTH)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0,width /height, 1, 100.0)
    glMatrixMode(GL_MODELVIEW)


def keyboard(key, w, h):
    global xaxis,yaxis,zaxis
    if (key=='x'):
        xaxis = xaxis + 10
    if (key=='y'):
        yaxis = yaxis + 10
    if (key=='z'):
        zaxis = zaxis + 10
print 'X:X轴+10 Y:Y轴+10 Z:Z轴+10'
glutInit()
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowPosition(400, 100)
glutInitWindowSize(640, 480)
glutCreateWindow("HiddenStrawberry")
glutDisplayFunc(display)
glutIdleFunc(display)
glutReshapeFunc(reshape)
glutKeyboardFunc(keyboard)
init(640, 480)
glutMainLoop()

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值