import pygtk pygtk.require('2.0') import os import gobject os.environ['PYGTK_USE_GIL_STATE_API'] = '' gobject.threads_init() from Togra import * from TograConstants import * from TograUtil import * from cube_gst_new import * import sys from togra import TograLogo import Image import math config = { 'file' : [], 'option' : [] } def readConfig (fname): f = file(fname) for l in f.readlines(): try: s = l.split ('#', 1)[0] s = s.split (' ', 1) if len (s) < 2: continue if s[0] not in config.keys(): config[s[0]] = [] config[s[0]].append (s[1].strip()) except: print "Error reading config file line: ", l raise GL_QUADS = 7 # for program.py, read config file named program.config #readConfig (sys.argv[0].rsplit ('.py', 1)[0] + '.config') readConfig(sys.argv[0][:-3] + '.config') if 'FULLSCREEN' in config['option']: scene = init(1) else: scene = init(0) if 'LOGO' in config['option']: path = [ [Vector(0,0,0), Vector(-0.5,-0.5,0), 0], [Vector(-1,-1,0), Vector(-0.5,-0.5,0), 300], [Vector(-20,15,5), Vector(0,10,10), 1000], [Vector(0, 25, 20), Vector(5,-2,-2), 2000], [Vector(20, 20, 0), Vector(0,0,-5), 3000], [Vector(0, 15, -20), Vector(-5,-2,-2), 4000], [Vector(-20,25,-5), Vector(0,10,10), 5000], [Vector(1,41,0), Vector(-0.5,-0.5,0), 5700], [Vector(0,40,0), Vector(-0.5,-0.5,0), 6000] ] path2 = [ [Vector(0,0,0), Vector(-0.5,-0.5,0), 0], [Vector(-1,-1,0), Vector(-0.5,-0.5,0), 300], [Vector(-20,-15,5), Vector(0,-10,10), 1000], [Vector(0, -25, 20), Vector(5,2,-2), 2000], [Vector(20, -20, 0), Vector(0,0,-5), 3000], [Vector(0, -15, -20), Vector(-5,2,-2), 4000], [Vector(-20,-25,-5), Vector(0,-10,10), 5000], [Vector(1,-39,0), Vector(-0.5,-0.5,0), 5700], [Vector(0,-40,0), Vector(-0.5,-0.5,0), 6000] ] curPath = path def keyPressed(key): global logo, path, curPath, path2 if key == ord('m'): if not logo.move(curPath): return if path == curPath: curPath = path2 else: curPath = path elif key == ord('d'): gst_dump() pauseQuitKeystrokes(scene, keyPressed) else: pauseQuitKeystrokes(scene) panelList = VectorList(4) panelList.append(Vector(0,-10,-10)) panelList.append(Vector(0,10,-10)) panelList.append(Vector(0,10,10)) panelList.append(Vector(0,-10,10)) panelNormals = VectorList(4) for i in range(4): panelNormals.append(Vector(1, 0, 0)) panelTexture = FloatList(8) panelTexture.append(0.01) panelTexture.append(0.99) panelTexture.append(0.99) panelTexture.append(0.99) panelTexture.append(0.99) panelTexture.append(0.01) panelTexture.append(0.01) panelTexture.append(0.01) cubeContainer = Container() translations = [Vector(10, 0, 0), Vector(0, 10, 0), Vector(-10, 0, 0), Vector(0, -10, 0), Vector(0, 0, 10), Vector(0, 0, -10)] rotations = [Vector(1, 0, 0), Vector(0, 0, 1), Vector(0, 0, 1), Vector(0, 0, 1), Vector(0, 1, 0), Vector(0, 1, 0)] rAmts = [0, 90, 180, 270, 270, 90] #colours = [(1, 0.5, 0.5, 1), (0.5, 1, 0.5, 1), (0.5, 0.5, 1, 1), # (1, 1, 0.5, 1), (1, 0, 0, 1), (0, 0, 1, 1)] colours = [(1, 1, 1, 1), (1, 1, 1, 1), (1, 1, 1, 1), (1, 1, 1, 1), (1, 0, 0, 1), (0, 0, 1, 1)] objects = [] textures = [] c = Container() c.transformContainer.append(Transform (scale=Vector(1.265,1.265,1.265))) fnames = config['file'] sr = StaticRender(panelList, GL_QUADS, normals=panelNormals, textureCoords=panelTexture) if 'sound' in config.keys(): ttextures = get_texture_objects(fnames, config['sound'][0]) else: ttextures = get_texture_objects(fnames, None) for i in range(len(fnames)): if fnames[i] == "": panel = Container(1) panel.append(sr) panelMaterial = Material(ambient=colours[i]) panel.setMaterial(panelMaterial) panel.transformContainer.append( Transform(translate=translations[i], rotateAxis=rotations[i], rotateAmount=rAmts[i])) cubeContainer.append(panel) continue panel = Container(1) panel.append(sr) objects.append(sr) panelMaterial = Material(ambient=colours[i]) panelMaterial.setTexture(ttextures[i]) textures.append(panelMaterial) panel.setMaterial(panelMaterial) panel.transformContainer.append(Transform(translate=translations[i], rotateAxis=rotations[i], rotateAmount=rAmts[i])) c.append(panel) cubeContainer.append(c) buttons = Container() translations = [Vector(0, -20, 20), Vector(0, 20, 20), Vector(0, -20, -20), Vector(0, 20, -20)] for i in range (min (4, len(fnames))): if fnames[i] == "": continue c = Container() c.append(objects[i]) c.transformContainer.append(Transform(translate=translations[i], scale=Vector(1,0.2,0.2))) c.setMaterial(textures[i]) buttons.append(c) buttons.transformContainer.append( Transform(rotateAxis=Vector(0,0,1), rotateAmount=180, translate=Vector(-1,0,0))) scene.view_from = Vector(-40,0,0) scene.newLight(Vector(-40, 0, 0), (1, 1, 1, 1)) scene.baseContainer.append(cubeContainer) scene.baseContainer.append(buttons) def calcGain (angle): A = math.sin (math.pi * angle / 180.0) if A == 0: return A dB = 10.0 * math.log (A) / math.log (2) g = math.pow (10, dB / 20) return g to_small = 1 to_big = 3 fixed = 0 i = 0 # Number of steps in a full rotation - multiple of # 4 steps = 32 # Number of steps per face of the cube q_step = steps/4 # Array of volumes gains = [] for g in range(1, steps/2 + 1): gains.append (calcGain (g * 360 / steps)) def messageFunc(list): global crp, to_small, to_big, logo, i global ttextures if 'LOGO' in config['option']: if list[0][2] is not None: list[0][2](*list[0][3:]) return if list[0][0] == 1: crp = None return if fixed: return # Every 'q_step'th notification is a new face spinning # in or out of view, so change texture size if i % q_step == 0: change_res(ttextures[to_small], 64) to_small += 1 to_small %= 4 change_res(ttextures[to_big], 256) to_big += 1 to_big %= 4 inter = (to_small + 1) % 4 # 'to_small' is fading out in "q_step" steps to 0 # 'inter' is fading in from 0 to 1.0 pos = i % q_step change_vol(ttextures[inter], gains[pos]) change_vol(ttextures[to_small], gains[pos + q_step]) i += 1 scene.messageFunction = messageFunc ROTATE_PERIOD = 20 * 1000 crp = rotatePath(Vector(0, 0, -1), ROTATE_PERIOD) handle = cubeContainer.transformContainer.append(crp) crp.setNotificationInterval(handle, 0, ROTATE_PERIOD / steps) scene.baseContainer.transformContainer.append( Transform(rotateAxis=Vector(1,0,0), rotateAmount=-90)) scene.setBackgroundColour(0,0,0) old_offset = 0 lastTarget = -1 def mouseFunc(*args): global crp, old_offset, lastTarget, to_big, to_small, fixed, i if (args[1] == 1): # Ignore mouse down return target = scene.pick(args[2], args[3]) targetNumber = -1 if len(target) > 0: for i in range(4): if target[0][2] == buttons[i]: targetNumber = i break if targetNumber == lastTarget: return globalTime = scene.getTime() if crp == None: offset = old_offset else: tcTime = crp.getBaseTime() offset = (globalTime - tcTime) % ROTATE_PERIOD offset /= float(ROTATE_PERIOD) offset *= 360 # in degrees offset += old_offset if targetNumber == -1: old_offset = offset destination_offset = offset + 360 period = ROTATE_PERIOD else: destination_offset = 90 * (targetNumber - 2) old_offset = destination_offset while destination_offset <= offset: destination_offset += 360 if destination_offset - offset < 180: period = int(1000.0 / 360 * (destination_offset - offset)) else: destination_offset -= 360 period = int(1000.0 / 360 * (offset - destination_offset)) rp = InterpolatedPath(2) rp.setType(2) rp.append((Vector(0,0,-1), offset), 0) rp.append((Vector(0,0,-1), destination_offset), period) rp.setMaxIndex(period) if targetNumber == -1: cubeContainer.transformContainer[0] = rp h = cubeContainer.transformContainer.getItemHandle(0) rp.setNotificationInterval(h, 0, ROTATE_PERIOD / steps) to_big = (lastTarget + 1) % 4 to_small = (lastTarget + 3) % 4 change_res(ttextures[lastTarget], 256) #change_vol(ttextures[lastTarget], 0.85) fixed = 0 i = 0 else: cubeContainer.transformContainer[0] = (rp) h = cubeContainer.transformContainer.getItemHandle(0) rp.setIterationScheme(h, 1) for i in range(4): if i == targetNumber: change_res(ttextures[i], 512) change_vol(ttextures[i], 1.0) else: change_res(ttextures[i], 64) change_vol(ttextures[i], 0) fixed = 1 crp = rp lastTarget = targetNumber scene.mouseFunction = mouseFunc if 'LOGO' in config['option']: logo = TograLogo(0.5, 10) c = logo.getContainer() c.transformContainer.append(Transform(translate=Vector(0,-20,0))) scene.baseContainer.append(c) GL_RGBA = 0x1908 if 'LCALOGO' in config['option']: im = Image.open("lca.png") im = im.convert("RGBA") w, h = im.size lca_logo = Logo(im.tostring(), 0, 0, w, h, GL_RGBA) lca_logo.setPositioning(TOP | HORIZONTAL_CENTER) scene.baseContainer.append(lca_logo) if 'TOGRALOGO' in config['option']: im = Image.open("togra3.png") im = im.convert("RGBA") w, h = im.size tog_logo = Logo(im.tostring(), 0, 0, w, h, GL_RGBA) tog_logo.setPositioning(BOTTOM | HORIZONTAL_CENTER) scene.baseContainer.append(tog_logo) scene.run()