Statistics
| Revision:

gvsig-3d / 2.0 / trunk / org.gvsig.gvsig3d.app / org.gvsig.gvsig3d.app.extension / src / main / java / org / gvsig / gvsig3d / app / extension / DefaultView3DDocument.java @ 257

History | View | Annotate | Download (9.13 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.util.prefs.Preferences;
26

    
27
import org.gvsig.app.ApplicationLocator;
28
import org.gvsig.app.project.ProjectPreferences;
29
import org.gvsig.app.project.documents.DocumentManager;
30
import org.gvsig.app.project.documents.view.BaseViewDocument;
31
import org.gvsig.fmap.mapcontext.MapContext;
32
import org.gvsig.fmap.mapcontext.ViewPort;
33
import org.gvsig.gvsig3d.Gvsig3DException;
34
import org.gvsig.gvsig3d.Gvsig3DLocator;
35
import org.gvsig.gvsig3d.map3d.MapContext3D;
36
import org.gvsig.osgvp.exceptions.node.NodeException;
37
import org.gvsig.osgvp.terrain.Extent;
38
import org.gvsig.osgvp.terrain.JavaDataDriver;
39
import org.gvsig.osgvp.terrain.LayerManager;
40
import org.gvsig.osgvp.terrain.Terrain;
41
import org.gvsig.osgvp.terrain.TerrainViewer;
42
import org.gvsig.osgvp.viewer.Camera;
43
import org.gvsig.osgvp.viewer.CanvasViewer;
44
import org.gvsig.osgvp.viewer.IViewerContainer;
45
import org.gvsig.osgvp.viewer.ViewerFactory;
46
import org.gvsig.osgvp.viewer.ViewerStateListener;
47
import org.gvsig.tools.locator.LocatorException;
48
import org.gvsig.tools.persistence.PersistentState;
49
import org.gvsig.tools.persistence.exception.PersistenceException;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53
/**
54
 * @author gvSIG Team
55
 * @version $Id$
56
 * 
57
 */
58
public class DefaultView3DDocument extends BaseViewDocument {
59

    
60
        private static final long serialVersionUID = 676711512482344764L;
61

    
62
        public static final String PERSISTENCE_DEFINITION_NAME = "DefaultView3DDocument";
63

    
64
        private IViewerContainer _canvas3d = null;
65
        private TerrainViewer _terrainViewer = null;
66
        private Terrain _terrain = null;
67
        // private ProjectCamera _projectCamera = null;
68
        private JavaDataDriver _terrainDataManager = null;
69
        private LayerManager _terrainLayerManager = null;
70
        private int _terrainType = Terrain.CoordinateSystemType.PROJECTED; // spheric
71
        // or
72
        // plane
73
        private Color _backgroundColor;
74
        private float _verticalExaggeration = 1;
75
        private DefaultView3DPanel _view = null;
76
        private static Preferences _prefs = Preferences.userRoot().node(
77
                        "gvsig.configuration.3D");
78
        private boolean _prespectiveMode = true;
79
        private static Logger _logger = LoggerFactory
80
                        .getLogger(DefaultView3DDocument.class.getName());
81

    
82
        public DefaultView3DDocument() {
83
                this(null);
84
        }
85

    
86
        public DefaultView3DDocument(DocumentManager factory) {
87
                super(factory);
88

    
89
                ProjectPreferences preferences = (ProjectPreferences) ApplicationLocator
90
                                .getManager().getPreferences("project");
91

    
92
        }
93

    
94
        public void saveToState(PersistentState state) throws PersistenceException {
95
                super.saveToState(state);
96
        }
97

    
98
        public void loadFromState(PersistentState state)
99
                        throws PersistenceException {
100
                super.loadFromState(state);
101
        }
102

    
103
        public String exportDocumentAsText() {
104
                // FIXME: jjdc PersistentManager getStateAsText o similar
105
                // PersistenceManager manager = ToolsLocator.getPersistenceManager();
106
                // PersistentState state = manager.getState(this);
107
                // return manager.getStateAsText(state);
108
                return null;
109
        }
110

    
111
        public void setStateFromText(String text) {
112
                // FIXME: jjdc PersistentManager getStateFromText o similar
113
                // PersistenceManager manager = ToolsLocator.getPersistenceManager();
114
                // PersistentState state = manager.getStateFromText(text);
115
                // this.loadFromState(state);
116
        }
117

    
118
        public int getTerrainType() {
119
                return _terrainType;
120
        }
121

    
122
        public void setTerrainType(int type) {
123
                _terrainType = type;
124
        }
125

    
126
        public float getVerticalExaggeration() {
127
                return _verticalExaggeration;
128
        }
129

    
130
        public void setVerticalExaggeration(float exaggeration) {
131
                _verticalExaggeration = exaggeration;
132
        }
133

    
134
        public Color getBackGroundColor() {
135
                return _backgroundColor;
136
        }
137

    
138
        public void setBackGroundColor(Color backGroundColor) {
139
                _backgroundColor = backGroundColor;
140
                if ((_backgroundColor != null) && (_terrainViewer != null)) {
141
                        float r, g, b, a;
142
                        r = ((float) _backgroundColor.getRed()) / 255.0f;
143
                        g = ((float) _backgroundColor.getGreen()) / 255.0f;
144
                        b = ((float) _backgroundColor.getBlue()) / 255.0f;
145
                        a = ((float) _backgroundColor.getAlpha()) / 255.0f;
146

    
147
                        _terrainViewer.setClearColor(r, g, b, a);
148
                }
149
        }
150

    
151
        public Terrain getTerrain() {
152
                return _terrain;
153
        }
154

    
155
        public void setTerrain(Terrain terrain) {
156
                _terrain = terrain;
157
        }
158

    
159
        public IViewerContainer getCanvas3d() {
160
                return _canvas3d;
161
        }
162

    
163
        public void setCanvas3d(CanvasViewer canvas3d) {
164
                _canvas3d = canvas3d;
165
        }
166

    
167
        // private void setVerticalEx(float exa) {
168
        //
169
        // SingleLayerIterator lyrIterator = new SingleLayerIterator(
170
        // (FLayers3D) getMapContext().getLayers());
171
        // while (lyrIterator.hasNext()) {
172
        // FLayer lyr = lyrIterator.next();
173
        //
174
        // Layer3DProps props = Layer3DProps.getLayer3DProps(lyr);
175
        // int type = props.getType();
176
        // if (exa != props.getVerticalEx()) {
177
        // if (type == Layer3DProps.layer3DElevation) {
178
        // props.setVerticalEx(exa);
179
        // HeightfieldLayer layer = (HeightfieldLayer)props.getTerrainLayer();
180
        // if(layer == null) return;
181
        // layer.setVerticalExaggeration(exa);
182
        // _view.getTerrain().getLayerManager().updateLayers();
183
        // } else if (type == Layer3DProps.layer3DVector) {
184
        // props.setVerticalEx(exa);
185
        // }
186
        // }
187
        //
188
        // }
189
        // }
190

    
191
        public boolean isPrespectiveModeActive() {
192
                return _prespectiveMode;
193
        }
194

    
195
        /**
196
         * Active the prespective mode. If we disable the prespective mode, the
197
         * ortho mode will be active
198
         * 
199
         * @param prespectiveMode
200
         *            enable/disable
201
         */
202
        public void setActivePrespectiveMode(boolean prespectiveMode) {
203
                _prespectiveMode = prespectiveMode;
204
        }
205

    
206
        private void configureProjectionViewMode() {
207
                if (_prespectiveMode) {
208
                        System.out.println("Setting up prespective projection");
209
                        // this.m_canvas3d.getOSGViewer().setProjectionMatrixAsPerspective(
210
                        // arg0, arg1, arg2, arg3);
211
                } else {
212
                        System.out.println("Setting up prespective projection");
213
                        this._canvas3d.getOSGViewer().setProjectionMatrixAsOrtho(-10, 10,
214
                                        -10, 10, 1, 1000);
215
                }
216
        }
217

    
218
        public DefaultView3DPanel getView() {
219
                return _view;
220
        }
221

    
222
        public void setView(DefaultView3DPanel view) {
223
                _view = view;
224
        }
225

    
226
        public IViewerContainer getOrCreateCanvas3D() {
227

    
228
                if (_canvas3d == null) {
229
                        try {
230
                                _terrainViewer = new TerrainViewer();
231
                        } catch (NodeException e1) {
232

    
233
                        }
234

    
235
                        // System.out.println("TerrainViewer is: " + _terrainViewer);
236

    
237
                        _canvas3d = ViewerFactory.getInstance().createViewer(
238
                                        ViewerFactory.VIEWER_TYPE.CANVAS_VIEWER, _terrainViewer);
239

    
240
                        if (_canvas3d == null)
241
                                System.out.println("ERROR; CANVAS NULL");
242

    
243
                        ViewerFactory.getInstance().startAnimator();
244

    
245
                        try {
246

    
247
                                _terrain = new Terrain();
248

    
249
                                _terrainDataManager = new JavaDataDriver();
250
                                _canvas3d.getOSGViewer()
251
                                                .addUpdateOperation(_terrainDataManager);
252

    
253
                                _terrainLayerManager = new LayerManager();
254
                                _terrain.setLayerManager(_terrainLayerManager);
255

    
256
                                _terrainViewer.addTerrain(_terrain);
257

    
258
                                _canvas3d.addKeyListener(new ViewerStateListener(_canvas3d
259
                                                .getOSGViewer()));
260

    
261
                                if (_canvas3d.getOSGViewer().getCameraManipulator() != null)
262
                                        _canvas3d.getOSGViewer().getCameraManipulator()
263
                                                        .computeHomePosition();
264

    
265
                                Camera cam;
266
                                cam = new Camera();
267

    
268
                                if (_terrainType == Terrain.CoordinateSystemType.PROJECTED) {
269
                                        Extent extent = _terrain.getExtent();
270
                                        double width = extent.xMax() - extent.xMin();
271
                                        double length = extent.yMax() - extent.yMin();
272

    
273
                                        double height = Math.sqrt(width * width + length * length) * 10f;
274
                                        cam.setViewByLookAt((float) (extent.xMax() / 2.0),
275
                                                        (float) (extent.yMax() / 2.0), (float) height,
276
                                                        (float) (extent.xMax() / 2.0),
277
                                                        (float) (extent.yMax() / 2.0), 0f, 0f, 1f, 0f);
278
                                        cam.setViewByLookAt(0, 0, 5000000 * 4.6, 0, 0, 0, 0, 1, 0);
279

    
280
                                        _terrainViewer.getTerrainCameraManipulator()
281
                                                        .setMinimumDistance(10);
282
                                        _terrainViewer.getTerrainCameraManipulator()
283
                                                        .setMaximumDistance(5000000 * 10);
284
                                } else {
285
                                        cam.setViewByLookAt(_terrain.getRadiusEquatorial() * 8.0,
286
                                                        0, 0, 0, 0, 0, 0, 0, 1);
287

    
288
                                        _terrainViewer.getTerrainCameraManipulator()
289
                                                        .setMinimumDistance(100);
290
                                        _terrainViewer
291
                                                        .getTerrainCameraManipulator()
292
                                                        .setMaximumDistance(
293
                                                                        (float) (_terrain.getRadiusEquatorial() * 10.0));
294
                                }
295

    
296
                                _terrainViewer.setCamera(cam);
297

    
298
                        } catch (NodeException e) {
299
                        }
300

    
301
                }
302

    
303
                return _canvas3d;
304
        }
305

    
306
        public JavaDataDriver getTerrainDataManager() {
307

    
308
                return _terrainDataManager;
309

    
310
        }
311

    
312
        public LayerManager getTerrainLayerManager() {
313

    
314
                return _terrainLayerManager;
315

    
316
        }
317

    
318
}