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

« back to all changes in this revision

Viewing changes to tutorial/lesson-002.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
2
 
# Copyright (C) 2001-2002 Jean-Baptiste 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
 
# Lesson 002: Modeling : a moving pyramid
19
 
 
20
 
# This lesson is similar to the lesson 001, but it displays a pyramid instead of
21
 
# a cube. As there is no Soya primitive to draw a pyramid, we'll draw it by
22
 
# hand, with the functions and classes provided by the soya.model module.
23
 
# The pyramid is made of a quad base and 4 triangles.
24
 
 
25
 
# Import some Soya module.
26
 
 
27
 
import soya, soya.soya3d as soya3d, soya.model as model
28
 
 
29
 
# Initializes Soya (and creates the 3D window).
30
 
 
31
 
soya.init()
32
 
 
33
 
# Creates the scene.
34
 
 
35
 
scene = soya3d.World()
36
 
 
37
 
# Creates the World that will contain the whole pyramid. As previously stated, the
38
 
# pyramid is composed of 5 faces; this world is used to group those 5 faces
39
 
# (exactly like a Frame can be used in Tkinter to group different widgets).
40
 
# The pyramid's parent is the scene.
41
 
 
42
 
pyramid = soya3d.World(scene)
43
 
 
44
 
# Creates a material for the pyramid.
45
 
 
46
 
material = model.Material()
47
 
 
48
 
# Creates the first face, the quad base of the pyramid.
49
 
# The first argument of the Face constructor is the parent World (the pyramid),
50
 
# the second is the list of vertices and the third is the material.
51
 
# The number of vertices determines the Face's nature:
52
 
#  - 1: a point
53
 
#  - 2: a line
54
 
#  - 3: a triangle
55
 
#  - 4: a quad
56
 
#  - +: a polygon
57
 
 
58
 
# By default, the face is only visible from one side; which of the two sides
59
 
# depends on the order of the vertices. You can make the two sides visible
60
 
# with "face.double_sided = 1".
61
 
 
62
 
# The vertices is a list of Vertex object. The first argument of the constructor
63
 
# is (again) the parent, and the three following one are the x, y and z
64
 
# coordinates.
65
 
66
 
# As 3D object, Point, Vector or Vertex have a parent too, though they are not
67
 
# considered as "children". The parent is used for automatic coordinates
68
 
# conversion, which will be detailed in a later lesson. Here, the coordinates
69
 
# are defined in the pyramid coordinate system.
70
 
71
 
# The order of the vertices in the list determines which side of the face is
72
 
# visible (for triangles, quads and polygons); you can get both side visible
73
 
# by setting the "double_sided" attribute to true.
74
 
 
75
 
model.Face(pyramid, [model.Vertex(pyramid,  0.5, -0.5,  0.5),
76
 
                     model.Vertex(pyramid, -0.5, -0.5,  0.5),
77
 
                     model.Vertex(pyramid, -0.5, -0.5, -0.5),
78
 
                     model.Vertex(pyramid,  0.5, -0.5, -0.5),
79
 
                     ], material)
80
 
 
81
 
# Similarly, creates the 4 triangles.
82
 
 
83
 
model.Face(pyramid, [model.Vertex(pyramid, -0.5, -0.5,  0.5),
84
 
                     model.Vertex(pyramid,  0.5, -0.5,  0.5),
85
 
                     model.Vertex(pyramid,  0.0,  0.5,  0.0),
86
 
                     ], material)
87
 
 
88
 
model.Face(pyramid, [model.Vertex(pyramid,  0.5, -0.5, -0.5),
89
 
                     model.Vertex(pyramid, -0.5, -0.5, -0.5),
90
 
                     model.Vertex(pyramid,  0.0,  0.5,  0.0),
91
 
                     ], material)
92
 
 
93
 
model.Face(pyramid, [model.Vertex(pyramid,  0.5, -0.5,  0.5),
94
 
                     model.Vertex(pyramid,  0.5, -0.5, -0.5),
95
 
                     model.Vertex(pyramid,  0.0,  0.5,  0.0),
96
 
                     ], material)
97
 
 
98
 
model.Face(pyramid, [model.Vertex(pyramid, -0.5, -0.5, -0.5),
99
 
                     model.Vertex(pyramid, -0.5, -0.5,  0.5),
100
 
                     model.Vertex(pyramid,  0.0,  0.5,  0.0),
101
 
                     ], material)
102
 
 
103
 
# Creates and moves the light (see lesson 001).
104
 
 
105
 
light = soya3d.Light(scene)
106
 
light.set_xyz(1.0, 0.7, 1.0)
107
 
 
108
 
# Creates and moves the camera (see lesson 001).
109
 
 
110
 
camera = soya3d.Camera(scene)
111
 
camera.z = 2.0
112
 
soya.set_root_widget(camera)
113
 
 
114
 
 
115
 
def advance_time(proportion):
116
 
  pyramid.rotate_lateral(proportion * 10.0)
117
 
  pyramid.rotate_incline(proportion *  5.0)
118
 
  
119
 
pyramid.advance_time = advance_time
120
 
 
121
 
 
122
 
import time
123
 
 
124
 
while 1:
125
 
  soya.render()
126
 
  
127
 
  scene.begin_round()
128
 
  scene.advance_time()
129
 
  
130
 
  time.sleep(0.1)