World Wind Java开发之十二——加载粗制三维模型(ExtrudedPolygon)(转)

ww可以根据DLG图批量生成假三维模型,这对于小区等特征相似的建筑物模型的构建是非常有用的。下面来看如何一步步实现假三维模型的加载:

1、Shp文件的制作

首先在arcmap下数字化几个建筑物,并新建height字段存储建筑物的高度。

2、代码实现

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      *  
  3.      * @方法名称: init3DModel ; 
  4.      * @方法描述: 导入简易三维模型 ; 
  5.      * @参数 :@param filePath :shp文件路径 
  6.      * @返回类型: void ; 
  7.      * @创建人:bluce; 
  8.      * @创建时间:2015年2月3日 下午6:10:22; 
  9.      * @throws 
  10.      */  
  11.     private void init3DModel(String filePath)  
  12.     {  
  13.   
  14.         Shapefile shapefile = new Shapefile(filePath);  
  15.         RenderableLayer layer = new RenderableLayer();  
  16.         layer.setName("简易三维模型");  
  17.         layer.setPickEnabled(true);  
  18.   
  19.         try  
  20.         {  
  21.             while (shapefile.hasNext())  
  22.             {  
  23.                 ShapefileRecord record = shapefile.nextRecord();  
  24.                 layer.addRenderable(makeShape(record));  
  25.   
  26.             }  
  27.   
  28.             wwPanel.getWorldWindowGLCanvas().getModel().getLayers()  
  29.                     .add(layer);  
  30.         }  
  31.         finally  
  32.         {  
  33.             shapefile.close();  
  34.         }  
  35.     }  
  36.   
  37.     /** 
  38.      *  
  39.      * @方法名称: makeShape ; 
  40.      * @方法描述:  根据shp文件每条记录创建模型 ; 
  41.      * @参数 :@param record 
  42.      * @参数 :@return  
  43.      * @返回类型: ExtrudedPolygon ; 
  44.      * @创建人:bluce; 
  45.      * @创建时间:2015年2月3日 下午6:11:08; 
  46.      * @throws 
  47.      */  
  48.     private ExtrudedPolygon makeShape(ShapefileRecord record)  
  49.     {  
  50.   
  51.         String IMAGE_PATH = "F:\data\wwj\build123sm.jpg";  
  52.         Double height = null;  
  53.         String[] heightKeys = new String[]  
  54.         { "height", "Height", "HEIGHT" };  
  55.   
  56.         for (String key : heightKeys)  
  57.         {  
  58.             Object o = record.getAttributes().getValue(key);  
  59.             if (o != null)  
  60.             {  
  61.                 height = Double.parseDouble(o.toString());  
  62.             }  
  63.         }  
  64.         // 顶部属性  
  65.         ShapeAttributes capAttrs = new BasicShapeAttributes();  
  66.         capAttrs.setOutlineMaterial(Material.GRAY);  
  67.         capAttrs.setInteriorMaterial(Material.CYAN);  
  68.   
  69.         // 边属性  
  70.         ShapeAttributes sideAttributes = new BasicShapeAttributes();  
  71.         sideAttributes.setInteriorMaterial(Material.LIGHT_GRAY);  
  72.         sideAttributes.setOutlineMaterial(Material.DARK_GRAY);  
  73.         sideAttributes.setImageSource(IMAGE_PATH);  
  74.   
  75.         // 创建拉伸多边形  
  76.         VecBuffer vb = record.getPointBuffer(0);  
  77.         Polygon pgonAirspace = new Polygon(vb.getLocations()); // 根据点串构建多边形  
  78.   
  79.         //纹理  
  80.         ArrayList<String> textures = new ArrayList<String>();   
  81.         for (int i = 0; i < pgonAirspace.getLocations().size(); i++)  
  82.         {  
  83.             textures.add(IMAGE_PATH);  
  84.         }  
  85.   
  86.         ExtrudedPolygon polygon = new ExtrudedPolygon(  
  87.                 pgonAirspace.getLocations(), height, textures);  
  88.   
  89.         polygon.setCapAttributes(capAttrs);  
  90.         polygon.setSideAttributes(sideAttributes);  
  91.         polygon.setAltitudeMode(WorldWind.CLAMP_TO_GROUND);  
  92.         return polygon;  
  93.     }  

效果如下图所示:

3、问题

发现模型加载不了纹理啊,查了几遍不知道问题出在哪里,希望懂的朋友看到留言给指点下啊,不胜感激。
----------------------------------------------2015年7月26日----------------------------------------------
感谢Q_H_Wang提供的解决方案: 加载不上纹理的是因为 pgonAirspace.getLocations().获取的坐标集合第一个元素与最后一个一样造成的,除掉最后一个元素即可加载。所以只需在添加纹理的地方加个减一即可:
[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. ArrayList<String> textures = new ArrayList<String>();  
  2.             for (int i = 0; i < pgonAirspace.getLocations().size() - 1; i++)  
  3.             {  
  4.                 textures.add(IMAGE_PATH);  
  5.             }  

原文地址:https://www.cnblogs.com/telwanggs/p/6774776.html