00001
00002 #ifdef RT_GLUT
00003
00004 #include "RenderTools.h"
00005 #include "RendergroupGLutView.h"
00006 #include "AbstractApplication.h"
00007 #include "XMLNode.h"
00008
00009 namespace RenderTools{
00010
00011 RendergroupGLutViewPtr RendergroupGLutView::s_instance;
00012 bool RendergroupGLutView::s_fullscreen;
00013
00014 RendergroupGLutView::RendergroupGLutView( void ):
00015 RendergroupAbstractView(){
00016 }
00017
00018 PropertyPtr RendergroupGLutView::create( const XMLNodePtr & xml ){
00019 RendergroupGLutViewPtr p( new RendergroupGLutView() );
00020 p->setName( xml );
00021 p->createProperties();
00022 p->setProperties( xml, false );
00023 p->initialize( true );
00024 return( dynamic_pointer_cast< AbstractProperty, RendergroupGLutView >( p ) );
00025 }
00026
00027 void RendergroupGLutView::createProperties( void ){
00028 RendergroupAbstractView::createProperties();
00029 createProperty( this, "fullscreen", & s_fullscreen );
00030 }
00031
00032 const string RendergroupGLutView::getTypeName( bool ofComponent ) const {
00033 return( "RendergroupGLView" );
00034 }
00035
00036 void RendergroupGLutView::onViewControllerEvent( const ViewControllerEvent & e ){
00037 }
00038
00039 RendergroupGLutView::~RendergroupGLutView( void ){
00040 }
00041
00042 void RendergroupGLutView::onInitialize( void ){
00044 if( ! s_instance ){
00045 s_instance = dynamic_pointer_cast< RendergroupGLutView, AbstractProperty >( getSharedPtr< AbstractProperty >() );
00046
00047 glutInitWindowSize( m_width, m_height );
00048 glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA | GLUT_RGBA );
00049 glutCreateWindow( "GLut" );
00050
00051 RenderTools::initializeGL();
00052
00053 glutDisplayFunc( RendergroupGLutView::displayFunc );
00054 glutMouseFunc( RendergroupGLutView::mouseFunc );
00055 glutMotionFunc( RendergroupGLutView::motionFunc );
00056 glutPassiveMotionFunc( RendergroupGLutView::passiveMotionFunc );
00057 glutIdleFunc( RendergroupGLutView::idleFunc );
00058 glutKeyboardFunc( RendergroupGLutView::keyPressFunc );
00059 glutKeyboardUpFunc( RendergroupGLutView::keyReleaseFunc );
00060 glutReshapeFunc( RendergroupGLutView::resizeFunc );
00061
00062 if( s_fullscreen ){
00063 glutFullScreen();
00064 m_width = glutGet( GLUT_SCREEN_WIDTH );
00065 m_height = glutGet( GLUT_SCREEN_HEIGHT );
00066 setSize( m_width, m_height );
00067 }
00068 }
00069 }
00070
00071 void RendergroupGLutView::onPropertyContainerEvent( const PropertyContainerEvent & ){
00072
00073 }
00074
00075 void RendergroupGLutView::onMouseEvent( const MouseEvent & ){
00076
00077 }
00078
00079 void RendergroupGLutView::onKeyboardEvent( const KeyboardEvent & ){
00080
00081 }
00082
00083 void RendergroupGLutView::show( bool state ){
00084 if( state ){
00085 glutShowWindow();
00086 }
00087 else{
00088 glutHideWindow();
00089 }
00090 ViewController::show( state );
00091 }
00092
00093 void RendergroupGLutView::startTimer( unsigned int millis ){
00094 static bool firstCall = true;
00095 if( firstCall ){
00096 firstCall = false;
00097 glutTimerFunc( millis, timerFunc, millis );
00098 }
00099 else{
00100 Error::warning( Error::ITEM_ALREADY_EXISTS, __FILE__, __LINE__ );
00101 }
00102 ViewController::startTimer( millis );
00103 }
00104
00105 void RendergroupGLutView::stopTimer( void ){
00106 ViewController::stopTimer();
00107 }
00108
00109 void RendergroupGLutView::timerFunc( int value ){
00110 if( s_instance->m_timer ){
00111 glutTimerFunc( value, timerFunc, value );
00112 s_instance->sendViewControllerEvent( ViewControllerEvent( ViewControllerEvent::TIMER ) );
00113 }
00114 }
00115
00116 void RendergroupGLutView::onPropertyEvent( const PropertyEvent & e ){
00117
00118 }
00119
00120 void RendergroupGLutView::onRender( void ){
00121 glutPostRedisplay();
00122 }
00123
00124 void RendergroupGLutView::onUpdate( Mat4 & parent ){
00125 glutPostRedisplay();
00126 }
00127
00128 void RendergroupGLutView::displayFunc( void ){
00129 s_instance->render();
00130 glutSwapBuffers();
00131 }
00132
00133 void RendergroupGLutView::idleFunc(){
00134 s_instance->render();
00135 }
00136
00137 void RendergroupGLutView::keyPressFunc( unsigned char k, int x, int y ){
00138 s_instance->onKeyboardEvent( KeyboardEvent( KeyboardEvent::KEY_DOWN, (int)k ) );
00139 }
00140
00141 void RendergroupGLutView::keyReleaseFunc( unsigned char k, int x, int y ){
00142 s_instance->onKeyboardEvent( KeyboardEvent( KeyboardEvent::KEY_UP, (int)k ) );
00143 }
00144
00145 void RendergroupGLutView::resizeFunc( int w, int h ){
00146 vector< float > values;
00147 values.push_back( w );
00148 values.push_back( h );
00149 ViewControllerPtr vc = dynamic_pointer_cast< ViewController, AbstractProperty >( s_instance->getSharedPtr< AbstractProperty >() );
00150 s_instance->sendViewControllerEvent( ViewControllerEvent( ViewControllerEvent::RESIZE, vc, values ) );
00151 }
00152
00153 void RendergroupGLutView::mouseFunc( int button, int state, int x, int y ){
00154 switch( button ){
00155 case GLUT_LEFT_BUTTON:
00156 s_instance->onMouseEvent( MouseEvent( state ? MouseEvent::MOUSE_DOWN : MouseEvent::MOUSE_UP, Vec2( x, y ), 0 ) );
00157 break;
00158 case GLUT_MIDDLE_BUTTON:
00159 s_instance->onMouseEvent( MouseEvent( state ? MouseEvent::MOUSE_DOWN : MouseEvent::MOUSE_UP, Vec2( x, y ), 1 ) );
00160 break;
00161 case GLUT_RIGHT_BUTTON:
00162 s_instance->onMouseEvent( MouseEvent( state ? MouseEvent::MOUSE_DOWN : MouseEvent::MOUSE_UP, Vec2( x, y ), 2 ) );
00163 break;
00164 }
00165 }
00166
00167 void RendergroupGLutView::motionFunc( int x, int y ){
00168 s_instance->onMouseEvent( MouseEvent( MouseEvent::MOUSE_MOVE, Vec2( x, y ) ) );
00169 }
00170
00171 void RendergroupGLutView::passiveMotionFunc( int x, int y ){
00172 s_instance->onMouseEvent( MouseEvent( MouseEvent::MOUSE_MOVE, Vec2( x, y ) ) );
00173 }
00174
00175 };
00176
00177 #else
00178
00179 int _______forced_public_symbol = 0;
00180
00181 #endif // RT_GLUT
00182