Projects for CG Class

project2/project2.py


#!/usr/bin/env python

import sys
import math
import time

from Image import *

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

texture = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

ROTSTEP = 3 
MOVSTEP = 0.5
ZOOMSTEP = 0.025
ACCSTEP = 30
ZOOM = 1
XROT = YROT = ZROT = XMOV = YMOV = 0.0

CUT = 1500
ACC = 10.0
SPEED = (
    59, 243, 365, 687, 365*12, 29*365, 84*365, 165*365, 249*365
)
SIZE = (
    0.03, 0.04, 0.05, 0.035, 0.35, 0.3, 0.45, 0.45, 0.3
)

def LoadTextures():
    for _i in range(14) :
        texture[_i] = LoadTexture("textures/%s.jpg" % _i)

def LoadTexture(file):
    image = open(file)
    ix = image.size[0]
    iy = image.size[1]
    image = image.tostring("raw", "RGBX", 0, -1)
    name = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, name)   # 2d texture (x and y size)
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
    return name

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

def myArrayInit() :
    global pointArray 
    pointArray = []
    pointArray.append(libgraphics.circle(4*0.1275, CUT))
    pointArray.append(libgraphics.circle(4*0.18, CUT))
    pointArray.append(libgraphics.circle(4*0.25, CUT))
    pointArray.append(libgraphics.circle(4*0.375, CUT))
    pointArray.append(libgraphics.circle(4*1.3, CUT))
    pointArray.append(libgraphics.circle(3*2.375, CUT))
    pointArray.append(libgraphics.circle(3*4.8, CUT))
    pointArray.append(libgraphics.circle(3*7.525, CUT))
    pointArray.append(libgraphics.circle(3*9.875, CUT))

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()
    glTranslatef(XMOV, YMOV, -20*ZOOM)
    glRotatef(XROT, 0.0, 1.0, 0.0)
    glRotatef(YROT, 1.0, 0.0, 0.0)
    glRotatef(ZROT, 0.0, 0.0, 1.0)

    glLightModelfv( GL_LIGHT_MODEL_AMBIENT,(0.1,0.1,0.1,0.1))
    glLightfv(GL_LIGHT0, GL_AMBIENT,(0,0,0,1))
    glLightfv(GL_LIGHT0, GL_POSITION, (0,0,0,1))
    glEnable(GL_LIGHT0)
    glEnable( GL_COLOR_MATERIAL )

    _i = 0
    for _array in pointArray :
        glMaterialfv( GL_FRONT, GL_EMISSION, (0,0,0,1))
        libgraphics.lineLoopArray(_array, 1,1,1)
        _posi = (time.time() % (SPEED[_i]/ACC))/(SPEED[_i]/ACC) * CUT
        _posi = int(_posi)
        glTranslatef(_array[_posi].x, _array[_posi].y,0, 0)
        glRotatef(5*_posi, 0.0, 0.0, 1.0)
        glBindTexture(GL_TEXTURE_2D, texture[_i])
        glEnable(GL_TEXTURE_2D)
        gluSpherePtr = gluNewQuadric()
        gluQuadricTexture(gluSpherePtr, True)
        gluSphere(gluSpherePtr, SIZE[_i], 20, 20)
        glDisable(GL_TEXTURE_2D)
        if(_i == 2):
            glTranslatef(pointArray[_i][int(1.75*_posi)%CUT].y/12, pointArray[_i][int(1.75*_posi)%CUT].x/12, 0)
            glBindTexture(GL_TEXTURE_2D, texture[13])
            glEnable(GL_TEXTURE_2D)
            gluSpherePtr = gluNewQuadric()
            gluQuadricTexture(gluSpherePtr, True)
            gluSphere(gluSpherePtr, 0.008, 20, 20)
            glDisable(GL_TEXTURE_2D)
            glTranslatef(-pointArray[_i][int(1.75*_posi)%CUT].y/12, -pointArray[_i][int(1.75*_posi)%CUT].x/12, 0)
        if(_i == 5):
            glColor3f(0.48,0.38,0.33)
            gluDisk(gluNewQuadric(), SIZE[_i] + 0.1, SIZE[_i] + 0.15, 30, 30)
            glColor3f(0.97,0.88,0.72)
            gluDisk(gluNewQuadric(), SIZE[_i] + 0.16, SIZE[_i] + 0.25, 30, 30)
            gluDisk(gluNewQuadric(), SIZE[_i] + 0.26, SIZE[_i] + 0.35, 30, 30)
            glColor3f(0.48,0.38,0.33)
            gluDisk(gluNewQuadric(), SIZE[_i] + 0.36, SIZE[_i] + 0.375, 30, 30)
        glRotatef(-5*_posi, 0.0, 0.0, 1.0)
        glTranslatef(-_array[_posi].x, -_array[_posi].y,0, 0)
        _i = _i + 1

    glMaterialfv( GL_FRONT, GL_EMISSION, (1,1,1,1) )
    glEnable(GL_TEXTURE_2D)
    gluSpherePtr = gluNewQuadric()
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
    gluQuadricTexture(gluSpherePtr, True)
    glBindTexture(GL_TEXTURE_2D, texture[9])
    gluSphere(gluSpherePtr, 0.4, 20, 20)
    glDisable(GL_TEXTURE_2D)

    glMaterialfv( GL_FRONT, GL_EMISSION, (0.02,0.02,0.02,0.02) )
    glEnable(GL_TEXTURE_2D)
    gluSpherePtr = gluNewQuadric()
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
    gluQuadricTexture(gluSpherePtr, True)
    glBindTexture(GL_TEXTURE_2D, texture[10])
    gluSphere(gluSpherePtr, 70, 20, 20)
    glDisable(GL_TEXTURE_2D)

    glutSwapBuffers()

def myKeyboardFunc(*args):
    global XROT
    global YROT
    global ZROT
    global ROTSTEP
    global XMOV
    global YMOV
    global ZOOM
    global MOVSTEP
    global ACC
    global ACCSTEP
    global ZOOMSTEP
    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
    elif args[0] == 'j' :
        XMOV = XMOV + MOVSTEP
    elif args[0] == 'l' :
        XMOV = XMOV - MOVSTEP
    elif args[0] == 'i' :
        ZOOM = ZOOM - ZOOMSTEP
        MOVSTEP = MOVSTEP * 0.95
        ZOOMSTEP = ZOOMSTEP * 0.98
    elif args[0] == 'k' :
        ZOOM = ZOOM + ZOOMSTEP
        MOVSTEP = MOVSTEP / 0.95
        ZOOMSTEP = ZOOMSTEP / 0.98
    elif args[0] == '8' :
        YMOV = YMOV - MOVSTEP
    elif args[0] == ',' :
        YMOV = YMOV + MOVSTEP
    elif args[0] == 'z' :
        ACC = 0.000001 
    elif args[0] == 'x' :
        ACC = 10.0 
    elif args[0] == 'v' :
        if ACC > ACCSTEP:
            ACC = ACC + ACCSTEP
        else :
            ACC = ACC / 0.75
    elif args[0] == 'c':
        if ACC > ACCSTEP:
            ACC = ACC - ACCSTEP
        else :
            ACC = ACC * 0.75

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 2")

    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: