001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/wpvs/j3d/Abstract3DRenderingEngine.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.awt.GraphicsConfigTemplate;
039    import java.awt.GraphicsDevice;
040    import java.awt.GraphicsEnvironment;
041    
042    import javax.media.j3d.BranchGroup;
043    import javax.media.j3d.Canvas3D;
044    import javax.media.j3d.GraphicsConfigTemplate3D;
045    import javax.media.j3d.Group;
046    import javax.media.j3d.Node;
047    import javax.media.j3d.PhysicalBody;
048    import javax.media.j3d.PhysicalEnvironment;
049    import javax.media.j3d.TransformGroup;
050    import javax.media.j3d.View;
051    import javax.media.j3d.ViewPlatform;
052    
053    /**
054     *
055     * This class serves a a superclass for all rendering engines of the WPV Service.
056     *
057     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
058     * @version $Revision: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
059     */
060    abstract public class Abstract3DRenderingEngine implements RenderingEngine {
061    
062        /**
063         * Scene to render
064         */
065        protected WPVSScene scene;
066    
067        /**
068         * the far clipping plane
069         */
070        protected float farClippingPlane;
071    
072        /**
073         * the near clipping plane.
074         */
075        protected double nearClippingPlane;
076    
077        /**
078         * Creates a new Abstract3DRenderEngine object with a nearclippingplane of 2.
079         *
080         * @param scene to be rendered
081         */
082        public Abstract3DRenderingEngine( WPVSScene scene ) {
083            this( scene, 2 );
084        }
085    
086        /**
087         * Creates a new Abstract3DRenderEngine object.
088         *
089         * @param scene to be rendered
090         * @param nearClippingPlane of the viewport
091         */
092        public Abstract3DRenderingEngine( WPVSScene scene, double nearClippingPlane ) {
093            this.scene = scene;
094            // clipping default
095            // farClippingPlane = (float)scene.getViewPoint().getFarClippingPlane();
096            farClippingPlane = (float)scene.getViewPoint().getFarClippingPlane();
097            this.nearClippingPlane = nearClippingPlane;
098        }
099    
100        /**
101         * Creates a new canvas each time this is called.
102         *
103         * The Canvas3D class provides a drawing canvas for 3D rendering. The Canvas3D object extends
104         * the Canvas object to include 3D-related information such as the size of the canvas in pixels,
105         * the Canvas3D's location, also in pixels, within a Screen3D object, and whether or not the
106         * canvas has stereo enabled. Because all Canvas3D objects contain a reference to a Screen3D
107         * object and because Screen3D objects define the size of a pixel in physical units, Java 3D can
108         * convert a Canvas3D size in pixels to a physical world size in meters. It can also determine
109         * the Canvas3D's position and orientation in the physical world.
110         *
111         * @param offscreen
112         *            true if the canvas3D is an offsreen canvas.
113         *
114         * @return A new canvas instance or <code>null</code> if no GraphicsEnvironment was found.
115         */
116        protected Canvas3D createCanvas( boolean offscreen ) {
117            // This class is used to obtain a valid GraphicsConfiguration that can be used by Java 3D.
118            // It instantiates objects and then sets all non-default attributes as desired.
119            GraphicsDevice[] gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
120            GraphicsConfigTemplate3D gc3D = new GraphicsConfigTemplate3D();
121            gc3D.setSceneAntialiasing( GraphicsConfigTemplate.PREFERRED );
122            gc3D.setDoubleBuffer( GraphicsConfigTemplate.REQUIRED );
123    
124            if ( gd != null && gd.length > 0 ) {
125                Canvas3D canvas = new Canvas3D( gd[0].getBestConfiguration( gc3D ), offscreen );
126                return canvas;
127            }
128            return null;
129        }
130    
131        /**
132         * A transform group is aplied as the transformation to the branches
133         *
134         * @return the transformgroup with ALLOW_TRANSFORM_READ, ALLOW_TRANSFORM_WRITE and
135         *         ALLOW_LOCAL_TO_VWORLD_READ set.
136         */
137        protected TransformGroup createTransformGroup() {
138            // creates the TransformGroup
139            // The TransformGroup node specifies a single spatial transformation, via a Transform3D
140            // object,
141            // that can position, orient, and scale all of its children.
142            TransformGroup viewTG = new TransformGroup();
143    
144            // Specifies that the node allows access to its object's transform information.
145            viewTG.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
146    
147            // Specifies that the node allows writing its object's transform information.
148            //viewTG.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
149    
150            // Specifies that this Node allows read access to its local coordinates to virtual world
151            // (Vworld) coordinates transform.
152            //viewTG.setCapability( Node.ALLOW_LOCAL_TO_VWORLD_READ );
153    
154            return viewTG;
155        }
156    
157        /**
158         * sets/defines the <code>View</code> of the scene and adds it to the submitted
159         * <code>BranchGroup</code>
160         *
161         * @param view
162         *            the scenes view
163         * @param viewGroup
164         */
165        protected void setView( View view, BranchGroup viewGroup ) {
166            ViewPoint viewPoint = scene.getViewPoint();
167    
168            // The ViewPatform class is used to set up the "view" side of a Java 3D scene graph.
169            ViewPlatform camera = new ViewPlatform();
170    
171            // RELATIVE_TO_FIELD_OF_VIEW tells Java 3D that it should modify the eyepoint position so it
172            // is located
173            // at the appropriate place relative to the window to match the specified field of view.
174            // This implies that the view frustum will change whenever the application changes the field
175            // of view.
176            camera.setViewAttachPolicy( View.RELATIVE_TO_FIELD_OF_VIEW );
177            camera.setViewAttachPolicy(View.NOMINAL_HEAD );
178    
179            view.setFieldOfView( viewPoint.getAngleOfView()  );
180            view.setWindowEyepointPolicy( View.RELATIVE_TO_FIELD_OF_VIEW );
181    
182            //set view parameters
183            view.setUserHeadToVworldEnable( true );
184            view.setSceneAntialiasingEnable( true );
185    
186            // The View object contains all parameters needed in rendering a three dimensional scene
187            // from one viewpoint.
188            view.setBackClipDistance( farClippingPlane );
189            view.setFrontClipDistance( nearClippingPlane );
190    
191            // creates the PhysicalBody and PhysicalEnvironment for the View
192            // and attachs it to the View
193            PhysicalEnvironment pe = new PhysicalEnvironment();
194            pe.setCoexistenceCenterInPworldPolicy( View.NOMINAL_HEAD );
195            view.setPhysicalEnvironment( pe );
196            PhysicalBody pb = new PhysicalBody( scene.getViewPoint().getObserverPosition(), scene.getViewPoint().getObserverPosition());
197    
198            view.setPhysicalBody( pb );
199    
200    
201            // attach the View to the ViewPlatform
202            view.attachViewPlatform( camera );
203    
204            TransformGroup viewTG = createTransformGroup();
205            viewTG.addChild( camera );
206            viewTG.setTransform( viewPoint.getViewMatrix() );
207            viewGroup.addChild( viewTG );
208     //       viewGroup.addChild( camera );
209        }
210    
211        /**
212         * adds a background to the scene
213         *
214         * @param vp
215         *            viewpoint
216         * @param background
217         *            the node to render in the background
218         * @param worldGroup
219         *            to add the Background to
220         */
221        protected void addBackground( @SuppressWarnings("unused")
222        ViewPoint vp, Group worldGroup, Node background ) {
223            // Point3d pp = vp.getObserverPosition();
224            // Point3d origin = new Point3d( pp.x, pp.y, pp.z );
225            // Bounds bounds = new BoundingSphere( origin, farClippingPlane );
226            // ExponentialFog fog = new ExponentialFog();
227            // fog.setColor( new Color3f( 0.7f, 0.7f, 0.7f ) );
228            // fog.setDensity( 0.001f );
229            // LinearFog fog = new LinearFog();
230            // fog.setColor( new Color3f( 0.7f, 0.7f, 0.7f ) );
231            // fog.setFrontDistance( 0 );
232            // fog.setBackDistance( 2000 );
233            // fog.setInfluencingBounds( bounds );
234            // worldGroup.addChild( fog );
235    
236            worldGroup.addChild( background );
237    
238        }
239    
240        /**
241         * sets the scenes back clip distance. default is 15000
242         *
243         * @param distance
244         */
245        public void setBackClipDistance( float distance ) {
246            farClippingPlane = distance;
247        }
248    
249        /**
250         * sets the scenes front clip distance. default is 2
251         *
252         * @param distance
253         */
254        public void setFrontClipDistance( float distance ) {
255            nearClippingPlane = distance;
256        }
257    }