QtBaseViewerPicking.cc 36.1 KB
Newer Older
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen      *
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
 *                           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/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
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 "QtGLGraphicsScene.hh"
#include "QtGLGraphicsView.hh"

Jan Möbius's avatar
Jan Möbius committed
59 60
#include <QGLFramebufferObject>

Jan Möbius's avatar
 
Jan Möbius committed
61 62 63 64 65 66 67 68 69 70
//== NAMESPACES ===============================================================

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

static const unsigned int  SELECTION_BUFFER_SIZE = 10000;
static const unsigned int  NAME_STACK_SIZE       = 2;

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


Jan Möbius's avatar
 
Jan Möbius committed
71
bool glViewer::pick( ACG::SceneGraph::PickTarget _pickTarget,
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
72 73 74 75 76 77 78
                     const QPoint&               _mousePos,
                     unsigned int&               _nodeIdx,
                     unsigned int&               _targetIdx,
                     ACG::Vec3d*                 _hitPointPtr )
{
  if (sceneGraphRoot_)
  {
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
79 80 81
    // unsigned int node, target;
    // QTime time;
    // time.start ();
Jan Möbius's avatar
Jan Möbius committed
82 83 84 85 86
    int rv = pickFromCache (_pickTarget, _mousePos, _nodeIdx, _targetIdx, _hitPointPtr);

    // cache will return -1 if a update is needed or caching is not supported
    if (rv < 0)
      rv = pickColor (_pickTarget, _mousePos, _nodeIdx, _targetIdx, _hitPointPtr);
Dirk Wilden's avatar
Dirk Wilden committed
87

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
88 89 90 91
    // printf ("ColorPicking took %d msec\n",time.restart ());
    // rv = -1;
    // node = _nodeIdx;
    // target = _targetIdx;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
92 93
    if (rv < 0)
      rv = pickGL (_pickTarget, _mousePos, _nodeIdx, _targetIdx, _hitPointPtr);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
94
    // printf ("GLPicking took %d msec\n",time.restart ());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
95

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
96 97
    // if (rv > 0 && (node != _nodeIdx || target != _targetIdx))
    //   printf ("***** Picking difference Color %d/%d GL %d/%d\n",node, target, _nodeIdx, _targetIdx);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
98 99 100 101 102 103 104 105 106 107
    if (rv > 0)
      return rv;
  }
  return false;
}


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

int glViewer::pickColor( ACG::SceneGraph::PickTarget _pickTarget,
Jan Möbius's avatar
 
Jan Möbius committed
108 109 110 111 112
                         const QPoint&               _mousePos,
                         unsigned int&               _nodeIdx,
                         unsigned int&               _targetIdx,
                         ACG::Vec3d*                 _hitPointPtr )
{
113 114 115 116 117
  GLint         w = glWidth(),
                h = glHeight(),
                l = scenePos().x(),
                b = scene()->height () - scenePos().y() - h,
                x = _mousePos.x(),
Jan Möbius's avatar
Jan Möbius committed
118 119 120
                y = scene()->height () - _mousePos.y(),
                pW = 1,
                pH = 1;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
121
  GLubyte       pixels[9][4];
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
122 123
  GLfloat       depths[9];
  int           hit = -1;
Jan Möbius's avatar
Jan Möbius committed
124 125

  // traversing order (center, top, bottom, ...)
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
126 127
  unsigned char order[9] = { 4, 7, 1, 3, 5, 0, 2, 6, 8 };

Jan Möbius's avatar
Jan Möbius committed
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
  if (pickCacheSupported_)
  {
    // delete pick cache if the size changed
    if (pickCache_ && pickCache_->size () != QSize (glWidth (), glHeight ()))
    {
      delete pickCache_;
      pickCache_ = NULL;
    }
    // create a new pick cache frambuffer object
    if (!pickCache_)
    {
      pickCache_ = new QGLFramebufferObject (glWidth (), glHeight (), QGLFramebufferObject::Depth);
      if (!pickCache_->isValid ())
      {
        pickCacheSupported_ = false;
        delete pickCache_;
        pickCache_ = NULL;
      }
    }
    if (pickCache_)
    {
      // the viewport for the framebuffer object
      l = 0;
      b = 0;
      x = _mousePos.x() - scenePos().x();
      y = glHeight() - (_mousePos.y() - scenePos().y());

      // we can only pick inside of our window
      if (x < 0 || y < 0 || x >= (int)glWidth() || y >= (int)glHeight())
        return 0;

      pickCache_->bind ();
    }
  }

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
163 164 165
  const ACG::GLMatrixd&  modelview  = properties_.glState().modelview();
  const ACG::GLMatrixd&  projection = properties_.glState().projection();

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
166 167 168
  ACG::Vec4f clear_color = properties_.glState().clear_color();
  properties_.glState().set_clear_color (ACG::Vec4f (0.0, 0.0, 0.0, 0.0));

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
169 170 171
  // prepare GL state
  makeCurrent();

172
  glViewport (l, b, w, h);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
173 174
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
175

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
176 177 178 179
  glMultMatrixd(projection.get_raw_data());
  glMatrixMode(GL_MODELVIEW);
  glLoadMatrixd(modelview.get_raw_data());
  glDisable(GL_LIGHTING);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
180
  glDisable(GL_BLEND);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
181 182 183 184 185
  glEnable(GL_DEPTH_TEST);
  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  properties_.glState().pick_init (true);

  // do the picking
Jan Möbius's avatar
Jan Möbius committed
186
  ACG::SceneGraph::PickAction action(properties_.glState(), _pickTarget, properties_.drawMode());
187
  ACG::SceneGraph::traverse_multipass(sceneGraphRoot_, action,properties_.glState() );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
188 189 190 191 192 193 194 195

  // restore GL state
  glMatrixMode( GL_PROJECTION );
  glLoadMatrixd(projection.get_raw_data());
  glMatrixMode( GL_MODELVIEW );
  glLoadMatrixd(modelview.get_raw_data());
  glEnable(GL_LIGHTING);

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
196 197
  properties_.glState().set_clear_color (clear_color);

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
198
  if (properties_.glState().pick_error ())
Jan Möbius's avatar
Jan Möbius committed
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
  {
    if (pickCache_ && pickCache_->isBound ())
      pickCache_->release ();
    return -1;
  }

  glPixelStorei(GL_PACK_ALIGNMENT, 1);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

  // we can only read inside our viewport
  if (x + 1 < w)
    pW++;

  if (y + 1 < h)
    pH++;

  if (x > 0)
  {
    x--;
    pW++;
  }
  if (y > 0)
  {
    y--;
    pH++;
  }

  if (pH != 3 || pW != 3)
  {
    // initialize unused values with 0
    for (int i = 0; i < 9; i++)
    {
      pixels[i][0] = 0;
      pixels[i][1] = 0;
      pixels[i][2] = 0;
      pixels[i][3] = 0;
      depths[i] = 0.0;
    }
  }

  // read from framebuffer
  glReadPixels (x, y, pW, pH, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  glReadPixels (x, y, pW, pH, GL_DEPTH_COMPONENT, GL_FLOAT, depths);

  // unbind pick cache
  if (pickCache_ && pickCache_->isBound ())
  {
    pickCache_->release ();
    updatePickCache_ = false;
248
    pickCacheTarget_ = _pickTarget;
Jan Möbius's avatar
Jan Möbius committed
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
  }

  // get first found pixel
  for (int i = 0; i < 9; i++)
  {
    if (hit < 0 && (pixels[order[i]][2] != 0 || pixels[order[i]][1] != 0 || pixels[order[i]][0] != 0 || pixels[order[i]][3] != 0))
    {
      hit = order[i];
      break;
    }
  }

  if (hit < 0)
    return 0;


  ACG::Vec4uc rgba;
  rgba[0] = pixels[hit][0];
  rgba[1] = pixels[hit][1];
  rgba[2] = pixels[hit][2];
  rgba[3] = pixels[hit][3];

  std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgba);

  // something wrong with the color stack ?
  if (rv.size () < 2)
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
275 276
    return -1;

Jan Möbius's avatar
Jan Möbius committed
277 278 279
  _nodeIdx   = rv[1];
  _targetIdx = rv[0];

280 281 282 283 284 285 286 287 288 289 290
//   // Debug Code to visualize picking cache ( DO NOT REMOVE!!!! Jan )
//   QImage murks(glWidth (),glHeight (),QImage::Format_ARGB32);
//   murks = pickCache_->toImage();
//   for ( int i = 0 ; i < glWidth() ; ++i ) 
//     for ( int j = 0 ; j < glHeight() ; ++j )  {
//       QColor bla (murks.pixel(i,j));
//       bla.setAlpha(255);
//       murks.setPixel(i,j,bla.rgba());
//     }
//   murks.save("murks.png");

Jan Möbius's avatar
Jan Möbius committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
  if (_hitPointPtr)
  {
    *_hitPointPtr = properties_.glState().unproject (
      ACG::Vec3d(_mousePos.x(), scene()->height () - _mousePos.y(),depths[hit]));
  }

  return 1;
}

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

int glViewer::pickFromCache( ACG::SceneGraph::PickTarget _pickTarget,
                             const QPoint&               _mousePos,
                             unsigned int&               _nodeIdx,
                             unsigned int&               _targetIdx,
                             ACG::Vec3d*                 _hitPointPtr )
{
  // do we need an update?
309 310
  if (!pickCacheSupported_ || updatePickCache_ || !pickCache_ ||
      pickCacheTarget_ != _pickTarget)
Jan Möbius's avatar
Jan Möbius committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    return -1;

  GLint         x = _mousePos.x() - scenePos().x(),
                y = glHeight() - (_mousePos.y() - scenePos().y()),
                pW = 1,
                pH = 1;
  GLubyte       pixels[9][4];
  GLfloat       depths[9];
  int           hit = -1;

  // traversing order (center, top, bottom, ...)
  unsigned char order[9] = { 4, 7, 1, 3, 5, 0, 2, 6, 8 };

  // can't pick outside
  if (x < 0 || y < 0 || x >= (int)glWidth() || y >= (int)glHeight())
    return 0;

  // bind cache framebuffer object
  pickCache_->bind ();

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
331 332 333
  glPixelStorei(GL_PACK_ALIGNMENT, 1);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

Jan Möbius's avatar
Jan Möbius committed
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
  // we can only read inside our viewport
  if (x + 1 < (int)glWidth ())
    pW++;

  if (y + 1 < (int)glHeight ())
    pH++;

  if (x > 0)
  {
    x--;
    pW++;
  }
  if (y > 0)
  {
    y--;
    pH++;
  }

  if (pH != 3 || pW != 3)
  {
    // initialize unused values with 0
    for (int i = 0; i < 9; i++)
    {
      pixels[i][0] = 0;
      pixels[i][1] = 0;
      pixels[i][2] = 0;
      pixels[i][3] = 0;
      depths[i] = 0.0;
    }
  }

  // read from framebuffer
  glReadPixels (x, y, pW, pH, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  glReadPixels (x, y, pW, pH, GL_DEPTH_COMPONENT, GL_FLOAT, depths);

  // unbind
  pickCache_->release ();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
371

Jan Möbius's avatar
Jan Möbius committed
372
  // get first found pixel
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
373
  for (int i = 0; i < 9; i++)
Jan Möbius's avatar
 
Jan Möbius committed
374
  {
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
375
    if (hit < 0 && (pixels[order[i]][2] != 0 || pixels[order[i]][1] != 0 || pixels[order[i]][0] != 0 || pixels[order[i]][3] != 0))
Jan Möbius's avatar
 
Jan Möbius committed
376
    {
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
377 378 379 380 381 382 383
      hit = order[i];
      break;
    }
  }

  if (hit < 0)
    return 0;
Jan Möbius's avatar
 
Jan Möbius committed
384 385


Jan Möbius's avatar
Dennis:  
Jan Möbius committed
386 387 388 389 390
  ACG::Vec4uc rgba;
  rgba[0] = pixels[hit][0];
  rgba[1] = pixels[hit][1];
  rgba[2] = pixels[hit][2];
  rgba[3] = pixels[hit][3];
Jan Möbius's avatar
 
Jan Möbius committed
391

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
392
  std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgba);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
393

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
394 395 396 397 398 399 400 401 402
  // something wrong with the color stack ?
  if (rv.size () < 2)
    return -1;

  _nodeIdx   = rv[1];
  _targetIdx = rv[0];

  if (_hitPointPtr)
  {
Jan Möbius's avatar
Jan Möbius committed
403 404
    *_hitPointPtr = properties_.glState().unproject(
      ACG::Vec3d(_mousePos.x(), scene()->height () - _mousePos.y(),depths[hit]));
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
  }

  return 1;
}

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

bool glViewer::pickGL( ACG::SceneGraph::PickTarget _pickTarget,
                       const QPoint&               _mousePos,
                       unsigned int&               _nodeIdx,
                       unsigned int&               _targetIdx,
                       ACG::Vec3d*                 _hitPointPtr )
{
  GLint         w = glWidth(),
                h = glHeight(),
                l = scenePos().x(),
                b = scene()->height () - scenePos().y() - h,
                x = _mousePos.x(),
                y = scene()->height () - _mousePos.y();
  GLint         viewport[4] = {l,b,w,h};
  GLuint        selectionBuffer[ SELECTION_BUFFER_SIZE ],
                nameBuffer[ NAME_STACK_SIZE ];

  const ACG::GLMatrixd&  modelview  = properties_.glState().modelview();
  const ACG::GLMatrixd&  projection = properties_.glState().projection();

  // prepare GL state
  makeCurrent();

  glSelectBuffer( SELECTION_BUFFER_SIZE, selectionBuffer );
  glRenderMode(GL_SELECT);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPickMatrix((GLdouble) x, (GLdouble) y, 3, 3, viewport);
  glMultMatrixd(projection.get_raw_data());
  glMatrixMode(GL_MODELVIEW);
  glLoadMatrixd(modelview.get_raw_data());
  glDisable(GL_LIGHTING);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
443
  glDisable(GL_BLEND);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
444 445 446 447
  glClear(GL_DEPTH_BUFFER_BIT);
  properties_.glState().pick_init (false);

  // do the picking
Jan Möbius's avatar
Jan Möbius committed
448
  ACG::SceneGraph::PickAction action(properties_.glState(), _pickTarget, properties_.drawMode());
449
  ACG::SceneGraph::traverse_multipass(sceneGraphRoot_, action, properties_.glState());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
450 451 452 453 454 455 456 457
  int hits = glRenderMode(GL_RENDER);

  // restore GL state
  glMatrixMode( GL_PROJECTION );
  glLoadMatrixd(projection.get_raw_data());
  glMatrixMode( GL_MODELVIEW );
  glLoadMatrixd(modelview.get_raw_data());
  glEnable(GL_LIGHTING);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
458
  glEnable(GL_BLEND);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

  // process hit record
  if ( hits > 0 )
  {
    GLuint *ptr = selectionBuffer,
    num_names,
    z,
    min_z=~(0u),
    max_z=0;

    for (int i=0; i<hits; ++i)
    {
      num_names = *ptr++;
      if ( num_names != NAME_STACK_SIZE )
      {
        std::cerr << "glViewer::pick() : namestack error\n\n";
        return false;
      }

      if ( (z = *ptr++) < min_z )
Jan Möbius's avatar
 
Jan Möbius committed
479
      {
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
480 481 482 483
        min_z = z;
        max_z = *ptr++;
        for (unsigned int j=0; j<num_names; ++j)
          nameBuffer[j] = *ptr++;
Jan Möbius's avatar
 
Jan Möbius committed
484
      }
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
485 486 487 488 489
      else ptr += 1+num_names;
    }

    _nodeIdx   = nameBuffer[0];
    _targetIdx = nameBuffer[1];
Jan Möbius's avatar
 
Jan Möbius committed
490

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
491 492 493 494 495 496 497
    if (_hitPointPtr)
    {
      GLuint zscale=~(0u);
      GLdouble min_zz = ((GLdouble)min_z) / ((GLdouble)zscale);
      GLdouble max_zz = ((GLdouble)max_z) / ((GLdouble)zscale);
      GLdouble zz     = 0.5F * (min_zz + max_zz);
      *_hitPointPtr = properties_.glState().unproject(ACG::Vec3d(x,y,zz));
Jan Möbius's avatar
 
Jan Möbius committed
498
    }
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
499 500

    return true;
Jan Möbius's avatar
 
Jan Möbius committed
501
  }
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
502 503
  else if (hits < 0)
    std::cerr << "glViewer::pick() : selection buffer overflow\n\n";
Jan Möbius's avatar
 
Jan Möbius committed
504 505 506 507 508 509

  return false;
}

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
510 511 512 513
bool glViewer::pick_region( ACG::SceneGraph::PickTarget                _pickTarget,
                            const QRegion&                             _region,
                            QList<QPair<unsigned int, unsigned int> >& _list)
{
514
  QRect    rect = _region.boundingRect();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
515 516 517
  GLint    w = glWidth(),
           h = glHeight(),
           l = scenePos().x(),
518 519 520
           b = scene()->height () - scenePos().y() - h,
           x = rect.x(),
           y = scene()->height () - rect.bottom();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
521 522
  GLubyte* buffer;

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  if (pickCacheSupported_)
  {
    // delete pick cache if the size changed
    if (pickCache_ && pickCache_->size () != QSize (glWidth (), glHeight ()))
    {
      delete pickCache_;
      pickCache_ = NULL;
    }
    // create a new pick cache frambuffer object
    if (!pickCache_)
    {
      pickCache_ = new QGLFramebufferObject (glWidth (), glHeight (), QGLFramebufferObject::Depth);
      if (!pickCache_->isValid ())
      {
        pickCacheSupported_ = false;
        delete pickCache_;
        pickCache_ = NULL;
      }
    }
    if (pickCache_)
    {
      // the viewport for the framebuffer object
      l = 0;
      b = 0;
      x = rect.x() - scenePos().x();
      y = glHeight() - (rect.bottom() - scenePos().y());

      // we can only pick inside of our window
      if (x < 0 || y < 0 || x >= (int)glWidth() || y >= (int)glHeight())
        return 0;

      pickCache_->bind ();
    }
  }
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
557 558 559 560

  const ACG::GLMatrixd&  modelview  = properties_.glState().modelview();
  const ACG::GLMatrixd&  projection = properties_.glState().projection();

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
561 562 563
  ACG::Vec4f clear_color = properties_.glState().clear_color();
  properties_.glState().set_clear_color (ACG::Vec4f (0.0, 0.0, 0.0, 0.0));

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
564 565 566 567 568 569 570 571 572 573 574
  // prepare GL state
  makeCurrent();

  glViewport (l, b, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  glMultMatrixd(projection.get_raw_data());
  glMatrixMode(GL_MODELVIEW);
  glLoadMatrixd(modelview.get_raw_data());
  glDisable(GL_LIGHTING);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
575
  glDisable(GL_BLEND);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
576 577 578 579 580
  glEnable(GL_DEPTH_TEST);
  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  properties_.glState().pick_init (true);

  // do the picking
Jan Möbius's avatar
Jan Möbius committed
581
  ACG::SceneGraph::PickAction action(properties_.glState(), _pickTarget, properties_.drawMode());
582
  ACG::SceneGraph::traverse_multipass(sceneGraphRoot_, action,properties_.glState());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
583 584 585 586 587 588 589

  // restore GL state
  glMatrixMode( GL_PROJECTION );
  glLoadMatrixd(projection.get_raw_data());
  glMatrixMode( GL_MODELVIEW );
  glLoadMatrixd(modelview.get_raw_data());
  glEnable(GL_LIGHTING);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
590
  glEnable(GL_BLEND);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
591

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
592 593
  properties_.glState().set_clear_color (clear_color);

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
594
  if (properties_.glState().pick_error ())
595 596 597 598 599
  {
    if (pickCache_ && pickCache_->isBound ())
      pickCache_->release ();
    return -1;
  }
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
600

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
601
  buffer = new GLubyte[4 * rect.width() * rect.height()];
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
602 603 604 605

  glPixelStorei(GL_PACK_ALIGNMENT, 1);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

606
  glReadPixels (x, y, rect.width(),
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
607
                rect.height(), GL_RGBA, GL_UNSIGNED_BYTE, buffer);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
608 609 610 611 612 613 614 615

  QSet<QPair<unsigned int, unsigned int> > found;

  for (int y = 0; y < rect.height (); y++)
    for (int x = 0; x < rect.width (); x++)
    {
      if (_region.contains (QPoint (rect.x() + x, rect.y() + y)))
      {
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
616 617
        int bPos = (((rect.height () - (y + 1)) * rect.width ()) + x) * 4;
        if (buffer[bPos + 2] != 0 || buffer[bPos + 1] != 0 || buffer[bPos] != 0 || buffer[bPos + 3] != 0)
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
618
        {
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
619 620 621 622 623
          ACG::Vec4uc rgba;
          rgba[0] = buffer[bPos];
          rgba[1] = buffer[bPos + 1];
          rgba[2] = buffer[bPos + 2];
          rgba[3] = buffer[bPos + 3];
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
624

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
625
          std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgba);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
626 627 628 629 630 631 632 633 634 635 636
          if (rv.size () < 2)
            continue;

          found << QPair<unsigned int, unsigned int>(rv[1], rv[0]);
        }
      }
    }

  delete buffer;
  _list = found.toList();

637 638 639 640 641 642 643 644
  // unbind pick cache
  if (pickCache_ && pickCache_->isBound ())
  {
    pickCache_->release ();
    updatePickCache_ = false;
    pickCacheTarget_ = _pickTarget;
  }

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
645 646 647
  return true;
}

Henrik Zimmer's avatar
Henrik Zimmer committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 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 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
//-----------------------------------------------------------------------------

bool glViewer::pick_region( ACG::SceneGraph::PickTarget                _pickTarget,
                            const QRegion&                             _region,
                            QList<QPair<unsigned int, unsigned int> >& _list,
                            QVector<ACG::Vec3d>&                       _points)
{
  QRect    rect = _region.boundingRect();
  GLint    w = glWidth(),
           h = glHeight(),
           l = scenePos().x(),
           b = scene()->height () - scenePos().y() - h,
           x = rect.x(),
           y = scene()->height () - rect.bottom();
  GLubyte* buffer;
  GLfloat* depths;

  if (pickCacheSupported_)
  {
    // delete pick cache if the size changed
    if (pickCache_ && pickCache_->size () != QSize (glWidth (), glHeight ()))
    {
      delete pickCache_;
      pickCache_ = NULL;
    }
    // create a new pick cache frambuffer object
    if (!pickCache_)
    {
      pickCache_ = new QGLFramebufferObject (glWidth (), glHeight (), QGLFramebufferObject::Depth);
      if (!pickCache_->isValid ())
      {
        pickCacheSupported_ = false;
        delete pickCache_;
        pickCache_ = NULL;
      }
    }
    if (pickCache_)
    {
      // the viewport for the framebuffer object
      l = 0;
      b = 0;
      x = rect.x() - scenePos().x();
      y = glHeight() - (rect.bottom() - scenePos().y());

      // we can only pick inside of our window
      if (x < 0 || y < 0 || x >= (int)glWidth() || y >= (int)glHeight())
        return 0;

      pickCache_->bind ();
    }
  }

  const ACG::GLMatrixd&  modelview  = properties_.glState().modelview();
  const ACG::GLMatrixd&  projection = properties_.glState().projection();

  ACG::Vec4f clear_color = properties_.glState().clear_color();
  properties_.glState().set_clear_color (ACG::Vec4f (0.0, 0.0, 0.0, 0.0));

  // prepare GL state
  makeCurrent();

  glViewport (l, b, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  glMultMatrixd(projection.get_raw_data());
  glMatrixMode(GL_MODELVIEW);
  glLoadMatrixd(modelview.get_raw_data());
  glDisable(GL_LIGHTING);
  glDisable(GL_BLEND);
  glEnable(GL_DEPTH_TEST);
  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  properties_.glState().pick_init (true);

  // do the picking
  ACG::SceneGraph::PickAction action(properties_.glState(), _pickTarget, properties_.drawMode());
  ACG::SceneGraph::traverse_multipass(sceneGraphRoot_, action,properties_.glState());

  // restore GL state
  glMatrixMode( GL_PROJECTION );
  glLoadMatrixd(projection.get_raw_data());
  glMatrixMode( GL_MODELVIEW );
  glLoadMatrixd(modelview.get_raw_data());
  glEnable(GL_LIGHTING);
  glEnable(GL_BLEND);

  properties_.glState().set_clear_color (clear_color);

  if (properties_.glState().pick_error ())
  {
    if (pickCache_ && pickCache_->isBound ())
      pickCache_->release ();
    return -1;
  }

  buffer = new GLubyte[4 * rect.width() * rect.height()];
  depths = new GLfloat[ rect.width() * rect.height() ];

  glPixelStorei(GL_PACK_ALIGNMENT, 1);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

  glReadPixels (x, y, rect.width(),
                rect.height(), GL_RGBA, GL_UNSIGNED_BYTE, buffer);

  glReadPixels (x, y, rect.width(), rect.height(), GL_DEPTH_COMPONENT, GL_FLOAT, depths);

  QImage depthmapimage(rect.width(), rect.height(), QImage::Format_Indexed8);
  // color map
  for ( int i = 0 ; i <= 255 ; i++ )
    depthmapimage.setColor( i, qRgb( i, i, i ) );

  for ( int i = 0 ; i < rect.width() ; i++ )
    for ( int j = 0 ; j < rect.height() ; j++ ) 
    {
      //std::cerr << "("<<i<<","<<j<<") depth " << depths[j*rect.width()+i] << std::endl;
      depthmapimage.setPixel(i,rect.height()-j-1, (unsigned int)(depths[j*rect.width()+i]*255));
    }

  depthmapimage.save("test.png");

  // needed for consistent ordering (Set->toList undefined order!!)
  //QList<QPair<unsigned int, unsigned int> > found;
  

  // 3d position for unproject
  ACG::Vec3d pos3d;
  for (int y = 0; y < rect.height (); y++)
    for (int x = 0; x < rect.width (); x++)
    {
      if (_region.contains (QPoint (rect.x() + x, rect.y() + y)))
      {
        int bPos = (((rect.height () - (y + 1)) * rect.width ()) + x) * 4;
        if (buffer[bPos + 2] != 0 || buffer[bPos + 1] != 0 || buffer[bPos] != 0 || buffer[bPos + 3] != 0)
        {
          ACG::Vec4uc rgba;
          rgba[0] = buffer[bPos];
          rgba[1] = buffer[bPos + 1];
          rgba[2] = buffer[bPos + 2];
          rgba[3] = buffer[bPos + 3];

          std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgba);
          if (rv.size () < 2)
            continue;

          QPair< unsigned int, unsigned int> curr(rv[1], rv[0]);

          // check if already contained
          if( !_list.contains(  curr))
          {
            _list << curr;
          
            // unproject depth to real (3D) depth value
            pos3d = properties_.glState().unproject(ACG::Vec3d(x+rect.x(),h-(y+rect.y()),depths[(rect.height()-y-1)*rect.width() + x]));
            _points << pos3d;
          }
        }
      }
    }

  delete buffer;
  //_list = found.toList();

  // unbind pick cache
  if (pickCache_ && pickCache_->isBound ())
  {
    pickCache_->release ();
    updatePickCache_ = false;
    pickCacheTarget_ = _pickTarget;
  }

  return true;
}

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

bool glViewer::pick_region( ACG::SceneGraph::PickTarget                _pickTarget,
                            const QRegion&                             _region,
                            QList<QPair<unsigned int, unsigned int> >& _list,
                            QVector<float>&                            _depths)
{
  QRect    rect = _region.boundingRect();
  GLint    w = glWidth(),
           h = glHeight(),
           l = scenePos().x(),
           b = scene()->height () - scenePos().y() - h,
           x = rect.x(),
           y = scene()->height () - rect.bottom();
  GLubyte* buffer;
  GLfloat* depths;

  if (pickCacheSupported_)
  {
    // delete pick cache if the size changed
    if (pickCache_ && pickCache_->size () != QSize (glWidth (), glHeight ()))
    {
      delete pickCache_;
      pickCache_ = NULL;
    }
    // create a new pick cache frambuffer object
    if (!pickCache_)
    {
      pickCache_ = new QGLFramebufferObject (glWidth (), glHeight (), QGLFramebufferObject::Depth);
      if (!pickCache_->isValid ())
      {
        pickCacheSupported_ = false;
        delete pickCache_;
        pickCache_ = NULL;
      }
    }
    if (pickCache_)
    {
      // the viewport for the framebuffer object
      l = 0;
      b = 0;
      x = rect.x() - scenePos().x();
      y = glHeight() - (rect.bottom() - scenePos().y());

      // we can only pick inside of our window
      if (x < 0 || y < 0 || x >= (int)glWidth() || y >= (int)glHeight())
        return 0;

      pickCache_->bind ();
    }
  }

  const ACG::GLMatrixd&  modelview  = properties_.glState().modelview();
  const ACG::GLMatrixd&  projection = properties_.glState().projection();

  ACG::Vec4f clear_color = properties_.glState().clear_color();
  properties_.glState().set_clear_color (ACG::Vec4f (0.0, 0.0, 0.0, 0.0));

  // prepare GL state
  makeCurrent();

  glViewport (l, b, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  glMultMatrixd(projection.get_raw_data());
  glMatrixMode(GL_MODELVIEW);
  glLoadMatrixd(modelview.get_raw_data());
  glDisable(GL_LIGHTING);
  glDisable(GL_BLEND);
  glEnable(GL_DEPTH_TEST);
  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  properties_.glState().pick_init (true);

  // do the picking
  ACG::SceneGraph::PickAction action(properties_.glState(), _pickTarget, properties_.drawMode());
  ACG::SceneGraph::traverse_multipass(sceneGraphRoot_, action,properties_.glState());

  // restore GL state
  glMatrixMode( GL_PROJECTION );
  glLoadMatrixd(projection.get_raw_data());
  glMatrixMode( GL_MODELVIEW );
  glLoadMatrixd(modelview.get_raw_data());
  glEnable(GL_LIGHTING);
  glEnable(GL_BLEND);

  properties_.glState().set_clear_color (clear_color);

  if (properties_.glState().pick_error ())
  {
    if (pickCache_ && pickCache_->isBound ())
      pickCache_->release ();
    return -1;
  }

  buffer = new GLubyte[4 * rect.width() * rect.height()];
  depths = new GLfloat[ rect.width() * rect.height() ];

  glPixelStorei(GL_PACK_ALIGNMENT, 1);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

  glReadPixels (x, y, rect.width(),
                rect.height(), GL_RGBA, GL_UNSIGNED_BYTE, buffer);

  glReadPixels (x, y, rect.width(), rect.height(), GL_DEPTH_COMPONENT, GL_FLOAT, depths);

  QImage depthmapimage(rect.width(), rect.height(), QImage::Format_Indexed8);
  // color map
  for ( int i = 0 ; i <= 255 ; i++ )
    depthmapimage.setColor( i, qRgb( i, i, i ) );

  for ( int i = 0 ; i < rect.width() ; i++ )
    for ( int j = 0 ; j < rect.height() ; j++ ) 
    {
      //std::cerr << "("<<i<<","<<j<<") depth " << depths[j*rect.width()+i] << std::endl;
      depthmapimage.setPixel(i,rect.height()-j-1, (unsigned int)(depths[j*rect.width()+i]*255));
    }

  depthmapimage.save("test.png");

  //QSet<QPair<unsigned int, unsigned int> > found;
  

  //int setsize = 0;
  for (int y = 0; y < rect.height (); y++)
    for (int x = 0; x < rect.width (); x++)
    {
      if (_region.contains (QPoint (rect.x() + x, rect.y() + y)))
      {
        int bPos = (((rect.height () - (y + 1)) * rect.width ()) + x) * 4;
        if (buffer[bPos + 2] != 0 || buffer[bPos + 1] != 0 || buffer[bPos] != 0 || buffer[bPos + 3] != 0)
        {
          ACG::Vec4uc rgba;
          rgba[0] = buffer[bPos];
          rgba[1] = buffer[bPos + 1];
          rgba[2] = buffer[bPos + 2];
          rgba[3] = buffer[bPos + 3];

          std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgba);
          if (rv.size () < 2)
            continue;

          QPair<unsigned int, unsigned int> curr(rv[1], rv[0]);
          //found << QPair<unsigned int, unsigned int>(rv[1], rv[0]);
          
          // added a new (targetidx/nodeidx) pair
          //if( setsize != found.size())
          if( !_list.contains(curr))
          {
            //setsize++;
            _list << curr;
            //_depths << depths[(rect.height()-y-1)*rect.width() + x];
            _depths << depths[(rect.height () - (y + 1)) *rect.width() + x];
          }
        }
      }
    }

  delete buffer;
  //_list = found.toList();

  // unbind pick cache
  if (pickCache_ && pickCache_->isBound ())
  {
    pickCache_->release ();
    updatePickCache_ = false;
    pickCacheTarget_ = _pickTarget;
  }

  return true;
}

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

bool glViewer::pick_region( ACG::SceneGraph::PickTarget                _pickTarget,
                            const QRegion&                             _region,
                            QList<QPair<unsigned int, unsigned int> >& _list,
                            QVector<float>&                            _depths,
                            QVector<ACG::Vec3d>&                       _points)
{
  QRect    rect = _region.boundingRect();
  GLint    w = glWidth(),
           h = glHeight(),
           l = scenePos().x(),
           b = scene()->height () - scenePos().y() - h,
           x = rect.x(),
           y = scene()->height () - rect.bottom();
  GLubyte* buffer;
  GLfloat* depths;

  if (pickCacheSupported_)
  {
    // delete pick cache if the size changed
    if (pickCache_ && pickCache_->size () != QSize (glWidth (), glHeight ()))
    {
      delete pickCache_;
      pickCache_ = NULL;
    }
    // create a new pick cache frambuffer object
    if (!pickCache_)
    {
      pickCache_ = new QGLFramebufferObject (glWidth (), glHeight (), QGLFramebufferObject::Depth);
      if (!pickCache_->isValid ())
      {
        pickCacheSupported_ = false;
        delete pickCache_;
        pickCache_ = NULL;
      }
    }
    if (pickCache_)
    {
      // the viewport for the framebuffer object
      l = 0;
      b = 0;
      x = rect.x() - scenePos().x();
      y = glHeight() - (rect.bottom() - scenePos().y());

      // we can only pick inside of our window
      if (x < 0 || y < 0 || x >= (int)glWidth() || y >= (int)glHeight())
        return 0;

      pickCache_->bind ();
    }
  }

  const ACG::GLMatrixd&  modelview  = properties_.glState().modelview();
  const ACG::GLMatrixd&  projection = properties_.glState().projection();

  ACG::Vec4f clear_color = properties_.glState().clear_color();
  properties_.glState().set_clear_color (ACG::Vec4f (0.0, 0.0, 0.0, 0.0));

  // prepare GL state
  makeCurrent();

  glViewport (l, b, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  glMultMatrixd(projection.get_raw_data());
  glMatrixMode(GL_MODELVIEW);
  glLoadMatrixd(modelview.get_raw_data());
  glDisable(GL_LIGHTING);
  glDisable(GL_BLEND);
  glEnable(GL_DEPTH_TEST);
  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  properties_.glState().pick_init (true);

  // do the picking
  ACG::SceneGraph::PickAction action(properties_.glState(), _pickTarget, properties_.drawMode());
  ACG::SceneGraph::traverse_multipass(sceneGraphRoot_, action,properties_.glState());

  // restore GL state
  glMatrixMode( GL_PROJECTION );
  glLoadMatrixd(projection.get_raw_data());
  glMatrixMode( GL_MODELVIEW );
  glLoadMatrixd(modelview.get_raw_data());
  glEnable(GL_LIGHTING);
  glEnable(GL_BLEND);

  properties_.glState().set_clear_color (clear_color);

  if (properties_.glState().pick_error ())
  {
    if (pickCache_ && pickCache_->isBound ())
      pickCache_->release ();
    return -1;
  }

  buffer = new GLubyte[4 * rect.width() * rect.height()];
  depths = new GLfloat[ rect.width() * rect.height() ];

  glPixelStorei(GL_PACK_ALIGNMENT, 1);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

  glReadPixels (x, y, rect.width(),
                rect.height(), GL_RGBA, GL_UNSIGNED_BYTE, buffer);

  glReadPixels (x, y, rect.width(), rect.height(), GL_DEPTH_COMPONENT, GL_FLOAT, depths);

  QImage depthmapimage(rect.width(), rect.height(), QImage::Format_Indexed8);
  // color map
  for ( int i = 0 ; i <= 255 ; i++ )
    depthmapimage.setColor( i, qRgb( i, i, i ) );

  for ( int i = 0 ; i < rect.width() ; i++ )
    for ( int j = 0 ; j < rect.height() ; j++ ) 
    {
      //std::cerr << "("<<i<<","<<j<<") depth " << depths[j*rect.width()+i] << std::endl;
      depthmapimage.setPixel(i,rect.height()-j-1, (unsigned int)(depths[j*rect.width()+i]*255));
    }

  depthmapimage.save("test.png");

  //QSet<QPair<unsigned int, unsigned int> > found;
  

  //int setsize = 0;
  for (int y = 0; y < rect.height (); y++)
    for (int x = 0; x < rect.width (); x++)
    {
      if (_region.contains (QPoint (rect.x() + x, rect.y() + y)))
      {
        int bPos = (((rect.height () - (y + 1)) * rect.width ()) + x) * 4;
        if (buffer[bPos + 2] != 0 || buffer[bPos + 1] != 0 || buffer[bPos] != 0 || buffer[bPos + 3] != 0)
        {
          ACG::Vec4uc rgba;
          rgba[0] = buffer[bPos];
          rgba[1] = buffer[bPos + 1];
          rgba[2] = buffer[bPos + 2];
          rgba[3] = buffer[bPos + 3];

          std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgba);
          if (rv.size () < 2)
            continue;

          QPair<unsigned int, unsigned int> curr(rv[1], rv[0]);
          //found << QPair<unsigned int, unsigned int>(rv[1], rv[0]);
          
          // added a new (targetidx/nodeidx) pair
          //if( setsize != found.size())
          if( !_list.contains(curr))
          {
            _list << curr;
            double curr_depth(depths[(rect.height()-(y+1))*rect.width() + x]);

            _depths << curr_depth;

            // unproject depth to real (3D) depth value
            _points << properties_.glState().unproject(ACG::Vec3d(x+rect.x(),h-(y+rect.y()),curr_depth));
          }
        }
      }
    }

  delete buffer;
  //_list = found.toList();

  // unbind pick cache
  if (pickCache_ && pickCache_->isBound ())
  {
    pickCache_->release ();
    updatePickCache_ = false;
    pickCacheTarget_ = _pickTarget;
  }

  return true;
}


Jan Möbius's avatar
Dennis:  
Jan Möbius committed
1170 1171
//-----------------------------------------------------------------------------

Jan Möbius's avatar
 
Jan Möbius committed
1172
bool
Jan Möbius's avatar
 
Jan Möbius committed
1173
glViewer::
Jan Möbius's avatar
 
Jan Möbius committed
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
fast_pick( const QPoint&  _mousePos,
           ACG::Vec3d&    _hitPoint )
{
  // get x,y,z values of pixel
  GLint     x(_mousePos.x()), y(glHeight() - _mousePos.y());
  GLfloat   z;


  makeCurrent();
  glPixelStorei(GL_PACK_ALIGNMENT, 1);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);


  if (z < 0.99999)
  {
1190
    _hitPoint = properties_.glState().unproject( ACG::Vec3d(x, y, z) );
Jan Möbius's avatar
 
Jan Möbius committed
1191 1192 1193 1194 1195 1196 1197 1198 1199
    return true;
  }
  else return false;
}


//=============================================================================

//=============================================================================