QtBaseViewerPicking.cc 8.27 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 48 49 50 51 52 53 54 55 56 57
//
//=============================================================================


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

#include "QtBaseViewer.hh"
#include "QtGLGraphicsScene.hh"
#include "QtGLGraphicsView.hh"

//== 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
58
bool glViewer::pick( ACG::SceneGraph::PickTarget _pickTarget,
Jan Möbius's avatar
 
Jan Möbius committed
59 60 61 62 63 64 65 66 67
                         const QPoint&               _mousePos,
                         unsigned int&               _nodeIdx,
                         unsigned int&               _targetIdx,
                         ACG::Vec3d*                 _hitPointPtr )
{
  if (sceneGraphRoot_)
  {
    GLint         w = glWidth(),
                  h = glHeight(),
Jan Möbius's avatar
 
Jan Möbius committed
68 69
		  l = scenePos().x(),
		  b = scene()->height () - scenePos().y() - h,
Jan Möbius's avatar
 
Jan Möbius committed
70
                  x = _mousePos.x(),
Jan Möbius's avatar
 
Jan Möbius committed
71 72
                  y = scene()->height () - _mousePos.y();
    GLint         viewport[4] = {l,b,w,h};
Jan Möbius's avatar
 
Jan Möbius committed
73 74 75
    GLuint        selectionBuffer[ SELECTION_BUFFER_SIZE ],
                  nameBuffer[ NAME_STACK_SIZE ];

76 77
    const ACG::GLMatrixd&  modelview  = properties_.glState().modelview();
    const ACG::GLMatrixd&  projection = properties_.glState().projection();
Jan Möbius's avatar
 
Jan Möbius committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97


    // 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);
    glClear(GL_DEPTH_BUFFER_BIT);
    glInitNames();
    glPushName((GLuint) 0);

    // do the picking
    ACG::SceneGraph::PickAction action(_pickTarget);
98
    ACG::SceneGraph::traverse(sceneGraphRoot_, action, properties_.glState());
Jan Möbius's avatar
 
Jan Möbius committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    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);


    // 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 )
        {
Jan Möbius's avatar
 
Jan Möbius committed
123
          std::cerr << "glViewer::pick() : namestack error\n\n";
Jan Möbius's avatar
 
Jan Möbius committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
          return false;
        }

        if ( (z = *ptr++) < min_z )
        {
          min_z = z;
          max_z = *ptr++;
          for (unsigned int j=0; j<num_names; ++j)
            nameBuffer[j] = *ptr++;
        }
        else ptr += 1+num_names;
      }

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

      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);
146
        *_hitPointPtr = properties_.glState().unproject(ACG::Vec3d(x,y,zz));
Jan Möbius's avatar
 
Jan Möbius committed
147 148 149 150 151
      }

      return true;
    }
    else if (hits < 0)
Jan Möbius's avatar
 
Jan Möbius committed
152
      std::cerr << "glViewer::pick() : selection buffer overflow\n\n";
Jan Möbius's avatar
 
Jan Möbius committed
153 154 155 156 157 158 159 160 161 162
  }

  return false;
}


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


bool
Jan Möbius's avatar
 
Jan Möbius committed
163
glViewer::
Jan Möbius's avatar
 
Jan Möbius committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
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)
  {
180
    _hitPoint = properties_.glState().unproject( ACG::Vec3d(x, y, z) );
Jan Möbius's avatar
 
Jan Möbius committed
181 182 183 184 185 186 187 188
    return true;
  }
  else return false;
}

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


Jan Möbius's avatar
 
Jan Möbius committed
189
void glViewer::pickMode( int _id )
Jan Möbius's avatar
 
Jan Möbius committed
190 191 192 193 194 195 196
{
  if (_id < (int) pick_modes_.size() )
  {
    pick_mode_idx_  = _id;
    pick_mode_name_ = pick_modes_[pick_mode_idx_].name;

    // adjust mouse tracking
Jan Möbius's avatar
 
Jan Möbius committed
197
    if ( properties_.pickingMode() )
Jan Möbius's avatar
 
Jan Möbius committed
198 199 200
      trackMouse(pick_modes_[pick_mode_idx_].tracking);

    // adjust Cursor
Jan Möbius's avatar
 
Jan Möbius committed
201
    if ( properties_.pickingMode() )
Jan Möbius's avatar
 
Jan Möbius committed
202
      setCursor( pick_modes_[pick_mode_idx_].cursor);
Jan Möbius's avatar
 
Jan Möbius committed
203 204 205 206 207 208 209 210 211 212

    // emit signal
    emit(signalPickModeChanged(pick_mode_name_));
  }
}


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


Jan Möbius's avatar
 
Jan Möbius committed
213
void glViewer::addPickMode(const std::string& _name,
Jan Möbius's avatar
 
Jan Möbius committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
                               bool _tracking,
                               int  _pos,
                               bool _visible,
                               QCursor _cursor)
{
  if ((unsigned int)_pos < pick_modes_.size())
  {
    std::vector<PickMode>::iterator it = pick_modes_.begin();
    it += _pos+1;
    pick_modes_.insert(it, PickMode(_name, _tracking, _visible, _cursor));
  }
  else
    pick_modes_.push_back(PickMode(_name, _tracking, _visible, _cursor));

  updatePickMenu();
}

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

Jan Möbius's avatar
 
Jan Möbius committed
233
void glViewer::setPickModeCursor(const std::string& _name, QCursor _cursor)
Jan Möbius's avatar
 
Jan Möbius committed
234 235 236 237 238 239
{
  for (uint i=0; i < pick_modes_.size(); i++)
    if ( pick_modes_[i].name == _name ){
      pick_modes_[i].cursor = _cursor;

      //switch cursor if pickMode is active
Jan Möbius's avatar
 
Jan Möbius committed
240
      if (pick_mode_name_ == _name && properties_.pickingMode() )
Jan Möbius's avatar
 
Jan Möbius committed
241
        setCursor(_cursor);
Jan Möbius's avatar
 
Jan Möbius committed
242 243 244 245 246 247
      break;
    }
}

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

Jan Möbius's avatar
 
Jan Möbius committed
248
void glViewer::setPickModeMouseTracking(const std::string& _name, bool _mouseTracking)
Jan Möbius's avatar
 
Jan Möbius committed
249 250 251 252 253 254
{
  for (uint i=0; i < pick_modes_.size(); i++)
    if ( pick_modes_[i].name == _name ){
      pick_modes_[i].tracking = _mouseTracking;

      //switch cursor if pickMode is active
Jan Möbius's avatar
 
Jan Möbius committed
255
      if (pick_mode_name_ == _name && properties_.pickingMode() )
Jan Möbius's avatar
 
Jan Möbius committed
256 257 258 259 260 261 262 263
        trackMouse(_mouseTracking);
      break;
    }
}

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


Jan Möbius's avatar
 
Jan Möbius committed
264
void glViewer::clearPickModes()
Jan Möbius's avatar
 
Jan Möbius committed
265 266 267 268 269 270 271 272 273 274 275
{
  pick_modes_.clear();
  pick_mode_idx_  = -1;
  pick_mode_name_ = "";
  updatePickMenu();
}


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


Jan Möbius's avatar
 
Jan Möbius committed
276
const std::string& glViewer::pickMode() const
Jan Möbius's avatar
 
Jan Möbius committed
277 278 279 280 281 282 283 284
{
  return pick_mode_name_;
}


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


Jan Möbius's avatar
 
Jan Möbius committed
285
void glViewer::pickMode(const std::string& _name)
Jan Möbius's avatar
 
Jan Möbius committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
{
  for (unsigned int i=0; i<pick_modes_.size(); ++i)
  {
    if (pick_modes_[i].name == _name)
    {
      pickMode( i );
      updatePickMenu();
      return;
    }
  }
}



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

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