Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / land-2.pyOpen in CoCalc
Author: William A. Stein
1
# Soya 3D tutorial
2
# Copyright (C) 2004 Jean-Baptiste 'Jiba' LAMY
3
# Copyright (C) 2001-2002 Bertrand 'blam!' LAMY
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19
20
# land-2: Landscape : script-generated landscape
21
22
# You create a landscape from a bitmap image, but also by giving the height of each
23
# vertex individually. In this lesson, learn how!
24
25
26
# This lesson shows how to make a landscape (also known as heightmap or terrain)
27
28
29
# Imports and inits Soya (see lesson basic-1.py).
30
31
import sys, os, os.path, random, soya, soya.sdlconst
32
33
soya.init()
34
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
35
36
# Creates the scene.
37
38
scene = soya.World()
39
40
# Creates a new landscape in the scene. land_size is the dimension of the landscape ;
41
# it must be of the form (2 ** n) + 1.
42
43
#land_size = 33
44
land_size = 2**6+1
45
land = soya.Land(scene, land_size, land_size)
46
47
# Sets a random value for each height.
48
# Other vertex-setting methods include:
49
# - Land.set_material (i, j, material)
50
# - Land.set_vertex_color (i, j, color) where color is a (red, green, blue, alpha) tuple
51
# - Land.set_vertex_option(i, j, hidden, invisible, non_solid, force_presence)
52
53
import math
54
55
for i in range(land_size):
56
for j in range(land_size):
57
#land.set_height(i, j, random.random())
58
land.set_height(i, j, math.sin(float(i)/10)*math.cos(float(j)/10))
59
60
# Multiplies all the heights by 4
61
62
land.multiply_height(4.0)
63
64
# Adds a light.
65
66
light = soya.Light(scene)
67
light.set_xyz(0.0, 15.0, 0.0)
68
69
# Add a camera and a loop to render
70
71
class MovableCamera(soya.Camera):
72
def __init__(self, parent):
73
soya.Camera.__init__(self, parent)
74
75
self.speed = soya.Vector(self)
76
self.rotation_lateral_speed = 0.0
77
self.rotation_vertical_speed = 0.0
78
79
def begin_round(self):
80
soya.Camera.begin_round(self)
81
82
for event in soya.process_event():
83
if event[0] == soya.sdlconst.KEYDOWN:
84
if event[1] == soya.sdlconst.K_UP: self.speed.z = -1.0
85
elif event[1] == soya.sdlconst.K_DOWN: self.speed.z = 1.0
86
elif event[1] == soya.sdlconst.K_LEFT: self.rotation_lateral_speed = 10.0
87
elif event[1] == soya.sdlconst.K_RIGHT: self.rotation_lateral_speed = -10.0
88
elif event[1] == soya.sdlconst.K_q: soya.IDLER.stop()
89
elif event[1] == soya.sdlconst.K_ESCAPE: soya.IDLER.stop()
90
if event[0] == soya.sdlconst.KEYUP:
91
if event[1] == soya.sdlconst.K_UP: self.speed.z = 0.0
92
elif event[1] == soya.sdlconst.K_DOWN: self.speed.z = 0.0
93
elif event[1] == soya.sdlconst.K_LEFT: self.rotation_lateral_speed = 0.0
94
elif event[1] == soya.sdlconst.K_RIGHT: self.rotation_lateral_speed = 0.0
95
96
def advance_time(self, proportion):
97
self.add_mul_vector(proportion, self.speed)
98
self.turn_lateral (self.rotation_lateral_speed * proportion)
99
self.turn_vertical(self.rotation_vertical_speed * proportion)
100
101
102
camera = MovableCamera(scene)
103
camera.set_xyz(16.0, 6.0, 0.0)
104
camera.look_at(soya.Point(scene, 16.0, 6.0, 10.0))
105
soya.set_root_widget(camera)
106
107
soya.Idler(scene).idle()
108