File: texture_and_lighting.py

package info (click to toggle)
python-visual 1%3A5.12-1.6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 7,708 kB
  • ctags: 7,635
  • sloc: cpp: 15,593; sh: 9,615; ansic: 6,631; python: 4,737; makefile: 384
file content (73 lines) | stat: -rw-r--r-- 3,248 bytes parent folder | download | duplicates (3)
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
from __future__ import division
from visual import *
# Bruce Sherwood, August 2006
# Demonstration of transparency (opacity), materials, and local lights in Visual 5

# Create a texture to apply to a sphere to make a beach ball
bands = zeros([16,16,4], float)
for i in range(len(bands)):
    for j in range(len(bands[0])):
        op = 1
        if i % 2 == 0: # every other band is partially transparent
            op = 0.3
            col = color.cyan
        else:
            # choose a color for an opaque band of the beach ball:
            col = [color.blue, color.green, color.red,
                   color.yellow, color.cyan][i//2 % 5]
        bands[i][j] = (col[0], col[1], col[2], op)
stripes = materials.texture(data = bands,
                       mapping = "spherical",
                       interpolate = False)

scene.width = scene.height = 800
scene.forward = (-0.2,-0.2,-1)
width = 10 # of wood table
thick = 0.5 # thickness of wood
depth = 7 # of wood table
height = 2 # of side bars of table
xhit = height-thick # x distance of center of ball from side bar when it hits 
R = 2 # radius of ball
H = 10 # height of underside of ceiling above floor
L = 5 # length of pendulum to center of hanging lamp
# top of floor is at y=0 for convenience
scene.visible = 0
floor = box(pos=(0,-thick/2,0), size=(width,thick,depth),
            shininess=0, color=color.orange, material=materials.wood)
left = box(pos=(-(width/2+thick/2),height/2-thick,0), size=(thick,height,depth),
            shininess=0, color=color.orange, material=materials.wood)
right = box(pos=(width/2+thick/2,height/2-thick,0), size=(thick,height,depth),
            shininess=0, color=color.orange, material=materials.wood)
back = box(pos=(0,height/2-thick,-(depth/2+thick/2)), size=(width+2*thick,height,thick),
            shininess=0, color=color.orange, material=materials.wood)
ceiling = box(pos=(0,H+thick/2,0), size=(width/10,thick,width/10), color=color.orange, material=materials.wood)
pendulum = frame(pos=(0,H,0), axis=(0,-1,0))
wire = curve(frame=pendulum, pos=[(0,0,0),(L,0,0)])
lamp = sphere(frame=pendulum, pos=(L,0,0), radius=0.03*L, color=color.white, material=materials.emissive)
sphere(pos=(0.1*width,R/4,0.45*depth), radius=R/4, color=color.red, material=materials.marble)
sphere(pos=(0.15*width,R/4,0.3*depth), radius=R/4, color=color.yellow, material=materials.marble)
sphere(pos=(0.15*width,R/4,-0.3*depth), radius=R/4, color=color.green, material=materials.marble)
sphere(pos=(0.1*width,R/4,-0.45*depth), radius=R/4, color=color.cyan, material=materials.marble)
scene.lights = []
scene.ambient = color.gray(0.25)
l1 = distant_light(direction=(6,2,4), color=color.gray(0.3))
l2 = distant_light(direction=(-10,2,4), color=color.gray(0.2))
lamplight = local_light(frame=pendulum, pos=(L,0,0), color=color.gray(0.5))
scene.center = (0,0.4*H,0)

ball = sphere(pos=(width/4,R,0), radius=R, up=(0,1,1), material=stripes)
xlimit = 0.5*width-R*sin(acos(1-(height-thick)/R))
v = vector(-0.5,0,0)
dt = 0.03
t = 0
scene.visible = 1

while 1:
    rate(100)
    ball.pos += v*dt
    ball.rotate(axis=(0,0,1), angle=-v.x*dt/R)
    if abs(ball.x) >= xlimit:
        v = -v
    angle = 0.02*cos(t)
    pendulum.rotate(axis=(1,0,0), angle=angle)
    t += dt