📄 graphicengine.cpp
字号:
/*********************************************************************************** In the name of Almighty ** ** GraphicEngine.cpp : Robocup 3D Soccer Simulator Developement Team Zigorat ** ** Date: 07/08/2007 ** Author: Mahdi Hamdarsi ** Research Coordinator: Amin Mohammadi ** Comments: This file contains a powerfull interface to use OpenGL/MESA ** libraries to implement 3D Objects ** ***********************************************************************************//*! \file GraphicEngine.cpp<pre><b>File:</b> GraphicEngine.cpp<b>Project:</b> Robocup Soccer Simulation Team: Zigorat<b>Authors:</b> Mahdi Hamdarsi, Amin Mohammadi<b>Created:</b> 07/08/2007<b>Last Revision:</b> $ID$<b>Contents:</b> This file contains a powerfull interface to use OpenGL/MESA libraries to implement 3D Objects<hr size=2><h2><b>Changes</b></h2><b>Date</b> <b>Author</b> <b>Comment</b>07/08/2007 Mahdi Initial version created</pre>*/#include "GraphicEngine.h"#include <GL/glut.h>#ifdef WIN32void CALLBACK quadric_error_callback()#elsevoid quadric_error_callback()#endif{ cout << "error in quadric" << endl; cout.flush(); return;} /*! This is the Multi-Filed OpenGL Engine instance */TOpenGLEngine * OpenGLEngine;const int iTeselation = 20; /*!< This sets how many slices should spheres and cylinders be devided tos *//*********************************************************************************************//************************************* Several callbacks ***********************************//*********************************************************************************************/void OnKeyPressed( unsigned char c, int x, int y );void OnDisplay(void);void OnIdle(void);void OnResize(int w, int h);void OnMousePress( int button, int state, int x, int y );void OnMouseDown( int x, int y);/*********************************************************************************************//*************************************** Class TRGBA ***************************************//*********************************************************************************************/TRGBA::TRGBA( int iRed, int iGreen, int iBlue, int iAlpha ){ m_Red = iRed / 255.0; m_Green = iGreen / 255.0; m_Blue = iBlue / 255.0; m_Alpha = iAlpha / 255.0;}/*********************************************************************************************//************************************ Class TOpenGLDrawing *********************************//*********************************************************************************************//*! This method draws a sphere \param radius radius of the sphere \param solid determines wheather to draw a solid or wired sphere \param num_slices number of slices & stacks to draw circle with */void TOpenGLDrawing::drawSphere( double radius, bool solid, int num_slices ){ if( num_slices == -1 ) num_slices = iTeselation; if( solid ) glutSolidSphere( radius, num_slices, num_slices ); else glutWireSphere( radius, num_slices, num_slices );}/*! This method draws a cube \param edge_size size of the edge of the cube \param solid determines wheather to draw a solid cube or a wired one*/void TOpenGLDrawing::drawCube( double edge_size, bool solid ){ if( solid ) glutSolidCube( edge_size ); else glutWireCube( edge_size );}/*! This method draws a box \param length Length of the box \param width Width of the box \param height Height of the box \param solid Wheather to draw a solid or a wired box */void TOpenGLDrawing::drawBox( double length, double width, double height, bool solid ){ glScalef( length, width, height ); if( solid ) glutSolidCube( 1 ); else glutWireCube( 1 ); glScalef( 1/length, 1/width, 1/height );}/*! This method changes current diffuse color of OpenGL/MESA drawing to the specified one \param color New color of drawings */void TOpenGLDrawing::changeDiffuseColor( const TRGBA & color ){ GLfloat diffuseMaterial[4] = { color.m_Red, color.m_Green, color.m_Blue, color.m_Alpha }; glColorMaterial(GL_FRONT, GL_DIFFUSE); glColor4fv(diffuseMaterial);}/*! This method changes current specular color of OpenGL/MESA drawing to the specified one \param color New color of drawings */void TOpenGLDrawing::changeSpecularColor( const TRGBA & color ){ GLfloat specularMaterial[4] = { color.m_Red, color.m_Green, color.m_Blue, color.m_Alpha }; glColorMaterial(GL_FRONT, GL_SPECULAR); glColor4fv(specularMaterial);}/*********************************************************************************************//************************************ Class TOpenGLCamera **********************************//*********************************************************************************************//*! This is the initializer of TOpenGLCamera class, currently does nothing */TOpenGLCamera::TOpenGLCamera(){}/*! This is the destructor of TOpenGLCamera class, currently does nothing */TOpenGLCamera::~TOpenGLCamera(){}/*! This method updates the OpenGL viewing position, first three are location of camera, the next three are the position which camera is looking at, & the last is the up direction for camera.*/void TOpenGLCamera::updateCamera() const{ double dx = m_Position.getX() + m_Direction.getX(); double dy = m_Position.getY() + m_Direction.getY(); double dz = m_Position.getZ() + m_Direction.getZ(); gluLookAt( m_Position.getX(), m_Position.getY(), m_Position.getZ(), dx, dy, dz, m_UpDirection.getX(), m_UpDirection.getY(), m_UpDirection.getZ() );}/*! This method returns viewing position \return VecPosition camera position */VecPosition TOpenGLCamera::getPosition() const{ return m_Position;}/*! This method sets the camera position \param dx x coordinate of camera \param dy y coordinate of camera \param dz z coordinate of camera \return bool indicating update was successful */bool TOpenGLCamera::setPosition( double dx, double dy, double dz ){ return setPosition( VecPosition( dx, dy, dz ) );}/*! This method sets the camera position \param pos Camera's position \return bool indicating update was successful */bool TOpenGLCamera::setPosition( const VecPosition & pos ){ if( m_Position != pos ) { m_Position = pos; return true; } else return false;}/*! This method returns the direction which camera is looking at \return VecPosition the direction which camera is looking at */VecPosition TOpenGLCamera::getDirection() const{ return m_Direction;}/*! This method sets the direction which camera is looking at \param dx x coordinate \param dy y coordinate \param dz z coordinate \return bool indicating update was successful */bool TOpenGLCamera::setDirection( double dx, double dy, double dz ){ return setDirection( VecPosition( dx, dy, dz ) );}/*! This method sets the direction which camera is looking at \param pos Direction which camera is looking at \return bool indicating update was successful */bool TOpenGLCamera::setDirection( const VecPosition & pos ){ if( m_Direction != pos ) { m_Direction = pos; return true; } else return false;}/*! This method returns up direction for the camera \return VecPosition up direction for the camera */VecPosition TOpenGLCamera::getUpDirection() const{ return m_UpDirection;}/*! This method sets the camera's up direction \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 update was successful */bool TOpenGLCamera::setUpDirection( double dx, double dy, double dz ){ return setUpDirection( VecPosition( dx, dy, dz ) );}/*! This method sets the camera's up direction \param pos camera's up direction \return bool indicating update was successful */bool TOpenGLCamera::setUpDirection( const VecPosition & dir ){ if( m_UpDirection != dir ) { m_UpDirection = dir; return true; } else return false;}/*! This method forces camera to update its attributes. */void TOpenGLCamera::forceUpdate() const{ updateCamera();}/*********************************************************************************************//*********************************** Class TOpenGLLight ************************************//*********************************************************************************************//*! This is the constructor of class class TOpenGLLight, Tells OpenGL to loosen itself for some lighhting effects \param iNumber Light number, currently OpenGL/MESA does not support more than 8 Lighting sources, GL_LIGHT0 .. GL_LIGHT7 */TOpenGLLight::TOpenGLLight( GLenum iNumber, bool en ){ m_ID = iNumber; setEnabled( en );}/*! This is the destructor of TOpenGLLight class, currently nothing is here to be freed */TOpenGLLight::~TOpenGLLight(){}/*! This method updates OpenGL/MESA's internal memory of lighting conditions \return bool indicating wheather updated */bool TOpenGLLight::updateLighting(){ if( !m_Enabled ) return false; m_LightDirecton.normalize(); GLfloat light_position[] = { m_LightPosition.getX(), m_LightPosition.getY(), m_LightPosition.getZ(), 1.0 }; GLfloat spot_direction[] = { m_LightDirecton.getX(), m_LightDirecton.getY(), m_LightDirecton.getZ(), 0.0 }; glLightfv(m_ID, GL_POSITION, light_position); glLightfv(m_ID, GL_SPOT_DIRECTION, spot_direction); return true;}/*! This method returns light sources position */VecPosition TOpenGLLight::getLightPosition() const{ return m_LightPosition;}/*! This method updates light sources position \param pos New position of light source \return bool indicating update was successful */bool TOpenGLLight::setLightPosition( const VecPosition & pos ){ m_LightPosition = pos; setEnabled( true ); updateLighting(); return true;}/*! This method updates light sources position \param dx New position's x coordinate of light source \param dy New position's y coordinate of light source \param dz New position's z coordinate of light source \return bool indicating update was successful */bool TOpenGLLight::setLightPosition( double dx, double dy, double dz ){ m_LightPosition = VecPosition( dx, dy, dz ); setEnabled( true ); updateLighting(); return true;}/*! This method returns light sources litting direction */VecPosition TOpenGLLight::getLightDirection() const{ return m_LightDirecton;}/*! This method updates light's direction \param pos New direction of light source \return bool indicating update was successful */bool TOpenGLLight::setLightDirection( const VecPosition & pos ){ m_LightDirecton = pos; updateLighting(); return true;}/*! This method updates light sources litting direction \param dx x coordinate of light sources litting direction \param dy y coordinate of light sources litting direction \param dz z coordinate of light sources litting direction \return bool indicating update was successful */bool TOpenGLLight::setLightDirection( double dx, double dy, double dz ){ m_LightDirecton = VecPosition( dx, dy, dz ); setEnabled( true ); updateLighting(); return true;}/*! This method returns light sources OpenGL/MESA 's internal identifier \return GLenum Light ID */GLenum TOpenGLLight::getID() const{ return m_ID;}/*! This method sets light sources OpenGL/MESA 's internal identifier \param id Light ID \return bool Indicating update was successful */bool TOpenGLLight::setID( const GLenum & id ){ m_ID = id; updateLighting(); return true;}/*! This method returns wheather light source is enabled \return bool wheather light source is enabled */bool TOpenGLLight::getEnabled() const{ return m_Enabled;}/*! This method sets wheather light source is enabled \param en wheather light source is enabled \return bool Indicating update was successful */bool TOpenGLLight::setEnabled( const bool & en ){ m_Enabled = en; if( m_Enabled ) { GLfloat light1_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat light1_diffuse[] = { 0.5, 0.5, 0.5, 1.0 }; GLfloat light1_specular[] = { 1, 1, 1, 1.0 }; GLfloat light1_position[] = { -2.0, 2.0, 1.0, 1.0 }; glLightfv(m_ID, GL_AMBIENT, light1_ambient); glLightfv(m_ID, GL_DIFFUSE, light1_diffuse); glLightfv(m_ID, GL_SPECULAR, light1_specular); glLightfv(m_ID, GL_POSITION, light1_position); glEnable(m_ID); } updateLighting(); return true;}/*! This method forces OpenGL/MESA to update light attributes \return bool indicating wheather updated */bool TOpenGLLight::forceUpdate( ){ return updateLighting();}/*********************************************************************************************//********************************** Class TOpenGLObject ************************************//*********************************************************************************************//*! This is the constructor of TOpenGLObject, initializes internal fields */TOpenGLObject::TOpenGLObject(){ m_GlobalID = OBJECT_UNKNOWN; m_DiffuseColor = COLOR_WHITE;}/*! This is the destructor of class TOpenGLObject, currently does nothing */TOpenGLObject::~TOpenGLObject(){}/*! This method returns global name for the graphic object \return int Global identifier for object */int TOpenGLObject::getGlobalID() const{ return m_GlobalID;}/*! This sets the global name of the graphic object \param gid Global identifier of the object \return bool indicating wheather update was successfull */bool TOpenGLObject::setGlobalID( const int & gid ){ m_GlobalID = gid; return true;}/*! This method returns current position of the graphic object \return VecPosition Curent position of the object */VecPosition TOpenGLObject::getPosition( ) const{ return m_Position;}/*! This method sets the current position of the object \param pos New position of the object \return bool Indicating wheather update was successfull */bool TOpenGLObject::setPosition( const VecPosition & pos ){ m_Position = pos; return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -