# include "sgl.h" /******************************************************* * sgl * static * members *******************************************************/ GLfloat sgl::time=0.0; GLfloat sgl::distance=60.0; GLfloat sgl::aperture=1.0; GLfloat sgl::focus=5.0; GLfloat sgl::view_ratio=1.0; GLfloat sgl::view_rotx=0.0; GLfloat sgl::view_roty=0.0; GLfloat sgl::view_rotz=0.0; /******************************************************* * init * * initialize scene: * environment variables * object creation/definition *******************************************************/ void sgl::init(int argc, char * argv[], int xi, int yi, int widthi, int heighti) { glutInit(&argc, argv); init(xi,yi,widthi,heighti); } void sgl::init(int argc, char * argv[]) { glutInit(&argc, argv); init(); } void sgl::init() { init(0,0,300,300); } void sgl::init(int xi, int yi, int widthi, int heighti) { glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowPosition(xi,yi); glutInitWindowSize(widthi,heighti); glutCreateWindow("Scene"); /* paint handlers */ //glutDisplayFunc(paint); // will have problems! glutReshapeFunc(reshape); /* key event handlers */ glutKeyboardFunc(downkey); glutSpecialFunc(downarrow); glutVisibilityFunc(visible); cout.precision(3); //glutMainLoop(); } /******************************************************* * advance * * advance animation according to sgl_scripts *******************************************************/ void sgl::advance(GLfloat dtime) { time+=dtime; # ifdef SHOWTIME cout << "(" << time << ")" << endl; # endif } void sgl::advance() { advance(1.0); } /******************************************************* * down key * * change view angle, exit upon ESC *******************************************************/ void sgl::downkey(unsigned char k, int x, int y) { switch (k) { case ',': distance -= 1.0; cout << " distance=" << distance << " focus=" << focus << endl; setProjection(); break; case '.': distance += 1.0; cout << " distance=" << distance << " focus=" << focus << endl; setProjection(); break; case '<': focus -= 1.0; cout << " distance=" << distance << " focus=" << focus << endl; setProjection(); break; case '>': focus += 1.0; cout << " distance=" << distance << " focus=" << focus << endl; setProjection(); break; case 'z': view_rotz += 5.0; break; case 'Z': view_rotz -= 5.0; break; case 27: /* Escape */ exit(0); break; default: return; } glutPostRedisplay(); } /******************************************************* * down arrow * change view angle by pressing arrows *******************************************************/ void sgl::downarrow(int k, int x, int y) { switch (k) { case GLUT_KEY_UP: view_rotx += 5.0; break; case GLUT_KEY_DOWN: view_rotx -= 5.0; break; case GLUT_KEY_LEFT: view_roty += 5.0; break; case GLUT_KEY_RIGHT: view_roty -= 5.0; break; default: return; } glutPostRedisplay(); } /******************************************************* * idle * * handle idle time -> animation repositioning *******************************************************/ void sgl::idle() { advance(); glutPostRedisplay(); } /******************************************************* * paint * * handle paint calls * - set matrix correclty * - display objects *******************************************************/ void sgl::paint() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(view_rotx, 1.0, 0.0, 0.0); glRotatef(view_roty, 0.0, 1.0, 0.0); glRotatef(view_rotz, 0.0, 0.0, 1.0); Link::paint(); glPopMatrix(); glutSwapBuffers(); } /******************************************************* * print *******************************************************/ void sgl::print() { Link::print(); } /******************************************************* * print *******************************************************/ void sgl::setProjection(GLfloat view_ratioi) { view_ratio=view_ratioi; setProjection(); } void sgl::setProjection() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0, 1.0, -view_ratio, view_ratio, focus, distance); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -40.0); } /******************************************************* * reshape * * handle new/change in window size or exposure *******************************************************/ void sgl::reshape(int width, int height) { GLfloat h = (GLfloat) height / (GLfloat) width * aperture; glViewport(0, 0, (GLint) width, (GLint) height); setProjection(h); } /******************************************************* * visible * * statandard glut visibility function *******************************************************/ void sgl::visible(int vis) { if (vis == GLUT_VISIBLE) glutIdleFunc(idle); else glutIdleFunc(NULL); }