Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / modeling-transparency-1.pyOpen in CoCalc
Author: William A. Stein
1
# Soya 3D tutorial
2
# Copyright (C) 2004 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
19
# modeling-transparency-1: Transparency : semi-transparent cubes
20
21
# In this lesson, we'll build a semi-transparent cube.
22
23
24
# Imports and inits Soya.
25
26
import sys, os, os.path, soya, soya.cube
27
28
soya.init()
29
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
30
31
# Creates the scene.
32
33
scene = soya.World()
34
35
# Creates a material, and set its diffuse color to a semi-transparent white (alpha = 0.5).
36
# See lesson modeling-material-1.py about materials and material colors.
37
# Similarly, you can use per-vertex semi-transparent colors.
38
39
material = soya.Material()
40
material.diffuse = (1.0, 1.0, 1.0, 0.5)
41
42
# Creates a cube using our material.
43
44
cube_world = soya.cube.Cube(material = material)
45
46
# For optimization pupose, the cube module creates one-sided faces, i.e. only the exterior
47
# side of the faces are drawn, and not the interior side. Here, as the cube is
48
# semi-transparent, the interior side becomes visible and so we need to draw both sides.
49
# This is done by setting the face's double_sided attribute to 1.
50
51
for face in cube_world.children:
52
face.double_sided = 1
53
54
# Compile the cube model.
55
56
cube_shape = cube_world.shapify()
57
58
# Creates several semi-transparent cubes.
59
60
cube1 = soya.Volume(scene, cube_shape)
61
cube1.set_xyz(-0.3, 0.3, 0.0)
62
cube1.rotate_lateral(30.0)
63
64
cube2 = soya.Volume(scene, cube_shape)
65
cube2.set_xyz(0.0, 0.0, -1.5)
66
cube2.rotate_lateral(30.0)
67
68
cube3 = soya.Volume(scene, cube_shape)
69
cube3.set_xyz(0.6, -0.4, 0.5)
70
cube3.rotate_lateral(30.0)
71
72
# Creates a light.
73
74
light = soya.Light(scene)
75
light.set_xyz(1.0, 0.7, 1.0)
76
77
# Creates a camera.
78
79
camera = soya.Camera(scene)
80
camera.set_xyz(0.0, 0.0, 3.0)
81
soya.set_root_widget(camera)
82
83
soya.Idler(scene).idle()
84
85