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

📄 graphicengine.cpp

📁 3D仿真组实物机器人环境下的机器人模型的设计工具。可以查看和修改现有模型的详细参数
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*********************************************************************************************//**************************************  Class TOpenGLCube  **********************************//*********************************************************************************************//*! This method returns the size of each edge of the current cube    \return Edge size of the current cube */double TOpenGLCube::getSize() const{  return m_Size;}/*! This method returns the size of each edge of the current cube    \param new_size Edge size of the current cube    \return bool indicating wheather update was successfull */bool TOpenGLCube::setSize( double new_size ){  m_Size = new_size;  return true;}/*! This method updates the cube every frame    \return bool indicating wheather cube was updated */bool TOpenGLCube::update(){  return true;}/*! This method internally renders the cube    \return bool indicating wheather cube is rendered correctly */bool TOpenGLCube::render(){  TOpenGLDrawing::changeDiffuseColor( m_DiffuseColor );  glTranslatef( m_Position.getX(), m_Position.getY(), m_Position.getZ() );  glRotatef(m_Direction.getX(), 1, 0, 0);  glRotatef(m_Direction.getY(), 0, 1, 0);  glRotatef(m_Direction.getZ(), 0, 0, 1);  TOpenGLDrawing::drawCube( m_Size );  return true;}/*********************************************************************************************//***********************************  Class TOpenGLSphere  ***********************************//*********************************************************************************************//*! This method returns the size of radius of the current sphere    \return radius of the current cube */double TOpenGLSphere::getRadius() const{  return m_Radius;}/*! This method returns the radius of the current sphere    \param new_size Radius of the current cube    \return bool indicating wheather update was successfull */bool TOpenGLSphere::setRadius( double new_size ){  m_Radius = new_size;  return true;}/*! This method updates the sphere every frame    \return bool indicating wheather sphere was updated */bool TOpenGLSphere::update(){  return true;}/*! This method internally renders the sphere    \return bool indicating wheather sphere is rendered correctly */bool TOpenGLSphere::render(){  TOpenGLDrawing::changeDiffuseColor( m_DiffuseColor );  glTranslatef( m_Position.getX(), m_Position.getY(), m_Position.getZ() );  glRotatef(m_Direction.getX(), 1, 0, 0);  glRotatef(m_Direction.getY(), 0, 1, 0);  glRotatef(m_Direction.getZ(), 0, 0, 1);  TOpenGLDrawing::drawSphere( m_Radius, 10 );  return true;}/*********************************************************************************************//***********************************  Class TOpenGLCylinder  *********************************//*********************************************************************************************//*! This is the constructor of the cylinder, initilizes internals, to not have    segmentation faults from the OpenGL */TOpenGLCylinder::TOpenGLCylinder(){  m_TopRadius    = 1;  m_BottomRadius = 1;  m_Height       = 1;  initQuadric();}/*! This is destructor, currently does nothing */TOpenGLCylinder::~TOpenGLCylinder(){  gluDeleteQuadric( m_Quad );}/*! This method returns the radius of top surface of cylinder    \return double Radius of top circle surface of cylinder */double TOpenGLCylinder::getTopRadius() const{  return m_TopRadius;}/*! This method sets the radius of top surface of cylinder    \param new_size New size of the radius    \return bool indicating wheather update was successfull */bool TOpenGLCylinder::setTopRadius( double new_size ){  if( new_size < 0.05 )    return false;  m_TopRadius = new_size;  return true;}/*! This method returns the radius of bottom surface of cylinder    \return double Radius of bottom circle surface of cylinder */double TOpenGLCylinder::getBottomRadius() const{  return m_BottomRadius;}/*! This method sets the radius of bottom surface of cylinder    \param new_size New size of the radius    \return bool indicating wheather update was successfull */bool TOpenGLCylinder::setBottomRadius( double new_size ){  if( new_size < 0.05 )    return false;  m_BottomRadius = new_size;  return true;}/*! This method returns the cylinders height    \return double The cylinder's height */double TOpenGLCylinder::getHeight() const{  return m_Height;}/*! This method sets the cylinders height    \param new_size New value for cylinder's height    \return bool Indicating wheather update was successfull */bool TOpenGLCylinder::setHeight( double new_size ){  m_Height = new_size;  return m_Height;}    /*! This procedure initializes theset of actions for OpenGL to    get along with our quad object.    \return bool Indicating wheather cylinder initialized */bool TOpenGLCylinder::initQuadric(){  // The torso of the cylinder  m_Quad = gluNewQuadric();  gluQuadricCallback  ( m_Quad, GLU_ERROR, quadric_error_callback );  gluQuadricDrawStyle ( m_Quad, GLU_FILL                          );  gluQuadricNormals   ( m_Quad, GLU_FLAT                          );  return true;}/*! This method updates the cylinder every frame    \return bool indicating wheather cylinder was updated */bool TOpenGLCylinder::update(){  glNewList           ( m_List, GL_COMPILE                      );  gluCylinder         ( m_Quad, m_BottomRadius, m_TopRadius, m_Height, iTeselation, iTeselation );  glEndList           (                                         );  return true;}/*! This method internally renders the cylinder    \return bool indicating wheather cylinder is rendered correctly */bool TOpenGLCylinder::render(){  TOpenGLDrawing::changeDiffuseColor( m_DiffuseColor );  glTranslatef( m_Position.getX(), m_Position.getY(), m_Position.getZ() - m_Height / 2.0 );    glRotatef(m_Direction.getX(), 1, 0, 0);  glRotatef(m_Direction.getY(), 0, 1, 0);  glRotatef(m_Direction.getZ(), 0, 0, 1);  glShadeModel(GL_SMOOTH);  glCallList( m_List );  return true;}/*********************************************************************************************//***********************************  Class TOpenGLCapsule  **********************************//*********************************************************************************************//*! This method initilizes the internals of the capsule (2 spheres) */TOpenGLCapsule::TOpenGLCapsule(){  head_top    = new TOpenGLSphere();  head_bottom = new TOpenGLSphere();}/*! This method finalizes the internals of the capsule (2 spheres) */TOpenGLCapsule::~TOpenGLCapsule(){  delete head_top;  delete head_bottom;}/*! This method updates the positions of the spheres on both sides    \return bool Indicating update was successfull */bool TOpenGLCapsule::update(){  TOpenGLCylinder::update();    VecPosition top = getPosition();    top.setZ( top.getZ() + m_Height * 3.0 / 2.0 );  head_top->setPosition( top );  head_top->setRadius( m_TopRadius - m_TopRadius / 75.0 );  top.setZ( getPosition().getZ() + m_Height / 2.0);  head_bottom->setPosition( top );  head_bottom->setRadius( m_BottomRadius - m_BottomRadius / 75.0 );  return true;}/*! This method renders the capsule    \return bool indicating rendering was successfull */bool TOpenGLCapsule::render(){  TOpenGLDrawing::changeDiffuseColor( m_DiffuseColor );  TOpenGLCylinder::render();  glPushMatrix();  head_bottom->render();  glPopMatrix();  glPushMatrix();  head_top->render();  glPopMatrix();  return true;}/********************************************************************//*************************  Engine routines  ************************//********************************************************************//*! This method initializes the OpenGLEngine    \param argc applications argument count    \param argv applications arguments    \param strTitle Title of MESA window */void InitializeOpenGLEngine( const char * strTitle ){  OpenGLEngine = new TOpenGLEngine( strTitle );}/*! This method finalizes the OpenGL application */void FinalizeOpenGLEngine(){  delete OpenGLEngine;  OpenGLEngine = NULL;}/**************************************************************************//*******************************  Callbacks  ******************************//**************************************************************************/int  _i_mouse_key;     /*!< Mouse key which was last pressed */bool _i_mouse_down;    /*!< Wheather mouse is down */int  _i_mouse_x;       /*!< Mouse X position */int  _i_mouse_z;       /*!< Mouse Y position *//*! This method is a callback for OnKeyPressed event    \param c key code    \param x X position of mouse cursor    \param y Y position of mouse cursor */void OnKeyPressed( unsigned char c, int x, int y ){  if(c == 27 || c == 'q' || c == 'Q')    exit (0);  VecPosition dirCamera = OpenGLEngine->getCameraDirection();  VecPosition posCamera = OpenGLEngine->getCameraPosition();  VecPosition pos = OpenGLEngine->getCameraPosition();  if( c == 'a' )  {    dirCamera.normalize();    VecPosition p = VecPosition(1, dirCamera.getTheta() + 90.0, 0, POLAR);    posCamera += p / 5.0;    OpenGLEngine->setCameraPosition( posCamera.getX(), posCamera.getY(), posCamera.getZ() );  }  if( c == 'd' )  {    dirCamera.normalize();    VecPosition p = VecPosition(1, dirCamera.getTheta() - 90.0, 0, POLAR);    posCamera += p / 5.0;    OpenGLEngine->setCameraPosition( posCamera.getX(), posCamera.getY(), posCamera.getZ() );  }  if( c == 's' )  {    dirCamera.normalize();    VecPosition p = VecPosition(1, dirCamera.getTheta(), 0, POLAR);    posCamera -= p / 5.0;    OpenGLEngine->setCameraPosition( posCamera.getX(), posCamera.getY(), posCamera.getZ() );  }  if( c == 'w' )  {    dirCamera.normalize();    VecPosition p = VecPosition(1, dirCamera.getTheta(), 0, POLAR);    posCamera += p / 5.0;    OpenGLEngine->setCameraPosition( posCamera.getX(), posCamera.getY(), posCamera.getZ() );  }  if( c == '+' )  {    pos.setZ( pos.getZ() + 0.2 );    OpenGLEngine->setCameraPosition( pos.getX(), pos.getY(), pos.getZ() );  }  if( c == '-' )  {    pos.setZ( pos.getZ() - 0.2 );    OpenGLEngine->setCameraPosition( pos.getX(), pos.getY(), pos.getZ() );  }}/*! This method is a callback for OnDisplay event */void OnDisplay(void){  OpenGLEngine->updateAll();  OpenGLEngine->renderAll(); }/*! This method is a callback for OnIdle event */void OnIdle(void){  if( _i_mouse_key == GLUT_RIGHT_BUTTON && _i_mouse_down )  {    VecPosition posCamera = OpenGLEngine->getCameraPosition();    posCamera.setZ( posCamera.getZ() + 0.04 );    OpenGLEngine->setCameraPosition( posCamera.getX(), posCamera.getY(), posCamera.getZ() );  }  else if( _i_mouse_key == GLUT_MIDDLE_BUTTON && _i_mouse_down )  {    VecPosition posCamera = OpenGLEngine->getCameraPosition();    posCamera.setZ( posCamera.getZ() - 0.04 );    OpenGLEngine->setCameraPosition( posCamera.getX(), posCamera.getY(), posCamera.getZ() );  }  glutPostRedisplay();}/*! This is a callback for OnWindowResized event    \param w Width of window    \param h Height of position */void OnResize(int w, int h){  OpenGLEngine->resizeEvent( w, h );}/*! This method is a callback for OnMouse* event    \param button Mouse button that is pressed    \param state Wheather mouse button is down or up    \param x X position of mouse cursor    \param y Y position of mouse cursor */void OnMousePress( int button, int state, int x, int y ){  _i_mouse_key = button;  if( button != GLUT_LEFT_BUTTON )  {    _i_mouse_down = state == GLUT_DOWN ? true : false;    return;  }  if( state == GLUT_UP )    _i_mouse_down = false;  if( state == GLUT_DOWN )  {    if( !_i_mouse_down )    {      _i_mouse_x = x;      _i_mouse_z = y;      return;    }    else      _i_mouse_down = true;  }}/*! This method is a callback for OnMouseDown event    \param x X position of mouse    \param y Y position of mouse */void OnMouseDown( int x, int y){  if( _i_mouse_key != GLUT_LEFT_BUTTON )    return;  double delta_x = x - _i_mouse_x;  double delta_z = y - _i_mouse_z;  _i_mouse_x = x;  _i_mouse_z = y;  AngDeg ang_x = -45.0 * delta_x / OpenGLEngine->getScreenWidth();  AngDeg ang_z = -45.0 * delta_z / OpenGLEngine->getScreenHeight();    VecPosition dirCamera = OpenGLEngine->getCameraDirection();  ang_x += dirCamera.getTheta();  ang_z += dirCamera.getPhi();  dirCamera.setVecPosition( 1, ang_x, ang_z, POLAR );  OpenGLEngine->setCameraDirection( dirCamera.getX(), dirCamera.getY(), dirCamera.getZ() );}/*--------------------------------------------------------------------*//*-----------------------------  End of file  ------------------------*//*--------------------------------------------------------------------*//*--------------------------  Testing purposes  ------------------------*/// #define TEST_ENGINE  /*!< Define this to check Engine status */#ifdef TEST_ENGINEint main(){  InitializeOpenGLEngine( 0, 0 );  OpenGLEngine->setLightPosition( VecPosition( 10,0,0 ), 0 );  OpenGLEngine->setLightDirection( VecPosition(-10,0,0), 0 );  OpenGLEngine->setCameraPosition( 2,-10,3 );  OpenGLEngine->setCameraDirection( 0,1,0 );  OpenGLEngine->setCameraUpDirection( 0,0,1 );    TOpenGLSphere a;  a.setRadius( 2 );  a.setDiffuseColor( COLOR_RED );  TOpenGLCube b;  b.setPosition( VecPosition(4) );  b.setSize( 1 );  b.setDiffuseColor( COLOR_BLUE );    TOpenGLCube c;  c.setPosition( VecPosition(6) );                                                      c.setSize( 1 );  c.setDiffuseColor( COLOR_BLACK );    OpenGLEngine->addObject( &a );  OpenGLEngine->addObject( &b );  OpenGLEngine->addObject( &c );  OpenGLEngine->startEngine();    FinalizeOpenGLEngine();}#endif

⌨️ 快捷键说明

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