KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > SpotLight


1 /*
2
3    Copyright 2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.image;
19
20 import java.awt.Color JavaDoc;
21
22 /**
23  * A light source which emits a light of constant intensity in all directions.
24  *
25  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
26  * @version $Id: SpotLight.java,v 1.5 2005/03/27 08:58:32 cam Exp $
27  */

28 public class SpotLight extends AbstractLight {
29     /**
30      * The light position, in user space
31      */

32     private double lightX, lightY, lightZ;
33
34     /**
35      * Point where the light points to
36      */

37     private double pointAtX, pointAtY, pointAtZ;
38
39     /**
40      * Specular exponent (light focus)
41      */

42     private double specularExponent;
43
44     /**
45      * Limiting cone angle
46      */

47     private double limitingConeAngle, limitingCos;
48
49     /**
50      * Light direction vector
51      */

52     private final double[] S = new double[3];
53
54     /**
55      * @return the light's x position
56      */

57     public double getLightX(){
58         return lightX;
59     }
60
61     /**
62      * @return the light's y position
63      */

64     public double getLightY(){
65         return lightY;
66     }
67
68     /**
69      * @return the light's z position
70      */

71     public double getLightZ(){
72         return lightZ;
73     }
74
75     /**
76      * @return x-axis coordinate where the light points to
77      */

78     public double getPointAtX(){
79         return pointAtX;
80     }
81
82     /**
83      * @return y-axis coordinate where the light points to
84      */

85     public double getPointAtY(){
86         return pointAtY;
87     }
88
89     /**
90      * @return z-axis coordinate where the light points to
91      */

92     public double getPointAtZ(){
93         return pointAtZ;
94     }
95
96     /**
97      * @return light's specular exponent (focus)
98      */

99     public double getSpecularExponent(){
100         return specularExponent;
101     }
102
103     /**
104      * @return light's limiting cone angle
105      */

106     public double getLimitingConeAngle(){
107         return limitingConeAngle;
108     }
109
110     public SpotLight(final double lightX, final double lightY, final double lightZ,
111                      final double pointAtX, final double pointAtY, final double pointAtZ,
112                      final double specularExponent, final double limitingConeAngle,
113                      final Color JavaDoc lightColor){
114         super(lightColor);
115
116         this.lightX = lightX;
117         this.lightY = lightY;
118         this.lightZ = lightZ;
119         this.pointAtX = pointAtX;
120         this.pointAtY = pointAtY;
121         this.pointAtZ = pointAtZ;
122         this.specularExponent = specularExponent;
123         this.limitingConeAngle = limitingConeAngle;
124         this.limitingCos = Math.cos(limitingConeAngle*Math.PI/180.);
125
126         S[0] = pointAtX - lightX;
127         S[1] = pointAtY - lightY;
128         S[2] = pointAtZ - lightZ;
129
130         double norm = Math.sqrt(S[0]*S[0]
131                                 + S[1]*S[1]
132                                 + S[2]*S[2]);
133
134         S[0] /= norm;
135         S[1] /= norm;
136         S[2] /= norm;
137     }
138
139     /**
140      * @return true if the light is constant over the whole surface
141      */

142     public boolean isConstant(){
143         return false;
144     }
145
146     /**
147      * Computes the light vector in (x, y, z)
148      *
149      * @param x x-axis coordinate where the light should be computed
150      * @param y y-axis coordinate where the light should be computed
151      * @param z z-axis coordinate where the light should be computed
152      * @param L array of length 3 where the result is stored
153      */

154     public final void getLight(final double x, final double y, final double z,
155                                final double L[]){
156         // Light Vector, L
157
L[0] = lightX - x;
158         L[1] = lightY - y;
159         L[2] = lightZ - z;
160
161         final double norm = Math.sqrt(L[0]*L[0] +
162                                       L[1]*L[1] +
163                                       L[2]*L[2]);
164
165         L[0] /= norm;
166         L[1] /= norm;
167         L[2] /= norm;
168         
169         double LS = -(L[0]*S[0] + L[1]*S[1] + L[2]*S[2]);
170         
171         if(LS > limitingCos){
172             double Iatt = limitingCos/LS;
173             Iatt *= Iatt;
174             Iatt *= Iatt;
175             Iatt *= Iatt;
176             Iatt *= Iatt;
177             Iatt *= Iatt;
178             Iatt *= Iatt; // akin Math.pow(Iatt, 64)
179

180             Iatt = 1 - Iatt;
181             LS = Iatt*Math.pow(LS, specularExponent);
182             
183             L[0] *= LS;
184             L[1] *= LS;
185             L[2] *= LS;
186         }
187         else{
188             L[0] = 0;
189             L[1] = 0;
190             L[2] = 0;
191         }
192     }
193 }
194
195
Popular Tags