summaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/jogamp/opengl/util/stereo/GenericStereoDevice.java
blob: 60adc7d74c9aafdffed3f2bbf04be0129cc7fbcf (plain)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
 * Copyright 2014 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */
package jogamp.opengl.util.stereo;

import com.jogamp.nativewindow.util.Dimension;
import com.jogamp.nativewindow.util.DimensionImmutable;
import com.jogamp.nativewindow.util.Point;
import com.jogamp.nativewindow.util.PointImmutable;
import com.jogamp.nativewindow.util.Rectangle;
import com.jogamp.nativewindow.util.RectangleImmutable;
import com.jogamp.opengl.math.FovHVHalves;
import com.jogamp.opengl.math.Vec3f;
import com.jogamp.opengl.util.stereo.StereoDeviceConfig;
import com.jogamp.opengl.util.stereo.EyeParameter;
import com.jogamp.opengl.util.stereo.LocationSensorParameter;
import com.jogamp.opengl.util.stereo.StereoDevice;
import com.jogamp.opengl.util.stereo.StereoDeviceFactory;
import com.jogamp.opengl.util.stereo.StereoDeviceRenderer;
import com.jogamp.opengl.util.stereo.StereoUtil;
import com.jogamp.opengl.util.stereo.generic.GenericStereoDeviceConfig;
import com.jogamp.opengl.util.stereo.generic.GenericStereoDeviceFactory;

public class GenericStereoDevice implements StereoDevice {
    /** A mono view configuration DK1, only one eye is supported */
    public static final GenericStereoDeviceConfig config01Mono01;
    /** A mono view configuration DK2, only one eye is supported */
    public static final GenericStereoDeviceConfig config01Mono02;

    /** A default stereo SBS view configuration DK1 */
    public static final GenericStereoDeviceConfig config02StereoSBS01;
    /** A default stereo SBS view configuration DK2 */
    public static final GenericStereoDeviceConfig config02StereoSBS02;

    /** A default stereo SBS lense view configuration DK1, utilizing similar settings as OculusVR DK1 */
    public static final GenericStereoDeviceConfig config03StereoSBSLense01;
    /** A default stereo SBS lense view configuration DK2, utilizing similar settings as OculusVR DK1 */
    public static final GenericStereoDeviceConfig config03StereoSBSLense02;

    private static final GenericStereoDeviceConfig[] configs;

    static {
        final Vec3f DEFAULT_EYE_POSITION_OFFSET_STEREO_LENSES = new Vec3f( 0.0f, 1.6f, -5.0f );  // 1.6 up, 5 forward
        final Vec3f DEFAULT_EYE_POSITION_OFFSET_STEREO        = new Vec3f( 0.0f, 0.3f,  3.0f );  // 0.3 up, 3 back
        final Vec3f DEFAULT_EYE_POSITION_OFFSET_MONO          = new Vec3f( 0.0f, 0.0f,  3.0f );  //         3 back

        final DimensionImmutable surfaceSizeInPixelDK1 = new Dimension(1280, 800);
        final float[] screenSizeInMetersDK1 = new float[] { 0.14976f, 0.0936f };
        final DimensionImmutable eyeTextureSizeDK1 = new Dimension(1122, 1553);

        final DimensionImmutable surfaceSizeInPixelDK2 = new Dimension(1920, 1080);
        final float[] screenSizeInMetersDK2 = new float[] { 0.12576f, 0.07074f };
        final DimensionImmutable eyeTextureSizeDK2 = new Dimension(1182, 1461);

        final float interpupillaryDistanceInMeters = 0.0635f;

        config01Mono01 = GenericStereoDeviceFactory.createMono("Def01Mono01",
                                    surfaceSizeInPixelDK1, screenSizeInMetersDK1,
                                    DEFAULT_EYE_POSITION_OFFSET_MONO);

        config02StereoSBS01 = GenericStereoDeviceFactory.createStereoSBS("Def02StereoSBS01",
                                  surfaceSizeInPixelDK1, screenSizeInMetersDK1,
                                  interpupillaryDistanceInMeters, 45f /* fovy */,
                                  DEFAULT_EYE_POSITION_OFFSET_STEREO);

        config03StereoSBSLense01 = GenericStereoDeviceFactory.createStereoSBSLense("Def03StereoSBSLense01",
                                  surfaceSizeInPixelDK1, screenSizeInMetersDK1,
                                  interpupillaryDistanceInMeters, 129f /* fovy */,
                                  eyeTextureSizeDK1,
                                  DEFAULT_EYE_POSITION_OFFSET_STEREO_LENSES);

        config01Mono02 = GenericStereoDeviceFactory.createMono("Def01Mono02",
                                    surfaceSizeInPixelDK2, screenSizeInMetersDK2,
                                    DEFAULT_EYE_POSITION_OFFSET_MONO);

        config02StereoSBS02 = GenericStereoDeviceFactory.createStereoSBS("Def02StereoSBS02",
                                  surfaceSizeInPixelDK2, screenSizeInMetersDK2,
                                  interpupillaryDistanceInMeters, 45f /* fovy */,
                                  DEFAULT_EYE_POSITION_OFFSET_STEREO);

        config03StereoSBSLense02 = GenericStereoDeviceFactory.createStereoSBSLense("Def03StereoSBSLense02",
                                  surfaceSizeInPixelDK2, screenSizeInMetersDK2,
                                  interpupillaryDistanceInMeters, 129f /* fovy */,
                                  eyeTextureSizeDK2,
                                  DEFAULT_EYE_POSITION_OFFSET_STEREO_LENSES);

        configs = new GenericStereoDeviceConfig[] {
                                 config01Mono01, config02StereoSBS01, config03StereoSBSLense01,
                                 config01Mono02, config02StereoSBS02, config03StereoSBSLense02 };
    }

    private final StereoDeviceFactory factory;
    public final int deviceIndex;
    public final GenericStereoDeviceConfig config;

    public final Point surfacePos;
    private final FovHVHalves[] defaultEyeFov;

    private int usedSensorBits;
    private boolean sensorsStarted = false;

    public GenericStereoDevice(final StereoDeviceFactory factory, final int deviceIndex, final StereoDeviceConfig customConfig) {
        this.factory = factory;
        this.deviceIndex = deviceIndex;

        if( customConfig instanceof GenericStereoDeviceConfig) {
            this.config = (GenericStereoDeviceConfig) customConfig;
        } else {
            final int cfgIdx = Math.min(deviceIndex % 10, configs.length-1);
            this.config = null != configs[cfgIdx] ? configs[cfgIdx] : config02StereoSBS01;
        }
        config.init();

        this.surfacePos = new Point(0, 0);

        defaultEyeFov = new FovHVHalves[config.defaultEyeParam.length];
        for(int i=0; i<defaultEyeFov.length; i++) {
            defaultEyeFov[i] = config.defaultEyeParam[i].fovhv;
        }

        // default
        usedSensorBits = 0;
    }

    @Override
    public final StereoDeviceFactory getFactory() { return factory; }

    @Override
    public String toString() {
        return "GenericStereoDevice["+config+", surfacePos "+surfacePos+
                ", sensorBits[enabled ["+StereoUtil.sensorBitsToString(getEnabledSensorBits())+"]]]";
    }

    public void setSurfacePosition(final int x, final int y) {
        surfacePos.set(x, y);
    }

    @Override
    public final void dispose() {
        stopSensors();
    }

    @Override
    public boolean isValid() { return true; }

    @Override
    public final PointImmutable getPosition() { return surfacePos; }

    @Override
    public final DimensionImmutable getSurfaceSize() { return config.surfaceSizeInPixels; }
    @Override
    public int getRequiredRotation() { return 0; }

    @Override
    public Vec3f getDefaultEyePositionOffset() { return config.defaultEyeParam[0].positionOffset; }

    @Override
    public final FovHVHalves[] getDefaultFOV() { return defaultEyeFov; }

    @Override
    public final LocationSensorParameter getLocationSensorParams() { return null; }

    @Override
    public final void resetLocationSensorOrigin() { }

    @Override
    public final boolean startSensors(final int desiredSensorBits, final int requiredSensorBits) {
        if( !sensorsStarted ) {
            if( requiredSensorBits != ( config.supportedSensorBits & requiredSensorBits ) ) {
                // required sensors not available
                return false;
            }
            if( 0 == ( config.supportedSensorBits & ( requiredSensorBits | desiredSensorBits ) ) ) {
                // no sensors available
                return false;
            }
            if( startSensorsImpl(true, desiredSensorBits, requiredSensorBits) ) {
                sensorsStarted = true;
                return true;
            } else {
                return false;
            }
        } else {
            // No state change -> Success
            return true;
        }
    }
    protected boolean startSensorsImpl(final boolean start, final int desiredSensorBits, final int requiredSensorBits) {
        // TODO: Add SPI for sensors
        // TODO: start sensors in override / or SPI
        // TODO: set usedSensorBits
        return false;
    }

    @Override
    public final boolean stopSensors() {
        if( sensorsStarted ) {
            if( startSensorsImpl(false, 0, 0) ) {
                sensorsStarted = false;
                usedSensorBits = 0;
                return true;
            } else {
                return false;
            }
        } else {
            // No state change -> Success
            return true;
        }
    }

    @Override
    public boolean getSensorsStarted() { return sensorsStarted; }

    @Override
    public final int getSupportedSensorBits() {
        return config.supportedSensorBits;
    }

    @Override
    public final int getEnabledSensorBits() {
        return usedSensorBits;
    }

    @Override
    public int[] getEyeRenderOrder() { return config.eyeRenderOrder; }

    @Override
    public int getSupportedDistortionBits() {
        return config.supportedDistortionBits;
    };

    @Override
    public int getRecommendedDistortionBits() {
        return config.recommendedDistortionBits;
    }

    @Override
    public int getMinimumDistortionBits() {
        return config.minimumDistortionBits;
    }

    @Override
    public final StereoDeviceRenderer createRenderer(final int distortionBits,
                                                     final int textureCount, final Vec3f eyePositionOffset,
                                                     final FovHVHalves[] eyeFov, final float pixelsPerDisplayPixel,
                                                     final int textureUnit) {
       final EyeParameter[] eyeParam = new EyeParameter[eyeFov.length];
       for(int i=0; i<eyeParam.length; i++) {
           final EyeParameter defaultEyeParam = config.defaultEyeParam[i];
           eyeParam[i] = new EyeParameter(i, eyePositionOffset, eyeFov[i],
                                          defaultEyeParam.distNoseToPupilX, defaultEyeParam.distMiddleToPupilY, defaultEyeParam.eyeReliefZ);
       }

       final boolean usePP = null != config.distortionMeshProducer && 0 != distortionBits; // use post-processing

       final RectangleImmutable[] eyeViewports = new RectangleImmutable[eyeParam.length];
       final DimensionImmutable totalTextureSize;
       if( 1 < eyeParam.length ) {
           // Stereo SBS
           final DimensionImmutable eye0TextureSize = config.eyeTextureSizes[0];
           final DimensionImmutable eye1TextureSize = config.eyeTextureSizes[1];
           final int maxHeight = Math.max(eye0TextureSize.getHeight(), eye1TextureSize.getHeight());

           totalTextureSize = new Dimension(eye0TextureSize.getWidth()+eye1TextureSize.getWidth(), maxHeight);
           if( 1 == textureCount ) { // validated in ctor below!
               // one big texture/FBO, viewport to target space
               eyeViewports[0] = new Rectangle(0, 0,
                                               eye0TextureSize.getWidth(),
                                               maxHeight);
               eyeViewports[1] = new Rectangle(eye0TextureSize.getWidth(), 0,
                                               eye1TextureSize.getWidth(),
                                               maxHeight);
           } else if( usePP ) {
               // two textures/FBOs w/ postprocessing, which renders textures/FBOs into target space
               eyeViewports[0] = new Rectangle(0, 0,
                                               eye0TextureSize.getWidth(),
                                               eye0TextureSize.getHeight());
               eyeViewports[1] = new Rectangle(0, 0,
                                               eye1TextureSize.getWidth(),
                                               eye1TextureSize.getHeight());
           } else {
               // two textures/FBOs w/o postprocessing, viewport to target space
               eyeViewports[0] = new Rectangle(0, 0,
                                               eye0TextureSize.getWidth(),
                                               eye0TextureSize.getHeight());
               eyeViewports[1] = new Rectangle(eye0TextureSize.getWidth(), 0,
                                               eye1TextureSize.getWidth(),
                                               eye1TextureSize.getHeight());
           }
       } else {
           // Mono
           final DimensionImmutable eye0TextureSize = config.eyeTextureSizes[0];
           totalTextureSize = eye0TextureSize;
           eyeViewports[0] = new Rectangle(0, 0, eye0TextureSize.getWidth(), eye0TextureSize.getHeight());
       }
       return new GenericStereoDeviceRenderer(this, distortionBits, textureCount, eyePositionOffset, eyeParam,
                                              pixelsPerDisplayPixel, textureUnit,
                                              config.eyeTextureSizes, totalTextureSize, eyeViewports);
    }
}