001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wpvs/j3d/Abstract3DRenderingEngine.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstraße 19
030     53177 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041     
042     ---------------------------------------------------------------------------*/
043    package org.deegree.ogcwebservices.wpvs.j3d;
044    
045    import java.awt.GraphicsConfigTemplate;
046    import java.awt.GraphicsDevice;
047    import java.awt.GraphicsEnvironment;
048    
049    import javax.media.j3d.BranchGroup;
050    import javax.media.j3d.Canvas3D;
051    import javax.media.j3d.GraphicsConfigTemplate3D;
052    import javax.media.j3d.Group;
053    import javax.media.j3d.Node;
054    import javax.media.j3d.PhysicalBody;
055    import javax.media.j3d.PhysicalEnvironment;
056    import javax.media.j3d.TransformGroup;
057    import javax.media.j3d.View;
058    import javax.media.j3d.ViewPlatform;
059    
060    /**
061     * 
062     * This class serves a a superclass for all rendering engines of the WPV Service.
063     * 
064     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
065     * @version $Revision: 9345 $ $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
066     */
067    abstract public class Abstract3DRenderingEngine implements RenderingEngine {
068    
069        protected WPVSScene scene;
070    
071        protected float farClippingPlane;
072    
073        protected double nearClippingPlane;
074    
075        /**
076         * Creates a new Abstract3DRenderEngine object with a nearclippingplane of 2.
077         * 
078         * @param scene to be rendered
079         */
080        public Abstract3DRenderingEngine( WPVSScene scene ) {
081            this( scene, 2 );
082        }
083    
084        /**
085         * Creates a new Abstract3DRenderEngine object.
086         * 
087         * @param scene to be rendered
088         * @param nearClippingPlane of the viewport
089         */
090        public Abstract3DRenderingEngine( WPVSScene scene, double nearClippingPlane ) {
091            this.scene = scene;
092            // clipping default
093            // farClippingPlane = (float)scene.getViewPoint().getFarClippingPlane();
094            farClippingPlane = (float)scene.getViewPoint().getFarClippingPlane();
095            this.nearClippingPlane = nearClippingPlane;
096        }
097    
098        /**
099         * Creates a new canvas each time this is called.
100         * 
101         * The Canvas3D class provides a drawing canvas for 3D rendering. The Canvas3D object extends
102         * the Canvas object to include 3D-related information such as the size of the canvas in pixels,
103         * the Canvas3D's location, also in pixels, within a Screen3D object, and whether or not the
104         * canvas has stereo enabled. Because all Canvas3D objects contain a reference to a Screen3D
105         * object and because Screen3D objects define the size of a pixel in physical units, Java 3D can
106         * convert a Canvas3D size in pixels to a physical world size in meters. It can also determine
107         * the Canvas3D's position and orientation in the physical world.
108         * 
109         * @param offscreen
110         *            true if the canvas3D is an offsreen canvas.
111         * 
112         * @return A new canvas instance or <code>null</code> if no GraphicsEnvironment was found.
113         */
114        protected Canvas3D createCanvas( boolean offscreen ) {
115            // This class is used to obtain a valid GraphicsConfiguration that can be used by Java 3D.
116            // It instantiates objects and then sets all non-default attributes as desired.
117            GraphicsDevice[] gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
118            GraphicsConfigTemplate3D gc3D = new GraphicsConfigTemplate3D();
119            gc3D.setSceneAntialiasing( GraphicsConfigTemplate.PREFERRED );
120            gc3D.setDoubleBuffer( GraphicsConfigTemplate.REQUIRED );
121    
122            if ( gd != null && gd.length > 0 ) {
123                Canvas3D canvas = new Canvas3D( gd[0].getBestConfiguration( gc3D ), offscreen );
124                return canvas;
125            }
126            return null;
127        }
128    
129        /**
130         * A transform group is aplied as the transformation to the branches
131         * 
132         * @return the transformgroup with ALLOW_TRANSFORM_READ, ALLOW_TRANSFORM_WRITE and
133         *         ALLOW_LOCAL_TO_VWORLD_READ set.
134         */
135        protected TransformGroup createTransformGroup() {
136            // creates the TransformGroup
137            // The TransformGroup node specifies a single spatial transformation, via a Transform3D
138            // object,
139            // that can position, orient, and scale all of its children.
140            TransformGroup viewTG = new TransformGroup();
141    
142            // Specifies that the node allows access to its object's transform information.
143            viewTG.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
144    
145            // Specifies that the node allows writing its object's transform information.
146            //viewTG.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
147    
148            // Specifies that this Node allows read access to its local coordinates to virtual world
149            // (Vworld) coordinates transform.
150            //viewTG.setCapability( Node.ALLOW_LOCAL_TO_VWORLD_READ );
151    
152            return viewTG;
153        }
154    
155        /**
156         * sets/defines the <code>View</code> of the scene and adds it to the submitted
157         * <code>BranchGroup</code>
158         * 
159         * @param view
160         *            the scenes view
161         * @param viewGroup
162         */
163        protected void setView( View view, BranchGroup viewGroup ) {
164            ViewPoint viewPoint = scene.getViewPoint();
165    
166            // The ViewPatform class is used to set up the "view" side of a Java 3D scene graph.
167            ViewPlatform camera = new ViewPlatform();
168    
169            // RELATIVE_TO_FIELD_OF_VIEW tells Java 3D that it should modify the eyepoint position so it
170            // is located
171            // at the appropriate place relative to the window to match the specified field of view.
172            // This implies that the view frustum will change whenever the application changes the field
173            // of view.
174            camera.setViewAttachPolicy( View.RELATIVE_TO_FIELD_OF_VIEW );
175            camera.setViewAttachPolicy(View.NOMINAL_HEAD );
176    
177            view.setFieldOfView( viewPoint.getAngleOfView()  );
178            view.setWindowEyepointPolicy( View.RELATIVE_TO_FIELD_OF_VIEW );
179            
180            //set view parameters
181            view.setUserHeadToVworldEnable( true );
182            view.setSceneAntialiasingEnable( true );
183     
184            // The View object contains all parameters needed in rendering a three dimensional scene
185            // from one viewpoint.
186            view.setBackClipDistance( farClippingPlane );
187            view.setFrontClipDistance( nearClippingPlane );
188    
189            // creates the PhysicalBody and PhysicalEnvironment for the View
190            // and attachs it to the View
191            PhysicalEnvironment pe = new PhysicalEnvironment();
192            pe.setCoexistenceCenterInPworldPolicy( View.NOMINAL_HEAD );
193            view.setPhysicalEnvironment( pe );
194            PhysicalBody pb = new PhysicalBody( scene.getViewPoint().getObserverPosition(), scene.getViewPoint().getObserverPosition());
195            
196            view.setPhysicalBody( pb );
197    
198            
199            // attach the View to the ViewPlatform
200            view.attachViewPlatform( camera );
201    
202            TransformGroup viewTG = createTransformGroup();
203            viewTG.addChild( camera );
204            viewTG.setTransform( viewPoint.getViewMatrix() );
205            viewGroup.addChild( viewTG );
206     //       viewGroup.addChild( camera );
207        }
208    
209        /**
210         * adds a background to the scene
211         * 
212         * @param vp
213         *            viewpoint
214         * @param background
215         *            the node to render in the background
216         * @param worldGroup
217         *            to add the Background to
218         */
219        protected void addBackground( @SuppressWarnings("unused")
220        ViewPoint vp, Group worldGroup, Node background ) {
221            // Point3d pp = vp.getObserverPosition();
222            // Point3d origin = new Point3d( pp.x, pp.y, pp.z );
223            // Bounds bounds = new BoundingSphere( origin, farClippingPlane );
224            // ExponentialFog fog = new ExponentialFog();
225            // fog.setColor( new Color3f( 0.7f, 0.7f, 0.7f ) );
226            // fog.setDensity( 0.001f );
227            // LinearFog fog = new LinearFog();
228            // fog.setColor( new Color3f( 0.7f, 0.7f, 0.7f ) );
229            // fog.setFrontDistance( 0 );
230            // fog.setBackDistance( 2000 );
231            // fog.setInfluencingBounds( bounds );
232            // worldGroup.addChild( fog );
233    
234            worldGroup.addChild( background );
235    
236        }
237    
238        /**
239         * sets the scenes back clip distance. default is 15000
240         * 
241         * @param distance
242         */
243        public void setBackClipDistance( float distance ) {
244            farClippingPlane = distance;
245        }
246    
247        /**
248         * sets the scenes front clip distance. default is 2
249         * 
250         * @param distance
251         */
252        public void setFrontClipDistance( float distance ) {
253            nearClippingPlane = distance;
254        }
255    }