001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/wpvs/j3d/TerrainModel.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    
037    package org.deegree.ogcwebservices.wpvs.j3d;
038    
039    import java.awt.image.BufferedImage;
040    
041    import javax.media.j3d.Appearance;
042    import javax.media.j3d.Material;
043    import javax.media.j3d.RenderingAttributes;
044    import javax.media.j3d.Shape3D;
045    import javax.vecmath.Color3f;
046    
047    import org.deegree.ogcwebservices.wpvs.configuration.RenderingConfiguration;
048    
049    /**
050     * The <code>TerrainModel</code> class is a base class for all dgm's (terrains). It is capable of loading a texture onto
051     * the geometry of it's subclasses.
052     * 
053     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
054     * 
055     * @author last edited by: $Author: rbezema $
056     * 
057     * @version $Revision: 20601 $, $Date: 2009-11-05 16:25:55 +0100 (Do, 05. Nov 2009) $
058     * 
059     */
060    
061    public abstract class TerrainModel extends Shape3D {
062    
063        private BufferedImage textureImage = null;
064    
065        /**
066         * Creates a TerrainModel with a default Appearance set.
067         * <p>
068         * The default apearance of this terrain is defined as:
069         * <ul>
070         * <li>specularColor = new Color3f( 0.7f, 0.7f, 0.7f )</li>
071         * <li>ambientColor = white</li>
072         * <li>diffuseColor = white</li>
073         * <li>shininess = 75f</li>
074         * <li>lighting is enabled</li>
075         * <li>matieral is writable (Material.ALLOW_COMPONENT_WRITE)</li>
076         * </ul>
077         * </p>
078         */
079        protected TerrainModel() {
080            setCapability( Shape3D.ALLOW_GEOMETRY_WRITE );
081            setAppearance( createDefaultApperance() );
082        }
083    
084        /**
085         * Create a terrain from the given buffered image
086         * 
087         * @param textureImage
088         */
089        protected TerrainModel( BufferedImage textureImage ) {
090            this();
091            this.textureImage = textureImage;
092        }
093    
094        /**
095         * This method implements all the necessary steps to generate a Shape3D Terrain (Elevation model). Before rendering
096         * this Class this method should therefor be called prior.
097         */
098        public abstract void createTerrain();
099    
100        /**
101         * Creates a J3D Appearance for the surface
102         * 
103         * @return a new Appearance object
104         */
105        private Appearance createDefaultApperance() {
106            Appearance appearance = new Appearance();
107    
108            // create a material
109            Color3f specular = new Color3f( 0.2f, 0.2f, 0.2f );
110            Color3f white = new Color3f( 1, 1, 1 );
111            Material targetMaterial = new Material();
112            targetMaterial.setAmbientColor( white );
113            targetMaterial.setDiffuseColor( white );
114    
115            targetMaterial.setSpecularColor( specular );
116            targetMaterial.setShininess( 65f );
117            targetMaterial.setCapability( Material.ALLOW_COMPONENT_WRITE );
118            targetMaterial.setCapability( Material.ALLOW_COMPONENT_READ );
119            RenderingConfiguration rc = RenderingConfiguration.getInstance();
120            targetMaterial.setLightingEnable( rc.isTerrainShadingEnabled() );
121    
122            // targetMaterial.setColorTarget( Material.AMBIENT_AND_DIFFUSE );
123            appearance.setMaterial( targetMaterial );
124    
125            appearance.setColoringAttributes( rc.getColoringAttributes() );
126    
127            appearance.setPolygonAttributes( rc.getTerrainPolygonAttributes() );
128    
129            RenderingAttributes ra = new RenderingAttributes();
130            ra.setDepthBufferEnable( true );
131            appearance.setRenderingAttributes( ra );
132    
133            return appearance;
134        }
135    
136        /**
137         * @param textureImage
138         *            An other texture value.
139         */
140        public void setTexture( BufferedImage textureImage ) {
141            if ( textureImage != null ) {
142                this.textureImage = textureImage;
143                Appearance appearance = getAppearance();
144                RenderingConfiguration rc = RenderingConfiguration.getInstance();
145    
146                appearance.setTextureAttributes( rc.getTextureAttributes() );
147                appearance.setTexture( rc.getTexture( textureImage ) );
148    
149                setAppearance( appearance );
150            }
151    
152        }
153    
154        /**
155         * @return the BufferedImage which can be used as a texture or <code>null</code>if no texture was defined.
156         */
157        public BufferedImage getTexture() {
158            return this.textureImage;
159        }
160    
161        @Override
162        public String toString() {
163            StringBuilder sb = new StringBuilder( 512 );
164            sb.append( "Elevationmodel as a J3D terrainModel: " );
165            if ( textureImage != null ) {
166                sb.append( " with textureImage and " );
167            }
168            sb.append( "Appearance: " ).append( getAppearance() ).append( "\n" );
169            return sb.toString();
170        }
171    
172    }