001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/wpvs/j3d/ColoredSurface.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 javax.media.j3d.Appearance;
039    import javax.media.j3d.ColoringAttributes;
040    import javax.media.j3d.Material;
041    import javax.media.j3d.RenderingAttributes;
042    import javax.media.j3d.TransparencyAttributes;
043    import javax.vecmath.Color3f;
044    
045    import org.deegree.model.spatialschema.Geometry;
046    import org.deegree.ogcwebservices.wpvs.configuration.RenderingConfiguration;
047    
048    /**
049     * 
050     * 
051     * 
052     * @version $Revision: 20601 $
053     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
054     * @author last edited by: $Author: rbezema $
055     * 
056     * @version 1.0. $Revision: 20601 $, $Date: 2009-11-05 16:25:55 +0100 (Do, 05. Nov 2009) $
057     * 
058     * @since 2.0
059     */
060    public class ColoredSurface extends DefaultSurface {
061    
062        private Appearance defaultAppearance;
063    
064        /**
065         * 
066         * @param objectID
067         *            an Id for this Surface, for example a db primary key
068         * @param parentId
069         *            an Id for the parent of this Surface, for example if this is a wall the parent is the building.
070         * @param geometry
071         * @param red
072         * @param green
073         * @param blue
074         * @param transparency
075         */
076        public ColoredSurface( String objectID, String parentId, Geometry geometry, float red, float green, float blue,
077                               float transparency ) {
078            super( objectID, parentId, geometry );
079            Material material = createMaterial( red, green, blue );
080    
081            setAppearance( createAppearance( material, transparency ) );
082        }
083    
084        /**
085         * 
086         * @param objectID
087         *            an Id for this Surface, for example a db primary key
088         * @param parentId
089         *            an Id for the parent of this Surface, for example if this is a wall the parent is the building.
090         * @param surface
091         * @param material
092         * @param transparency
093         */
094        public ColoredSurface( String objectID, String parentId, Geometry surface, Material material, float transparency ) {
095            super( objectID, parentId, surface );
096            setAppearance( createAppearance( material, transparency ) );
097        }
098    
099        /**
100         * 
101         * @param objectID
102         *            an Id for this Surface, for example a db primary key
103         * @param parentId
104         *            an Id for the parent of this Surface, for example if this is a wall the parent is the building.
105         * @param surface
106         * @param app
107         */
108        public ColoredSurface( String objectID, String parentId, Geometry surface, Appearance app ) {
109            super( objectID, parentId, surface );
110            setAppearance( app );
111        }
112    
113        @Override
114        public Appearance getAppearance() {
115            return defaultAppearance;
116        }
117    
118        @Override
119        public void setAppearance( Appearance appearance ) {
120            this.defaultAppearance = appearance;
121            super.setAppearance( appearance );
122        }
123    
124        /**
125         * create a simple colored Material
126         * 
127         * @param red
128         * @param green
129         * @param blue
130         * @return a new Material with specular and ambient and diffuse color.
131         */
132        private Material createMaterial( float red, float green, float blue ) {
133            Color3f color = new Color3f( red, green, blue );
134            Material targetMaterial = new Material();
135            targetMaterial.setAmbientColor( color );
136            targetMaterial.setDiffuseColor( color );
137            targetMaterial.setSpecularColor( color );
138            targetMaterial.setShininess( 75.0f );
139            targetMaterial.setLightingEnable( true );
140            targetMaterial.setCapability( Material.ALLOW_COMPONENT_WRITE );
141            return targetMaterial;
142        }
143    
144        /**
145         * create Appearance from a material and a opacity value
146         * 
147         * @param material
148         * @param transparency
149         * @return a default appearance created with the material properties
150         */
151        private Appearance createAppearance( Material material, float transparency ) {
152            // create the appearance and it's material properties
153            Appearance appearance = new Appearance();
154    
155            RenderingConfiguration rc = RenderingConfiguration.getInstance();
156            ColoringAttributes ca = rc.getColoringAttributes();
157            if ( !rc.isObjectShadingEnabled() && material != null ) {
158                material.setLightingEnable( false );
159                Color3f amColor = new Color3f();
160                material.getDiffuseColor( amColor );
161                ca = new ColoringAttributes( amColor, ca.getShadeModel() );
162            }
163            appearance.setMaterial( material );
164            // the coloring attributes
165            appearance.setColoringAttributes( ca );
166    
167            // the polygon attributes
168            appearance.setPolygonAttributes( rc.getSurfacePolygonAttributes() );
169    
170            RenderingAttributes ra = new RenderingAttributes();
171            ra.setDepthBufferEnable( true );
172            appearance.setRenderingAttributes( ra );
173    
174            if ( transparency != 0f ) {
175                TransparencyAttributes transpAtt = new TransparencyAttributes( TransparencyAttributes.BLENDED, transparency );
176                appearance.setTransparencyAttributes( transpAtt );
177            }
178            return appearance;
179        }
180    }