001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wpvs/j3d/TexturedSurface.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 java.awt.image.BufferedImage; 039 import java.util.ArrayList; 040 import java.util.List; 041 042 import javax.media.j3d.Appearance; 043 import javax.media.j3d.Material; 044 import javax.media.j3d.Texture; 045 import javax.vecmath.Point3d; 046 import javax.vecmath.TexCoord2f; 047 048 import org.deegree.framework.log.ILogger; 049 import org.deegree.framework.log.LoggerFactory; 050 import org.deegree.model.spatialschema.Geometry; 051 import org.deegree.model.spatialschema.MultiSurface; 052 import org.deegree.model.spatialschema.Surface; 053 import org.deegree.ogcwebservices.wpvs.configuration.RenderingConfiguration; 054 055 import com.sun.j3d.utils.geometry.GeometryInfo; 056 import com.sun.j3d.utils.geometry.NormalGenerator; 057 import com.sun.j3d.utils.image.TextureLoader; 058 059 /** 060 * 061 * 062 * 063 * @version $Revision: 18195 $ 064 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 065 * @author last edited by: $Author: mschneider $ 066 * 067 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $ 068 * 069 */ 070 public class TexturedSurface extends ColoredSurface { 071 private static ILogger LOG = LoggerFactory.getLogger( TexturedSurface.class ); 072 073 private Texture texture = null; 074 075 private BufferedImage textureImg = null; 076 077 /** 078 * creates a TexturedSurface from a geometry, Material and a texture image. Since a texture image be somehow 079 * transparent it is useful to be able to define a surfaces color. 080 * 081 * @param objectID 082 * an Id for this Surface, for example a db primary key 083 * @param parentID 084 * an Id for the parent of this Surface, for example if this is a wall the parent is the building. 085 * @param geometry 086 * the ogc:geometry surface which holds the point references of a polygon, not to be confused with a j3d 087 * Object which this class represents. 088 * @param material 089 * @param transparency 090 * @param textureImg 091 * @param textureCoords 092 */ 093 public TexturedSurface( String objectID, String parentID, Geometry geometry, Material material, float transparency, 094 BufferedImage textureImg, List<TexCoord2f> textureCoords ) { 095 super( objectID, parentID, geometry, material, transparency ); 096 097 this.textureImg = textureImg; 098 createTexture( textureImg ); 099 setAppearance( createAppearance() ); 100 addGeometry( geometry, textureCoords ); 101 } 102 103 private void createTexture( BufferedImage textureImg ) { 104 try { 105 texture = new TextureLoader( textureImg, TextureLoader.GENERATE_MIPMAP ).getTexture(); 106 texture.setEnable( true ); 107 texture.setMipMapMode( Texture.MULTI_LEVEL_MIPMAP ); 108 texture.setCapability( Texture.ALLOW_ENABLE_WRITE ); 109 } catch ( Exception e ) { 110 e.printStackTrace(); 111 } 112 } 113 114 /** 115 * @return the texture of this surface. 116 */ 117 public BufferedImage getTexture() { 118 return textureImg; 119 } 120 121 /** 122 * this method must be called before adding the surface to a Group 123 */ 124 @Override 125 public void compile() { 126 //GeometryInfo geomInfo = getGeometryInfo( surface ); 127 setAppearanceOverrideEnable( true ); 128 } 129 130 /** 131 * Adds the given surface and texcoords to this shape. 132 * 133 * @param surface 134 * to be added 135 * @param texCoords 136 * to be added. 137 */ 138 public void addGeometry( Geometry surface, List<TexCoord2f> texCoords ) { 139 List<Point3d> coordinates = new ArrayList<Point3d>(); 140 List<Integer> contourCount = new ArrayList<Integer>( 200 ); 141 if ( surface != null ) { 142 if ( surface instanceof MultiSurface ) { 143 for ( Surface multiSurface : ( (MultiSurface) surface ).getAllSurfaces() ) { 144 extractSurface( multiSurface, coordinates, contourCount ); 145 } 146 } else if ( surface instanceof Surface ) { 147 extractSurface( (Surface) surface, coordinates, contourCount ); 148 } else { 149 throw new UnsupportedOperationException( "Don't know how to create a textured surface from given geometry" ); 150 } 151 } 152 GeometryInfo geometryInfo = new GeometryInfo( GeometryInfo.POLYGON_ARRAY ); 153 154 geometryInfo.setCoordinates( coordinates.toArray( new Point3d[0] ) ); 155 int[] sc = new int[contourCount.size()]; 156 int stripCountNumber = 0; 157 for ( int i = 0; i < contourCount.size(); ++i ) { 158 sc[i] = contourCount.get( i ).intValue(); 159 stripCountNumber += sc[i]; 160 } 161 LOG.logDebug( "Geometry has number of vertices: " + coordinates.size() 162 + " the number of Strip counts adds up to: " + stripCountNumber ); 163 geometryInfo.setStripCounts( sc ); 164 // we don't actually need the contours, just an array which has the right length. 165 geometryInfo.setContourCounts( new int[] { sc.length } ); 166 167 if ( texCoords != null ) { 168 geometryInfo.setTextureCoordinateParams( 1, 2 ); 169 geometryInfo.setTextureCoordinates( 0, texCoords.toArray( new TexCoord2f[0] ) ); 170 } 171 NormalGenerator ng = new NormalGenerator(); 172 ng.generateNormals( geometryInfo ); 173 174 geometryInfo.convertToIndexedTriangles(); 175 addGeometry( geometryInfo.getIndexedGeometryArray( true ) ); 176 } 177 178 // /** 179 // * Adds the given surface and texcoords to this shape. 180 // * 181 // * @param surface 182 // * to be added 183 // * @param texCoords 184 // * to be added. 185 // */ 186 // public void addGeometry( Surface surface, List<TexCoord2f> texCoords ) { 187 // LOG.logDebug( "Adding geometry to textured surface: " + surface + "with texCoords: " + texCoords ); 188 // List<Point3d> coordinates = new ArrayList<Point3d>(); 189 // List<Integer> contourCount = new ArrayList<Integer>( 200 ); 190 // if ( surface != null ) { 191 // // SurfaceBoundary sb = surface.getSurfaceBoundary(); 192 // // if ( sb != null ) { 193 // // Ring outerRing = sb.getExteriorRing(); 194 // // if ( outerRing != null ) { 195 // // Position[] pos = outerRing.getPositions(); 196 // // if ( pos != null ) { 197 // // // the surface start and end point are the same, ignoring the last node. 198 // // for ( int i = 0; i < pos.length - 1; ++i ) { 199 // // coordinates.add( pos[i].getAsPoint3d() ); 200 // // } 201 // // contourCount.add( new Integer( pos.length - 1 ) ); 202 // // } 203 // // 204 // // Ring[] innerRings = sb.getInteriorRings(); 205 // // if ( innerRings != null ) { 206 // // for ( Ring innerRing : innerRings ) { 207 // // pos = innerRing.getPositions(); 208 // // if ( pos != null ) { 209 // // // the surface start and end point are the same, ignoring the last node. 210 // // for ( int i = 0; i < pos.length - 1; ++i ) { 211 // // coordinates.add( pos[i].getAsPoint3d() ); 212 // // } 213 // // contourCount.add( new Integer( pos.length - 1 ) ); 214 // // } 215 // // } 216 // // } 217 // // } 218 // // 219 // // } 220 // extractSurface( surface, coordinates, contourCount ); 221 // } 222 // GeometryInfo geometryInfo = new GeometryInfo( GeometryInfo.POLYGON_ARRAY ); 223 // 224 // geometryInfo.setCoordinates( coordinates.toArray( new Point3d[0] ) ); 225 // int[] sc = new int[contourCount.size()]; 226 // int stripCountNumber = 0; 227 // for ( int i = 0; i < contourCount.size(); ++i ) { 228 // sc[i] = contourCount.get( i ).intValue(); 229 // stripCountNumber += sc[i]; 230 // } 231 // LOG.logDebug( "Geometry has number of vertices: " + coordinates.size() 232 // + " the number of Strip counts adds up to: " + stripCountNumber ); 233 // geometryInfo.setStripCounts( sc ); 234 // // we don't actually need the contours, just an array which has the right length. 235 // geometryInfo.setContourCounts( new int[] { sc.length } ); 236 // 237 // if ( texCoords != null ) { 238 // geometryInfo.setTextureCoordinateParams( 1, 2 ); 239 // geometryInfo.setTextureCoordinates( 0, texCoords.toArray( new TexCoord2f[0] ) ); 240 // } 241 // NormalGenerator ng = new NormalGenerator(); 242 // ng.generateNormals( geometryInfo ); 243 // 244 // geometryInfo.convertToIndexedTriangles(); 245 // addGeometry( geometryInfo.getGeometryArray() ); 246 // } 247 248 /** 249 * Creates an appearance by setting the texture and default texture coordinates if none were given. 250 * 251 * @return the altered appearance 252 */ 253 public Appearance createAppearance() { 254 Appearance ap = getAppearance(); 255 if ( texture != null ) { 256 ap.setTexture( texture ); 257 ap.setTextureAttributes( RenderingConfiguration.getInstance().getTextureAttributes() ); 258 } 259 return ap; 260 } 261 }