1 |
ksteube |
1147 |
""" |
2 |
|
|
@author: John NGUI |
3 |
|
|
""" |
4 |
|
|
|
5 |
|
|
import vtk |
6 |
|
|
from position import GlobalPosition |
7 |
|
|
from constant import Viewport |
8 |
|
|
|
9 |
|
|
class Light: |
10 |
|
|
""" |
11 |
|
|
Class that defines a light. A light controls the lighting for the |
12 |
|
|
rendered object and works in a similar way to L{Camera <camera.Camera>}. |
13 |
|
|
""" |
14 |
|
|
|
15 |
|
|
# The SOUTH_WEST default viewport is used when there is only one viewport. |
16 |
|
|
# This saves the user from specifying the viewport when there is only one. |
17 |
jongui |
1148 |
def __init__(self, scene, viewport = Viewport.SOUTH_WEST): |
18 |
ksteube |
1147 |
""" |
19 |
|
|
Initialise the light. |
20 |
|
|
|
21 |
|
|
@type scene: L{Scene <scene.Scene>} object |
22 |
|
|
@param scene: Scene in which components are to be added to |
23 |
|
|
@type viewport: L{Viewport <constant.Viewport>} constant |
24 |
|
|
@param viewport: Viewport in which objects are to be rendered on |
25 |
|
|
""" |
26 |
|
|
|
27 |
|
|
self.__viewport = viewport |
28 |
|
|
self.__vtk_light = vtk.vtkLight() |
29 |
|
|
|
30 |
jongui |
1148 |
# Keeps track whether light has been modified. |
31 |
|
|
self.__modified = True |
32 |
|
|
# Keeps track whether the modification to the light was due to the |
33 |
|
|
# instantiation. If it is, then __setupLight() method is called. |
34 |
|
|
self.__initialization = True |
35 |
jongui |
1158 |
scene._addVisualizationModules(self) |
36 |
ksteube |
1147 |
|
37 |
jongui |
1158 |
def __setupLight(self, scene): |
38 |
ksteube |
1147 |
""" |
39 |
|
|
Set up the light and associate it with the renderer. |
40 |
jongui |
1158 |
|
41 |
|
|
@type scene: L{Scene <scene.Scene>} object |
42 |
|
|
@param scene: Scene in which objects are to be rendered on |
43 |
ksteube |
1147 |
""" |
44 |
|
|
|
45 |
jongui |
1158 |
scene._addLight(self.__viewport, self.__vtk_light) |
46 |
ksteube |
1147 |
|
47 |
|
|
def setColor(self, color): |
48 |
|
|
""" |
49 |
|
|
Set the light color. |
50 |
|
|
|
51 |
|
|
@type color: L{Color <constant.Color>} constant |
52 |
|
|
@param color: Light color |
53 |
|
|
""" |
54 |
|
|
|
55 |
|
|
self.__vtk_light.SetColor(color) |
56 |
jongui |
1148 |
self.__modified = True |
57 |
ksteube |
1147 |
|
58 |
|
|
def setFocalPoint(self, position): |
59 |
|
|
""" |
60 |
|
|
Set the focal point of the light. |
61 |
|
|
|
62 |
|
|
@type position: L{GlobalPosition <position.GlobalPosition>} object |
63 |
|
|
@param position: Light focal point |
64 |
|
|
""" |
65 |
|
|
|
66 |
|
|
self.__vtk_light.SetFocalPoint(position._getGlobalPosition()) |
67 |
jongui |
1148 |
self.__modified = True |
68 |
ksteube |
1147 |
|
69 |
|
|
def setPosition(self, position): |
70 |
|
|
""" |
71 |
|
|
Set the position of the light. |
72 |
|
|
|
73 |
|
|
@type position: L{GlobalPosition <position.GlobalPosition>} object |
74 |
|
|
@param position: Light position |
75 |
|
|
""" |
76 |
|
|
|
77 |
|
|
self.__vtk_light.SetPosition(position._getGlobalPosition()) |
78 |
jongui |
1148 |
self.__modified = True |
79 |
ksteube |
1147 |
|
80 |
|
|
# Elevation and azimuth is set to zero so that users do not |
81 |
|
|
# have to change both at the same time. |
82 |
|
|
def setAngle(self, elevation = 0, azimuth = 0): |
83 |
|
|
""" |
84 |
|
|
An alternative to set the position and focal point of the light |
85 |
|
|
by using the elevation and azimuth. |
86 |
|
|
|
87 |
|
|
@type elevation: Number |
88 |
|
|
@param elevation: Degree to rotate the light to the top and bottom |
89 |
|
|
@type azimuth: Number |
90 |
|
|
@param azimuth: Degree to rotate the light to the left and right |
91 |
|
|
""" |
92 |
|
|
|
93 |
|
|
# NOTE: The elevation angle of light does not appear to suffer the same |
94 |
|
|
# constraint as the elevation angle of camera where the elevation |
95 |
|
|
# angle is constraint between 90/-90. |
96 |
|
|
self.__vtk_light.SetDirectionAngle(elevation, azimuth) |
97 |
jongui |
1148 |
self.__modified = True |
98 |
ksteube |
1147 |
|
99 |
|
|
def setIntensity(self, intensity): |
100 |
|
|
""" |
101 |
|
|
Set the intensity (brightness) of the light. |
102 |
|
|
|
103 |
|
|
@type intensity: Number |
104 |
|
|
@param intensity: Intensity (brightness) of the light |
105 |
|
|
""" |
106 |
|
|
|
107 |
|
|
self.__vtk_light.SetIntensity(intensity) |
108 |
jongui |
1148 |
self.__modified = True |
109 |
ksteube |
1147 |
|
110 |
jongui |
1148 |
def _isModified(self): |
111 |
|
|
""" |
112 |
|
|
Return whether the Light has been modified. |
113 |
|
|
|
114 |
|
|
@rtype: Boolean |
115 |
|
|
@return: True or False |
116 |
|
|
""" |
117 |
|
|
|
118 |
|
|
if (self.__modified == True): |
119 |
|
|
return True |
120 |
|
|
else: |
121 |
|
|
return False |
122 |
|
|
|
123 |
jongui |
1158 |
def _render(self, scene): |
124 |
jongui |
1148 |
""" |
125 |
|
|
Render the light. |
126 |
jongui |
1158 |
|
127 |
|
|
@type scene: L{Scene <scene.Scene>} object |
128 |
|
|
@param scene: Scene in which objects are to be rendered on |
129 |
jongui |
1148 |
""" |
130 |
|
|
|
131 |
|
|
if(self._isModified() == True): |
132 |
|
|
# Will only be true once only when the light is instantiated. |
133 |
|
|
if(self.__initialization == True): |
134 |
jongui |
1158 |
self.__setupLight(scene) |
135 |
jongui |
1148 |
self.__initialization == False |
136 |
|
|
|
137 |
|
|
self.__isModified = False |
138 |
|
|
|
139 |
|
|
|