001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wpvs/j3d/InteractiveWPVSRenderer.java $
002    
003    /*----------------------------------------------------------------------------
004     This file is part of deegree, http://deegree.org/
005     Copyright (C) 2001-2009 by:
006       Department of Geography, University of Bonn
007     and
008       lat/lon GmbH
009    
010     This library is free software; you can redistribute it and/or modify it under
011     the terms of the GNU Lesser General Public License as published by the Free
012     Software Foundation; either version 2.1 of the License, or (at your option)
013     any later version.
014     This library is distributed in the hope that it will be useful, but WITHOUT
015     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
016     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
017     details.
018     You should have received a copy of the GNU Lesser General Public License
019     along with this library; if not, write to the Free Software Foundation, Inc.,
020     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021    
022     Contact information:
023    
024     lat/lon GmbH
025     Aennchenstr. 19, 53177 Bonn
026     Germany
027     http://lat-lon.de/
028    
029     Department of Geography, University of Bonn
030     Prof. Dr. Klaus Greve
031     Postfach 1147, 53001 Bonn
032     Germany
033     http://www.geographie.uni-bonn.de/deegree/
034    
035     e-mail: info@deegree.org
036    ----------------------------------------------------------------------------*/
037    package org.deegree.ogcwebservices.wpvs.j3d;
038    
039    import java.awt.image.BufferedImage;
040    import java.awt.image.RenderedImage;
041    
042    import javax.media.j3d.BoundingSphere;
043    import javax.media.j3d.BranchGroup;
044    import javax.media.j3d.Canvas3D;
045    import javax.media.j3d.Group;
046    import javax.media.j3d.ImageComponent;
047    import javax.media.j3d.ImageComponent2D;
048    import javax.media.j3d.Light;
049    import javax.media.j3d.Locale;
050    import javax.media.j3d.OrderedGroup;
051    import javax.media.j3d.View;
052    import javax.media.j3d.VirtualUniverse;
053    import javax.vecmath.Point3d;
054    
055    import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
056    import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
057    import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
058    
059    
060    /**
061     * This class sill/shoud provide the ability to render a scence object (s.
062     * com.sun.j3d.loaders.Scene) or a Canvas3D. It's currently used for testing.
063     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
064     * @version $Revision: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
065     */
066    public class InteractiveWPVSRenderer extends Abstract3DRenderingEngine {
067    
068        private int width  = 800;
069    
070        private int height = 600;
071    
072        private Canvas3D offScreenCanvas3D;
073    
074        private View view;
075    
076        private boolean newSize;
077    
078        /**
079         * initialzies the render class with a default width and height (801x601)
080         * @param scene
081         */
082        public InteractiveWPVSRenderer(WPVSScene scene)
083        {
084            this( scene, 801, 601 );
085        }
086    
087        /**
088         * initialzies the render class with the submitted width and height
089         * @param scene
090         * @param width
091         * @param height
092         */
093        public InteractiveWPVSRenderer(WPVSScene scene, int width, int height)
094        {
095            super( scene, 2 );
096            this.width = width;
097            this.height = height;
098            view = new View();
099        }
100    
101        /**
102         * @param scene
103         */
104        public void setScene(WPVSScene scene) {
105            this.scene = scene;
106        }
107    
108        /**Create the VirtualUniverse for the application.
109         * @return the VirtualUniverse for the application.
110         */
111        protected VirtualUniverse createVirtualUniverse() {
112            return new VirtualUniverse();
113        }
114    
115        /**
116         * Simple utility method that creates a Locale for the
117         * VirtualUniverse
118         * @param u the universe
119         * @return the Locale
120         */
121        protected Locale createLocale( VirtualUniverse u ) {
122            return new Locale( u );
123        }
124    
125        /**
126         * returns the width of the offscreen rendering target
127         * @return the width of the offscreen rendering target
128         */
129        public int getWidth() {
130            return width;
131        }
132    
133        /**
134         * @param width the new width of the screen
135         */
136        public void setWidth( int width ) {
137            newSize = true;
138            this.width = width;
139        }
140    
141        /**
142         * returns the height of the offscreen rendering target
143         * @return the height of the offscreen rendering target
144         */
145        public int getHeight() {
146            return height;
147        }
148    
149        /**
150         * @param height the new Height of the screen
151         */
152        public void setHeight( int height ) {
153            newSize = true;
154            this.height = height;
155        }
156    
157        /** renders the scene to an <code>BufferedImage</code>
158         * @return a <code>BufferedImage</code> where the scene has been rendered to
159         */
160        public Object renderScene( ) {
161            if ( newSize ) {
162                view.removeCanvas3D( offScreenCanvas3D );
163    
164                offScreenCanvas3D = createOffscreenCanvas3D();
165    
166                newSize = false;
167            }
168            view.addCanvas3D( offScreenCanvas3D );
169            view.startView();
170    
171            // The viewGroup contains nodes necessary for rendering, viewing, etc
172            // View, ViewPlatform, Canvas3D, PhysBody, PhysEnviron, and Lights
173            BranchGroup viewGroup = new BranchGroup();
174    
175            // The sceneGroup conatins obejcts of the scene graph
176            BranchGroup sceneGroup = new BranchGroup();
177    
178            setView( view,  viewGroup );
179    //        createMouseBehaviours( viewGroup );
180    
181            OrderedGroup terrainGroup = new OrderedGroup();
182    
183            addBackground( scene.getViewPoint(), terrainGroup, scene.getBackground() );
184    
185            // add the lights to the view
186            Light[] lights = scene.getLights();
187            for (int i = 0; i < lights.length; i++) {
188                viewGroup.addChild( lights[i] );
189            }
190            /*
191            // add the terrain to the view
192            Shape3D terrain[] = scene.getTerrain();
193            for (int i = terrain.length-1; i >= 0; i--) {
194                terrainGroup.addChild( terrain[i] );
195            }
196            sceneGroup.addChild( terrainGroup );
197    
198            // add the features to the view
199            Group[] features = scene.getFeatures();
200            for (int i = 0; i < features.length; i++) {
201                sceneGroup.addChild( features[i] );
202            }
203            */
204            sceneGroup.compile();
205    
206            viewGroup.compile();
207    
208            VirtualUniverse universe = createVirtualUniverse();
209            Locale locale = createLocale( universe );
210    
211            locale.addBranchGraph(sceneGroup);
212            locale.addBranchGraph(viewGroup);
213    
214            //BranchGroup mainGroup = new BranchGroup();
215    //        mainGroup.addChild( sceneGroup );
216    //        mainGroup.addChild( viewGroup );
217    //        mainGroup.addChild( bg[0] );
218    
219            return offScreenCanvas3D;
220        }
221    
222        /**
223         * creates and returns a canvas for offscreen rendering
224         * @return a canvas for rendering
225         */
226        protected Canvas3D createOffscreenCanvas3D()
227        {
228            Canvas3D offScreenCanvas3D = createCanvas( false );
229    
230    //        offScreenCanvas3D.getScreen3D().setSize( width, height );
231    
232            offScreenCanvas3D.getScreen3D().setPhysicalScreenHeight( 0.0254/90 * height );
233            offScreenCanvas3D.getScreen3D().setPhysicalScreenWidth( 0.0254/90 * width );
234    
235            BufferedImage renderedImage =
236                new BufferedImage( width, height, BufferedImage.TYPE_3BYTE_BGR );
237    
238            ImageComponent2D imageComponent =
239                new ImageComponent2D( ImageComponent.FORMAT_RGB8, renderedImage );
240    
241            imageComponent.setCapability( ImageComponent.ALLOW_IMAGE_READ );
242    
243    //        offScreenCanvas3D.setOffScreenBuffer( imageComponent );
244    
245            return offScreenCanvas3D;
246        }
247    
248        @SuppressWarnings("unused")
249        private void createMouseBehaviours( Group scene  ){
250            Point3d origin = new Point3d(-2584400.880145242, 528.7904086212667, 5615449.9824785);
251            BoundingSphere bounds = new BoundingSphere(origin, 250000);
252    //        TransformGroup viewTrans = vp.getViewPlatformTransform();
253    
254    //      Create the rotate behavior node
255            MouseRotate behavior1 = new MouseRotate(offScreenCanvas3D);
256            scene.addChild(behavior1);
257            behavior1.setSchedulingBounds(bounds);
258    
259            // Create the zoom behavior node
260            MouseZoom behavior2 = new MouseZoom(offScreenCanvas3D);
261            scene.addChild(behavior2);
262            behavior2.setSchedulingBounds(bounds);
263    
264            // Create the translate behavior node
265            MouseTranslate behavior3 = new MouseTranslate(offScreenCanvas3D);
266            scene.addChild(behavior3);
267            behavior3.setSchedulingBounds(bounds);
268        }
269    
270        /**
271         * Called to render the scene into the offscreen Canvas3D
272         * @param offScreenCanvas3D the canvas to render into
273         * @return the Rendered Image
274         */
275        protected RenderedImage getImage(Canvas3D offScreenCanvas3D)
276        {
277            offScreenCanvas3D.renderOffScreenBuffer();
278            offScreenCanvas3D.waitForOffScreenRendering();
279    
280            ImageComponent2D imageComponent = offScreenCanvas3D.getOffScreenBuffer();
281    
282            return imageComponent.getImage();
283    
284        }
285    
286    }