/
SceneCoordsHelper.java
236 lines (224 loc) · 9.24 KB
/
SceneCoordsHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package gov.pnnl.svf.util;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GL2ES2;
import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
import com.jogamp.opengl.glu.gl2.GLUgl2;
import gov.pnnl.svf.scene.Scene;
import gov.pnnl.svf.scene.SceneExt;
import java.awt.Point;
import java.nio.FloatBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.math.geometry.Vector3D;
/**
* Helper class used to encapsulate the functionality for getting scene
* coordinates. This class is not thread safe.
*
* @author Amelia Bleeker
*/
public class SceneCoordsHelper {
private static final Logger logger = Logger.getLogger(SceneCoordsHelper.class.getName());
private final SceneExt scene;
private final double[] window = new double[3];
private final FloatBuffer z = FloatBuffer.allocate(1);
private final double[] position = new double[3];
private final int[] viewport = new int[4];
private final double[] modelview = new double[]{
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0};
private final double[] projection = new double[]{
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0};
/**
* Constructor
*
* @param scene reference to the scene
*
* @throws NullPointerException if the scene argument is null
*/
public SceneCoordsHelper(final Scene scene) {
super();
if (scene == null) {
throw new NullPointerException("scene");
}
this.scene = scene.getExtended();
}
/**
* Get the location of the pick in scene coordinates. The depth buffer must
* be utilized for this function to work. The GL context must be current.
*
* @param gl reference to gl
* @param glu reference to glu
* @param x the x screen location
* @param y the y screen location
*
* @return the location of the pick or zero vector if it failed
*/
public Vector3D unProject(final GL2 gl, final GLUgl2 glu, final int x, final int y) {
z.clear();
gl.glGetDoublev(GLMatrixFunc.GL_MODELVIEW_MATRIX, modelview, 0);
gl.glGetDoublev(GLMatrixFunc.GL_PROJECTION_MATRIX, projection, 0);
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
window[0] = x;
window[1] = y;
gl.glReadPixels(x, y, 1, 1, GL2ES2.GL_DEPTH_COMPONENT, GL.GL_FLOAT, z);
window[2] = z.get();
return unProject(window[0], window[1], window[2], modelview, projection, viewport);
}
/**
* Un-project the screen coordinates to scene coordinates without the GL
* context being current. This will use the last filled or identity
* matrices.
*
* @param x the x location on the screen
* @param y the y location on the screen
* @param z the z depth in the scene
* @param viewport the viewport (x, y, width, height)
*
* @return the unprojected scene coordinate or zero vector if failed
*/
public Vector3D unProject(final double x, final double y, final double z, final int[] viewport) {
return unProject(x, y, z, modelview, projection, viewport);
}
/**
* Un-project the screen coordinates to scene coordinates without the GL
* context being current.
*
* @param x the x location on the screen
* @param y the y location on the screen
* @param z the z depth in the scene
* @param modelview the modelview matrix
* @param projection the projection matrix
* @param viewport the viewport (x, y, width, height)
*
* @return the unprojected scene coordinate or zero vector if failed
*/
public Vector3D unProject(final double x, final double y, final double z, final double[] modelview, final double[] projection, final int[] viewport) {
synchronized (this) {
if (scene.getGLU().gluUnProject(x, viewport[3] - y, z, modelview, 0, projection, 0, viewport, 0, position, 0)) {
return new Vector3D(position[0], position[1], position[2]);
} else {
if (scene.getSceneBuilder().isVerbose()) {
logger.log(Level.WARNING, "Unable to unproject the scene pick.");
}
return Vector3D.ZERO;
}
}
}
/**
* Un-project the screen coordinates to scene coordinates without the GL
* context being current. This will use the last filled or identity
* matrices.
*
* @param x the x location on the screen
* @param y the y location on the screen
* @param z the z depth in the scene
* @param viewport the viewport (x, y, width, height)
*
* @return the unprojected scene coordinate or zero vector if failed
*/
public Vector3D unProject(final float x, final float y, final float z, final int[] viewport) {
return unProject(x, y, z, modelview, projection, viewport);
}
/**
* Un-project the screen coordinates to scene coordinates without the GL
* context being current.
*
* @param x the x location on the screen
* @param y the y location on the screen
* @param z the z depth in the scene
* @param modelview the modelview matrix
* @param projection the projection matrix
* @param viewport the viewport (x, y, width, height)
*
* @return the unprojected scene coordinate or zero vector if failed
*/
public Vector3D unProject(final float x, final float y, final float z, final double[] modelview, final double[] projection, final int[] viewport) {
synchronized (this) {
if (scene.getGLU().gluUnProject(x, viewport[3] - y, z, modelview, 0, projection, 0, viewport, 0, position, 0)) {
return new Vector3D(position[0], position[1], position[2]);
} else {
if (scene.getSceneBuilder().isVerbose()) {
logger.log(Level.WARNING, "Unable to unproject the scene pick.");
}
return Vector3D.ZERO;
}
}
}
/**
* Get the location of the pick in screen coordinates. The depth buffer must
* be utilized for this function to work. The GL context must be current.
*
* @param gl reference to gl
* @param glu reference to glu
* @param x the x scene location
* @param y the y scene location
* @param z the z scene location
*
* @return the projected screen coordinate or zero if failed
*/
public Point project(final GL2 gl, final GLUgl2 glu, final int x, final int y, final int z) {
gl.glGetDoublev(GLMatrixFunc.GL_MODELVIEW_MATRIX, modelview, 0);
gl.glGetDoublev(GLMatrixFunc.GL_PROJECTION_MATRIX, projection, 0);
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
position[0] = x;
position[1] = y;
position[2] = z;
return project(position[0], position[1], position[2], modelview, projection, viewport);
}
/**
* Project the scene coordinates to screen coordinates without the GL
* context being current.
*
* @param x the x location in the scene
* @param y the y location in the scene
* @param z the z location in the scene
* @param modelview the modelview matrix
* @param projection the projection matrix
* @param viewport the viewport (x, y, width, height)
*
* @return the projected screen coordinate or zero if failed
*/
public Point project(final double x, final double y, final double z, final double[] modelview, final double[] projection, final int[] viewport) {
synchronized (this) {
if (scene.getGLU().gluProject(x, y, z, modelview, 0, projection, 0, viewport, 0, window, 0)) {
return new Point((int) window[0], (int) (viewport[3] - window[1]));
} else {
if (scene.getSceneBuilder().isVerbose()) {
logger.log(Level.WARNING, "Unable to project the scene pick.");
}
return new Point(0, 0);
}
}
}
/**
* Un-project the screen coordinates to scene coordinates without the GL
* context being current.
*
* @param x the x location in the scene
* @param y the y location in the scene
* @param z the z location in the scene
* @param modelview the modelview matrix
* @param projection the projection matrix
* @param viewport the viewport (x, y, width, height)
*
* @return the projected screen coordinate or zero if failed
*/
public Point project(final float x, final float y, final float z, final double[] modelview, final double[] projection, final int[] viewport) {
synchronized (this) {
if (scene.getGLU().gluProject(x, y, z, modelview, 0, projection, 0, viewport, 0, window, 0)) {
return new Point((int) window[0], (int) (viewport[3] - window[1]));
} else {
if (scene.getSceneBuilder().isVerbose()) {
logger.log(Level.WARNING, "Unable to project the scene pick.");
}
return new Point(0, 0);
}
}
}
}