001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wpvs/j3d/WPVSScene.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.ogcwebservices.wpvs.j3d;
037    
038    import java.util.ArrayList;
039    import java.util.Calendar;
040    import java.util.GregorianCalendar;
041    import java.util.List;
042    
043    import javax.media.j3d.AmbientLight;
044    import javax.media.j3d.BoundingSphere;
045    import javax.media.j3d.DirectionalLight;
046    import javax.media.j3d.Light;
047    import javax.media.j3d.Node;
048    import javax.media.j3d.OrderedGroup;
049    import javax.vecmath.Color3f;
050    import javax.vecmath.Point3d;
051    import javax.vecmath.Vector3f;
052    
053    import org.deegree.framework.log.ILogger;
054    import org.deegree.framework.log.LoggerFactory;
055    import org.deegree.i18n.Messages;
056    import org.deegree.ogcwebservices.wpvs.utils.SunLight;
057    import org.deegree.ogcwebservices.wpvs.utils.SunPosition;
058    
059    /**
060     * This class represents the basic class for creation of a 3D perspective views as specified in the
061     * OGC Web Perpective View Service specification. A WPVS scene is defined by a scene model and a
062     * date determining the light conditions. Additional elements are 3D or 2.5D-features that are
063     * placed into the scene, atmospheric conditions influencing the light and visibility (e.g. fog,
064     * rain etc., but currently not implemented) and additional light placed into the scene (e.g. street
065     * lights, spots, lighted windows etc.).
066     * <p>
067     * -----------------------------------------------------------------------
068     * </p>
069     *
070     * @author <a href="mailto:lupp@lat-lon.de">Katharina Lupp</a>
071     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
072     *
073     * @author last edited by: $Author: mschneider $
074     * @version $Revision: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
075     */
076    public class WPVSScene {
077    
078        private static ILogger LOG = LoggerFactory.getLogger( WPVSScene.class );
079    
080        private Calendar calendar;
081    
082        private OrderedGroup scene;
083    
084        private List<Light> lights;
085    
086        private ViewPoint viewPoint;
087    
088        private Node background;
089    
090        /**
091         * Creates a new instance of WPVScene
092         *
093         * @param scene
094         *            java3D representation of the scene.
095         * @param viewPoint
096         *            object that describes the viewers position and the looking direction
097         * @param calendar
098         *            describtion of the date and time for which the scene shall be rendered --> light
099         *            conditions
100         * @param lights
101         *            lights in addition to sun and ambient light (e.g. street lights, spots etc.)
102         * @param background
103         *            scene background; have to be a <code>Shape3D</code> or a <code>Background</code>
104         */
105        public WPVSScene( OrderedGroup scene, ViewPoint viewPoint, Calendar calendar,
106                         List<Light> lights, Node background ) {
107            if ( lights != null ) {
108                this.lights = lights;
109            } else {
110                this.lights = new ArrayList<Light>();
111            }
112            this.scene = scene;
113            this.viewPoint = viewPoint;
114            this.calendar = calendar;
115            if( calendar == null ){
116                LOG.logDebug( Messages.getMessage( "WPVS_STANDARD_TIME" ) );
117                this.calendar = new GregorianCalendar(2007, 2, 21, 12, 00 );
118            }
119            this.background = background;
120            createDayLight();
121        }
122    
123        /**
124         * creates the light that results from the sun (direct light) and the ambient of the sky.
125         */
126        private void createDayLight() {
127    
128            int latitute = 52;
129            SunPosition sp = new SunPosition( calendar );
130            SunLight sl = new SunLight( latitute, sp );
131            Color3f sunlightColor = sl.calculateSunlight(  );
132            double vPos = sp.getVerticalSunposition( latitute );
133            double hPos = sp.getHorizontalSunPosition(  );
134    
135            Point3d p = getViewPoint().getObserverPosition();
136            Point3d origin = new Point3d( p.x, p.y, p.z );
137            BoundingSphere light_bounds = new BoundingSphere( origin, 250000 );
138    
139            // Directional Light: A DirectionalLight node defines an oriented light with an origin at
140            // infinity.
141            DirectionalLight headlight = new DirectionalLight();
142            headlight.setInfluencingBounds( light_bounds );
143            headlight.setColor( sunlightColor );
144            headlight.setDirection( (float) Math.sin( hPos ), (float) Math.sin( vPos ), (float) -Math.abs( Math.cos( hPos ) ) );
145            Vector3f tmp = new Vector3f();
146            headlight.getDirection( tmp );
147            lights.add( headlight );
148            // Ambient Light: Ambient light is that light that seems to come from all directions.
149            // Ambient light has only an ambient reflection component.
150            // It does not have diffuse or specular reflection components.
151            AmbientLight al = new AmbientLight();
152            al.setInfluencingBounds( light_bounds );
153            al.setColor( new Color3f( 0.7f * sunlightColor.x, 0.65f * sunlightColor.y, 0.6f * sunlightColor.z ) );
154    
155            lights.add( al );
156        }
157    
158        /**
159         * @return the background object of the scene.
160         */
161        public Node getBackground() {
162            return background;
163        }
164    
165        /**
166         * @param background
167         *            sets the <code>Background</code> object of the scene
168         */
169        public void setBackground( Node background ) {
170            this.background = background;
171        }
172    
173    
174        /**
175         * get the date and the time for determining time depending the light conditions of the scene
176         *
177         * @return describtion of the date and time for which the scene shall be rendered --> light
178         *         conditions
179         */
180        public Calendar getDate() {
181            return calendar;
182        }
183    
184        /**
185         * set the date and the time for determining time depending the light conditions of the scene
186         *
187         * @param calendar
188         *            describtion of the date and time for which the scene shall be rendered --> light
189         *            conditions
190         */
191        public void setDate( Calendar calendar ) {
192            if ( calendar == null ) {
193                LOG.logDebug( Messages.getMessage( "WPVS_STANDARD_TIME" ) );
194                this.calendar = new GregorianCalendar(2007, 2, 21, 12, 00 );
195            }
196            this.calendar = calendar;
197        }
198    
199        /**
200         * @return Java3D representation of the scene.
201         */
202        public OrderedGroup getScene() {
203            return scene;
204        }
205    
206        /**
207         * gets the position of the viewer, the directions he looks and his field of view in radians
208         *
209         * @return object that describes the viewers position and the point he looks at
210         */
211        public ViewPoint getViewPoint() {
212            return viewPoint;
213        }
214    
215        /**
216         * defines the position of the viewer and the point he looks at.
217         *
218         * @param viewPoint
219         *            object that describes the viewers position and the point he looks at
220         */
221        public void setViewPoint( ViewPoint viewPoint ) {
222            this.viewPoint = viewPoint;
223        }
224    
225        /**
226         * adds a light to the scene. this can be ambient, directional and point light.
227         *
228         * @param light
229         *            a light in addition to sun and basic ambient light (e.g. street lights, spots
230         *            etc.)
231         */
232        public void addLight( Light light ) {
233            this.lights.add( light );
234        }
235    
236        /**
237         * returns the lights of the scene
238         *
239         * @return lights including sun and basic ambient light (e.g. street lights, spots etc.)
240         */
241        public Light[] getLights() {
242            return lights.toArray( new Light[lights.size()] );
243        }
244    
245        /**
246         * sets the lights of the scene. this can be ambient, directional and point light.
247         *
248         * @param lights
249         *            lights in addition to sun and basic ambient light (e.g. street lights, spots etc.)
250         */
251        public void setLights( Light[] lights ) {
252            this.lights.clear();
253            setDate( calendar );
254            createDayLight();
255            if ( lights != null ) {
256                for ( int i = 0; i < lights.length; i++ ) {
257                    addLight( lights[i] );
258                }
259            }
260        }
261    }