-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathStereoOptionSceneController.java
More file actions
290 lines (253 loc) · 10.4 KB
/
StereoOptionSceneController.java
File metadata and controls
290 lines (253 loc) · 10.4 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright (C) 2012 United States Government as represented by the Administrator of the
* National Aeronautics and Space Administration.
* All Rights Reserved.
*/
package gov.nasa.worldwind;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.render.DrawContext;
import com.jogamp.opengl.*;
/**
* TODO: This file needs to be updated to implement "correct" stereo, as described at:
* http://www.orthostereo.com/geometryopengl.html
* <p/>
* <p/>
* This scene controller draws in stereo, either red-blue anaglyph or device supported if the display device provides
* stereo directly. It can also draw without applying stereo. To select stereo, prior to calling this class' constructor
* set the Java VM property <code>gov.nasa.worldwind.stereo.mode</code> to "device" for device supported stereo (if
* provided by the device) or "redblue" for red-blue anaglyph stereo. If the property is not set or is any other value,
* this class does not draw in stereo.
* <p/>
* The {@link WorldWindow} instance must support stereo in order to use device-supported stereo. A stereo
* <code>WorldWindow</code> is selected by specifying the Java VM property described above prior to creating it. See
* {@link gov.nasa.worldwind.awt.WorldWindowGLCanvas} for further details.
* <p/>
* Note: The logic and much of the code here was contributed by Xander Enzmann of Mitre Corporation.
*
* @author tag
* @version $Id: StereoOptionSceneController.java 1171 2013-02-11 21:45:02Z dcollins $
*/
public class StereoOptionSceneController extends BasicSceneController implements StereoSceneController
{
/**
* The default focus angle. May be specified in the WorldWind configuration file as the
* <code>gov.nasa.worldwind.StereoFocusAngle</code> property. The default if not specified in the configuration is
* 1.6 degrees.
*/
protected static final double DEFAULT_FOCUS_ANGLE = Configuration.getDoubleValue(AVKey.STEREO_FOCUS_ANGLE, 1.6);
/** The current stereo mode. May not be set to null; use {@link AVKey#STEREO_MODE_NONE} instead. */
protected String stereoMode = AVKey.STEREO_MODE_NONE;
/** The angle between eyes. Larger angles give increased 3D effect. */
protected Angle focusAngle = Angle.fromDegrees(DEFAULT_FOCUS_ANGLE);
/** Indicates whether left and right eye positions are swapped. */
protected boolean swapEyes = false;
/** Indicates the GL drawable capabilities. Non-null only after this scene controller draws once. */
protected GLCapabilitiesImmutable capabilities;
/** Indicates whether hardware device stereo is available. Valid only after this scene controller draws once. */
protected boolean hardwareStereo = false;
/**
* Indicates whether stereo is being applied, either because a stereo device is being used or a stereo mode is in
* effect. This field is included because the question is asked every frame, and tracking the answer via a boolean
* avoids the overhead of more complicated logic that determines the stereo-drawing implementation to call.
*/
protected boolean inStereo = false;
/** Constructs an instance and initializes its stereo mode to */
public StereoOptionSceneController()
{
String stereo = System.getProperty(AVKey.STEREO_MODE);
if ("redblue".equalsIgnoreCase(stereo))
this.setStereoMode(AVKey.STEREO_MODE_RED_BLUE);
else if ("device".equalsIgnoreCase(stereo))
this.setStereoMode(AVKey.STEREO_MODE_DEVICE);
}
public void setStereoMode(String mode)
{
this.stereoMode = mode != null ? mode : AVKey.STEREO_MODE_NONE;
// If device-implemented stereo is used, stereo is considered always in effect no matter what the stereo mode.
this.inStereo = this.isHardwareStereo() || AVKey.STEREO_MODE_RED_BLUE.equals(this.stereoMode);
}
public String getStereoMode()
{
return this.stereoMode;
}
/**
* {@inheritDoc} The default focus angle is 1.6 degrees.
*
* @param a the left-right eye direction difference. If null, the angle is set to 0.
*/
public void setFocusAngle(Angle a)
{
this.focusAngle = a != null ? a : Angle.ZERO;
}
public Angle getFocusAngle()
{
return this.focusAngle;
}
public void setSwapEyes(boolean swapEyes)
{
this.swapEyes = swapEyes;
}
public boolean isSwapEyes()
{
return this.swapEyes;
}
public boolean isHardwareStereo()
{
return this.hardwareStereo;
}
/**
* {@inheritDoc}
* <p/>
* If the display device is providing stereo -- {@link #isHardwareStereo()} is <code>true</code> -- this method
* returns true even if the stereo mode is {@link AVKey#STEREO_MODE_NONE}. In this case, individual stereo images
* are drawn for left and right eyes in order to prevent a blurred scene.
*/
public boolean isInStereo()
{
return this.inStereo;
}
@Override
protected void draw(DrawContext dc)
{
// Capture the capabilities actually in use.
if (this.capabilities == null)
{
this.capabilities = dc.getGLContext().getGLDrawable().getChosenGLCapabilities();
this.hardwareStereo = this.capabilities.getStereo();
this.inStereo = this.isHardwareStereo() ? true : this.isInStereo();
}
// If stereo isn't to be applied, just draw and return.
if (!isInStereo())
{
super.draw(dc);
return;
}
// Check if pitch is in correct range (50 - 90 degrees) for current stereo implementation to
// work correctly (temporary hack)
View dcView = dc.getView();
Boolean pitchInRange = (dcView.getPitch().compareTo(Angle.fromDegrees(50)) > 0
&& dcView.getPitch().compareTo(Angle.POS90) < 0);
if (AVKey.STEREO_MODE_DEVICE.equals(this.stereoMode) && this.isHardwareStereo() && pitchInRange)
this.doDrawToStereoDevice(dc);
else if (AVKey.STEREO_MODE_RED_BLUE.equals(this.stereoMode) && pitchInRange)
this.doDrawStereoRedBlue(dc);
else // AVKey.STEREO_MODE_NONE
this.doDrawStereoNone(dc);
}
/**
* Implement no stereo ("Mono") while using a stereo device.
* <p/>
* Note that this method draws the image twice, once to each of the left and right eye buffers, even when stereo is
* not in effect. This is to prevent the stereo device from drawing blurred scenes.
*
* @param dc the current draw context.
*/
protected void doDrawStereoNone(DrawContext dc)
{
// If running on a stereo device but want to draw a normal image, both buffers must be filled or the
// display will be blurry.
GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility.
gl.glDrawBuffer(GL2.GL_BACK_LEFT);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
super.draw(dc);
gl.glDrawBuffer(GL2.GL_BACK_RIGHT);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
super.draw(dc);
}
/**
* Implement stereo using the red-blue anaglyph technique.
*
* @param dc the current draw context.
*/
protected void doDrawStereoRedBlue(DrawContext dc)
{
GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility.
View dcView = dc.getView();
// Draw the left eye
if (this.isSwapEyes())
{
if (this.isHardwareStereo())
gl.glDrawBuffer(GL2.GL_BACK_RIGHT);
gl.glColorMask(false, true, true, true); // right eye in green/blue
}
else
{
if (this.isHardwareStereo())
gl.glDrawBuffer(GL2.GL_BACK_LEFT);
gl.glColorMask(true, false, false, true); // left eye in red only
}
if (this.isHardwareStereo())
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
super.draw(dc);
// Move the view to the right eye
Angle viewHeading = dcView.getHeading();
dcView.setHeading(dcView.getHeading().subtract(this.getFocusAngle()));
dcView.apply(dc);
// Draw the right eye frame green and blue only
try
{
gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
if (this.isSwapEyes())
{
if (this.isHardwareStereo())
gl.glDrawBuffer(GL2.GL_BACK_RIGHT);
gl.glColorMask(true, false, false, true); // right eye in red only
}
else
{
if (this.isHardwareStereo())
gl.glDrawBuffer(GL2.GL_BACK_LEFT);
gl.glColorMask(false, true, true, true); // right eye in green/blue
}
if (this.isHardwareStereo())
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
super.draw(dc);
}
finally
{
// Restore the original view heading
dcView.setHeading(viewHeading);
dcView.apply(dc);
gl.glColorMask(true, true, true, true);
}
}
/**
* Implement stereo using the stereo-enabled graphics device. The mode has an effect only if the display device
* implements stereo.
*
* @param dc the current draw context.
*/
protected void doDrawToStereoDevice(DrawContext dc)
{
GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility.
View dcView = dc.getView();
// Draw the left eye
if (this.isSwapEyes())
gl.glDrawBuffer(GL2.GL_BACK_RIGHT);
else
gl.glDrawBuffer(GL2.GL_BACK_LEFT);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
super.draw(dc);
// Move the view to the right eye
Angle viewHeading = dcView.getHeading();
dcView.setHeading(dcView.getHeading().subtract(this.getFocusAngle()));
dcView.apply(dc);
// Draw the right eye
try
{
if (this.isSwapEyes())
gl.glDrawBuffer(GL2.GL_BACK_LEFT);
else
gl.glDrawBuffer(GL2.GL_BACK_RIGHT);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
super.draw(dc);
}
finally
{
// Restore the original view heading
dcView.setHeading(viewHeading);
dcView.apply(dc);
}
}
}