Projects for CG Class

project1/project1.py


#!/usr/bin/env python

import sys
import math

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

import libgraphics

ESCAPE = '33'
WINSIZE_X = 800
WINSIZE_Y = 600
WINPOSITION_X = 100
WINPOSITION_Y = 100

ROTSTEP =3 
XROT = 0.0
YROT = 0.0
ZROT = 0.0

def myInit(Width, Height) :
    glClearColor(0.85, 0.71, 0.20, 1.0)
    glClearDepth(1.0)
    glDepthFunc(GL_LESS)
    glEnable(GL_DEPTH_TEST)
    glShadeModel(GL_SMOOTH)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)
    myArrayInit()

def myArrayInit() :
    global pointArray 
    pointArray = []
    pointArray.append(libgraphics.oval(4.1, 3.25, 40))    # 0 face
    pointArray.append(libgraphics.oval(1.0, 0.8, 30))    # 1 nose1
    pointArray.append(libgraphics.circle(1.3, 30))        # 2 ear1
    pointArray.append(libgraphics.circle(0.2, 30))        # 3 nose2
    pointArray.append(libgraphics.circle(0.4, 30))        # 4 eye
    pointArray.append(libgraphics.circle(0.5, 10, 1.5*math.pi, 2.2*math.pi))    # 5 mouth
    pointArray.append(libgraphics.circle(0.5, 10, 0.8*math.pi, 1.6*math.pi))    # 6 mouth
    pointArray.append(libgraphics.oval(1.2, 0.6, 30))        # 7 ear2

def myResizeFunc(Width, Height):
    glViewport(0, 0, Width, Height)        # Reset The Current Viewport And Perspective Transformation
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)

def myDisplayFunc():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glColor3f(0.0, 0.0, 0.0)
    glTranslatef(0.0, 0.0, -11)

    glRotatef(XROT, 0.0, 1.0, 0.0)
    glRotatef(YROT, 1.0, 0.0, 0.0)
    glRotatef(ZROT, 0.0, 0.0, 1.0)

#    ears
    glTranslatef(3.2, 1.8, +0.1)
    libgraphics.linedPoly(pointArray[2], 0.56, 0.33, 0.12)
    glTranslatef(-3.2, -1.8, +0.0)

    glTranslatef(-3.2, 1.8, +0.0)
    libgraphics.linedPoly(pointArray[2], 0.56, 0.33, 0.12)
    glTranslatef(+3.2, -1.8, +0.0)

    glTranslatef(-3.5, +1.2, +0.5)
    rotatedArray = libgraphics.D2rotateArray(pointArray[7], 0.3*math.pi)
    libgraphics.linedPoly(rotatedArray, 0.81, 0.61, 0.11)

    glTranslatef(+7.0, +0.0, +0.0)
    rotatedArray = libgraphics.D2rotateArray(pointArray[7], -0.3*math.pi)
    libgraphics.linedPoly(rotatedArray, 0.81, 0.61, 0.11)
    glTranslatef(-3.5, -1.2, 0.0)

#    face
    glTranslatef(0.0, -0.8, 0.5)
    libgraphics.linedPoly(pointArray[0], 0.56, 0.33, 0.12)

#    eyes
    glTranslatef(1.6, -0.6, 0.5)
    libgraphics.linedPoly(pointArray[4], 0.0, 0.0, 0.0, 1)
    glTranslatef(-3.2, -0.0, 0.0)
    libgraphics.linedPoly(pointArray[4], 0.0, 0.0, 0.0, 1)
    glTranslatef(+1.6, +0.6, 0.0)

#    nose
    glTranslatef(0.0, -1.35, 0.5)
    glBegin(GL_POLYGON)
    glColor3f(1.0, 1.0, 1.0)
    for point in pointArray[1] :
        glVertex3f(point.x, point.y, 0.0)
    glEnd()

    glTranslatef(0.0, 0.3, 0.1)
    glTranslatef(0.0, 0.0, 0.1)
    glColor3f(0.0, 0.0, 0.0)
    libgraphics.linedPoly(pointArray[3], 0.0, 0.0, 0.0, 1)
    glTranslatef(0.0, 0.2, 0.1)

    glTranslatef(-0.5, -0.4, 0.1)
    glLineWidth(10.0)
    glBegin(GL_LINE_STRIP)
    for point in pointArray[5] :
        glVertex3f(point.x, point.y, 0.0)
    glEnd()
    glTranslatef(+1.0, 0.0, 0.1)
    glBegin(GL_LINE_STRIP)
    for point in pointArray[6] :
        glVertex3f(point.x, point.y, 0.0)
    glEnd()
#    ...    

    glutSwapBuffers()

def myKeyboardFunc(*args):
    global XROT
    global YROT
    global ZROT
    global ROTSTEP
    if args[0] == ESCAPE:
        glutDestroyWindow(window)
        sys.exit()
    elif args[0] == 'a' :
        XROT = XROT - ROTSTEP
    elif args[0] == 'd' :
        XROT = XROT + ROTSTEP
    elif args[0] == 'w' :
        YROT = YROT + ROTSTEP
    elif args[0] == 's' :
        YROT = YROT - ROTSTEP
    elif args[0] == 'e' :
        ZROT = ZROT + ROTSTEP
    elif args[0] == 'q' :
        ZROT = ZROT - ROTSTEP

def main():
    global window

    glutInit(())
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
    glutInitWindowSize(WINSIZE_X, WINSIZE_Y)
    glutInitWindowPosition(WINPOSITION_X, WINPOSITION_Y)
    window = glutCreateWindow("CG Project 1")

    glutDisplayFunc(myDisplayFunc)
    glutIdleFunc(myDisplayFunc)
    glutReshapeFunc(myResizeFunc)
    glutKeyboardFunc(myKeyboardFunc)
#    glutFullScreen()

    myInit(WINSIZE_X, WINSIZE_Y)

    glutMainLoop()

if __name__ == '__main__' :
    main()

Advertisements

One Response

Subscribe to comments with RSS.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: