Statistics
| Revision:

root / trunk / libraries / lib3DMap / src / org / gvsig / gvsig3d / gui / Compass.java @ 26227

History | View | Annotate | Download (4.97 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *  osgVP. OSG Virtual Planets.
3
 *
4
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
5
 * of the Valencian Government (CIT)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
 * MA  02110-1301, USA.
21
 *
22
 */
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 Instituto de Automática e Informática Industrial, UPV.
26
 */
27

    
28
package org.gvsig.gvsig3d.gui;
29

    
30
import java.io.FileNotFoundException;
31

    
32
import org.gvsig.osgvp.Image;
33
import org.gvsig.osgvp.Matrix;
34
import org.gvsig.osgvp.MatrixTransform;
35
import org.gvsig.osgvp.PositionAttitudeTransform;
36
import org.gvsig.osgvp.Quat;
37
import org.gvsig.osgvp.Texture2D;
38
import org.gvsig.osgvp.Vec3;
39
import org.gvsig.osgvp.Vec4;
40
import org.gvsig.osgvp.exceptions.image.ImageException;
41
import org.gvsig.osgvp.exceptions.image.LoadImageException;
42
import org.gvsig.osgvp.exceptions.node.NodeException;
43
import org.gvsig.osgvp.features.Polygon;
44
import org.gvsig.osgvp.features.Quad;
45
import org.gvsig.osgvp.planets.Planet;
46
import org.gvsig.osgvp.util.Util;
47
import org.gvsig.osgvp.viewer.Camera;
48

    
49
import com.iver.ai2.gvsig3d.resources.ResourcesFactory;
50

    
51
/**
52
 *  @
53
 */
54

    
55
/**
56
 * Class to represent 3D axis (x,y,z) in the scene
57
 */
58
public class Compass extends PositionAttitudeTransform {
59

    
60
        private MatrixTransform _matrix;
61
        private Matrix _mat;
62
        private int _cameraType;
63
        private Planet _planet;
64

    
65
        public static class Mode {
66
                // default
67
                public static int FLAT = 0;
68
                public static int SPHERIC = 1;
69

    
70
        }
71

    
72
        public Compass(Planet planet) throws NodeException {
73

    
74
                super();
75
                _matrix = new MatrixTransform();
76
                _planet = planet;
77
                Polygon pol = new Polygon();
78
                Quad quad = new Quad();
79
                Texture2D tex = new Texture2D();
80
                _cameraType = Compass.Mode.FLAT;
81

    
82
                // quad.addVertex(new Vec3(-0.5,-0.5,0.0), new Vec4(0.0,1.0,1.0,0.5));
83
                // quad.addVertex(new Vec3(0.5,-0.5,0.0), new Vec4(1.0,0.0,1.0,0.5));
84
                // quad.addVertex(new Vec3(0.5,0.5,0.0), new Vec4(1.0,1.0,0.0,0.5));
85
                // quad.addVertex(new Vec3(-0.5,0.5,0.0), new Vec4(1.0,0.0,1.0,0.5));
86

    
87
                pol.addVertex(new Vec3(-0.5, -0.5, 0.0), new Vec4(1.0, 1.0, 1.0, 1));
88
                pol.addVertex(new Vec3(0.5, -0.5, 0.0), new Vec4(1.0, 1.0, 1.0, 1));
89
                pol.addVertex(new Vec3(0.5, 0.5, 0.0), new Vec4(1.0, 1.0, 1.0, 1));
90
                pol.addVertex(new Vec3(-0.5, 0.5, 0.0), new Vec4(1.0, 1.0, 1.0, 1));
91

    
92
                // StateSet ss =quad.getOrCreateStateSet();
93
                // Image im;
94
                // try {
95
                // im = new Image(ResourcesFactory.getResourcePath("compass.png"));
96
                // tex.setImage(im);
97
                // } catch (LoadImageException e) {
98
                // // TODO Auto-generated catch block
99
                // e.printStackTrace();
100
                // } catch (ImageException e) {
101
                // // TODO Auto-generated catch block
102
                // e.printStackTrace();
103
                // }
104
                // quad.getOrCreateStateSet().setTextureMode(0, Node.Mode.ON |
105
                // Node.Mode.OVERRIDE);
106
                // quad.getOrCreateStateSet().setTexture2D(tex,0 , Node.Mode.ON);
107
                // _matrix.addChild(quad);
108

    
109
                pol.setTexture(ResourcesFactory.getResourcePath("compass.png"));
110
                pol.setEnabledBlending(true);
111
                _matrix.addChild(pol);
112
                _mat = new Matrix();
113
                this.addChild(_matrix);
114
        }
115

    
116
        public void setPanetType(int type) {
117

    
118
                _cameraType = type;
119

    
120
        }
121

    
122
        /**
123
         * Update compass rotation respect a camera.
124
         * 
125
         * @param camera
126
         */
127
        public void update(Camera camera) {
128

    
129
                Matrix auxmat = new Matrix();
130

    
131
                if (_cameraType == Mode.SPHERIC) {
132
                        Vec3 o, i, n, e, z, v, iTo;
133

    
134
                        o = camera.getEye();
135
                        i = _planet.convertXYZToLatLongHeight(camera.getEye());
136
                        i = _planet.convertLatLongHeightToXYZ(new Vec3(i.x(), i.y(), 0));
137
                        iTo = o.sub(i);
138
                        Vec3 northPole = _planet.convertLatLongHeightToXYZ(new Vec3(90, 0,
139
                                        0));
140
                        z = i.sub(northPole);
141
                        e = iTo.crossProduct(z);
142
                        n = iTo.crossProduct(e);
143
                        v = camera.getCenter().sub(camera.getEye());
144
                        v.normalize();
145

    
146
                        double vX, vY;
147

    
148
                        vX = v.dotProduct(e) / e.length();
149
                        vY = v.dotProduct(n) / n.length();
150

    
151
                        double ang = Math.atan2(vX, vY);
152

    
153
                        Quat quat = new Quat();
154
                        quat.makeRotate(ang, new Vec3(0, 0, 1));
155
                        auxmat.setRotate(quat);
156
                        _matrix.setMatrix(auxmat);
157
                }
158

    
159
                else {
160

    
161
                        Vec3 look = camera.getCenter().sub(camera.getEye());
162
                        look.normalize();
163
                        Vec3 east = new Vec3(1, 0, 0);
164
                        Vec3 north = new Vec3(0, 1, 0);
165

    
166
                        double vX = look.dotProduct(east);
167
                        double vY = look.dotProduct(north);
168

    
169
                        double angle = Math.atan2(vX, vY);
170

    
171
                        Quat quat = new Quat();
172
                        quat.makeRotate(angle, new Vec3(0, 0, 1));
173

    
174
                        auxmat.setRotate(quat);
175
                        _matrix.setMatrix(auxmat);
176

    
177
                }
178

    
179
        }
180

    
181
}