gvsig-3d / 2.0 / trunk / org.gvsig.gvsig3d.app / org.gvsig.gvsig3d.app.extension / src / main / java / org / gvsig / gvsig3d / app / extension / DefaultView3DDocument.java @ 306
History | View | Annotate | Download (13.9 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.gvsig3d.app.extension; |
23 |
|
24 |
import java.awt.Color; |
25 |
import java.awt.geom.Rectangle2D; |
26 |
import java.util.prefs.Preferences; |
27 |
|
28 |
import org.gvsig.app.ApplicationLocator; |
29 |
import org.gvsig.app.project.ProjectPreferences; |
30 |
import org.gvsig.app.project.documents.DocumentManager; |
31 |
import org.gvsig.app.project.documents.view.BaseViewDocument; |
32 |
import org.gvsig.gvsig3d.Gvsig3DLocator; |
33 |
import org.gvsig.gvsig3d.Gvsig3DManager; |
34 |
import org.gvsig.gvsig3d.app.camera.ProjectCamera; |
35 |
import org.gvsig.gvsig3d.hud.Hud; |
36 |
import org.gvsig.osgvp.exceptions.node.ChildIndexOutOfBoundsExceptions; |
37 |
import org.gvsig.osgvp.exceptions.node.NodeException; |
38 |
import org.gvsig.osgvp.geometry.Envelope3D; |
39 |
import org.gvsig.osgvp.terrain.JavaDataDriver; |
40 |
import org.gvsig.osgvp.terrain.LayerManager; |
41 |
import org.gvsig.osgvp.terrain.Terrain; |
42 |
import org.gvsig.osgvp.terrain.TerrainViewer; |
43 |
import org.gvsig.osgvp.terrain.exceptions.TerrainExtentException; |
44 |
import org.gvsig.osgvp.viewer.Camera; |
45 |
import org.gvsig.osgvp.viewer.CanvasViewer; |
46 |
import org.gvsig.osgvp.viewer.DisplaySettings; |
47 |
import org.gvsig.osgvp.viewer.IViewerContainer; |
48 |
import org.gvsig.osgvp.viewer.ViewerFactory; |
49 |
import org.gvsig.osgvp.viewer.ViewerStateListener; |
50 |
import org.gvsig.tools.ToolsLocator; |
51 |
import org.gvsig.tools.dynobject.DynStruct; |
52 |
import org.gvsig.tools.persistence.PersistenceManager; |
53 |
import org.gvsig.tools.persistence.PersistentState; |
54 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
55 |
import org.gvsig.tools.util.Callable; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
/**
|
60 |
* @author AI2
|
61 |
* @version $Id$
|
62 |
*
|
63 |
*/
|
64 |
public class DefaultView3DDocument extends BaseViewDocument { |
65 |
|
66 |
private static final long serialVersionUID = 676716312482344764L; |
67 |
|
68 |
public static final String PERSISTENCE_DEFINITION_NAME = "DefaultView3DDocument"; |
69 |
|
70 |
private IViewerContainer _canvas3d = null; |
71 |
private TerrainViewer _terrainViewer = null; |
72 |
|
73 |
private Terrain _terrain = null; |
74 |
// private ProjectCamera _projectCamera = null;
|
75 |
private JavaDataDriver _terrainDataManager = null; |
76 |
private LayerManager _terrainLayerManager = null; |
77 |
private int _terrainType = Terrain.CoordinateSystemType.PROJECTED; // spheric |
78 |
// or
|
79 |
// plane
|
80 |
private Color _backgroundColor; |
81 |
private float _verticalExaggeration = 1; |
82 |
private DefaultView3DPanel _view = null; |
83 |
private static Preferences _prefs = Preferences.userRoot().node( |
84 |
"gvsig.configuration.3D");
|
85 |
private boolean _perspectiveMode = true; |
86 |
|
87 |
private boolean _properties = false; |
88 |
|
89 |
private ProjectCamera _projectCamera;
|
90 |
private static Logger _logger = LoggerFactory |
91 |
.getLogger(DefaultView3DDocument.class.getName()); |
92 |
|
93 |
public static final Rectangle2D sphericGlobalBounds = new Rectangle2D.Double( |
94 |
-180, -90, 360, 180); |
95 |
public static final Rectangle2D planeGlobalBounds = new Rectangle2D.Double( |
96 |
-20000000, -10000000, 40000000, 20000000); |
97 |
|
98 |
public static final String BACKGROUNDCOLOR = "backgroundColor"; |
99 |
|
100 |
public static final String TERRAINTYPE = "terrainType"; |
101 |
|
102 |
public static final String PROJECTCAMERA = "projectCamera"; |
103 |
|
104 |
private boolean _isAnimated = false; |
105 |
|
106 |
private Gvsig3DManager _manager;
|
107 |
|
108 |
public DefaultView3DDocument() {
|
109 |
this(null); |
110 |
_backgroundColor = new Color(0, 0, 0, 255); |
111 |
} |
112 |
|
113 |
public DefaultView3DDocument(DocumentManager factory) {
|
114 |
super(factory);
|
115 |
|
116 |
ProjectPreferences preferences = (ProjectPreferences) ApplicationLocator |
117 |
.getManager().getPreferences("project");
|
118 |
_backgroundColor = new Color(0, 0, 0, 255); |
119 |
|
120 |
} |
121 |
|
122 |
public int getTerrainType() { |
123 |
return _terrainType;
|
124 |
} |
125 |
|
126 |
public void setTerrainType(int type) { |
127 |
_terrainType = type; |
128 |
} |
129 |
|
130 |
public float getVerticalExaggeration() { |
131 |
return _verticalExaggeration;
|
132 |
} |
133 |
|
134 |
public void setVerticalExaggeration(float exaggeration) { |
135 |
_verticalExaggeration = exaggeration; |
136 |
} |
137 |
|
138 |
public Color getBackGroundColor() { |
139 |
return _backgroundColor;
|
140 |
} |
141 |
|
142 |
public void setBackGroundColor(Color backGroundColor) { |
143 |
_backgroundColor = backGroundColor; |
144 |
if ((_backgroundColor != null) && (_terrainViewer != null)) { |
145 |
float r, g, b, a;
|
146 |
r = ((float) _backgroundColor.getRed()) / 255.0f; |
147 |
g = ((float) _backgroundColor.getGreen()) / 255.0f; |
148 |
b = ((float) _backgroundColor.getBlue()) / 255.0f; |
149 |
a = ((float) _backgroundColor.getAlpha()) / 255.0f; |
150 |
|
151 |
_terrainViewer.setClearColor(r, g, b, a); |
152 |
} |
153 |
} |
154 |
|
155 |
public Terrain getTerrain() {
|
156 |
return _terrain;
|
157 |
} |
158 |
|
159 |
public void setTerrain(Terrain terrain) { |
160 |
_terrain = terrain; |
161 |
} |
162 |
|
163 |
public IViewerContainer getCanvas3d() {
|
164 |
return _canvas3d;
|
165 |
} |
166 |
|
167 |
public void setCanvas3d(CanvasViewer canvas3d) { |
168 |
_canvas3d = canvas3d; |
169 |
} |
170 |
|
171 |
// private void setVerticalEx(float exa) {
|
172 |
//
|
173 |
// SingleLayerIterator lyrIterator = new SingleLayerIterator(
|
174 |
// (FLayers3D) getMapContext().getLayers());
|
175 |
// while (lyrIterator.hasNext()) {
|
176 |
// FLayer lyr = lyrIterator.next();
|
177 |
//
|
178 |
// Layer3DProps props = Layer3DProps.getLayer3DProps(lyr);
|
179 |
// int type = props.getType();
|
180 |
// if (exa != props.getVerticalEx()) {
|
181 |
// if (type == Layer3DProps.layer3DElevation) {
|
182 |
// props.setVerticalEx(exa);
|
183 |
// HeightfieldLayer layer = (HeightfieldLayer)props.getTerrainLayer();
|
184 |
// if(layer == null) return;
|
185 |
// layer.setVerticalExaggeration(exa);
|
186 |
// _view.getTerrain().getLayerManager().updateLayers();
|
187 |
// } else if (type == Layer3DProps.layer3DVector) {
|
188 |
// props.setVerticalEx(exa);
|
189 |
// }
|
190 |
// }
|
191 |
//
|
192 |
// }
|
193 |
// }
|
194 |
|
195 |
public boolean isPerspectiveModeActive() { |
196 |
return _perspectiveMode;
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* Active the perspective mode. If we disable the perspective mode, the
|
201 |
* ortho mode will be active
|
202 |
*
|
203 |
* @param perspectiveMode
|
204 |
* enable/disable
|
205 |
*/
|
206 |
public void setActivePerspectiveMode(boolean perspectiveMode) { |
207 |
_perspectiveMode = perspectiveMode; |
208 |
} |
209 |
|
210 |
private void configureProjectionViewMode() { |
211 |
if (_perspectiveMode) {
|
212 |
System.out.println("Setting up perspective projection"); |
213 |
// this.m_canvas3d.getOSGViewer().setProjectionMatrixAsPerspective(
|
214 |
// arg0, arg1, arg2, arg3);
|
215 |
} else {
|
216 |
System.out.println("Setting up perspective projection"); |
217 |
this._canvas3d.getOSGViewer().setProjectionMatrixAsOrtho(-10, 10, |
218 |
-10, 10, 1, 1000); |
219 |
} |
220 |
} |
221 |
|
222 |
public DefaultView3DPanel getView() {
|
223 |
return _view;
|
224 |
} |
225 |
|
226 |
public void setView(DefaultView3DPanel view) { |
227 |
_view = view; |
228 |
} |
229 |
|
230 |
public IViewerContainer getOrCreateCanvas3D() {
|
231 |
|
232 |
// create libJOSG objects
|
233 |
if (_canvas3d == null) { |
234 |
try {
|
235 |
_terrainViewer = new TerrainViewer() {
|
236 |
// Hack to allow stereo in animation.
|
237 |
@Override
|
238 |
public void frame() { |
239 |
try {
|
240 |
setFusionDistance((float) this.getTerrain(0) |
241 |
.getZoom()); |
242 |
} catch (ChildIndexOutOfBoundsExceptions e) {
|
243 |
; |
244 |
} |
245 |
super.frame();
|
246 |
} |
247 |
}; |
248 |
} catch (NodeException e1) {
|
249 |
_logger.error("Command: " + "Error creating planer viewer.", e1); |
250 |
} |
251 |
|
252 |
// Getting 3D preferences
|
253 |
boolean compatibilitySwing = false; |
254 |
compatibilitySwing = _prefs.getBoolean("compatibilitySwing",
|
255 |
compatibilitySwing); |
256 |
|
257 |
if (compatibilitySwing) {
|
258 |
_canvas3d = ViewerFactory.getInstance() |
259 |
.createViewer(ViewerFactory.VIEWER_TYPE.JPANEL_VIEWER, |
260 |
_terrainViewer); |
261 |
} else {
|
262 |
_canvas3d = ViewerFactory.getInstance() |
263 |
.createViewer(ViewerFactory.VIEWER_TYPE.CANVAS_VIEWER, |
264 |
_terrainViewer); |
265 |
} |
266 |
|
267 |
ViewerFactory.getInstance().addViewerToAnimator(_canvas3d); |
268 |
ViewerFactory.getInstance().startAnimator(); |
269 |
|
270 |
this.configureProjectionViewMode();
|
271 |
|
272 |
_canvas3d.getOSGViewer().setDisplaySettings(new DisplaySettings());
|
273 |
|
274 |
if (getBackGroundColor() != null) { |
275 |
float r, g, b, a;
|
276 |
r = ((float) _backgroundColor.getRed()) / 255.0f; |
277 |
g = ((float) _backgroundColor.getGreen()) / 255.0f; |
278 |
b = ((float) _backgroundColor.getBlue()) / 255.0f; |
279 |
a = ((float) _backgroundColor.getAlpha()) / 255.0f; |
280 |
_terrainViewer.setClearColor(r, g, b, a); |
281 |
} |
282 |
|
283 |
try {
|
284 |
if (_terrainType == Terrain.CoordinateSystemType.PROJECTED) {
|
285 |
try {
|
286 |
_terrain = new Terrain("Earth", _terrainType, "WKT", |
287 |
"EPSG:23030", planeGlobalBounds.getMinX(),
|
288 |
planeGlobalBounds.getMinY(), |
289 |
planeGlobalBounds.getMaxX(), |
290 |
planeGlobalBounds.getMaxY(), 6378137.0,
|
291 |
6356752.3142);
|
292 |
|
293 |
} catch (TerrainExtentException e) {
|
294 |
// TODO Auto-generated catch block
|
295 |
e.printStackTrace(); |
296 |
} |
297 |
} else {
|
298 |
_terrain = new Terrain();
|
299 |
} |
300 |
|
301 |
_terrain.setRasterTerrainTechnique(Terrain.TerrainTechnique.SHADER); |
302 |
/*
|
303 |
* _terrain = new Terrain();
|
304 |
* _terrain.setCoordinateSystemType(_terrainType); if
|
305 |
* (_terrainType == Terrain.CoordinateSystemType.PROJECTED) {
|
306 |
* _terrain.setCoordinateSystemName("EPSG:23030");
|
307 |
* _terrain.setExtent(CacheService.planeGlobalBounds.getMinX(),
|
308 |
* CacheService.planeGlobalBounds.getMinY(),
|
309 |
* CacheService.planeGlobalBounds.getMaxX(),
|
310 |
* CacheService.planeGlobalBounds.getMaxY()); }
|
311 |
*/
|
312 |
|
313 |
_terrainDataManager = new JavaDataDriver();
|
314 |
_canvas3d.getOSGViewer() |
315 |
.addUpdateOperation(_terrainDataManager); |
316 |
|
317 |
_terrainLayerManager = new LayerManager();
|
318 |
_terrain.setLayerManager(_terrainLayerManager); |
319 |
|
320 |
_terrainViewer.addTerrain(_terrain); |
321 |
|
322 |
_manager = Gvsig3DLocator.getManager(); |
323 |
|
324 |
Hud hud = _manager |
325 |
.createDefaultHudInstance(_canvas3d, _terrain); |
326 |
|
327 |
try {
|
328 |
_terrainViewer.addNodeToCameraHUD(hud); |
329 |
} catch (NodeException e) {
|
330 |
_logger.error("Command: " + "Error adding node to hud.", e); |
331 |
} |
332 |
|
333 |
_canvas3d.addKeyListener(new ViewerStateListener(_canvas3d
|
334 |
.getOSGViewer())); |
335 |
|
336 |
if (_canvas3d.getOSGViewer().getCameraManipulator() != null) |
337 |
_canvas3d.getOSGViewer().getCameraManipulator() |
338 |
.computeHomePosition(); |
339 |
|
340 |
Camera cam; |
341 |
cam = new Camera();
|
342 |
if (_projectCamera == null) { |
343 |
|
344 |
if (_terrainType == Terrain.CoordinateSystemType.PROJECTED) {
|
345 |
Envelope3D extent = new Envelope3D(_terrain.xMin(),
|
346 |
_terrain.yMin(), 0, _terrain.xMax(),
|
347 |
_terrain.yMax(), 0);
|
348 |
double width = extent.xMax() - extent.xMin();
|
349 |
double length = extent.yMax() - extent.yMin();
|
350 |
|
351 |
double height = Math.sqrt(width * width + length |
352 |
* length) * 10f;
|
353 |
cam.setViewByLookAt((float) (extent.xMax() / 2.0), |
354 |
(float) (extent.yMax() / 2.0), (float) height, |
355 |
(float) (extent.xMax() / 2.0), |
356 |
(float) (extent.yMax() / 2.0), 0f, 0f, 1f, 0f); |
357 |
cam.setViewByLookAt(0, 0, 5000000 * 4.6, 0, 0, 0, 0, 1, |
358 |
0);
|
359 |
|
360 |
_terrainViewer.getTerrainCameraManipulator() |
361 |
.setMinimumDistance(10);
|
362 |
_terrainViewer.getTerrainCameraManipulator() |
363 |
.setMaximumDistance(5000000 * 12); |
364 |
} else {
|
365 |
cam.setViewByLookAt( |
366 |
_terrain.getRadiusEquatorial() * 8.0, 0, 0, 0, |
367 |
0, 0, 0, 0, 1); |
368 |
|
369 |
_terrainViewer.getTerrainCameraManipulator() |
370 |
.setMinimumDistance(100);
|
371 |
_terrainViewer |
372 |
.getTerrainCameraManipulator() |
373 |
.setMaximumDistance( |
374 |
(float) (_terrain.getRadiusEquatorial() * 10.0)); |
375 |
} |
376 |
|
377 |
_projectCamera = new ProjectCamera();
|
378 |
_projectCamera.setCamera(_terrainViewer.getCamera()); |
379 |
|
380 |
} else {
|
381 |
cam = _projectCamera.getCamera(); |
382 |
} |
383 |
|
384 |
_terrainViewer.setCamera(cam); |
385 |
|
386 |
} catch (NodeException e) {
|
387 |
} |
388 |
|
389 |
// _canvas3d.addKeyListener(new Key3DListener(_terrain));
|
390 |
} |
391 |
|
392 |
return _canvas3d;
|
393 |
|
394 |
} |
395 |
|
396 |
public JavaDataDriver getTerrainDataManager() {
|
397 |
|
398 |
return _terrainDataManager;
|
399 |
|
400 |
} |
401 |
|
402 |
public LayerManager getTerrainLayerManager() {
|
403 |
|
404 |
return _terrainLayerManager;
|
405 |
|
406 |
} |
407 |
|
408 |
public TerrainViewer getTerrainViewer() {
|
409 |
return _terrainViewer;
|
410 |
} |
411 |
|
412 |
public void setTerrainViewer(TerrainViewer terrainViewer) { |
413 |
_terrainViewer = terrainViewer; |
414 |
} |
415 |
|
416 |
public void setPropertiesCreated(boolean value) { |
417 |
this._properties = value;
|
418 |
} |
419 |
|
420 |
public boolean getPropertiesCreated() { |
421 |
return _properties;
|
422 |
} |
423 |
|
424 |
public void setAnimated(boolean isAnimated) { |
425 |
this._isAnimated = isAnimated;
|
426 |
} |
427 |
|
428 |
public boolean getAnimated() { |
429 |
return _isAnimated;
|
430 |
} |
431 |
|
432 |
public void loadFromState(PersistentState state) |
433 |
throws PersistenceException {
|
434 |
|
435 |
super.loadFromState(state);
|
436 |
_backgroundColor = (Color) state.get(BACKGROUNDCOLOR);
|
437 |
_terrainType = state.getInt(TERRAINTYPE); |
438 |
_projectCamera = (ProjectCamera) state.get(PROJECTCAMERA); |
439 |
|
440 |
} |
441 |
|
442 |
public void saveToState(PersistentState state) throws PersistenceException { |
443 |
|
444 |
super.saveToState(state);
|
445 |
state.set(BACKGROUNDCOLOR, _backgroundColor); |
446 |
state.set(TERRAINTYPE, _terrainType); |
447 |
//Save the camera position
|
448 |
_projectCamera = new ProjectCamera();
|
449 |
_projectCamera.setCamera(_terrainViewer.getCamera()); |
450 |
state.set(PROJECTCAMERA, _projectCamera); |
451 |
|
452 |
} |
453 |
|
454 |
public static class RegisterPersistence implements Callable { |
455 |
|
456 |
public Object call() throws Exception { |
457 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
458 |
|
459 |
DynStruct definition = manager |
460 |
.getDefinition(PERSISTENCE_DEFINITION_NAME); |
461 |
if (definition == null) { |
462 |
definition = manager |
463 |
.addDefinition(DefaultView3DDocument.class, |
464 |
PERSISTENCE_DEFINITION_NAME, |
465 |
PERSISTENCE_DEFINITION_NAME |
466 |
+ " Persistence definition", null, null); |
467 |
} |
468 |
// Description
|
469 |
definition.addDynFieldObject(BACKGROUNDCOLOR) |
470 |
.setClassOfValue(Color.class).setMandatory(false); |
471 |
definition.addDynFieldInt(TERRAINTYPE).setMandatory(true);
|
472 |
definition.addDynFieldObject(PROJECTCAMERA) |
473 |
.setClassOfValue(ProjectCamera.class).setMandatory(true);
|
474 |
|
475 |
return Boolean.TRUE; |
476 |
} |
477 |
|
478 |
} |
479 |
|
480 |
} |