################################################################
# tutorial.py
Andy Beall 1999
#
# This script demonstrates the following techniques:
# - transparency (alpha type)
# - rotation animations
# - animated background colors
# - keyboard handling
# - expiration timers
# - VRML object initialization
#
# The user can press either the "1", "2", or "3" key and toggle
# between the different modes.
################################################################
################################################################
import vrut
import math
vrut.go(vrut.NICE)
# These are constants
INITIALIZE = 1
ANIMATE = 2
SPEED = 1
# These are globals
angle = 0.0
red = .6
green = .6
blue = 1
key1 = 0
key2 = 0
key3 = 0
# Load the VRML geometry files here
tunnel = vrut.addchild('tut_torus.wrl')
ground = vrut.addchild('tut_ground.wrl')
################################################################
# Callback function to handle whenever user presses a key
def mykeyboard(key):
global key1, key2, key3
if key == '1':
key1 = key1 ^ 1
if key1 == 1:
tunnel.alpha(1)
else:
tunnel.appearance()
if key == '2':
key2 = key2 ^ 1
if key == '3':
key3 = key3 ^ 1
if key3 == 1:
ground.curtain(vrut.OPEN)
else:
ground.curtain(vrut.CLOSE)
################################################################\
# Callback function to handle expiration timers
def mytimer(num):
global angle, red, green, blue
# Handle all initializations
if num == INITIALIZE:
# Initialize the VRUT context
vrut.reset(vrut.HEAD_POS)
vrut.translate(vrut.HEAD_POS, 3, 0, 0)
vrut.setfov(60, 1.33)
vrut.override()
# Initialize the geometry
tunnel.center(10,0,0)
ground.center(10,0,0)
ground.curtain(vrut.CLOSE)
ground.translate(0,-1,0)
vrut.starttimer(ANIMATE, .01)
# Handle all animations
if num == ANIMATE:
angle = angle - SPEED
tunnel.rotate(0,1,0, angle)
if key2 == 1:
red = (math.sin(angle/101)+1)/2
green = (math.sin(angle/83)+1)/2
blue = (math.sin(angle/59)+1)/2
if key3 == 1:
ground.rotate(0,1,0, -angle/3)
vrut.clearcolor(red, green, blue)
vrut.starttimer(ANIMATE, .01)
################################################################\
################################################################\
# One-time setup of callback events
vrut.callback(vrut.TIMER_EVENT, 'mytimer')
vrut.callback(vrut.KEYBOARD_EVENT, 'mykeyboard')
vrut.starttimer(INITIALIZE, .1)