QtBaseViewerStereo.cc 28.1 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
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
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
   
Jan Möbius committed
42
43
44
45
46
47




//=============================================================================
//
Jan Möbius's avatar
   
Jan Möbius committed
48
//  CLASS glViewer - IMPLEMENTATION
Jan Möbius's avatar
   
Jan Möbius committed
49
50
51
52
53
54
55
56
57
58
//
//=============================================================================


//== INCLUDES =================================================================

#include "QtBaseViewer.hh"
#include <QStatusBar>
#include <QToolButton>

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
59
60
#include <OpenFlipper/common/GlobalOptions.hh>

61
62
#include <QGLFramebufferObject>

63
#include <ACG/ShaderUtils/GLSLShader.hh>
64
#include <ACG/GL/globjects.hh>
65

Jan Möbius's avatar
Jan Möbius committed
66
#include <ACG/ShaderUtils/gldebug.h>
Jan Möbius's avatar
   
Jan Möbius committed
67

Dirk Wilden's avatar
Dirk Wilden committed
68
69
#include "crc32.hh"

Jan Möbius's avatar
   
Jan Möbius committed
70
71
72
73
74
//== NAMESPACES ===============================================================


//== IMPLEMENTATION ==========================================================

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
static const char* customAnaglyphProg = {
  "!!ARBfp1.0"
  "TEMP left, right, lmul, rmul;"
  "TEX left, fragment.texcoord[0], texture[0], RECT;"
  "TEX right, fragment.texcoord[0], texture[1], RECT;"
  "DP3 lmul.r, left, program.env[0];"
  "DP3 lmul.g, left, program.env[1];"
  "DP3 lmul.b, left, program.env[2];"
  "DP3 rmul.r, right, program.env[3];"
  "DP3 rmul.g, right, program.env[4];"
  "DP3 rmul.b, right, program.env[5];"
  "ADD result.color, lmul, rmul;"
  "END"
};

//-----------------------------------------------------------------------------


Jan Möbius's avatar
   
Jan Möbius committed
93
void
Jan Möbius's avatar
   
Jan Möbius committed
94
glViewer::setStereoMode(bool _b)
Jan Möbius's avatar
   
Jan Möbius committed
95
96
97
{
  stereo_ = _b;

98
  if (!stereo_) {
Jan Möbius's avatar
   
Jan Möbius committed
99
100
101
102
    makeCurrent();
    glDrawBuffer(GL_BACK);
  }

Jan Möbius's avatar
Jan Möbius committed
103
104
  updateProjectionMatrix ();

Jan Möbius's avatar
   
Jan Möbius committed
105
106
107
  updateGL();
}

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
108
109
110
111
112
113
//-----------------------------------------------------------------------------


void
glViewer::drawScene_glStereo()
{
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
114
  double l, r, t, b, w, h, a, radians, wd2, ndfl, zerop, xrange;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
115
116
117
118
119

  w = glWidth();
  h = glHeight();
  a = w / h;

120
  double fovy = OpenFlipperSettings().value("Core/Projection/FOVY",45.0).toDouble();
121
  radians = fovy * 0.5 / 180.0 * M_PI;
122
123
124
125
  wd2     = properties_.nearPlane() * tan(radians);
  zerop   = properties_.nearPlane() + ((properties_.farPlane() - properties_.nearPlane()) * OpenFlipperSettings().value("Core/Stereo/FocalDistance",0.5).toDouble() );
  ndfl    = properties_.nearPlane() / zerop ;
  xrange  = a * wd2 * 2 * zerop / properties_.nearPlane();
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
126
127
128
129
130
131

  l = -a*wd2;
  r =  a*wd2;
  t =  wd2;
  b = -wd2;

132
  double offset  = 0.5 * OpenFlipperSettings().value("Core/Stereo/EyeDistance",0.07).toDouble()  * xrange;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
133
134
135
136
137
  double offset2 = offset * ndfl;

  // left eye
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
138
  glFrustum(l+offset2, r+offset2, b, t, properties_.nearPlane(), properties_.farPlane());
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
139
  glTranslatef(+offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
140
141
142
143
144
145
146
147
148
149
  glMatrixMode(GL_MODELVIEW);
  glDrawBuffer(GL_BACK_LEFT);
  glstate_->clearBuffers ();
  glClear(GL_DEPTH_BUFFER_BIT);
  drawScene_mono();


  // right eye
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
150
  glFrustum(l-offset2, r-offset2, b, t, properties_.nearPlane(), properties_.farPlane());
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
151
  glTranslatef(-offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
152
153
154
155
156
157
158
159
160
  glMatrixMode(GL_MODELVIEW);
  glDrawBuffer(GL_BACK_RIGHT);
  glstate_->clearBuffers ();
  glClear(GL_DEPTH_BUFFER_BIT);
  drawScene_mono();
  glDrawBuffer(GL_BACK);
}


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
void glViewer::drawScenePhilipsStereo() {

    updateScenePhilipsStereo();

    if (!philipsStereoInitialized_)
        return;

    int vp_l, vp_b, vp_w, vp_h;
    glstate_->get_viewport(vp_l, vp_b, vp_w, vp_h);

    // ======================================================================================================
    // Disable textures
    // ======================================================================================================
    pDepthStencilTexture_.disable();
    pColorTexture_.disable();

    // ======================================================================================================
    // Render the scene
    // ======================================================================================================
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    drawScene_mono();

    // ======================================================================================================
    // Copy Scene to Textures
    // ======================================================================================================
    pColorTexture_.enable();
    pColorTexture_.bind();

    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, vp_l, vp_b, vp_w, vp_h);

    pColorTexture_.disable();

    pDepthStencilTexture_.enable();
    pDepthStencilTexture_.bind();

    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, vp_l, vp_b, vp_w, vp_h);

    pDepthStencilTexture_.disable();

200
201
    glClearColor(.5, .4, .3, 0);
    
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
    // Turn on shader program
    pProgram_->use();

    // ======================================================================================================
    // Bind textures to different texture units and tell shader where to find them
    // ======================================================================================================
    glActiveTextureARB(GL_TEXTURE0_ARB);
    pColorTexture_.enable();
    pColorTexture_.bind();

    glActiveTextureARB(GL_TEXTURE1_ARB);
    pDepthStencilTexture_.enable();
    pDepthStencilTexture_.bind();

    pProgram_->setUniform("ColorTexture", 0);
    pProgram_->setUniform("DepthStencil", 1);

    // ======================================================================================================
    // Render plain textured
    // ======================================================================================================
    glDisable(GL_LIGHTING);
    glDisable(GL_COLOR_MATERIAL);
    glDisable(GL_DEPTH_TEST);

    // ======================================================================================================
    // Setup orthogonal projection
    // ======================================================================================================
    glstate_->push_projection_matrix();
    glstate_->push_modelview_matrix();

    glstate_->reset_projection();
    glstate_->reset_modelview();

    glstate_->ortho(0, vp_w, 0, vp_h, 0, 1);

    // ======================================================================================================
    // Bind textures to different texture units and tell shader where to find them
    // ======================================================================================================
    glColor3f(1.0, 1.0, 1.0);

    // ======================================================================================================
    // Clear buffers
    // ======================================================================================================
245
    glClearColor(.5, .4, .3, 0);
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
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // ======================================================================================================
    // Render a simple quad (rest is done by shader)
    // ======================================================================================================
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 1.0f);
    glVertex2i(0, vp_h);
    glTexCoord2f(1.0f, 1.0f);
    glVertex2i(vp_w, vp_h);
    glTexCoord2f(1.0f, 0.0f);
    glVertex2i(vp_w, 0);
    glTexCoord2f(0.0f, 0.0f);
    glVertex2i(0, 0);
    glEnd();

    pProgram_->disable();

    glBindTexture(GL_TEXTURE_2D, 0);

    // ======================================================================================================
    // Cleanup (color and depth textures)
    // ======================================================================================================
    pDepthStencilTexture_.del();
    pColorTexture_.del();

    // ======================================================================================================
    // Compute the header required for the display
    // ======================================================================================================
    uchar *header = new uchar[6];

    //   Header ID
    //   Basic identifier used by the display to verify the header
    header[0] = 241; // Header_ID1 = 11110001

    //   Header content type
    //   This entry controls the displays internal rendering based on the input data specified here.
    //   There is no info about how this changes the rendering
    //   Possible values:
    //   0 No Depth
    //   1 Signage
    //   2 Movie
    //   3 Game
    //   4 CGI
    //   5 Still
291
    header[1] = OpenFlipperSettings().value("Core/Stereo/Philips/Content").toInt(); // Hdr_Content_type (Game) = 00000011 (Gaming Mode)
292
293
294
295
296
297
298
299
300
301

    //   Header Factor
    //   Each 3D Display has a 'Display recommended depth value', which corresponds to an
    //   acceptable maximum depth factor value for that specific type of display. This value strongly
    //   depends on the lens design. The factor field in the header contains the percentage to be
    //   used from the display recommended depth value. The value of 64 corresponds with the 100%
    //   of the display recommended depth value. It is allowed to use values higher than 64. The factor
    //   works on a linear scale and is multiplied with the factor controlled by the user in the Display
    //   Control Tool.
    //   Value range: 0-255 (default 64)
302
    header[2] = OpenFlipperSettings().value("Core/Stereo/Philips/Factor").toInt(); // Hdr_Factor
303
304
305
306
307
308
309
310

    //   Header Offset CC
    //   Values in the Depth map equal to the header-offset value will be located on the plane of the
    //   display. All values in the disparity map with a higher value will de displayed in front of the
    //   display.
    //   Offset_CC is the offset controlled by the Content Creator. In the system there is also an
    //   Offset_user present, which is controlled by the user using the Display Control Tool.
    //   Value Range: 0-255 (default 128)
311
    header[3] = OpenFlipperSettings().value("Core/Stereo/Philips/Offset").toInt(); // Hdr_Offset_CC
312
313
314
315
316
317
318
319
320
321

    //   Header select
    //   When all select signals are low the rendering settings are set to optimal settings for the content
    //   type denoted by Hdr_content_type. By making select signals high the settings for Factor and
    //   Offset_cc can be controlled individually by the header.
    //   Possible Values:
    //   0 Use Displays defaults and automatic optimizations
    //   1 Use Header provided factor
    //   2 Use Header provided offset
    //   3 Use both factor and offset
322
    header[4] = OpenFlipperSettings().value("Core/Stereo/Philips/Select").toInt(); // Hdr_Factor_select(1) + Hdr_Offset_select(1) + reserved(6)
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361

    //   Unused Header entry (leave at 0 !)
    header[5] = 0; // Reserved

    //   Header checksum.
    //   The 4-byte EDC field H(6) − H(9) contains an Error Detection Code computed over the first 6
    //   header bytes. This EDC uses the standard CRC-32 polynomial as defined in IEEE 802.3 and ITU-T
    //   V.42. The initial value and final XOR value are both 0.
    //   unsigned long has 32bit = 4Byte
    unsigned long checksum = CalcCRC32(&header[0], 6);

    //   Store the complete header in a bit vector
    std::vector<uchar> bitVector;

    // For all bytes of the header
    for (int i = 0; i < 6; i++) {

        // For each bit of a headers byte
        for (int j = 7; j >= 0; --j) {

            // Red and Green component have to be 0
            bitVector.push_back(0);
            bitVector.push_back(0);

            // If bit is set, the full component will be set to one otherwise zero
            // And the order of the bits has to be reversed!
            if (header[i] & (1 << j)) {
                bitVector.push_back(255);
            } else {
                bitVector.push_back(0);
            }

            // Only every second pixel is used for the header
            // Skip every odd one by filling in 0 for RGB
            bitVector.push_back(0);
            bitVector.push_back(0);
            bitVector.push_back(0);
        }
    }
362

363
364
365
    // Append checksum to header.
    // Reversed bit order!
    for (int i = 31; i >= 0; i--) {
366

367
368
369
        // Red and Green component have to be 0
        bitVector.push_back(0);
        bitVector.push_back(0);
370

371
372
373
374
        if (checksum & (1 << i))
            bitVector.push_back(255);
        else
            bitVector.push_back(0);
375

376
377
378
379
380
381
        // Only every second pixel is used for the header
        // Skip every odd one by filling in 0 for RGB
        bitVector.push_back(0);
        bitVector.push_back(0);
        bitVector.push_back(0);
    }
382

383
384
385
386
    // Select the top left of the renderbuffer and
    // write complete header into these bits
    glRasterPos2i(0, glHeight() - 1);
    glDrawPixels(bitVector.size() / 3, 1, GL_RGB, GL_UNSIGNED_BYTE, &bitVector[0]);
387

388
389
390
391
392
    // ======================================================================================================
    // Reset projection and modelview
    // ======================================================================================================
    glstate_->pop_projection_matrix();
    glstate_->pop_modelview_matrix();
393

394
}
395

396
//-----------------------------------------------------------------------------
397

398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
void
glViewer::updateScenePhilipsStereo()
{
     int vp_l, vp_b, vp_w, vp_h;
     glstate_->get_viewport (vp_l, vp_b, vp_w, vp_h);

     // Does shader program exist?
     if (!pProgram_) {

        // ======================================================================================================
        // Setup the shaders used to render color and depth info next to each other
        // ======================================================================================================
        GLSL::PtrVertexShader vertexShader;
        GLSL::PtrFragmentShader fragmentShader;

        QString vshaderFile = OpenFlipper::Options::shaderDirStr() + QDir::separator() + "Philips/Vertex.glsl";
        QString fshaderFile = OpenFlipper::Options::shaderDirStr() + QDir::separator() + "Philips/Fragment42.glsl";

Jan Möbius's avatar
Jan Möbius committed
416
417
        vertexShader = GLSL::loadVertexShader(vshaderFile.toUtf8());
        fragmentShader = GLSL::loadFragmentShader(fshaderFile.toUtf8());
418
419
420
421
422
423
424
425
426
427
428
        pProgram_ = GLSL::PtrProgram(new GLSL::Program());

        if ((vertexShader == 0) || (fragmentShader == 0) || (pProgram_ == 0)) {
            std::cerr << "Unable to load shaders for philips display rendering!";
            philipsStereoInitialized_ = false;
            return;
        }

        pProgram_->attach(vertexShader);
        pProgram_->attach(fragmentShader);
        pProgram_->link();
429
    }
430

431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
    // Does color texture exist?
    if (!pColorTexture_.is_valid()) {
        // ======================================================================================================
        // creating a color texture
        // ======================================================================================================

        pColorTexture_.enable();
        pColorTexture_.bind();
        GLenum texTarget = GL_TEXTURE_2D;
        GLenum texInternalFormat = GL_RGBA;
        GLenum texFormat = GL_RGBA;
        GLenum texType = GL_UNSIGNED_BYTE;
        GLenum texFilterMode = GL_NEAREST;
        glTexImage2D(texTarget, 0, texInternalFormat, vp_w, vp_h, 0, texFormat, texType, NULL);

        glTexParameterf(texTarget, GL_TEXTURE_MIN_FILTER, texFilterMode);
        glTexParameterf(texTarget, GL_TEXTURE_MAG_FILTER, texFilterMode);
        glTexParameterf(texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameterf(texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(texTarget, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

        if(!pColorTexture_.is_valid()) {
            philipsStereoInitialized_ = false;
        }
    }
456

457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
    // Does depth stencil texture exist?
    if (!pDepthStencilTexture_.is_valid()) {
        // ======================================================================================================
        // creating an 24-bit depth + 8-bit stencil texture
        // ======================================================================================================

        pDepthStencilTexture_.enable();
        pDepthStencilTexture_.bind();
        GLenum texTarget = GL_TEXTURE_2D;
        GLenum texInternalFormat = GL_DEPTH24_STENCIL8_EXT;
        GLenum texFormat = GL_DEPTH_STENCIL_EXT;
        GLenum texType = GL_UNSIGNED_INT_24_8_EXT;
        GLenum texFilterMode = GL_NEAREST;
        glTexImage2D(texTarget, 0, texInternalFormat, vp_w, vp_h, 0, texFormat, texType, NULL);

        glTexParameterf(texTarget, GL_TEXTURE_MIN_FILTER, texFilterMode);
        glTexParameterf(texTarget, GL_TEXTURE_MAG_FILTER, texFilterMode);
        glTexParameterf(texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameterf(texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(texTarget, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

        if(!pDepthStencilTexture_.is_valid()) {
            philipsStereoInitialized_ = false;
        }
    }
482

483
484
485
    // Resize target textures
    if (glstate_->viewport_width() != pTexWidth_ ||
            glstate_->viewport_height() != pTexHeight_) {
486

487
488
489
490
        // Color texture
        pColorTexture_.bind();
        glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB, glstate_->viewport_width(), glstate_->viewport_height(), 0,
                GL_RGB, GL_UNSIGNED_BYTE, 0);
Dirk Wilden's avatar
Dirk Wilden committed
491

492
493
494
495
        // Depth stencil texture
        pDepthStencilTexture_.bind();
        glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB, glstate_->viewport_width(), glstate_->viewport_height(), 0,
                GL_RGB, GL_UNSIGNED_BYTE, 0);
Dirk Wilden's avatar
Dirk Wilden committed
496

497
        glBindTexture(GL_TEXTURE_RECTANGLE_NV, 0);
498

499
500
501
        pTexWidth_ = glstate_->viewport_width();
        pTexHeight_ = glstate_->viewport_height();
    }
Dirk Wilden's avatar
Dirk Wilden committed
502

503
    philipsStereoInitialized_ = true;
504
505
}

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
506
507
508
509
510
511
//-----------------------------------------------------------------------------


void
glViewer::drawScene_anaglyphStereo()
{
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
512
 double l, r, t, b, w, h, a, radians, wd2, ndfl, zerop, xrange;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
513
514
515
516
517

  w = glWidth();
  h = glHeight();
  a = w / h;

518
  double fovy = OpenFlipperSettings().value("Core/Projection/FOVY", 45.0).toDouble();
519
  radians = fovy * 0.5 / 180.0 * M_PI;
520
521
522
523
  wd2     = properties_.nearPlane() * tan(radians);
  zerop   = properties_.nearPlane() + ((properties_.farPlane() - properties_.nearPlane()) * OpenFlipperSettings().value("Core/Stereo/FocalDistance", 0.5).toDouble() );
  ndfl    = properties_.nearPlane() / zerop ;
  xrange  = a * wd2 * 2 * zerop / properties_.nearPlane();
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
524
525
526
527
528
529

  l = -a*wd2;
  r =  a*wd2;
  t =  wd2;
  b = -wd2;

530
  double offset  = 0.5 * OpenFlipperSettings().value("Core/Stereo/EyeDistance", 0.07).toDouble() * xrange;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
531
532
533
534
535
  double offset2 = offset * ndfl;

  // left eye
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
536
  glFrustum(l+offset2, r+offset2, b, t, properties_.nearPlane(), properties_.farPlane());
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
537
  glTranslatef(offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
538
539
540
541
542
543
544
545
546
547

  glMatrixMode(GL_MODELVIEW);
  glstate_->clearBuffers ();
  glClear(GL_DEPTH_BUFFER_BIT);

  // draw red channel for left eye
  glColorMask(GL_TRUE,GL_FALSE,GL_FALSE,GL_TRUE);
  drawScene_mono();
  glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);

548

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
549
550
551
  // right eye
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
552
  glFrustum(l-offset2, r-offset2, b, t, properties_.nearPlane(), properties_.farPlane());
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
553
  glTranslatef(-offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
  glMatrixMode(GL_MODELVIEW);
  glClear(GL_DEPTH_BUFFER_BIT);

  // draw green and blue channel for right eye
  glColorMask(GL_FALSE,GL_TRUE,GL_TRUE,GL_TRUE);
  drawScene_mono();
  glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);

}

//-----------------------------------------------------------------------------

void
glViewer::updateCustomAnaglyphStereo()
{
  if (!customAnaglyphSupported_)
    return;

  if (!agProgram_)
  {
    GLint errorPos;

    glGenProgramsARB (1, &agProgram_);

    glGetError ();
    glBindProgramARB (GL_FRAGMENT_PROGRAM_ARB, agProgram_);
    glProgramStringARB (GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
                        strlen (customAnaglyphProg), customAnaglyphProg);

    glGetIntegerv (GL_PROGRAM_ERROR_POSITION_ARB, &errorPos);
    if (glGetError () != GL_NO_ERROR || errorPos != -1)
    {
      printf("Error loading program %d %s\n",errorPos, glGetString(GL_PROGRAM_ERROR_STRING_ARB));
      glDeleteProgramsARB (1, &agProgram_);
      customAnaglyphSupported_ = false;
      return;
    }
  }

  if (!agTexture_[0])
    glGenTextures (2, agTexture_);

  if (!agTexture_[0])
  {
    finiCustomAnaglyphStereo ();
    customAnaglyphSupported_ = false;
    return;
  }

  if (glstate_->viewport_width () != agTexWidth_ ||
      glstate_->viewport_height () != agTexHeight_)
  {
    glBindTexture (GL_TEXTURE_RECTANGLE_NV, agTexture_[0]);
    glTexImage2D (GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB, glstate_->viewport_width (),
                  glstate_->viewport_height (), 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
    glBindTexture (GL_TEXTURE_RECTANGLE_NV, agTexture_[1]);
    glTexImage2D (GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB, glstate_->viewport_width (),
                  glstate_->viewport_height (), 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
    glBindTexture (GL_TEXTURE_RECTANGLE_NV, 0);

    agTexWidth_ = glstate_->viewport_width ();
    agTexHeight_ = glstate_->viewport_height ();
  }
}

//-----------------------------------------------------------------------------

void
glViewer::finiCustomAnaglyphStereo()
{
  if (!customAnaglyphSupported_)
    return;

  if (agProgram_)
    glDeleteProgramsARB (1, &agProgram_);

  if (agTexture_[0])
    glDeleteTextures (2, agTexture_);
}

//-----------------------------------------------------------------------------

void
glViewer::drawScene_customAnaglyphStereo()
{
  updateCustomAnaglyphStereo ();

  if (!customAnaglyphSupported_)
    return;

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
644
  double l, r, t, b, w, h, a, radians, wd2, ndfl, zerop, xrange;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
645
646
647
648
649

  w = glWidth();
  h = glHeight();
  a = w / h;

650
  double fovy = OpenFlipperSettings().value("Core/Projection/FOVY",45.0).toDouble();
651
  radians = fovy * 0.5 / 180.0 * M_PI;
652
653
654
655
  wd2     = properties_.nearPlane() * tan(radians);
  zerop   = properties_.nearPlane() + ((properties_.farPlane() - properties_.nearPlane()) * OpenFlipperSettings().value("Core/Stereo/FocalDistance",0.5).toDouble() );
  ndfl    = properties_.nearPlane() / zerop ;
  xrange  = a * wd2 * 2 * zerop / properties_.nearPlane();
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
656
657
658
659
660
661

  l = -a*wd2;
  r =  a*wd2;
  t =  wd2;
  b = -wd2;

662
  double offset  = 0.5 * OpenFlipperSettings().value("Core/Stereo/EyeDistance",0.07).toDouble() * xrange;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
663
664
665
666
667
668
669
670
671
672
673
  double offset2 = offset * ndfl;

  int vp_l, vp_b, vp_w, vp_h;
  glstate_->get_viewport (vp_l, vp_b, vp_w, vp_h);

  std::vector<float> le = OpenFlipper::Options::anaglyphLeftEyeColorMatrix();
  std::vector<float> re = OpenFlipper::Options::anaglyphRightEyeColorMatrix();

  // left eye
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
674
  glFrustum(l+offset2, r+offset2, b, t, properties_.nearPlane(), properties_.farPlane());
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
675
  glTranslatef(offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
676
677
678
679
680
681
682
683
684
685
686
687
  glMatrixMode(GL_MODELVIEW);
  glstate_->clearBuffers ();
  glClear(GL_DEPTH_BUFFER_BIT);
  drawScene_mono();

  glBindTexture (GL_TEXTURE_RECTANGLE_NV, agTexture_[0]);
  glCopyTexSubImage2D (GL_TEXTURE_RECTANGLE_NV, 0, 0, 0, vp_l, vp_b, vp_w, vp_h);
  glBindTexture (GL_TEXTURE_RECTANGLE_NV, 0);

  // right eye
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
688
  glFrustum(l-offset2, r-offset2, b, t, properties_.nearPlane(), properties_.farPlane());
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
689
  glTranslatef(-offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
  glMatrixMode(GL_MODELVIEW);
  glstate_->clearBuffers ();
  glClear(GL_DEPTH_BUFFER_BIT);
  drawScene_mono();

  glBindTexture (GL_TEXTURE_RECTANGLE_NV, agTexture_[1]);
  glCopyTexSubImage2D (GL_TEXTURE_RECTANGLE_NV, 0, 0, 0, vp_l, vp_b, vp_w, vp_h);
  glBindTexture (GL_TEXTURE_RECTANGLE_NV, 0);

  glActiveTexture (GL_TEXTURE0);
  glBindTexture (GL_TEXTURE_RECTANGLE_NV, agTexture_[0]);
  glEnable (GL_TEXTURE_RECTANGLE_NV);

  glActiveTexture (GL_TEXTURE1);
  glBindTexture (GL_TEXTURE_RECTANGLE_NV, agTexture_[1]);
  glEnable (GL_TEXTURE_RECTANGLE_NV);

  glEnable (GL_FRAGMENT_PROGRAM_ARB);
  glBindProgramARB (GL_FRAGMENT_PROGRAM_ARB, agProgram_);

  glProgramEnvParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 0, le[0], le[3], le[6], 0.0);
  glProgramEnvParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 1, le[1], le[4], le[7], 0.0);
  glProgramEnvParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 2, le[2], le[5], le[8], 0.0);

  glProgramEnvParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 3, re[0], re[3], re[6], 0.0);
  glProgramEnvParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 4, re[1], re[4], re[7], 0.0);
  glProgramEnvParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 5, re[2], re[5], re[8], 0.0);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, vp_w, vp_h, 0, 0, 1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glDisable (GL_DEPTH_TEST);

  glBegin (GL_QUADS);
  glMultiTexCoord2f (GL_TEXTURE0, 0, vp_h);
  glMultiTexCoord2f (GL_TEXTURE1, 0, vp_h);
  glVertex2i(0, 0);
  glMultiTexCoord2f (GL_TEXTURE0, 0, 0);
  glMultiTexCoord2f (GL_TEXTURE1, 0, 0);
  glVertex2i(0, vp_h);
  glMultiTexCoord2f (GL_TEXTURE0, vp_w, 0);
  glMultiTexCoord2f (GL_TEXTURE1, vp_w, 0);
  glVertex2i(vp_w, vp_h);
  glMultiTexCoord2f (GL_TEXTURE0, vp_w, vp_h);
  glMultiTexCoord2f (GL_TEXTURE1, vp_w, vp_h);
  glVertex2i(vp_w, 0);
  glEnd ();

  glEnable (GL_DEPTH_TEST);

  glBindProgramARB (GL_FRAGMENT_PROGRAM_ARB, 0);
  glDisable (GL_FRAGMENT_PROGRAM_ARB);
745

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
746
747
748
749
750
751
752
753
754
755
756
  glActiveTexture (GL_TEXTURE1);
  glBindTexture (GL_TEXTURE_RECTANGLE_NV, 0);
  glDisable (GL_TEXTURE_RECTANGLE_NV);

  glActiveTexture (GL_TEXTURE0);
  glBindTexture (GL_TEXTURE_RECTANGLE_NV, 0);
  glDisable (GL_TEXTURE_RECTANGLE_NV);

}


Jan Möbius's avatar
   
Jan Möbius committed
757
758
//=============================================================================
//=============================================================================