Statistics
| Revision:

gvsig-3d / 2.1 / trunk / org.gvsig.gvsig3d / org.gvsig.gvsig3d.lib / org.gvsig.gvsig3d.lib.impl / src / main / java / org / gvsig / gvsig3d / impl / compass / CompassImpl.java @ 385

History | View | Annotate | Download (5 KB)

1
/* gvSIG 3D extension for gvSIG
2
 *
3
 * Copyright (C) 2012 Prodevelop.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Prodevelop, S.L.
22
 *   Pza. Don Juan de Villarrasa, 14 - 5
23
 *   46001 Valencia
24
 *   Spain
25
 *
26
 *   +34 963 510 612
27
 *   +34 963 510 968
28
 *   prode@prodevelop.es
29
 *   http://www.prodevelop.es
30
 */
31
/*
32
 * AUTHORS:
33
 * 2012 AI2 - Instituto Universitario de Automatica e Informatica Industrial.
34
 * Universitat Politecnica de Valencia (UPV)
35
 * http://www.ai2.upv.es
36
 */
37

    
38

    
39
package org.gvsig.gvsig3d.impl.compass;
40

    
41
import org.gvsig.gvsig3d.compass.Compass;
42
import org.gvsig.gvsig3d.resources.ResourcesFactory;
43
import org.gvsig.osgvp.core.osg.Matrix;
44
import org.gvsig.osgvp.core.osg.MatrixTransform;
45
import org.gvsig.osgvp.core.osg.PositionAttitudeTransform;
46
import org.gvsig.osgvp.core.osg.Quat;
47
import org.gvsig.osgvp.core.osg.Texture2D;
48
import org.gvsig.osgvp.core.osg.Vec3;
49
import org.gvsig.osgvp.core.osg.Vec4;
50
import org.gvsig.osgvp.exceptions.node.NodeException;
51
import org.gvsig.osgvp.features.Polygon;
52
import org.gvsig.osgvp.features.Quad;
53
import org.gvsig.osgvp.terrain.Terrain;
54
import org.gvsig.osgvp.viewer.Camera;
55

    
56
/**
57
 * Class to represent 3D axis (x,y,z) in the scene
58
 * 
59
 * @author Jesus Zarzoso- jzarzoso@ai2.upv.es
60
 * @version $Id$
61
 * 
62
 */
63
public class CompassImpl extends PositionAttitudeTransform implements Compass {
64

    
65
        private MatrixTransform _matrix;
66
        private Matrix _mat;
67
        private int _cameraType;
68
        private Terrain _terrain;
69

    
70
        public CompassImpl(Terrain terrain) throws NodeException {
71

    
72
                super();
73
                _matrix = new MatrixTransform();
74
                _terrain = terrain;
75
                Polygon pol = new Polygon();
76
                Quad quad = new Quad();
77
                Texture2D tex = new Texture2D();
78
                _cameraType = Compass.Mode.FLAT;
79

    
80
                // quad.addVertex(new Vec3(-0.5,-0.5,0.0), new Vec4(0.0,1.0,1.0,0.5));
81
                // quad.addVertex(new Vec3(0.5,-0.5,0.0), new Vec4(1.0,0.0,1.0,0.5));
82
                // quad.addVertex(new Vec3(0.5,0.5,0.0), new Vec4(1.0,1.0,0.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

    
85
                pol.addVertex(new Vec3(-0.5, -0.5, 0.0), new Vec4(1.0, 1.0, 1.0, 1));
86
                pol.addVertex(new Vec3(0.5, -0.5, 0.0), new Vec4(1.0, 1.0, 1.0, 1));
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

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

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

    
114
        public void setPanetType(int type) {
115

    
116
                _cameraType = type;
117

    
118
        }
119

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

    
127
                Matrix auxmat = new Matrix();
128

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

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

    
144
                        double vX, vY;
145

    
146
                        vX = v.dotProduct(e) / e.length();
147
                        vY = v.dotProduct(n) / n.length();
148

    
149
                        double ang = Math.atan2(vX, vY);
150

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

    
157
                else {
158

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

    
164
                        double vX = look.dotProduct(east);
165
                        double vY = look.dotProduct(north);
166

    
167
                        double angle = Math.atan2(vX, vY);
168

    
169
                        Quat quat = new Quat();
170
                        quat.makeRotate(angle, new Vec3(0, 0, 1));
171

    
172
                        auxmat.setRotate(quat);
173
                        _matrix.setMatrix(auxmat);
174

    
175
                }
176

    
177
        }
178

    
179
}