CoCalc Public Fileswww / talks / 2006-05-09-sage-digipen / tutorial / modeling-shadow-1.py
Author: William A. Stein
1# Soya 3D tutorial
2# Copyright (C) 2001-2004 Jean-Baptiste LAMY
3#
4# This program is free software; you can redistribute it and/or modify
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
20
21# Shadows must be enabled both on the light that casts the shadow, and on the shape
22# that projects it.
23
24# The shadow algorithm assume that non-double sided faces are part of closed volumes, so
25# you should take this into account when designing your models.
26# Nothing is assumed for double sided faces, though, so in doubt, use double sided faces.
27
30
31
32# Imports and inits Soya (see lesson basic-1.py).
33
34import sys, os, os.path, soya, soya.cube
35
36soya.init()
37soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
38
39# Creates the scene.
40
41scene = soya.World()
42
44
45sword = soya.World.get("sword")
46
47# Creates a simple shape shapifier object. A shapifier is an object that says how a world
48# is compiled into a shape. When no shapifier is specified, Soya uses the default shapifier
49# (an instance of SimpleShapifier, that does not include shadows).
50
51shapifier = soya.SimpleShapifier()
52
53# Sets the 'shadow' shapifier property to true.
54
56
57# Assigns the shapifier to the sword.
58
59sword.shapifier = shapifier
60
61# Compiles the sword model to a shadowed shape. Notice that is you save the sword now,
62# the shapifier would be saved with it too, and thus you can use 'soya.Shape.get("sword")'.
63
64sword_shape = sword.shapify()
65
66# Create a rotating volume class, and a rotating volume.
67
68class RotatingVolume(soya.Volume):
69  def __init__(self, parent = None, shape = None):
70    soya.Volume.__init__(self, parent, shape)
71
74
75    self.rotate_lateral(proportion * 2.0)
76
77
78volume = RotatingVolume(scene, sword_shape)
79volume.set_xyz(1.0, -1.0, 0.0)
80volume.rotate_vertical(90.0)
81
83# Notice that the wall can be any object (including e.g. a landscape,...) : receiving
84# shadows doesn't require any particular property.
85
86wall_model = soya.World()
87wall_face = soya.Face(wall_model, [
88  soya.Vertex(wall_model, 0.0, -5.0, -5.0),
89  soya.Vertex(wall_model, 0.0, -5.0,  5.0),
90  soya.Vertex(wall_model, 0.0,  5.0,  5.0),
91  soya.Vertex(wall_model, 0.0,  5.0, -5.0),
92  ])
93wall_face.double_sided = 1
94wall = soya.Volume(scene, wall_model.shapify())
95wall.set_xyz(-1.0, 0.0, 0.0)
96
97# Creates a light.
98
99light = soya.Light(scene)
100light.set_xyz(5.0, 0.0, 0.0)
101
102# Sets the light's shadow color. Semitransparent color (alpha < 1.0) are often used.
103
104light.shadow_color = (0.0, 0.0, 0.0, 0.5)
105
106# Enables shadows for this light
107