File: cubes.py

package info (click to toggle)
glitch 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 488 kB
  • ctags: 649
  • sloc: python: 3,432; makefile: 2
file content (81 lines) | stat: -rw-r--r-- 1,918 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

import math

import gobject
import gtk

import glitch, glitch.gtk
from glitch.limbo.lights import LightSwitch, DiffuseLight
from glitch.limbo.material import Material
from glitch.limbo.objects import Cube

def tick():
    if paused:
        return True

    global time
    theta = (3 * time) % 360
    theta2 = (time) % 360
    time += 1

    rotate.angle = theta

    camera.eye[0] = 3 * math.sin(theta2 * (math.pi / 180))
    camera.eye[2] = 3 * math.cos(theta2 * (math.pi / 180))
    camera.refresh()
    return True

def key_press(w, ev):
    global paused
    paused = True

def key_release(w, ev):
    global paused
    paused = False

if __name__ == '__main__':
    """            camera
                    |
                  light
              /     |     \
        material material material
             |      |     |
           trans   trans  trans
              \     |     /
                  rotate
                    |
                   cube
"""

    cube = Cube()

    rotate = glitch.Rotate(y=1, children=[cube])

    (trans1, trans2, trans3) = (
        glitch.Translate(0, 0, math.sqrt(3)/2, children=[rotate]),
        glitch.Translate(-1, 0, -math.sqrt(3)/2, children=[rotate]),
        glitch.Translate(1, 0, -math.sqrt(3)/2, children=[rotate]))

    (mat1, mat2, mat3) = (
        Material(1, 0, 0, children=[trans1]),
        Material(0, 1, 0, children=[trans2]),
        Material(0, 0, 1, children=[trans3]))

    time = 0
    paused = False
    camera = glitch.gtk.GtkCamera(eye=[0, 1, 3], children=[
        LightSwitch(children=[
            DiffuseLight(y=2, children=[mat1, mat2, mat3])])])

    w = gtk.Window()
    w.add(camera)
    w.connect('key-press-event', key_press)
    w.connect('key-release-event', key_release)
    w.show_all()

    gobject.timeout_add(30, tick)

    if '__IP' not in dir():
        w.connect('destroy', lambda w: gtk.main_quit())
        gtk.main()