Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / modeling-material-2.pyOpen in CoCalc
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
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-material-2: Texture (skin) : textured cube and pyramid
20
21
# In addition to colors and shininess, materials can have a texture.
22
# Textures are used to put a bitmap image on the surface of a 3D object.
23
# It is sometimes called "skin" by graphists ; in Blender it corresponds
24
# to the UV texture map.
25
26
# Soya image loading is done through the PIL (Python Imaging Library), so you need the PIL.
27
# When the material is saved, the raw image data are included in the material file and so
28
# the final user may not need the PIL.
29
30
31
# Imports and inits Soya.
32
33
import sys, os, os.path, soya, soya.cube
34
35
soya.init()
36
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
37
38
# Creates the scene.
39
40
scene = soya.World()
41
42
# Creates the material
43
44
material = soya.Material()
45
46
# Sets the material's texture. The texture must be a soya.Image object, or None for no
47
# texture (the default). Texture images dimensions MUST BE POWER OF 2
48
# (e.g. 64x64, 128x256,...).
49
# Here we use soya.Image.get(filename) to load the image tutorials/data/images/block2.png.
50
51
# You can also use soya.open_image(filename) for loading an image from any file,
52
# or soya.image_from_pil(PIL.Image.Image) to turn a PIL image into a Soya image.
53
54
material.texture = soya.Image.get("block2.png")
55
56
# Creates the World that will contain the pyramid. We don't create the pyramid in the
57
# scene since we are going to compile the pyramid into a shape.
58
59
pyramid_world = soya.World()
60
61
# Creates the 5 faces of the pyramid (see lesson modeling-1).
62
# The third argument of the face constructor is the material ; it defaults to
63
# soya.DEFAULT_MATERIAL (a white material).
64
65
# For each vertex, we specify the texture coordinates (sometimes called u,v) as the
66
# 5th and 6th arguments of the constructor. They correspond to the tex_x and tex_y
67
# attributes of the vertex.
68
69
soya.Face(pyramid_world, [soya.Vertex(pyramid_world, 0.5, -0.5, 0.5, 0.0, 0.0),
70
soya.Vertex(pyramid_world, -0.5, -0.5, 0.5, 0.0, 1.0),
71
soya.Vertex(pyramid_world, -0.5, -0.5, -0.5, 1.0, 1.0),
72
soya.Vertex(pyramid_world, 0.5, -0.5, -0.5, 1.0, 0.0),
73
], material)
74
75
soya.Face(pyramid_world, [soya.Vertex(pyramid_world, -0.5, -0.5, 0.5, 0.0, 1.0),
76
soya.Vertex(pyramid_world, 0.5, -0.5, 0.5, 0.0, 0.0),
77
soya.Vertex(pyramid_world, 0.0, 0.5, 0.0, 1.0, 0.0),
78
], material)
79
80
soya.Face(pyramid_world, [soya.Vertex(pyramid_world, 0.5, -0.5, -0.5, 1.0, 0.0),
81
soya.Vertex(pyramid_world, -0.5, -0.5, -0.5, 1.0, 1.0),
82
soya.Vertex(pyramid_world, 0.0, 0.5, 0.0, 0.0, 1.0),
83
], material)
84
85
soya.Face(pyramid_world, [soya.Vertex(pyramid_world, 0.5, -0.5, 0.5, 0.0, 0.0),
86
soya.Vertex(pyramid_world, 0.5, -0.5, -0.5, 1.0, 0.0),
87
soya.Vertex(pyramid_world, 0.0, 0.5, 0.0, 0.0, 1.0),
88
], material)
89
90
soya.Face(pyramid_world, [soya.Vertex(pyramid_world, -0.5, -0.5, -0.5, 1.0, 1.0),
91
soya.Vertex(pyramid_world, -0.5, -0.5, 0.5, 0.0, 1.0),
92
soya.Vertex(pyramid_world, 0.0, 0.5, 0.0, 1.0, 0.0),
93
], material)
94
95
# Creates a cube.
96
# soya.cube generates texture coordinates automatically.
97
98
cube_world = soya.cube.Cube(None, material)
99
100
# Creates a subclass of Volume that permanently rotates.
101
# See the timemanagement-* lesson series for more info.
102
103
class RotatingVolume(soya.Volume):
104
def advance_time(self, proportion):
105
pass
106
#self.rotate_lateral(2.0 * proportion)
107
108
# Create a rotating volume in the scene, using the cube shape.
109
110
cube = RotatingVolume(scene, cube_world.shapify())
111
cube.x = -1.0
112
cube.rotate_vertical(30.0)
113
114
# Create a rotating volume in the scene, using the pyramid shape.
115
116
pyramid = RotatingVolume(scene, pyramid_world.shapify())
117
pyramid.x = 1.0
118
pyramid.rotate_vertical(30.0)
119
120
# Creates a light.
121
122
light = soya.Light(scene)
123
light.set_xyz(0.5, 1.0, 2.0)
124
125
# Creates a camera.
126
127
camera = soya.Camera(scene)
128
camera.set_xyz(0.0, 0.0, 3.0)
129
camera.set_xyz(-1.0, 0.0, 1.4)
130
soya.set_root_widget(camera)
131
132
soya.Idler(scene).idle()
133
134