Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / ray-1.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
# ray-1: Ray : the light sword
20
21
# This lesson is a light sword, which can be controlled with the mouse.
22
23
import sys, os, os.path, soya, soya.cube, soya.ray, soya.widget as widget, soya.sdlconst
24
25
soya.init()
26
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
27
28
# Creates the scene.
29
30
scene = soya.World()
31
32
sword = soya.World(scene)
33
sword.set_shape(soya.Shape.get("sword"))
34
sword.y = -1.5
35
36
# Adds a light, and a camera
37
38
light = soya.Light(scene)
39
light.set_xyz(0.0, 0.2, 1.0)
40
41
camera = soya.Camera(scene)
42
camera.set_xyz(0.0, -0.5, 2.0)
43
soya.set_root_widget(camera)
44
45
# Creates the material for the ray.
46
47
material = soya.Material()
48
material.shininess = 0.5
49
material.diffuse = (0.6, 0.5, 0.7, 1.0)
50
material.additive_blending = 1 # Additive blending istypically used for FX
51
52
# Creates the ray.
53
54
ray = soya.ray.Ray(sword, length = 20)
55
ray.z = -0.2
56
ray.endpoint = soya.Point(sword, 0.0, 0.0, -1.95)
57
ray.material = material
58
59
# Hide the mouse cursor
60
61
soya.cursor_set_visible(0)
62
63
64
65
def begin_round():
66
# Processes the events
67
68
# soya.cursor.coalesce_motion_event() removes all mouse motion events, except
69
# the last one -- we use it for speed gain, since many mouse motion event
70
# often occur at the same time.
71
72
for event in soya.process_event():
73
if event[0] == soya.sdlconst.MOUSEMOTION:
74
75
# For mouse motion event, rotate the laser (quite) toward the mouse.
76
# The formulas are empirical; see soya.cursor for a better algorithm
77
# if you want to translate mouse positions into 3D coordinates.
78
79
mouse = soya.Point(
80
scene,
81
(float(event[1]) / camera.get_screen_width () - 0.5) * 2.0,
82
(float(event[2]) / camera.get_screen_height() - 0.5) * -4.0,
83
(float(event[2]) / camera.get_screen_height() - 0.5) * -1.0 + 0.25,
84
)
85
sword.look_at(mouse)
86
87
sword.begin_round = begin_round
88
89
# Main loop
90
91
soya.Idler(scene).idle()
92
93
94
# TODO (left as an exercice):
95
# Turn this tutorial lesson into a full Doom-like game!
96