My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# -*- coding: utf-8 -*-
# Copyright (C) 2012, Almar Klein
#
# Visvis is distributed under the terms of the (new) BSD License.
# The full license can be found in 'license.txt'.

""" Module light

Defines a light source to light polygonal surfaces. Each axes has up
to eight lights associated with it.

"""

import OpenGL.GL as gl

from visvis.core import misc
from visvis.core.misc import PropWithDraw, DrawAfter, basestring


def _testColor(value, canBeScalar=True):
""" _testColor(value, canBeScalar=True)

Tests a color whether it is a sequence of 3 or 4 values.
It returns a 4 element tuple or raises an error if the suplied
data is incorrect.

"""

# Deal with named colors
if isinstance(value, basestring):
value = misc.getColor(value)

# Value can be a scalar
if canBeScalar and isinstance(value, (int, float)):
if value <= 0:
value = 0.0
if value >= 1:
value = 1.0
return value

# Otherwise it must be a sequence of 3 or 4 elements
elif not hasattr(value, '__len__'):
raise ValueError("Given value can not represent a color.")
elif len(value) == 4:
return (value[0], value[1], value[2], value[3])
elif len(value) == 3:
return (value[0], value[1], value[2], 1.0)
else:
raise ValueError("Given value can not represent a color.")


def _getColor(color, ref):
""" _getColor(color, reference)

Get the real color as a 4 element tuple, using the reference
color if the given color is a scalar.

"""
if isinstance(color, float):
return (color*ref[0], color*ref[1], color*ref[2], ref[3])
else:
return color



# todo: implement spot light and attenuation
class Light(object):
""" Light(axes, index)

A Light object represents a light source in the scene. It
determines how lit objects (such as Mesh objects) are visualized.

Each axes has 8 light sources, of which the 0th is turned on
by default. De 0th light source provides the ambient light in the
scene (the ambient component is 0 by default for the other light
sources). Obtain the lights using the axes.light0 and axes.lights
properties.

The 0th light source is a directional camera light by default; it
shines in the direction in which you look. The other lights are
oriented at the origin by default.

"""

def __init__(self, axes, index):

# Store axes and index of the light (OpenGl can handle up to 8 lights)
self._axes = axes.GetWeakref()
self._index = index
self._on = False

# The three light properties
self._color = (1, 1, 1, 1)
self._ambient = 0.0
self._diffuse = 1.0
self._specular = 1.0

# The main light has an ambien component by default
if index == 0:
self._ambient = 0.2

# Position or direction
if index == 0:
self._position = (0,0,1,0)
self._camLight = True
else:
self._position = (0,0,0,1)
self._camLight = False


def Draw(self):
# Draw axes
axes = self._axes()
if axes:
axes.Draw()

@PropWithDraw
def color():
""" Get/Set the reference color of the light. If the ambient,
diffuse or specular properties specify a scalar, that scalar
represents the fraction of *this* color.
"""
def fget(self):
return self._color
def fset(self, value):
self._color = _testColor(value, True)
return locals()


@PropWithDraw
def ambient():
""" Get/Set the ambient color of the light. This is the color
that is everywhere, coming from all directions, independent of
the light position.

The value can be a 3- or 4-element tuple, a character in
"rgbycmkw", or a scalar between 0 and 1 that indicates the
fraction of the reference color.
"""
def fget(self):
return self._ambient
def fset(self, value):
self._ambient = _testColor(value)
return locals()


@PropWithDraw
def diffuse():
""" Get/Set the diffuse color of the light. This component is the
light that comes from one direction, so it's brighter if it comes
squarely down on a surface than if it barely glances off the
surface. It depends on the light position how a material is lit.
"""
def fget(self):
return self._diffuse
def fset(self, value):
self._diffuse = _testColor(value)
return locals()


@PropWithDraw
def specular():
""" Get/Set the specular color of the light. This component
represents the light that comes from the light source and bounces
off a surface in a particular direction. This is what makes
materials appear shiny.

The value can be a 3- or 4-element tuple, a character in
"rgbycmkw", or a scalar between 0 and 1 that indicates the
fraction of the reference color.
"""
def fget(self):
return self._specular
def fset(self, value):
self._specular = _testColor(value)
return locals()


@PropWithDraw
def position():
""" Get/Set the position of the light. Can be represented as a
3 or 4 element tuple. If the fourth element is a 1, the light
has a position, if it is a 0, it represents a direction (i.o.w. the
light is a directional light, like the sun).
"""
def fget(self):
return self._position
def fset(self, value):
if len(value) == 3:
self._position = value[0], value[1], value[2], 1
elif len(value) == 4:
self._position = value[0], value[1], value[2], value[3]
else:
tmp = "Light position should be a 3 or 4 element sequence."
raise ValueError(tmp)
return locals()


@PropWithDraw
def isDirectional():
""" Get/Set whether the light is a directional light. A directional
light has no real position (it can be thought of as infinitely far
away), but shines in a particular direction. The sun is a good
example of a directional light.
"""
def fget(self):
return self._position[3] == 0
def fset(self, value):
# Get fourth element
if value:
fourth = 0
else:
fourth = 1
# Set position
tmp = self._position
self._position = tmp[0], tmp[1], tmp[2], fourth
return locals()


@PropWithDraw
def isCamLight():
""" Get/Set whether the light is a camera light. A camera light
moves along with the camera, like the lamp on a miner's hat.
"""
def fget(self):
return self._camLight
def fset(self, value):
self._camLight = bool(value)
return locals()


@DrawAfter
def On(self, on=True):
""" On(on=True)
Turn the light on.
"""
self._on = bool(on)


@DrawAfter
def Off(self):
""" Off()
Turn the light off.
"""
self._on = False


@property
def isOn(self):
""" Get whether the light is on.
"""
return self._on


def _Apply(self):
""" _Apply()

Apply the light position and other properties.

"""
thisLight = gl.GL_LIGHT0 + self._index
if self._on:
# Enable and set position
gl.glEnable(thisLight)
gl.glLightfv(thisLight, gl.GL_POSITION, self._position)
# Set colors
amb, dif, spe = gl.GL_AMBIENT, gl.GL_DIFFUSE, gl.GL_SPECULAR
gl.glLightfv(thisLight, amb, _getColor(self._ambient, self._color))
gl.glLightfv(thisLight, dif, _getColor(self._diffuse, self._color))
gl.glLightfv(thisLight, spe, _getColor(self._specular, self._color))
else:
# null-position means that the ligth is off
gl.glLightfv(thisLight, gl.GL_POSITION, (0.0, 0.0, 0.0, 0.0))
gl.glDisable(thisLight)

Change log

7ad08940ce29 by almar on Apr 25, 2012   Diff
Some more fixes for Py3k. I tested some
examples om Python 2.7 with good results.
Still have some weird errors on Python 3.1
though.
Go to: 
Project members, sign in to write a code review

Older revisions

e400f18f0161 by almar on Apr 24, 2012   Diff
Updated copyright texts.
c96c5f8b1db9 by almar on Jan 19, 2012   Diff
Fixed  issue 32 .
cb537cd7bbf8 by almar on Sep 20, 2011   Diff
Implemented shading for Mesh class.
Also implemented a Shader class that
makes things a lot easier.
All revisions of this file

File info

Size: 8528 bytes, 275 lines
Powered by Google Project Hosting