Skip to content

Commit 7ff9783

Browse files
authored
Merge pull request #214 from NASAWorldWind/collada
Add Collada example
2 parents b5e061c + 8ff7474 commit 7ff9783

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main.class=gov.nasa.worldwindx.examples.ColladaViewer
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Copyright 2006-2009, 2017, 2020 United States Government, as represented by the
3+
* Administrator of the National Aeronautics and Space Administration.
4+
* All rights reserved.
5+
*
6+
* The NASA World Wind Java (WWJ) platform is licensed under the Apache License,
7+
* Version 2.0 (the "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software distributed
12+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
13+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
14+
* specific language governing permissions and limitations under the License.
15+
*
16+
* NASA World Wind Java (WWJ) also contains the following 3rd party Open Source
17+
* software:
18+
*
19+
* Jackson Parser – Licensed under Apache 2.0
20+
* GDAL – Licensed under MIT
21+
* JOGL – Licensed under Berkeley Software Distribution (BSD)
22+
* Gluegen – Licensed under Berkeley Software Distribution (BSD)
23+
*
24+
* A complete listing of 3rd Party software notices and licenses included in
25+
* NASA World Wind Java (WWJ) can be found in the WorldWindJava-v2.2 3rd-party
26+
* notices and licenses PDF found in code directory.
27+
*/
28+
package gov.nasa.worldwindx.examples;
29+
30+
import gov.nasa.worldwind.*;
31+
import gov.nasa.worldwind.avlist.AVKey;
32+
import gov.nasa.worldwind.geom.Position;
33+
import gov.nasa.worldwind.layers.Layer;
34+
import gov.nasa.worldwind.layers.LayerList;
35+
import gov.nasa.worldwind.layers.RenderableLayer;
36+
import gov.nasa.worldwind.ogc.collada.ColladaRoot;
37+
import gov.nasa.worldwind.ogc.collada.impl.ColladaController;
38+
import gov.nasa.worldwind.util.WWUtil;
39+
40+
import javax.swing.*;
41+
import java.awt.*;
42+
import java.io.File;
43+
44+
/**
45+
* Shows how to load <a href="https://www.khronos.org/collada/">COLLADA</a> 3D models.
46+
*/
47+
public class ColladaViewer extends ApplicationTemplate {
48+
49+
public static class AppFrame extends ApplicationTemplate.AppFrame {
50+
51+
public AppFrame() {
52+
super(true, true, false); // Don't include the layer panel; we're using the on-screen layer tree.
53+
54+
// Size the WorldWindow to take up the space typically used by the layer panel.
55+
Dimension size = new Dimension(1400, 800);
56+
this.setPreferredSize(size);
57+
this.pack();
58+
WWUtil.alignComponent(null, this, AVKey.CENTER);
59+
LayerList layers = getWwd().getModel().getLayers();
60+
for (Layer layer : layers) {
61+
String layerName = layer.getName();
62+
if (layerName != null && layerName.toLowerCase().startsWith("bing")) {
63+
layer.setEnabled(true);
64+
break;
65+
}
66+
}
67+
}
68+
69+
/**
70+
* Adds the specified <code>colladaRoot</code> to this app frame's <code>WorldWindow</code> as a new
71+
* <code>Layer</code>.
72+
*
73+
* @param colladaRoot the ColladaRoot to add a new layer for.
74+
*/
75+
protected void addColladaLayer(ColladaRoot colladaRoot) {
76+
// Create a ColladaController to adapt the ColladaRoot to the WorldWind renderable interface.
77+
ColladaController colladaController = new ColladaController(colladaRoot);
78+
79+
// Adds a new layer containing the ColladaRoot to the end of the WorldWindow's layer list.
80+
RenderableLayer layer = new RenderableLayer();
81+
layer.addRenderable(colladaController);
82+
this.getWwd().getModel().getLayers().add(layer);
83+
}
84+
}
85+
86+
// A <code>Thread</code> that loads a COLLADA file and displays it in an <code>AppFrame</code>.
87+
public static class WorkerThread extends Thread {
88+
89+
// Indicates the source of the COLLADA file loaded by this thread. Initialized during construction.
90+
protected Object colladaSource;
91+
92+
// Geographic position of the COLLADA model.
93+
protected Position position;
94+
95+
// Indicates the <code>AppFrame</code> the COLLADA file content is displayed in. Initialized during
96+
// construction.
97+
protected AppFrame appFrame;
98+
99+
/**
100+
* Creates a new worker thread from a specified <code>colladaSource</code> and <code>appFrame</code>.
101+
*
102+
* @param colladaSource the source of the COLLADA file to load. May be a {@link java.io.File}, a {@link
103+
* java.net.URL}, or an {@link java.io.InputStream}, or a {@link String} identifying a file path or URL.
104+
* @param position the geographic position of the COLLADA model.
105+
* @param appFrame the <code>AppFrame</code> in which to display the COLLADA source.
106+
*/
107+
public WorkerThread(Object colladaSource, Position position, AppFrame appFrame) {
108+
this.colladaSource = colladaSource;
109+
this.position = position;
110+
this.appFrame = appFrame;
111+
}
112+
113+
/**
114+
* Loads this worker thread's COLLADA source into a new
115+
* <code>{@link gov.nasa.worldwind.ogc.collada.ColladaRoot}</code>, then adds the new <code>ColladaRoot</code>
116+
* to this worker thread's <code>AppFrame</code>.
117+
*/
118+
@Override
119+
public void run() {
120+
try {
121+
final ColladaRoot colladaRoot = ColladaRoot.createAndParse(this.colladaSource);
122+
colladaRoot.setPosition(this.position);
123+
colladaRoot.setAltitudeMode(WorldWind.RELATIVE_TO_GROUND);
124+
125+
// Schedule a task on the EDT to add the parsed document to a layer
126+
SwingUtilities.invokeLater(() -> {
127+
appFrame.addColladaLayer(colladaRoot);
128+
});
129+
} catch (Exception e) {
130+
e.printStackTrace();
131+
}
132+
}
133+
}
134+
135+
public static void main(String[] args) {
136+
137+
// Set camera position and pitch angle.
138+
Configuration.setValue(AVKey.INITIAL_LATITUDE, 40.028);
139+
Configuration.setValue(AVKey.INITIAL_LONGITUDE, -105.27284091410579);
140+
Configuration.setValue(AVKey.INITIAL_ALTITUDE, 4000);
141+
Configuration.setValue(AVKey.INITIAL_PITCH, 50);
142+
143+
// Set the application frame to update, a position for the model, and a path to the COLLADA file.
144+
final AppFrame af = (AppFrame) start("WorldWind COLLADA Viewer", AppFrame.class);
145+
final Position MackyAuditoriumPosition = Position.fromDegrees(40.009993372683, -105.272774533734);
146+
final File ColladaFile = new File("testData/collada/cu_macky/CU Macky.dae");
147+
148+
// Invoke the <code>Thread</code> to load the COLLADA model asynchronously.
149+
new WorkerThread(ColladaFile, MackyAuditoriumPosition, af).start();
150+
151+
}
152+
}

0 commit comments

Comments
 (0)