⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qglviewerwidget.cc

📁 penMesh is a generic and efficient data structure for representing and manipulating polygonal meshes
💻 CC
📖 第 1 页 / 共 2 页
字号:
void QGLViewerWidget::wheelEvent(QWheelEvent* _event){  // Use the mouse wheel to zoom in/out  float d = -(float)_event->delta() / 120.0 * 0.2 * radius_;  translate(Vec3f(0.0, 0.0, d));  updateGL();  _event->accept();}//----------------------------------------------------------------------------void QGLViewerWidget::keyPressEvent( QKeyEvent* _event){  switch( _event->key() )  {    case Key_Print:      slotSnapshot();      break;    case Key_H:      std::cout << "Keys:\n";      std::cout << "  Print\tMake snapshot\n";      std::cout << "  C\tenable/disable back face culling\n";      std::cout << "  F\tenable/disable fog\n";      std::cout << "  I\tDisplay information\n";      std::cout << "  N\tenable/disable display of vertex normals\n";      std::cout << "  Shift N\tenable/disable display of face normals\n";      std::cout << "  Shift P\tperformance check\n";      break;    case Key_C:      if ( glIsEnabled( GL_CULL_FACE ) )      {	glDisable( GL_CULL_FACE );	std::cout << "Back face culling: disabled\n";      }      else      {	glEnable( GL_CULL_FACE );	std::cout << "Back face culling: enabled\n";      }      updateGL();      break;    case Key_F:      if ( glIsEnabled( GL_FOG ) )      {	glDisable( GL_FOG );	std::cout << "Fog: disabled\n";      }      else      {	glEnable( GL_FOG );	std::cout << "Fog: enabled\n";      }      updateGL();      break;    case Key_I:      std::cout << "Scene radius: " << radius_ << std::endl;      std::cout << "Scene center: " << center_ << std::endl;      break;    case Key_P:      if (_event->state() & ShiftButton)      {        double fps = performance();              std::cout << "fps: "                   << std::setiosflags (std::ios_base::fixed)                  << fps << std::endl;      }    break;        case Key_Q:    case Key_Escape:      qApp->quit();        }  _event->ignore();}//----------------------------------------------------------------------------voidQGLViewerWidget::translate( const Vec3f& _trans ){  // Translate the object by _trans  // Update modelview_matrix_  makeCurrent();  glLoadIdentity();  glTranslated( _trans[0], _trans[1], _trans[2] );  glMultMatrixd( modelview_matrix_ );  glGetDoublev( GL_MODELVIEW_MATRIX, modelview_matrix_);}//----------------------------------------------------------------------------voidQGLViewerWidget::rotate( const Vec3f& _axis, float _angle ){  // Rotate around center center_, axis _axis, by angle _angle  // Update modelview_matrix_  Vec3f t( modelview_matrix_[0]*center_[0] + 	   modelview_matrix_[4]*center_[1] +	   modelview_matrix_[8]*center_[2] + 	   modelview_matrix_[12],	   modelview_matrix_[1]*center_[0] + 	   modelview_matrix_[5]*center_[1] +	   modelview_matrix_[9]*center_[2] + 	   modelview_matrix_[13],	   modelview_matrix_[2]*center_[0] + 	   modelview_matrix_[6]*center_[1] +	   modelview_matrix_[10]*center_[2] + 	   modelview_matrix_[14] );    makeCurrent();  glLoadIdentity();  glTranslatef(t[0], t[1], t[2]);  glRotated( _angle, _axis[0], _axis[1], _axis[2]);  glTranslatef(-t[0], -t[1], -t[2]);   glMultMatrixd(modelview_matrix_);  glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix_);}//----------------------------------------------------------------------------boolQGLViewerWidget::map_to_sphere( const QPoint& _v2D, Vec3f& _v3D ){  if ( (_v2D.x() >= 0) && (_v2D.x() <= width()) &&       (_v2D.y() >= 0) && (_v2D.y() <= height()) )   {    double x  = (double)(_v2D.x() - 0.5*width())  / (double)width();    double y  = (double)(0.5*height() - _v2D.y()) / (double)height();    double sinx         = sin(M_PI * x * 0.5);    double siny         = sin(M_PI * y * 0.5);    double sinx2siny2   = sinx * sinx + siny * siny;        _v3D[0] = sinx;    _v3D[1] = siny;    _v3D[2] = sinx2siny2 < 1.0 ? sqrt(1.0 - sinx2siny2) : 0.0;        return true;  }  else return false;}//----------------------------------------------------------------------------voidQGLViewerWidget::update_projection_matrix(){  makeCurrent();  glMatrixMode( GL_PROJECTION );  glLoadIdentity();  gluPerspective(45.0, (GLfloat) width() / (GLfloat) height(),		 0.01*radius_, 100.0*radius_);  glGetDoublev( GL_PROJECTION_MATRIX, projection_matrix_);  glMatrixMode( GL_MODELVIEW );}//----------------------------------------------------------------------------voidQGLViewerWidget::view_all(){    translate( Vec3f( -(modelview_matrix_[0]*center_[0] + 		      modelview_matrix_[4]*center_[1] +		      modelview_matrix_[8]*center_[2] + 		      modelview_matrix_[12]),		    -(modelview_matrix_[1]*center_[0] + 		      modelview_matrix_[5]*center_[1] +		      modelview_matrix_[9]*center_[2] + 		      modelview_matrix_[13]),		    -(modelview_matrix_[2]*center_[0] + 		      modelview_matrix_[6]*center_[1] +		      modelview_matrix_[10]*center_[2] + 		      modelview_matrix_[14] +		      3.0*radius_) ) );}//----------------------------------------------------------------------------voidQGLViewerWidget::set_scene_pos( const Vec3f& _cog, float _radius ){  center_ = _cog;  radius_ = _radius;  glFogf( GL_FOG_START,      1.5*_radius );  glFogf( GL_FOG_END,        3.0*_radius );  update_projection_matrix();  view_all();}//----------------------------------------------------------------------------void QGLViewerWidget::add_draw_mode(const std::string& _s){  ++n_draw_modes_;  // insert in popup menu  popup_menu_->insertItem(_s.c_str(), n_draw_modes_);  // store draw mode  draw_mode_names_.push_back(_s);}//----------------------------------------------------------------------------void QGLViewerWidget::del_draw_mode(const std::string& _s){  std::vector<std::string>::iterator it =    std::find(draw_mode_names_.begin(), draw_mode_names_.end(), _s );  #if _DEBUG  assert( it != draw_mode_names_.end() );#else  if ( it == draw_mode_names_.end() )    return;#endif  for(int idx=0; size_t(idx) < popup_menu_->count(); ++idx)  {    int id = popup_menu_->idAt(idx);    if (id!=-1 && popup_menu_->text(id) == _s.c_str())    {      popup_menu_->removeItemAt(idx);      draw_mode_names_.erase(it, it+1);      break;    }  }}//----------------------------------------------------------------------------voidQGLViewerWidget::slotPopupMenu(int _id){  // un-check all entries  for (size_t i=1; i <= n_draw_modes_; ++i)    popup_menu_->setItemChecked(i, false);  // save draw mode  draw_mode_ = _id;  // check selected draw mode  popup_menu_->setItemChecked(_id, true);}//----------------------------------------------------------------------------double QGLViewerWidget::performance(){  setCursor( waitCursor );  double fps(0.0);  makeCurrent();  glMatrixMode(GL_MODELVIEW);  glPushMatrix();  OpenMesh::Utils::Timer timer;  unsigned int  frames = 60;  const float   angle  = 360.0/(float)frames;  unsigned int  i;  Vec3f         axis;  glFinish();  timer.start();  for (i=0, axis=Vec3f(1,0,0); i<frames; ++i)  { rotate(axis, angle); paintGL(); swapBuffers(); }  timer.stop();  qApp->processEvents();  timer.cont();  for (i=0, axis=Vec3f(0,1,0); i<frames; ++i)  { rotate(axis, angle); paintGL(); swapBuffers(); }  timer.stop();  qApp->processEvents();  timer.cont();  for (i=0, axis=Vec3f(0,0,1); i<frames; ++i)  { rotate(axis, angle); paintGL(); swapBuffers(); }  timer.stop();  glFinish();  timer.stop();  glPopMatrix();  updateGL();  fps = ( (3.0 * frames) / timer.seconds() );  setCursor( pointingHandCursor );  return fps;}voidQGLViewerWidget::slotSnapshot( void ){    QImage image;  size_t w(width()), h(height());  GLenum buffer( GL_BACK );  try  {    image = QImage(w, h, 32);  // 32-bit image      std::vector<GLubyte> fbuffer(3*w*h);    qApp->processEvents();    makeCurrent();    updateGL();    glFinish();        glReadBuffer( buffer );    glPixelStorei(GL_PACK_ALIGNMENT, 1);    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);    paintGL();    glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, &fbuffer[0] );        unsigned int x,y,offset;        for (y=0; y<h; ++y) {      for (x=0; x<w; ++x) {        offset = 3*(y*w + x);        image.setPixel(x, h-y-1, qRgb(fbuffer[offset],                                      fbuffer[offset+1],                                      fbuffer[offset+2]));      }    }            QString name = "snapshot-";#if defined(_MSC_VER)    {      std::stringstream s;      QDateTime         dt = QDateTime::currentDateTime();      s << dt.date().year()         << std::setw(2) << std::setfill('0') << dt.date().month()         << std::setw(2) << std::setfill('0') << dt.date().day()        << std::setw(2) << std::setfill('0') << dt.time().hour()        << std::setw(2) << std::setfill('0') << dt.time().minute()        << std::setw(2) << std::setfill('0') << dt.time().second();      name += QString(s.str().c_str());    }#else    name += QDateTime::currentDateTime().toString( "yyMMddhhmmss" );#endif    name += ".png";    image.save( name, "PNG");  }  catch( std::bad_alloc& )  {    qWarning("Mem Alloc Error");  }  }//=============================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -