001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wpvs/j3d/TexturedHeightMapTerrain.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     This class uses some code fragments taken from J3D.org open source project
026     which has been publish under LGPL at www.jd3.org.
027    
028     Contact:
029    
030     Andreas Poth
031     lat/lon GmbH
032     Aennchenstr. 19
033     53177 Bonn
034     Germany
035     E-Mail: poth@lat-lon.de
036    
037     Prof. Dr. Klaus Greve
038     Department of Geography
039     University of Bonn
040     Meckenheimer Allee 166
041     53115 Bonn
042     Germany
043     E-Mail: greve@giub.uni-bonn.de
044    
045     ---------------------------------------------------------------------------*/
046    package org.deegree.ogcwebservices.wpvs.j3d;
047    
048    // Standard imports
049    import java.awt.image.BufferedImage;
050    import java.net.URL;
051    
052    import javax.media.j3d.GeometryArray;
053    import javax.vecmath.Vector3f;
054    
055    import org.deegree.framework.util.ImageUtils;
056    import org.j3d.geom.GeometryData;
057    import org.j3d.geom.UnsupportedTypeException;
058    import org.j3d.geom.terrain.ElevationGridGenerator;
059    
060    /**
061     * 
062     * 
063     * 
064     * @version $Revision: 9345 $
065     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
066     * @author last edited by: $Author: apoth $
067     * 
068     * $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
069     */
070    public class TexturedHeightMapTerrain extends HeightMapTerrain {
071    
072        /**
073         * @param width
074         *            width of the terrains bbox
075         * @param depth
076         *            depth/height of the terrains bbox
077         * @param heights
078         *            terrain data; heights
079         * @param translation of the lowerleft point to the Java3D model.
080         * @param geometryType for a description see {@link HeightMapTerrain#HeightMapTerrain(float, float, float[][], Vector3f, int, boolean)}
081         * @param centerTerrain 
082         */
083        public TexturedHeightMapTerrain( float width, float depth, float[][] heights, Vector3f translation, int geometryType, boolean centerTerrain ) {
084            super( width, depth, heights, translation, geometryType, centerTerrain );
085        }
086    
087        /**
088         * No translation of the lowerleft point.
089         * @param width
090         *            width of the terrains bbox
091         * @param depth
092         *            depth/height of the terrains bbox
093         * @param heights
094         *            terrain data; heights
095         * @param geometryType for a description see {@link HeightMapTerrain#HeightMapTerrain(float, float, float[][], Vector3f, int, boolean)}
096         * @param centerTerrain 
097         * @param texture
098         */
099        public TexturedHeightMapTerrain( float width, float depth, float[][] heights, int geometryType, boolean centerTerrain,
100                                      BufferedImage texture ) {
101    
102            super( width, depth, heights, new Vector3f( 0, 0, 0 ), geometryType, centerTerrain );
103            setTexture( texture );
104        }
105    
106        /**
107         * No translation of the lowerleft point.
108         * @param width
109         *            width of the terrains bbox
110         * @param depth
111         *            depth/height of the terrains bbox
112         * @param heights
113         *            terrain data; heights
114         * @param geometryType for a description see {@link HeightMapTerrain#HeightMapTerrain(float, float, float[][], Vector3f, int, boolean)}
115         * @param centerTerrain 
116         * @param textureFile
117         */
118        public TexturedHeightMapTerrain( float width, float depth, float[][] heights, int geometryType, boolean centerTerrain, URL textureFile ) {
119    
120            super( width, depth, heights, new Vector3f( 0, 0, 0 ), geometryType, centerTerrain );
121            try {
122                setTexture(ImageUtils.loadImage( textureFile ) );
123            } catch ( Exception e ) {
124                e.printStackTrace();
125            }
126        }
127    
128        /**
129         * Must be called before rendering the terrain!!i<br>
130         */
131        @Override
132        public void createTerrain(  ) {
133            //System.out.println( "terrain#material: " +getAppearance().getMaterial() ); 
134            ElevationGridGenerator gridGenerator = new ElevationGridGenerator(
135                                                                               getTerrainWidth(),
136                                                                               getTerrainDepth(),
137                                                                               getTerrainHeights()[0].length,
138                                                                               getTerrainHeights().length,
139                                                                               getTranslation(),
140                                                                               isTerrainCentered() );
141    
142            // set the terrain into the elevation grid handler
143            gridGenerator.setTerrainDetail( getTerrainHeights(), 0 );
144    
145            GeometryData data = new GeometryData();
146            data.geometryType = getGeometryType();
147            data.geometryComponents = GeometryData.NORMAL_DATA;
148            if ( getTexture() != null ){
149                data.geometryComponents |= GeometryData.TEXTURE_2D_DATA;
150            }
151            try {
152                gridGenerator.generate( data );
153            } catch ( UnsupportedTypeException ute ) {
154                System.out.println( "Geometry type is not supported" );
155            }
156            int format = GeometryArray.COORDINATES | GeometryArray.NORMALS;
157            if ( getTexture() != null )
158                format |= GeometryArray.TEXTURE_COORDINATE_2;
159            GeometryArray geom = createGeometryArray( data, format );
160    
161            geom.setCoordinates( 0, data.coordinates );
162            geom.setNormals( 0, data.normals );
163            if ( getTexture() != null ){
164                geom.setTextureCoordinates( 0, 0, data.textureCoordinates );
165            }
166            setGeometry( geom );
167        }
168    
169    //    private void addTexture() {
170    //        Appearance appearance = getAppearance();
171    //        appearance.setMaterial( targetMaterial );
172    //
173    //        PolygonAttributes targetPolyAttr = new PolygonAttributes();
174    //        int capabilities = PolygonAttributes.ALLOW_MODE_WRITE | 
175    //                           PolygonAttributes.ALLOW_CULL_FACE_WRITE |
176    //                           PolygonAttributes.ALLOW_NORMAL_FLIP_WRITE | 
177    //                           PolygonAttributes.POLYGON_FILL;
178    //        targetPolyAttr.setCapability( capabilities );
179    //        appearance.setPolygonAttributes( targetPolyAttr );
180    //        if ( getTexture() != null ) {
181    //            try {
182    //                Texture texture = new TextureLoader( getTexture() ).getTexture();
183    //                texture.setEnable( true );
184    //                texture.setCapability( Texture.ALLOW_ENABLE_WRITE );
185    //                appearance.setTexture( texture );
186    //            } catch ( Exception e ) {
187    //                e.printStackTrace();
188    //            }
189    //            setCapability( Shape3D.ALLOW_GEOMETRY_WRITE );
190    //            setAppearance( appearance );
191    //        }
192    //    }
193    
194    }