/* earth.c texture globe needs readtex.c and earth_sm.rgb * A little bit different "Hello, World!" program ;-) * Author: Norman Walter * e-mail: walternn@studi.informatik.uni-stuttgart.de * www: http://www.norman-interactive.com * Date: 22.3.2002 */ #include #include #include #include #include #include #define ANIMATE 10 #define POINT_FILTER 20 #define LINEAR_FILTER 21 #define FASTER 30 #define SLOWER 31 #define QUIT 100 static int frames=0,timefps,timebase=0; int qt, rt; static GLuint Globe, Globe2, Globe3, Globe4; static GLboolean Animate = GL_TRUE; static GLfloat Xrot = -66.55, Yrot = -23.45, Zrot = 0.0; static GLfloat DZrot = 1.0; static GLfloat rotation = 0.0; static GLboolean rotate = GL_TRUE; void DisplaySplash(); static void drawText(GLfloat x, GLfloat y, char * msg) { int i, len; glPushMatrix(); glLoadIdentity(); glTranslatef( 0.0, 0.0, -10.0 ); /* -10 from 10 in frustum */ glRasterPos2f(x, y); /* relative to -1.0 1.0 in frustum */ len = strlen(msg); glColor3f(1.0f, 1.0f, 1.0f); for (i=0; i 360.0) { Zrot -= 360.0; } glutPostRedisplay(); } } static void Display( void ) { ++frames; timefps=glutGet(GLUT_ELAPSED_TIME); int fps; struct tm *today; time_t my_time; char ascii_string[100]; my_time = time(&my_time); today = localtime(&my_time); strcpy(ascii_string, asctime(today)); if (timefps - timebase > 1000) { fps = (frames*1000.0/(timefps-timebase)); timebase = timefps; frames = 0; } char sFPS[256]={0}; sprintf( sFPS, "%d FPS", fps ); glClear( GL_COLOR_BUFFER_BIT | GLUT_DEPTH | GL_DEPTH_BUFFER_BIT ); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); float lPos[] = {0.0,0.0,9.0,0.0}; glLightfv(GL_LIGHT0,GL_POSITION,lPos); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glShadeModel(GL_SMOOTH); float diff[] = {1.0,1.0,1.0,1.0}; glMaterialfv(GL_FRONT,GL_DIFFUSE,diff); glPushMatrix(); rotation += 1.0f; if ( rotate ){ // glRotatef(rotation++, 1, 1, 1); if ( rotation > 360.0f ){ rotation = 0.0f; } } glPushMatrix(); //glTranslatef( 1.2f, 1.2f, 1.2f); //glTranslatef( 0.0f, 0.0f, 2.0f); glRotatef( rotation, 0, 0, 1 ); glRotatef(Xrot, 1.0, 0.0, 0.0); glRotatef(Yrot, 0.0, 1.0, 0.0); glRotatef(Zrot, 0.0, 0.0, 1.0); glScalef(2.5, 2.5, 2.5); glCallList(Globe); glPopMatrix(); glPushMatrix(); glRotatef( rotation, 0, 0, 1 ); //glTranslatef( -1.2f, -1.2f, -1.2f); //glTranslatef( 0.0f, 0.0f, 2.0f); glRotatef(Xrot, 1.0, 0.0, 0.0 ); glRotatef(Yrot, 0.0, 1.0, 0.0 ); glRotatef(Zrot, 0.0, 0.0, 1.0 ); glScalef(2.5, 2.5, 2.5); glCallList(Globe2); glPopMatrix(); glPushMatrix(); //glTranslatef( 9.0f, 0.0f, 0.0f); //glTranslatef( 0.0f, 0.0f, 0.0f); glRotatef(Xrot, 1.0, 0.0, 0.0 ); glRotatef(Yrot, 0.0, 1.0, 0.0 ); glRotatef(Zrot, 0.0, 0.0, 1.0 ); glScalef(2.5, 2.5, 2.5); glCallList(Globe3); glPopMatrix(); glPushMatrix(); //glTranslatef( -1.2f, -1.2f, -1.2f); //glTranslatef( 0.0f, 0.0f, 0.0f); glRotatef(Xrot, 1.0, 0.0, 0.0 ); glRotatef(Yrot, 0.0, 1.0, 0.0 ); glRotatef(Zrot, 0.0, 0.0, 1.0 ); glScalef(2.5, 2.5, 2.5); glCallList(Globe4); glPopMatrix(); glDisable(GL_LIGHTING); drawText(-0.9, -0.7, sFPS); drawText(-0.9, -0.8, "Right click for menu :)"); drawText(-0.9, -0.9, ascii_string); glutSwapBuffers(); } /* end display */ static void Reshape( int width, int height ) { glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); //glOrtho(-1.0, 1.0, -1.0, 1.0, 10.0, 10.0); glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glTranslatef( 0.0, 0.0, -70.0 ); } /* end reshape */ static void ModeMenu(int entry) { if (entry==ANIMATE) { Animate = !Animate; } else if (entry==POINT_FILTER) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } else if (entry==LINEAR_FILTER) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } else if (entry==QUIT) { exit(0); } else if (entry==SLOWER) { DZrot /= 2; } else if (entry==FASTER) { DZrot *= 2; } glutPostRedisplay(); } /* end ModeMenu */ static void Key( unsigned char key, int x, int y ) { switch (key) { case 27: exit(0); break; } glutPostRedisplay(); } static void Init( void ) { glClearColor (0.0, 0.0, 0.0, 0.0); glClear( GL_COLOR_BUFFER_BIT | GLUT_DEPTH | GL_DEPTH_BUFFER_BIT ); qt = loadTexture("earth_small.rgb"); //rt = loadTexture("earth_small.rgb"); /* globe and globe2 */ GLUquadricObj *q = gluNewQuadric(); Globe = glGenLists(1); glNewList(Globe, GL_COMPILE); glTranslatef( 0.0f, 0.0f, -2.0f); gluQuadricNormals(q, GL_SMOOTH); gluQuadricTexture(q, GL_TRUE); gluSphere (q, 1.0, 15, 15); glEndList(); gluDeleteQuadric(q); GLUquadricObj *r = gluNewQuadric(); Globe2 = glGenLists(1); glNewList(Globe2, GL_COMPILE); glTranslatef( 0.0f, 0.0f, 2.0f); gluQuadricNormals(r, GL_SMOOTH); gluQuadricTexture(r, GL_TRUE); gluSphere (r, 1.0, 15, 15); glEndList(); gluDeleteQuadric(r); GLUquadricObj *s = gluNewQuadric(); Globe3 = glGenLists(1); glNewList(Globe3, GL_COMPILE); glTranslatef( 2.0f, 0.0f, 0.0f); gluQuadricNormals(s, GL_SMOOTH); gluQuadricTexture(s, GL_TRUE); gluSphere (s, 1.0, 15, 15); glEndList(); gluDeleteQuadric(s); GLUquadricObj *t = gluNewQuadric(); Globe4 = glGenLists(1); glNewList(Globe4, GL_COMPILE); glTranslatef( -2.0f, 0.0f, 0.0f); gluQuadricNormals(t, GL_SMOOTH); gluQuadricTexture(t, GL_TRUE); gluSphere (t, 1.0, 15, 15); glEndList(); gluDeleteQuadric(t); glEnable(GL_CULL_FACE); glEnable(GL_NORMALIZE); Reshape(400,400); glutTimerFunc( 50, DisplaySplash, 0 ); } /* end init */ int loadTexture(char *file){ int retval; int wtf; glGenTextures(1, &retval); glBindTexture(GL_TEXTURE_2D, retval); wtf = LoadRGBMipmaps(file, GL_RGB); glEnable( GL_TEXTURE_2D ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); return retval; } void DisplaySplash(){ struct tm *today ; time_t my_time; int tnext = 0; my_time = time(&my_time); today = localtime(&my_time); tnext = today->tm_sec + 3; glutSetWindowTitle("Splash Screen"); glClear( GL_COLOR_BUFFER_BIT | GLUT_DEPTH | GL_DEPTH_BUFFER_BIT ); drawText(-0.9, -0.7, "Splashing"); glutSwapBuffers(); while ( tnext > today->tm_sec ){ my_time = time(&my_time); today = localtime(&my_time); } glutSetWindowTitle(""); } int main(int argc, char *argv[]) { int pwid; glutInit( &argc, argv ); glutInitWindowSize( 400, 400 ); glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); pwid = glutCreateWindow(argv[0]); Init(); glutReshapeFunc( Reshape ); glutKeyboardFunc( Key ); glutDisplayFunc( Display ); glutIdleFunc( Idle ); glutCreateMenu(ModeMenu); glutAddMenuEntry("Point Filtered", POINT_FILTER); glutAddMenuEntry("Linear Filtered", LINEAR_FILTER); glutAddMenuEntry("Toggle Animation", ANIMATE); glutAddMenuEntry("Rotate Faster", FASTER); glutAddMenuEntry("Rotate Slower", SLOWER); glutAddMenuEntry("Quit", QUIT); glutAttachMenu(GLUT_RIGHT_BUTTON); glutMainLoop(); return 0; } /* end main of earth.c */