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

📄 graphicengine.cpp

📁 3D仿真组实物机器人环境下的机器人模型的设计工具。可以查看和修改现有模型的详细参数
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*! This method returns current direction of the graphic object    \return VecPosition Curent direction of the object */VecPosition TOpenGLObject::getDirection( ) const{  return m_Direction;}/*! This method sets the current direction of the object    \param pos New direction of the object    \return bool Indicating wheather update was successfull */bool TOpenGLObject::setDirection( const VecPosition & dir ){  m_Direction = dir;  return true;}/*! This method returns currnet diffuse color of this object    \return TRGBA current diffuse color of object */TRGBA TOpenGLObject::getDiffuseColor() const{  return m_DiffuseColor;}/*! This method changes current difuse color of object    \param color New diffuse color for object */bool TOpenGLObject::setDiffuseColor( const TRGBA & color ){  m_DiffuseColor = color;  return true;}/*! This method returns currnet Specular color of this object    \return TRGBA current Specular color of object */TRGBA TOpenGLObject::getSpecularColor() const{  return m_SpecularColor;}/*! This method changes current difuse coor of object    \param color New Specular color for object */bool TOpenGLObject::setSpecularColor( const TRGBA & color ){  m_SpecularColor = color;  return true;}/*********************************************************************************************//***********************************  Class TOpenGLQuadric  **********************************//*********************************************************************************************//*! This is the constructor of TOpenGLQuadric, Just cleans the internals */TOpenGLQuadric::TOpenGLQuadric(){  m_ListCount = -1;  m_List      = 0;}/*! This is the destructor of class TOpenGLQuadric */TOpenGLQuadric::~TOpenGLQuadric(){}/*! This method initializes the quadric; for it to work correctly the child    class should set the number of quadric eleemnts, and then call it. */bool TOpenGLQuadric::init(){  if( m_ListCount < 0 )  {    cerr << "TOpenGLQuadric::init : List count less than zero" << endl;    cerr.flush();    return false;  }  m_List = glGenLists(m_ListCount);  return true;}/*********************************************************************************************//************************************  Class TOpenGLScene  ***********************************//*********************************************************************************************//*! This is the constructor of TOpenGLScene, initilizes all scene    related OpenGL/MESA properties */TOpenGLScene::TOpenGLScene( ){  glClearColor(0.0, 0.0, 0.0, 0.0);  glShadeModel(GL_SMOOTH);  glEnable(GL_COLOR_MATERIAL);  glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, 1 );  GLfloat globalAmbient[] = { 0.1, 0.1, 0.1, 1.0 };  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient);}/*! This is the destructor of TOpenGLScene */TOpenGLScene::~ TOpenGLScene( ){  m_Objects.clear();}/*! This method adds an object to our list    \param obj Object to add to scene    \return bool indicating wheather object was added or not */bool TOpenGLScene::addObject( POpenGLObject obj ){  if( obj != NULL )    m_Objects.push_front( obj );  else    return false;  return true;}/*! This method removes an object from the display list    \param obj Object to remove from scene    \return bool indicating wheather object was deleted or not */bool TOpenGLScene::removeObject( POpenGLObject obj ){  if( obj == NULL )    return false;  for( unsigned i = 0; i < m_Objects.size(); i++ )    if( m_Objects[i] == obj )    {      m_Objects.erase( m_Objects.begin() + i );      return true;    }  return false;}/*! This method empties object list    \return bool indicating anything was deleted or not */bool TOpenGLScene::clear( ){  if( m_Objects.size() != 0 )    m_Objects.clear();  else    return false;  return true;}/*! This method calls all of the scene objects to update themselves    for a  new rendering, anything that  has to be done,  should be    done at this point, like rotating or moving    \return int number of objects that couldn't update themselves */int TOpenGLScene::update( ){  int iError = 0;  for( unsigned i = 0; i < m_Objects.size(); i++ )    if( !( m_Objects[i]->update( ) ) )      iError++;  return iError;}/*! This method is the heart of rendering, it initializes current frame    and calls all objects render method, at the end of this, everything    is drawn    \return int Number of objects that didn't render themselves */int TOpenGLScene::render( ){  int iError = 0;  for( unsigned i = 0; i < m_Objects.size(); i++ )  {    glPushMatrix();    if( !( m_Objects[i]->render( ) ) )      iError++;    glPopMatrix();  }  return iError;}/*********************************************************************************************//*************************************  Class TOpenGLEngine  *********************************//*********************************************************************************************//*! This is the  constructor of  TOpenGLEngine,  which is responsible for    anything in a 3D scene, ie. lightings, camera, objects, fogs, meshings,    textures & etc    \param strTitle Title of window to display */TOpenGLEngine::TOpenGLEngine( const char * strTitle ){  int argc = 0;  glutInit(&argc, NULL);  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);  glutInitWindowSize (600, 600);  glutInitWindowPosition (400, 200);  m_WindowID = glutCreateWindow( strTitle );  m_ScreenWidth = 300;  m_ScreenHeight = 300;  m_Scene = new TOpenGLScene;  glEnable( GL_LIGHTING );  glEnable(GL_DEPTH_TEST);  for( int i = 0; i < MAX_LIGHTING; i++ )    m_Lights[i].setEnabled( false );  m_Lights[0].setID( GL_LIGHT0 );  m_Lights[1].setID( GL_LIGHT1 );  m_Lights[2].setID( GL_LIGHT2 );  m_Lights[3].setID( GL_LIGHT3 );  m_Lights[4].setID( GL_LIGHT4 );  m_Lights[5].setID( GL_LIGHT5 );  m_Lights[6].setID( GL_LIGHT6 );  m_Lights[7].setID( GL_LIGHT7 );  setOnIdle( OnIdle );  setOnKeyPressed( OnKeyPressed );  setOnMouse( OnMousePress );  setOnResize( OnResize );  setOnDisplay( OnDisplay );  setOnMouseDrag( OnMouseDown );}/*! This is the destructor of TOpenGLEngine, which shuts down everything    mentioned above */TOpenGLEngine::~TOpenGLEngine(){  delete m_Scene;}/*! This method is responsible to update everything in the world. */void TOpenGLEngine::updateAll(){  m_Scene->update();}/*! This method renders everything */void TOpenGLEngine::renderAll(){  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  glLoadIdentity();  m_Camera.forceUpdate();  /// Render everything  for( int i = 0; i < MAX_LIGHTING; i++ )  {    glPushMatrix();    m_Lights[ i ].forceUpdate();    glPopMatrix();  }  m_Scene->render();  glBegin( GL_LINES );  glVertex3f(0,0,0);  glVertex3f(100,0,0);  glVertex3f(0,0,0);  glVertex3f(0,100,0);  glVertex3f(0,0,0);  glVertex3f(0,0,100);  glEnd();  glFlush();  glutSwapBuffers();}/*! This method adds an object to the scene    \param obj Object to add to the scene    \return bool Indicating wheather object was added */bool TOpenGLEngine::addObject( POpenGLObject obj ){  return m_Scene->addObject( obj );}/*! This method removes an object from the scene    \param obj Object to remove from the scene    \return bool Indicating wheather object was removed */bool TOpenGLEngine::removeObject( POpenGLObject obj ){  return m_Scene->removeObject( obj );}/*! This method clears the scene    \return bool Indicating wheather scene objects were deleted */bool TOpenGLEngine::clearScene( ){  return m_Scene->clear();}/*! This method returns camera's position    \return VecPosition current position of camera */VecPosition TOpenGLEngine::getCameraPosition( ) const{  return m_Camera.getPosition();}/*! This method sets the current position of camera    \param dx x coordinate of camera's new position    \param dy y coordinate of camera's new position    \param dz z coordinate of camera's new position    \return bool indicating wheather update was successfull */bool TOpenGLEngine::setCameraPosition( double dx, double dy, double dz ){  return m_Camera.setPosition( dx, dy, dz );}/*! This method returns camera's looking direction    \return VecPosition current looking direction of camera */VecPosition TOpenGLEngine::getCameraDirection( ) const{  return m_Camera.getDirection();}/*! This method sets the current looking direction of camera    \param dx x coordinate of camera's looking direction    \param dy y coordinate of camera's looking direction    \param dz z coordinate of camera's looking direction    \return bool indicating wheather update was successfull */bool TOpenGLEngine::setCameraDirection( double dx, double dy, double dz ){  return m_Camera.setDirection( dx, dy, dz );}/*! This method returns camera's up direction    \return VecPosition current up direction of camera */VecPosition TOpenGLEngine::getCameraUpDirection( ) const{  return m_Camera.getUpDirection();}/*! This method sets the current up direction of camera    \param dx x coordinate of camera's up direction    \param dy y coordinate of camera's up direction    \param dz z coordinate of camera's up direction    \return bool indicating wheather update was successfull */bool TOpenGLEngine::setCameraUpDirection( double dx, double dy, double dz ){  return m_Camera.setUpDirection( dx, dy, dz );}/*! This method returns the source position for light No. iNr,    if light is not enabled, origin is returned    \param pos Light position to return    \param iNr Light number    \return bool indicating wheather light is enabled */bool TOpenGLEngine::getLightPosition( VecPosition & pos, int iNr ) const{  pos = VecPosition( 0 );  if( m_Lights[ iNr ].getEnabled() )    pos = m_Lights[ iNr ].getLightPosition();  else    return false;  return true;}/*! This method sets the light position, if not enabled, enables it.    \param pos New position of light source    \param iNr Light number    \return bool indicating wheather update was successful */bool TOpenGLEngine::setLightPosition( const VecPosition & pos, int iNr ){  m_Lights[ iNr ].setEnabled( true );  return ( m_Lights[ iNr ].setLightPosition( pos ) );}/*! This method returns the light direction for light No. iNr,    if light is not enabled, origin is returned    \param pos Light direction to return    \param iNr Light number    \return bool indicating wheather light is enabled */bool TOpenGLEngine::getLightDirection( VecPosition & pos, int iNr ) const{  pos = VecPosition( 0 );  if( m_Lights[ iNr ].getEnabled() )    pos = m_Lights[ iNr ].getLightDirection();  else    return false;  return true;}/*! This method sets the light direction, if not enabled, enables it.    \param pos New direction of light source    \param iNr Light number    \return bool indicating wheather update was successful */bool TOpenGLEngine::setLightDirection( const VecPosition & pos, int iNr ){  m_Lights[ iNr ].setEnabled( true );  return ( m_Lights[ iNr ].setLightDirection( pos ) );}/*! This method returns the screen width    \return double Screen width */double TOpenGLEngine::getScreenWidth() const{  return m_ScreenWidth;}/*! This method returns the screen Height    \return double Screen height */double TOpenGLEngine::getScreenHeight() const{  return m_ScreenHeight;}/*! This method sets the OnMouseDrag event callback    \param drag OnMouseDrag event callback */void TOpenGLEngine::setOnMouseDrag( void (*drag)(int x, int y) ){  glutMotionFunc( drag );}/*! This method sets the OnMouseMove event callback    \param drag OnMouseMove callback */void TOpenGLEngine::setOnMouse( void (*mouse)(int button, int state, int x, int y) ) const{  glutMouseFunc( mouse );}/*! This method sets the OnKeyPressed event callback    \param drag OnKeyPressed callback */void TOpenGLEngine::setOnKeyPressed( void (*keyboard)( unsigned char c, int x, int y ) ) const{  glutKeyboardFunc ( keyboard );}/*! This method sets the OnResize event callback    \param drag OnResize callback */void TOpenGLEngine::setOnResize(void(*resize)(int w, int h) ){  glutReshapeFunc( resize );}/*! This method sets the OnIdle  event callback    \param drag OnIdle callback */void TOpenGLEngine::setOnIdle(void(*idle)(void)){  glutIdleFunc( idle );}/*! This method sets the OnDisplay event callback    \param drag OnDisplay callback */void TOpenGLEngine::setOnDisplay(void (*display)(void) ){  glutDisplayFunc( display  ); }/*! This method should be called whenever the drawing window is resized    \param width New width of window    \param height New Height of window */void TOpenGLEngine::resizeEvent( int width, int height ){  glMatrixMode (GL_PROJECTION);  glLoadIdentity ();  gluPerspective( 45, 1, 0.1, 100);  glMatrixMode (GL_MODELVIEW);  m_ScreenHeight = height;  m_ScreenWidth = width;}/*! This method is the main loop of the OpenGLEngine class */void TOpenGLEngine::startEngine() const{  glutMainLoop();}/*! This method returns the Windows ID created by the OpenGL/MESA library    \return int Window ID created */int TOpenGLEngine::getWindowID() const{  return m_WindowID;}

⌨️ 快捷键说明

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