File: mesh.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 (92 lines) | stat: -rw-r--r-- 2,627 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
82
83
84
85
86
87
88
89
90
91
92

from math import sqrt, pi, sin, cos

import glitch, glitch.glut
from glitch.limbo.lights import LightSwitch, AmbientLight, DiffuseLight
from glitch.limbo.objects import Grid
from glitch.limbo.material import Material
from glitch.limbo.mesh import Mesh
from glitch.limbo.spread import Spread

def make_tetrahedron():
    r3 = sqrt(3)
    mesh = Mesh()
    mesh.vertices = [(0, 0, 0), (0.5, 0, r3/2), (1, 0, 0), (0.5, r3/2, r3/6)]
    mesh.faces = [(0, 2, 1), (1, 3, 0), (0, 3, 2), (1, 2, 3)]
    return mesh

def make_cube():
    mesh = Mesh()
    mesh.vertices = [
        (0., 0., 0.), (0., 0., 1.), (0., 1., 0.), (0., 1., 1.),
        (1., 0., 0.), (1., 0., 1.), (1., 1., 0.), (1., 1., 1.)]
    mesh.faces = [
        (1, 3, 2), (2, 0, 1), # x = 0
        (4, 6, 7), (7, 5, 4), # x = 1
        (0, 4, 5), (5, 1, 0), # y = 0
        (7, 6, 2), (2, 3, 7), # y = 1
        (0, 2, 6), (6, 4, 0), # z = 0
        (5, 7, 3), (3, 1, 5), # z = 1
        ]
    return mesh

def make_sphere():
    # Number of slices.
    m = 20
    # Number of wedges.
    n = 10
    r = 0.5

    vertices = []
    faces = []

    for i in xrange(1, n):
        s = i * pi / n
        r_ = r * sin(s)
        vertices.extend([
            (r + r_ * sin(t * 2 * pi / m),
             r * (1 - cos(s)),
             r + r_ * cos(t * 2 * pi / m))
                for t in xrange(m)])

    for i in xrange(n - 2):
        for j in xrange(m):
            faces.append(
                (m * i + j,
                 m * i + (j + 1) % m,
                 m * (i + 1) + j))
            faces.append(
                (m * i + (j + 1) % m,
                 m * (i + 1) + (j + 1) % m,
                 m * (i + 1) + j))

    l = len(vertices)
    p0 = (0.5, 0, 0.5)
    p1 = (0.5, 1, 0.5)
    vertices.extend([p0, p1])

    for i in xrange(m):
        faces.append((l, (i + 1) % m, i))
        faces.append((l + 1, l - m + i, l - m + (i + 1) % m))

    mesh = Mesh()
    mesh.vertices = vertices
    mesh.faces = faces
    return mesh

if __name__ == '__main__':
    mesh1 = make_tetrahedron()
    mesh2 = make_cube()
    mesh3 = make_sphere()

    camera = glitch.glut.GLUTCamera(eye=[0, 4, 5], ref=[2.5, 0, 2.5], children=[
        glitch.Rotate(90, x=3, children=[
            glitch.Scale(5, 5, 1, children=[Grid()])]),
        LightSwitch(children=[
            AmbientLight(intensity=0.05, x=-1, z=4, y=2, children=[
                DiffuseLight(x=-1, z=4, y=2, children=[
                    Material(r=0.9, g=0.5, b=0.5, children=[
                        Spread(x=2, z=2, children=[mesh1, mesh2, mesh3])
                    ])])])])])
    camera.run()