00001
00002 #include "GLUTCore.h"
00003 #include <stdio.h>
00004 #if defined( _WIN32 )
00005 #include <time.h>
00006 #else
00007 #include <sys/time.h>
00008 #endif
00009
00010 #ifdef GLEXT
00011 #include "GLEXTHandler.h"
00012 #endif
00013
00014 #include <stdlib.h>
00015
00016 float GLUTCore::fGLUTCamDistance;
00017 GLUTCore GLUT;
00018
00019
00020 int oldX, oldY, buttonPressed = 0, rbuttonPressed = 0;
00021
00022 void DefaultMousePress( int b, int state, int x, int y )
00023 {
00024 GLUT.miLastMouseX = x; GLUT.miLastMouseY = y;
00025 if( b == GLUT_LEFT_BUTTON )
00026 buttonPressed = !buttonPressed;
00027 if( b == GLUT_RIGHT_BUTTON )
00028 rbuttonPressed = !rbuttonPressed;
00029 }
00030
00031 void DefaultMouseDrag( int x, int y )
00032 {
00033
00034 float dx = x - GLUT.miLastMouseX;
00035 float dy = y - GLUT.miLastMouseY;
00036 GLUT.miLastMouseX = x;
00037 GLUT.miLastMouseY = y;
00038 if( buttonPressed )
00039 {
00040
00041 GLUT.miSetMouseX += (int)dx;
00042 GLUT.miSetMouseY += (int)dy;
00043 }
00044 if( rbuttonPressed )
00045 {
00046 GLUTCore::fGLUTCamDistance += dy/10.0;
00047 DefaultReshape( GLUT.miWidth, GLUT.miHeight );
00048 }
00049 glLoadIdentity();
00050 glTranslatef( 0.0f, 0.0f, -GLUTCore::fGLUTCamDistance );
00051 glRotatef( GLUT.miSetMouseY*0.5f, 1.0f, 0.0f, 0.0f );
00052 glRotatef( GLUT.miSetMouseX*0.5f, 0.0f, 0.0f, 1.0f );
00053 }
00054
00055 void DefaultReshape( int Width, int Height )
00056 {
00057
00058 GLUT.miWidth = Width;
00059 GLUT.miHeight = Height;
00060 glViewport( 0, 0, (GLint)Width, (GLint)Height );
00061 glMatrixMode( GL_PROJECTION );
00062 glLoadIdentity();
00063 gluPerspective( 45.0f, (float)Width/(float)Height, 0.1f, 100.0f );
00064 glMatrixMode(GL_MODELVIEW);
00065 glLoadIdentity();
00066 glTranslatef( 0.0f, 0.0f, -GLUTCore::fGLUTCamDistance );
00067 glRotatef( GLUT.miSetMouseY*0.5f, 1.0f, 0.0f, 0.0f );
00068 glRotatef( GLUT.miSetMouseX*0.5f, 0.0f, 0.0f, 1.0f );
00069 }
00070
00071 void DefaultDraw()
00072 {
00073 glClearColor(.1,.1,.2,0.0);
00074 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00075 glBegin( GL_QUADS );
00076 glVertex3f( -1.0f, -1.0f, 0.0f );
00077 glVertex3f( 1.0f, -1.0f, 0.0f );
00078 glVertex3f( 1.0f, 1.0f, 0.0f );
00079 glVertex3f( -1.0f, 1.0f, 0.0f );
00080 glEnd();
00081
00082
00083 glutSwapBuffers();
00084 glutPostRedisplay();
00085 }
00086
00087 GLUTCore::GLUTCore()
00088 {
00089 mDF = DefaultDraw;
00090 mRS = DefaultReshape;
00091 mMP = DefaultMousePress;
00092 mMD = DefaultMouseDrag;
00093 miSetMouseX = 0;
00094 miSetMouseY = 0;
00095 }
00096
00097 float GLUTCore::TackFPS()
00098 {
00099 #ifdef WIN32
00100
00101
00102
00103 if( !wglGetCurrentDC() ) exit ( 0 );
00104 #endif
00105 unsigned iCurSecond, iCurMillisecond;
00106 iNumFrames++;
00107 #if defined( WIN32 )
00108 SYSTEMTIME systemTime;
00109 GetSystemTime( &systemTime );
00110 iCurSecond = systemTime.wSecond;
00111 iCurMillisecond = systemTime.wMilliseconds*1000;
00112 #else
00113 struct timeval T;
00114 gettimeofday( &T, 0 );
00115 iCurSecond = T.tv_sec;
00116 iCurMillisecond = T.tv_usec;
00117 #endif
00118 if( iCurSecond != iLastSecond )
00119 {
00120 iLastSecond = iCurSecond;
00121 printf( "FPS: %d\n", iNumFrames );
00122 iNumFrames = 0;
00123 }
00124 iDeltaMS = (iCurSecond - iTmrLastSecond)*1000000 + (iCurMillisecond - iTmrLastMillisecond );
00125 iTmrLastSecond = iCurSecond;
00126 iTmrLastMillisecond = iCurMillisecond;
00127 return ((float)iDeltaMS)/1000000.0f;
00128 }
00129
00130 void WMClose()
00131 {
00132 exit( 0 );
00133 }
00134
00135 void GLUTCore::Init( int argc, char**argv, int iWidth, int iHeight, char * sName )
00136 {
00137 fGLUTCamDistance = 5.0;
00138 iNumFrames = 0;
00139 iLastSecond = 0;
00140 miWidth = iWidth;
00141 miHeight = iHeight;
00142
00143
00144 glutInit( &argc, argv );
00145 glutInitWindowPosition( 0, 0 );
00146 glutInitWindowSize( iWidth, iHeight );
00147 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
00148 glutCreateWindow(sName);
00149
00150
00151
00152 glutDisplayFunc(mDF);
00153 glutReshapeFunc(mRS);
00154 glutMouseFunc( mMP);
00155 glutMotionFunc( mMD);
00156 glutPassiveMotionFunc( mMD );
00157
00158 #ifdef GLEXT
00159 SetupAllExtensions();
00160 #endif
00161
00162 }
00163
00164 void GLUTCore::DrawAndDoMainLoop()
00165 {
00166
00167 mRS( miWidth, miHeight );
00168 mDF();
00169 glutMainLoop();
00170 }
00171
00172