~jtaylor/ubuntu/oneiric/soya/fix-780305

« back to all changes in this revision

Viewing changes to tutorial/lesson-110.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Dequènes (Duck)
  • Date: 2005-01-30 09:55:06 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050130095506-f21p6v6cgaobhn5j
Tags: 0.9.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Soya 3D tutorial
2
 
# Copyright (C) 2001-2002 Bertrand 'blam!' LAMY
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
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
 
 
19
 
# ---------------------------------------------------
20
 
# Lesson 110: Shape and visibility computation (Tree)
21
 
# ---------------------------------------------------
22
 
 
23
 
# We will see in this tutorial some technics to speed up rendering
24
 
# of big Shapes (like the Shape of a level)
25
 
 
26
 
import random, time
27
 
 
28
 
import soya
29
 
import soya.model
30
 
import soya.soya3d
31
 
 
32
 
 
33
 
soya.init()
34
 
 
35
 
 
36
 
# Create a world
37
 
scene = soya.soya3d.World()
38
 
 
39
 
# Add a light
40
 
light = soya.soya3d.Light(scene)
41
 
light.set_xyz(0.0, 10.0, 0.0)
42
 
 
43
 
# Create a world and add some randomized triangles
44
 
world = soya.soya3d.World()
45
 
i = 0
46
 
while (i < 100):
47
 
  x = random.random() * 10.0
48
 
  y = 0.0
49
 
  z = random.random() * 10.0
50
 
  v1 = soya.model.Vertex (world, x + random.random() * 2.0, y + random.random() * 2.0, z + random.random() * 2.0)
51
 
  v2 = soya.model.Vertex (world, x + random.random() * 2.0, y + random.random() * 2.0, z + random.random() * 2.0)
52
 
  v3 = soya.model.Vertex (world, x + random.random() * 2.0, y + random.random() * 2.0, z + random.random() * 2.0)
53
 
  f = soya.model.Face (world, [v1, v2, v3])
54
 
  f.double_sided = 1
55
 
  i = i + 1
56
 
 
57
 
# Create the Shape from the world
58
 
shape = world.shapify()
59
 
 
60
 
 
61
 
# Tree construction
62
 
# -----------------
63
 
#   Construct a sphere tree with all the faces of the shape.
64
 
 
65
 
shape._build_tree()
66
 
 
67
 
 
68
 
# Tree optimization
69
 
# -----------------
70
 
#   Optimize the tree (try to have a more balanced tree).
71
 
#   For now there are 2 modes of optimization.
72
 
 
73
 
collapse = 0.9
74
 
#   This value is used by all the mode of optimization. It means that the
75
 
#   children sphere must have a radius inferior than the parent radius multiplied
76
 
#   by collapse else the child and parent are gathered.
77
 
 
78
 
# Mode 0: fast. Take 1 parameter: the maximum children radius expressed in percentage
79
 
#   of the parent radius.
80
 
max_children_radius_percent = 0.5
81
 
shape._optimize_tree(collapse, 0, max_children_radius_percent)
82
 
 
83
 
# Mode 0: slow but compute the best possible tree. Take no parameter.
84
 
shape._optimize_tree(collapse, 1)
85
 
 
86
 
 
87
 
scene.set_shape(shape)
88
 
 
89
 
 
90
 
# If you always want to build a tree when your World is turned to Shape, you can
91
 
# use the shapify_args:
92
 
world.shapify_args=('tree', { "collapse":0.9, "mode":0, "params":[0.5] })
93
 
shape = world.shapify()
94
 
# This shape will have a Tree
95
 
scene.set_shape(shape)
96
 
 
97
 
 
98
 
# Add a camera and a loop to render
99
 
camera = soya.soya3d.Camera(scene)
100
 
camera.set_xyz(10.0, 2.0, 10.0)
101
 
camera.look_at(soya.soya3d.Point(scene, 0.0, 0.0, 0.0))
102
 
soya.set_root_widget(camera)
103
 
 
104
 
while(1):
105
 
  soya.render()
106
 
  time.sleep(0.1)
107