/* []----------------------------------------[] | Glut.inc | []----------------------------------------[] | | | AUTHOR: MFSomers 2005. | | USE: All glut stuff for main | | code ready to be | | included if needed ... | | | []----------------------------------------[] */ // Copyright (C) 2005 M.F. Somers, Theoretical Chemistry Department, Leiden University // // This is free software; you can redistribute it and/or modify it under the terms of // the GNU General Public License as published by the Free Software Foundation. // // http://www.gnu.org/licenses/gpl.txt #if defined(__linux__) # include "glut.h" #elif defined(_WIN32) || defined(WIN32) # include "glut.h" # elif defined(__APPLE__) && defined(__MACH__) # include # elif defined(__APPLE__) # include "glut.h" # elif defined(FREEBSD) # include "glut.h" # else # error No GLUT support for this platform ... #endif /* ----------------------------------------------------------------------- */ int Glut_CurrentMouseXPos, Glut_CurrentMouseYPos, Glut_LeftMouseButtonPressed, Glut_RightMouseButtonPressed = 0; /* ----------------------------------------------------------------------- */ /* Called whenever glut needs to render the scene */ void Glut_RenderScene( void ) { RenderOpenGLScene( Dynamics -> GetFinalStateParticlesPointer(), TheInputData.Colors, Dynamics -> TheBox, Dynamics -> Periodicity ); glutSwapBuffers(); } /* ----------------------------------------------------------------------- */ /* Called whenever glut is doing nothing and we can do a time step or so */ void Glut_Idle( void ) { if( OpenGL_DynamicsRunning && !Dynamics -> StopTrajectory ) DoSomeOfTheWork( Dynamics ); if( Dynamics -> StopTrajectory ) { DumpFinalStateAndCleanup( Dynamics, TheInputData, 1 ); if( StdOutFileName[ 0 ] ) fclose( StdOutFilePointer ); // only close if they have been redirected ... if( StdErrFileName[ 0 ] ) fclose( StdErrFilePointer ); exit( 0 ); } if( ( CPU_SECONDS() - OpenGL_LastRefreshTime >= 0.035 ) || OpenGL_RefreshScreen ) // do 25-30 frames/s roughly glutPostRedisplay(); } /* ----------------------------------------------------------------------- */ /* Called whenever the window is resized */ void Glut_ChangeSizeOfWindow( int Width, int Height ) { ResizeOpenGLScene( Width, Height, Dynamics -> TheBox ); } /* ----------------------------------------------------------------------- */ /* Called when mouse is activly (with button down) moved */ void Glut_ActiveMouseMovementHandler( int MousePosX, int MousePosY ) { int dX = MousePosX - Glut_CurrentMouseXPos; int dY = MousePosY - Glut_CurrentMouseYPos; int HasBeenUpdated = 0; int dPhi, dTheta, dZoom; dPhi = dTheta = dZoom = 0; if( Glut_LeftMouseButtonPressed ) { // check if moving mouse to the left if( dX < -OPENGL_MOUSE_TRIGGER ) { --dPhi; HasBeenUpdated = 1; } // check if moving mouse to the right if( dX > OPENGL_MOUSE_TRIGGER ) { ++dPhi; HasBeenUpdated = 1; } // check if moving mouse upwards if( dY < -OPENGL_MOUSE_TRIGGER ) { --dTheta; HasBeenUpdated = 1; } // check if moving mouse downwards if( dY > OPENGL_MOUSE_TRIGGER ) { ++dTheta; HasBeenUpdated = 1; } } if( Glut_RightMouseButtonPressed ) { // check if moving mouse upwards if( dY < -OPENGL_MOUSE_TRIGGER ) { --dZoom; HasBeenUpdated = 1; } // check if moving mouse downwards if( dY > OPENGL_MOUSE_TRIGGER ) { ++dZoom; HasBeenUpdated = 1; } } if( HasBeenUpdated ) { OpenGL_ModelOrientationPhi += ( double )( dPhi ) * OPENGL_DELTA_ANGLE; OpenGL_ModelOrientationTheta += ( double )( dTheta ) * OPENGL_DELTA_ANGLE; OpenGL_ModelOrientationZoomF += ( double )( dZoom ) * OPENGL_DELTA_ZOOM; ChangeViewPointOfOpenGLScene( OpenGL_ModelOrientationPhi, OpenGL_ModelOrientationTheta, OpenGL_ModelOrientationZoomF ); glutPostRedisplay(); } } /* ----------------------------------------------------------------------- */ /* Called when mouse is pasivly (without button down) moved */ void Glut_PassiveMouseMovementHandler( int MousePosX, int MousePosY ) { Glut_CurrentMouseXPos = MousePosX; Glut_CurrentMouseYPos = MousePosY; } /* ----------------------------------------------------------------------- */ /* Called when a mouse button event is encoutered */ void Glut_MouseButtonHandler( int Button, int State, int MousePosX, int MousePosY ) { if( State == GLUT_DOWN ) { if( Button == GLUT_LEFT_BUTTON ) Glut_LeftMouseButtonPressed = 1; if( Button == GLUT_RIGHT_BUTTON ) Glut_RightMouseButtonPressed = 1; } if( State == GLUT_UP ) { if( Button == GLUT_LEFT_BUTTON ) Glut_LeftMouseButtonPressed = 0; if( Button == GLUT_RIGHT_BUTTON ) Glut_RightMouseButtonPressed = 0; } } /* ----------------------------------------------------------------------- */ /* Called whenever a key stroke is hit */ void Glut_KeyboardHandler( unsigned char Key, int MouseXPos, int MouseYPos ) { if( Key == 27 ) { Dynamics -> StopTrajectory = 1; return; } // stop rendering for a while and take snap-shot of current situation if( OpenGL_DynamicsRunning ) DumpFinalStateAndCleanup( Dynamics, TheInputData, 0 ); OpenGL_DynamicsRunning = !OpenGL_DynamicsRunning; } /* ----------------------------------------------------------------------- */ /* Called by main program to setup glut rendering if needed */ void SetupGlutRendering( int argc, char *argv[] ) { if( !TheInputData.DoDynamics ) return; // init glut glutInit( &argc, argv ); glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA ); glutInitWindowPosition( 100, 100 ); glutInitWindowSize( 320, 320 ); glutCreateWindow( "3D model view" ); // set call-backs for glut glutDisplayFunc( Glut_RenderScene ); glutIdleFunc( Glut_Idle ); glutReshapeFunc( Glut_ChangeSizeOfWindow ); glutKeyboardFunc( Glut_KeyboardHandler ); glutMotionFunc( Glut_ActiveMouseMovementHandler ); glutPassiveMotionFunc( Glut_PassiveMouseMovementHandler ); glutMouseFunc( Glut_MouseButtonHandler ); // setup the OpenGL default global state SetupOpenGLScene( 320, 320, TheInputData.TheBox ); // now enter the glut main event loop glutMainLoop(); } /* ----------------------------------------------------------------------- */