001        package com.croftsoft.core.media.jogl.camera;
002    
003        import com.croftsoft.core.lang.EnumUnknownException;
004        import com.croftsoft.core.math.axis.AxisAngle;
005        import com.croftsoft.core.math.axis.AxisAngleImp;
006        import com.croftsoft.core.math.axis.AxisAngleMut;
007        import com.croftsoft.core.math.matrix.Matrix3x3;
008         
009        /***********************************************************************
010        * Implementation.
011        *  
012        * @version
013        *   $Id: JoglCameraImp.java,v 1.5 2008/05/16 20:25:34 croft Exp $
014        * @since
015        *   2008-05-09
016        * @author
017        *   <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
018        ***********************************************************************/
019    
020        public final class  JoglCameraImp
021          implements JoglCameraMut
022        ////////////////////////////////////////////////////////////////////////
023        ////////////////////////////////////////////////////////////////////////
024        {
025          
026        private final AxisAngleMut  axisAngleMut;
027        
028        //
029        
030        private double  x, y, z;
031          
032        ////////////////////////////////////////////////////////////////////////
033        ////////////////////////////////////////////////////////////////////////
034        
035        public  JoglCameraImp (
036          final AxisAngle  axisAngle,
037          final double     x,
038          final double     y,
039          final double     z )
040        ////////////////////////////////////////////////////////////////////////
041        {
042          axisAngleMut = new AxisAngleImp ( axisAngle );
043          
044          this.x = x;
045          
046          this.y = y;
047          
048          this.z = z;
049        }
050        
051        public  JoglCameraImp ( )
052        ////////////////////////////////////////////////////////////////////////
053        {
054          this ( new AxisAngleImp ( ), 0, 0, 0 );
055        }
056    
057        ////////////////////////////////////////////////////////////////////////
058        // accessor methods
059        ////////////////////////////////////////////////////////////////////////
060    
061        public AxisAngle  getAxisAngle ( ) { return axisAngleMut; }
062        
063        public double  getX ( ) { return x; }
064        
065        public double  getY ( ) { return y; }
066        
067        public double  getZ ( ) { return z; }
068         
069        ////////////////////////////////////////////////////////////////////////
070        // mutator methods
071        ////////////////////////////////////////////////////////////////////////
072        
073        public void  setAxisAngle ( final AxisAngle  axisAngle )
074        ////////////////////////////////////////////////////////////////////////
075        {
076          axisAngleMut.copy ( axisAngle );
077        }
078        
079        public void  setX ( final double  x )
080        ////////////////////////////////////////////////////////////////////////
081        {
082          this.x = x;
083        }
084        
085        public void  setY ( final double  y )
086        ////////////////////////////////////////////////////////////////////////
087        {
088          this.y = y;
089        }
090        
091        public void  setZ ( final double  z )
092        ////////////////////////////////////////////////////////////////////////
093        {
094          this.z = z;
095        }
096        
097        ////////////////////////////////////////////////////////////////////////
098        // relative mutator methods
099        ////////////////////////////////////////////////////////////////////////
100        
101        public void  rotate ( final AxisAngle  axisAngle )
102        ////////////////////////////////////////////////////////////////////////
103        {
104          final AxisAngleMut  newAxisAngleMut
105            = axisAngleMut.toQuat ( ).multiply ( axisAngle.toQuat ( ) )
106            .toAxisAngle ( );
107    
108          newAxisAngleMut.normalize ( );
109    
110          axisAngleMut.copy ( newAxisAngleMut );
111        }
112        
113        public void  rotate (
114          final Axis    axis,
115          final double  degrees )
116        ////////////////////////////////////////////////////////////////////////
117        {
118          switch ( axis )
119          {
120            case X:
121              
122              rotate ( new AxisAngleImp ( degrees, 1, 0, 0 ) );
123    
124              break;
125              
126            case Y:
127              
128              rotate ( new AxisAngleImp ( degrees, 0, 1, 0 ) );
129    
130              break;
131              
132            case Z:
133              
134              rotate ( new AxisAngleImp ( degrees, 0, 0, 1 ) );
135              
136              break;
137              
138            default:
139              
140              throw new EnumUnknownException ( axis );
141          }
142        }
143         
144        public void  rotate (
145          final Rotation  rotation,
146          final double    degrees )
147        ////////////////////////////////////////////////////////////////////////
148        {
149          switch ( rotation )
150          {
151            case PITCH_DOWN:
152    
153              rotate ( Axis.X, -degrees );
154    
155              break;             
156    
157            case PITCH_UP:
158    
159              rotate ( Axis.X, degrees );
160              
161              break;             
162    
163            case ROLL_LEFT:
164    
165              rotate ( Axis.Z, degrees );
166    
167              break;             
168    
169            case ROLL_RIGHT:
170    
171              rotate ( Axis.Z, -degrees );
172    
173              break;             
174    
175            case YAW_LEFT:
176    
177              rotate ( Axis.Y, degrees );
178    
179              break;             
180    
181            case YAW_RIGHT:
182    
183              rotate ( Axis.Y, -degrees );
184    
185              break;             
186    
187            default:
188    
189              throw new EnumUnknownException ( rotation );
190          }
191        }
192    
193        public void  translate (
194          final Axis    axis,
195          final double  distance )
196        ////////////////////////////////////////////////////////////////////////
197        {
198          final Matrix3x3  rotationMatrix = axisAngleMut.toRotationMatrix ( );
199          
200          double  deltaX = 0, deltaY = 0, deltaZ = 0;
201    
202          switch ( axis )
203          {
204            case X:
205              
206              deltaX += rotationMatrix.get ( 0, 0 ) * distance;
207    
208              deltaY += rotationMatrix.get ( 1, 0 ) * distance;
209    
210              deltaZ += rotationMatrix.get ( 2, 0 ) * distance;
211    
212              break;
213              
214            case Y:
215              
216              deltaX += rotationMatrix.get ( 0, 1 ) * distance;
217    
218              deltaY += rotationMatrix.get ( 1, 1 ) * distance;
219    
220              deltaZ += rotationMatrix.get ( 2, 1 ) * distance;
221    
222              break;
223              
224            case Z:
225              
226              deltaX += rotationMatrix.get ( 0, 2 ) * distance;
227    
228              deltaY += rotationMatrix.get ( 1, 2 ) * distance;
229    
230              deltaZ += rotationMatrix.get ( 2, 2 ) * distance;
231    
232              break;
233              
234            default:
235              
236              throw new EnumUnknownException ( axis );
237          }
238    
239          x += deltaX;
240    
241          y += deltaY;
242    
243          z += deltaZ;
244        }
245         
246        public void  translate (
247          final Translation  translation,
248          final double       distance )
249        ////////////////////////////////////////////////////////////////////////
250        {
251          switch ( translation )
252          {
253            case BACKWARD:
254              
255              translate ( Axis.Z, distance );
256              
257              break;
258              
259            case DOWN:
260              
261              translate ( Axis.Y, -distance );
262              
263              break;
264              
265            case FORWARD:
266              
267              translate ( Axis.Z, -distance );
268              
269              break;
270              
271            case LEFT:
272              
273              translate ( Axis.X, -distance );
274              
275              break;
276              
277            case RIGHT:
278              
279              translate ( Axis.X, distance );
280              
281              break;
282              
283            case UP:
284              
285              translate ( Axis.Y, distance );
286              
287              break;
288              
289            default:
290              
291              throw new EnumUnknownException ( translation );
292          }
293        }
294    
295        ////////////////////////////////////////////////////////////////////////
296        ////////////////////////////////////////////////////////////////////////
297        }