三、三维模型添加

本章介绍WorldWindJava中加载三维模型


一、首先我们看WorldWindJava中给出的实例。
打开test->gov.nasa.worldwind.ogc.collada,可以看到有三个class文件。仔细分析ColladaViewer.java和ColladaMovingModel.java文件。
二、加载自己的三维模型
在包esi.control中新建class文件ColladaViewUtil.java文件,编写代码如下

package esi.control;


import javax.swing.SwingUtilities;

import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.layers.RenderableLayer;
import gov.nasa.worldwind.ogc.collada.ColladaRoot;
import gov.nasa.worldwind.ogc.collada.impl.ColladaController;
import esi.oceanPlatform.*;

public class ColladaViewUtil extends WWJMFS{


    public static class WorkerThread extends Thread{
        private Object colladaSource;
        private Position position;
        private String colladaName;
        private WorldWindow wwd;

        public WorkerThread(Object colladaSource,Position position,String colladaName,WorldWindow wwd){
            this.colladaSource=colladaSource;
            this.position=position;
            this.colladaName=colladaName;
            this.wwd=wwd;
        }

        public void run(){
            try {
                ColladaRoot colladaRoot=ColladaRoot.createAndParse(this.colladaSource);
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        addColladaLayer(colladaRoot,colladaName,wwd);
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        protected void addColladaLayer(ColladaRoot colladaRoot,String colladaName, WorldWindow wwd)
        {
            // Create a ColladaController to adapt the ColladaRoot to the World Wind renderable interface.
            colladaRoot.setPosition(this.position);
            colladaRoot.setHeading(Angle.fromDegrees(-15));
            colladaRoot.setAltitudeMode(WorldWind.RELATIVE_TO_GROUND);
            ColladaController colladaController = new ColladaController(colladaRoot);

            // Adds a new layer containing the ColladaRoot to the end of the WorldWindow's layer list.
            RenderableLayer layer = new RenderableLayer();
            layer.addRenderable(colladaController);
            layer.setName(colladaName);
            wwd.getModel().getLayers().add(layer);
        }
    }
}
在WorldWindJava源码中,testData->collada中为一些collada模型,我们现在在三维地球中添加一个三维建筑(cu_macky/CU Macky.dae),我们先把cu_macky文件夹复制到WWJMFS项目中。
然后需要在WWJMFS.java中进行调用,代码如下:
new ColladaViewUtil.WorkerThread(new File("E:/ESI/WorkSpace/WWJMFS/testData/collada/cu_macky/CU Macky.dae"),Position.fromDegrees(35.9844, 120.2742,0),"三维建筑",wwd).start();

可以得到如图:
三、三维模型添加_第1张图片

三、加载模型动画
在项目中,有时需要添加一个动态的模型,例如航拍过程的模拟,就需要一架飞机按照固定路线飞行,此节我们需要完成这个任务。首先,我们分析ColladaMovingModel.java文件,并在WWJMFS项目中创建ColladaMovingModelUtil.java,代码如下

package esi.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.ogc.collada.ColladaRoot;

public class ColladaMovingModelUtil extends ColladaViewUtil{
    public static class WorkerThread extends ColladaViewUtil.WorkerThread{

        public WorkerThread(Object colladaSource, Position position,
                String colladaName, WorldWindow wwd) {
            super(colladaSource, position, colladaName, wwd);
        }

        private Timer timer1;
        private Timer timer2;
        @Override
        protected void addColladaLayer(ColladaRoot colladaRoot,String colladaName, WorldWindow wwd )
        {
            super.addColladaLayer(colladaRoot,colladaName,wwd);

            // Rotate the duck to an upright position
            colladaRoot.setPitch(Angle.fromDegrees(0));


            int delay = 100; //milliseconds

            timer1=new Timer(delay, new ActionListener()
            {
                public void actionPerformed(ActionEvent evt)
                {
                    double deltaAddDegrees = 0.0001;
                    Position position = colladaRoot.getPosition();
                    if("36.2844°".substring(0, 7).equals(position.latitude.toString().substring(0, 7))
                            ||"120.5742°".substring(0, 7).equals(position.longitude.toString().substring(0, 7))){
                        timer1.stop();
                        timer2.start();
                    }else{
                       position = position.add(Position.fromDegrees(deltaAddDegrees, deltaAddDegrees));
                       colladaRoot.setHeading(Angle.fromDegrees(-100));
                       colladaRoot.setPosition(position);
                    }
                }
            });
            timer2=new Timer(delay, new ActionListener()
            {
                public void actionPerformed(ActionEvent evt)
                {
                    double deltaSubDegrees=-0.0001;
                    Position position = colladaRoot.getPosition();
                    if("35.9526°".substring(0, 7).equals(position.latitude.toString().substring(0, 7))
                            ||"120.2128°".substring(0, 7).equals(position.longitude.toString().substring(0, 7))){
                        timer2.stop();
                        timer1.start();
                    }else{
                       position = position.add(Position.fromDegrees(deltaSubDegrees, deltaSubDegrees));
                       colladaRoot.setHeading(Angle.fromDegrees(80));
                       colladaRoot.setPosition(position);
                    }
                }
            });
            timer1.start();
        }
    }
}

同样在WWJMFS.java文件中调用该函数,代码,

new ColladaMovingModelUtil.WorkerThread("E:/ESI/WorkSpace/WWJMFS/testData/collada/model.dae", Position.fromDegrees(35.9526, 120.2128,500), "航拍", wwd).start();

其中model.dae模型为我在3D Warehouse 中下载,模型如下图
三、三维模型添加_第2张图片


本章结束

你可能感兴趣的:(WorldWind,java,collada)