QtBaseViewerStereo.cc 10.8 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
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
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
//
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
Jan Möbius's avatar
Jan Möbius committed
26 27 28
//   $Revision$
//   $Author$
//   $Date$
Jan Möbius's avatar
 
Jan Möbius committed
29 30 31 32 33 34 35 36
//
//=============================================================================




//=============================================================================
//
Jan Möbius's avatar
 
Jan Möbius committed
37
//  CLASS glViewer - IMPLEMENTATION
Jan Möbius's avatar
 
Jan Möbius committed
38 39 40 41 42 43 44 45 46 47
//
//=============================================================================


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

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

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

Jan Möbius's avatar
 
Jan Möbius committed
50 51 52 53 54 55

//== NAMESPACES ===============================================================


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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
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
74
void
Jan Möbius's avatar
 
Jan Möbius committed
75
glViewer::setStereoMode(bool _b)
Jan Möbius's avatar
 
Jan Möbius committed
76 77 78
{
  stereo_ = _b;

79
  if (!stereo_) {
Jan Möbius's avatar
 
Jan Möbius committed
80 81 82 83 84 85 86
    makeCurrent();
    glDrawBuffer(GL_BACK);
  }

  updateGL();
}

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
87 88 89 90 91 92
//-----------------------------------------------------------------------------


void
glViewer::drawScene_glStereo()
{
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
93
  double l, r, t, b, w, h, a, radians, wd2, ndfl, zerop, xrange;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
94 95 96 97 98 99 100

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

  radians = fovy_ * 0.5 / 180.0 * M_PI;
  wd2     = near_ * tan(radians);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
101 102 103
  zerop   = near_ + ((far_ - near_) * OpenFlipper::Options::focalDistance ());
  ndfl    = near_ / zerop ;
  xrange  = a * wd2 * 2 * zerop / near_;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
104 105 106 107 108 109

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
110
  double offset  = 0.5 * OpenFlipper::Options::eyeDistance () * xrange;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
111 112 113 114 115 116
  double offset2 = offset * ndfl;

  // left eye
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(l+offset2, r+offset2, b, t, near_, far_);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
117
  glTranslatef(+offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
118 119 120 121 122 123 124 125 126 127 128
  glMatrixMode(GL_MODELVIEW);
  glDrawBuffer(GL_BACK_LEFT);
  glstate_->clearBuffers ();
  glClear(GL_DEPTH_BUFFER_BIT);
  drawScene_mono();


  // right eye
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(l-offset2, r-offset2, b, t, near_, far_);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
129
  glTranslatef(-offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  glMatrixMode(GL_MODELVIEW);
  glDrawBuffer(GL_BACK_RIGHT);
  glstate_->clearBuffers ();
  glClear(GL_DEPTH_BUFFER_BIT);
  drawScene_mono();
  glDrawBuffer(GL_BACK);
}


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


void
glViewer::drawScene_anaglyphStereo()
{
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
145
 double l, r, t, b, w, h, a, radians, wd2, ndfl, zerop, xrange;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
146 147 148 149 150 151 152

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

  radians = fovy_ * 0.5 / 180.0 * M_PI;
  wd2     = near_ * tan(radians);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
153 154 155
  zerop   = near_ + ((far_ - near_) * OpenFlipper::Options::focalDistance ());
  ndfl    = near_ / zerop ;
  xrange  = a * wd2 * 2 * zerop / near_;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
156 157 158 159 160 161

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
162
  double offset  = 0.5 * OpenFlipper::Options::eyeDistance () * xrange;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
163 164 165 166 167 168
  double offset2 = offset * ndfl;

  // left eye
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(l+offset2, r+offset2, b, t, near_, far_);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
169
  glTranslatef(offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

  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);

  
  // right eye
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(l-offset2, r-offset2, b, t, near_, far_);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
185
  glTranslatef(-offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
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
  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
276
  double l, r, t, b, w, h, a, radians, wd2, ndfl, zerop, xrange;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
277 278 279 280 281 282 283

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

  radians = fovy_ * 0.5 / 180.0 * M_PI;
  wd2     = near_ * tan(radians);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
284 285 286
  zerop   = near_ + ((far_ - near_) * OpenFlipper::Options::focalDistance ());
  ndfl    = near_ / zerop ;
  xrange  = a * wd2 * 2 * zerop / near_;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
287 288 289 290 291 292

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
293
  double offset  = 0.5 * OpenFlipper::Options::eyeDistance () * xrange;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
294 295 296 297 298 299 300 301 302 303 304 305
  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();
  glFrustum(l+offset2, r+offset2, b, t, near_, far_);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
306
  glTranslatef(offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
307 308 309 310 311 312 313 314 315 316 317 318 319
  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();
  glFrustum(l-offset2, r-offset2, b, t, near_, far_);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
320
  glTranslatef(-offset, 0.0, 0.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
321 322 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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  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);
  
  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
388 389
//=============================================================================
//=============================================================================