001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wpvs/j3d/TexturedHeightMapTerrain.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree. 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 http://www.geographie.uni-bonn.de/deegree/ 007 and 008 lat/lon GmbH 009 http://lat-lon.de/ 010 011 Additional copyright notes: 012 013 This class uses some code fragments taken from J3D.org open source project 014 which has been published under LGPL at www.jd3.org. 015 016 This library is free software; you can redistribute it and/or modify it under 017 the terms of the GNU Lesser General Public License as published by the Free 018 Software Foundation; either version 2.1 of the License, or (at your option) 019 any later version. 020 This library is distributed in the hope that it will be useful, but WITHOUT 021 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 022 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 023 details. 024 You should have received a copy of the GNU Lesser General Public License 025 along with this library; if not, write to the Free Software Foundation, Inc., 026 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 027 028 Contact information: 029 030 lat/lon GmbH 031 Aennchenstr. 19, 53177 Bonn 032 Germany 033 034 Department of Geography, University of Bonn 035 Prof. Dr. Klaus Greve 036 Postfach 1147, 53001 Bonn 037 Germany 038 039 e-mail: info@deegree.org 040 ----------------------------------------------------------------------------*/ 041 package org.deegree.ogcwebservices.wpvs.j3d; 042 043 import static org.deegree.framework.log.LoggerFactory.getLogger; 044 045 import java.awt.image.BufferedImage; 046 import java.net.URL; 047 import java.util.Arrays; 048 049 import javax.media.j3d.GeometryArray; 050 import javax.vecmath.Vector3f; 051 052 import org.deegree.framework.log.ILogger; 053 import org.deegree.framework.util.ImageUtils; 054 import org.deegree.ogcwebservices.wpvs.configuration.RenderingConfiguration; 055 import org.j3d.geom.GeometryData; 056 import org.j3d.geom.UnsupportedTypeException; 057 import org.j3d.geom.terrain.ElevationGridGenerator; 058 059 /** 060 * 061 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 062 * @author last edited by: $Author: rbezema $ 063 * 064 * $Revision: 20601 $, $Date: 2009-11-05 16:25:55 +0100 (Do, 05 Nov 2009) $ 065 */ 066 public class TexturedHeightMapTerrain extends HeightMapTerrain { 067 068 private static final ILogger LOG = getLogger( TexturedHeightMapTerrain.class ); 069 070 /** 071 * @param width 072 * width of the terrains bbox 073 * @param depth 074 * depth/height of the terrains bbox 075 * @param heights 076 * terrain data; heights 077 * @param translation 078 * of the lowerleft point to the Java3D model. 079 * @param geometryType 080 * for a description see 081 * {@link HeightMapTerrain#HeightMapTerrain(float, float, float[][], Vector3f, int, boolean)} 082 * @param centerTerrain 083 */ 084 public TexturedHeightMapTerrain( float width, float depth, float[][] heights, Vector3f translation, 085 int geometryType, boolean centerTerrain ) { 086 super( width, depth, heights, translation, geometryType, centerTerrain ); 087 } 088 089 /** 090 * No translation of the lowerleft point. 091 * 092 * @param width 093 * width of the terrains bbox 094 * @param depth 095 * depth/height of the terrains bbox 096 * @param heights 097 * terrain data; heights 098 * @param geometryType 099 * for a description see 100 * {@link HeightMapTerrain#HeightMapTerrain(float, float, float[][], Vector3f, int, boolean)} 101 * @param centerTerrain 102 * @param texture 103 */ 104 public TexturedHeightMapTerrain( float width, float depth, float[][] heights, int geometryType, 105 boolean centerTerrain, BufferedImage texture ) { 106 107 super( width, depth, heights, new Vector3f( 0, 0, 0 ), geometryType, centerTerrain ); 108 setTexture( texture ); 109 } 110 111 /** 112 * No translation of the lowerleft point. 113 * 114 * @param width 115 * width of the terrains bbox 116 * @param depth 117 * depth/height of the terrains bbox 118 * @param heights 119 * terrain data; heights 120 * @param geometryType 121 * for a description see 122 * {@link HeightMapTerrain#HeightMapTerrain(float, float, float[][], Vector3f, int, boolean)} 123 * @param centerTerrain 124 * @param textureFile 125 */ 126 public TexturedHeightMapTerrain( float width, float depth, float[][] heights, int geometryType, 127 boolean centerTerrain, URL textureFile ) { 128 129 super( width, depth, heights, new Vector3f( 0, 0, 0 ), geometryType, centerTerrain ); 130 try { 131 setTexture( ImageUtils.loadImage( textureFile ) ); 132 } catch ( Exception e ) { 133 LOG.logError( e ); 134 } 135 } 136 137 /** 138 * Must be called before rendering the terrain!!i<br> 139 */ 140 @Override 141 public void createTerrain() { 142 // System.out.println( "terrain#material: " +getAppearance().getMaterial() ); 143 ElevationGridGenerator gridGenerator = new ElevationGridGenerator( getTerrainWidth(), getTerrainDepth(), 144 getTerrainHeights()[0].length, 145 getTerrainHeights().length, 146 getTranslation(), isTerrainCentered() ); 147 148 // set the terrain into the elevation grid handler 149 gridGenerator.setTerrainDetail( getTerrainHeights(), 0 ); 150 151 GeometryData data = new GeometryData(); 152 data.geometryType = getGeometryType(); 153 data.geometryComponents = GeometryData.NORMAL_DATA; 154 155 if ( getTexture() != null ) { 156 data.geometryComponents |= GeometryData.TEXTURE_2D_DATA; 157 } 158 try { 159 gridGenerator.generate( data ); 160 } catch ( UnsupportedTypeException ute ) { 161 LOG.logError( "Geometry type is not supported.", ute ); 162 } 163 int format = GeometryArray.COORDINATES | GeometryArray.NORMALS; 164 if ( getTexture() != null ) { 165 format |= GeometryArray.TEXTURE_COORDINATE_2; 166 } 167 if ( !RenderingConfiguration.getInstance().isTerrainShadingEnabled() ) { 168 format |= GeometryArray.COLOR_3; 169 } 170 GeometryArray geom = createGeometryArray( data, format ); 171 172 geom.setCoordinates( 0, data.coordinates ); 173 geom.setNormals( 0, data.normals ); 174 if ( !RenderingConfiguration.getInstance().isTerrainShadingEnabled() ) { 175 float[] colors = new float[geom.getVertexCount() * 3]; 176 Arrays.fill( colors, 1 ); 177 geom.setColors( 0, colors ); 178 } 179 180 if ( getTexture() != null ) { 181 geom.setTextureCoordinates( 0, 0, data.textureCoordinates ); 182 } 183 setGeometry( geom ); 184 } 185 186 // private void addTexture() { 187 // Appearance appearance = getAppearance(); 188 // appearance.setMaterial( targetMaterial ); 189 // 190 // PolygonAttributes targetPolyAttr = new PolygonAttributes(); 191 // int capabilities = PolygonAttributes.ALLOW_MODE_WRITE | 192 // PolygonAttributes.ALLOW_CULL_FACE_WRITE | 193 // PolygonAttributes.ALLOW_NORMAL_FLIP_WRITE | 194 // PolygonAttributes.POLYGON_FILL; 195 // targetPolyAttr.setCapability( capabilities ); 196 // appearance.setPolygonAttributes( targetPolyAttr ); 197 // if ( getTexture() != null ) { 198 // try { 199 // Texture texture = new TextureLoader( getTexture() ).getTexture(); 200 // texture.setEnable( true ); 201 // texture.setCapability( Texture.ALLOW_ENABLE_WRITE ); 202 // appearance.setTexture( texture ); 203 // } catch ( Exception e ) { 204 // e.printStackTrace(); 205 // } 206 // setCapability( Shape3D.ALLOW_GEOMETRY_WRITE ); 207 // setAppearance( appearance ); 208 // } 209 // } 210 211 }