#include #include #include #include #include using namespace std; const float START_X = -2; const float END_X = 2; const float START_Y = -2; const float END_Y = 2; const float START_Z = -2; const float END_Z = 2; const int TOTAL_STEPS = 200; const float D_THETA = M_PI / float(TOTAL_STEPS); // a milisecond is 10-3 of a second const int DELAY = 10; // Globals. float Center_X, Center_Y, Center_Z; float T,Theta,Phi; float Radius; // Drawing routine. void drawScene(void) { glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); // in this entire world, we want everything back by 5. glLoadIdentity(); glTranslatef(-0.5,-0.5,-5.0); glColor3f(1.0,0.0,0.0); glPushMatrix(); glTranslatef(START_X, START_Y, START_Z); glutWireSphere(.5,10,10); glPopMatrix(); glColor3f(0.0,0.0,1.0); glPushMatrix(); glTranslatef(END_X, END_Y, END_Z); glutWireSphere(.5,10,10); glPopMatrix(); // draw an axis, that stays put. glColor3f(0.0,0.0,0.0); glBegin(GL_LINES); glVertex3f(-2,0,0); glVertex3f(2,0,0); glVertex3f(0,-2,0); glVertex3f(0,2,0); glVertex3f(0,0,-2); glVertex3f(0,0,2); glEnd(); glPushMatrix(); glTranslatef(Center_X, Center_Y, Center_Z); glutWireSphere(.5,10,10); glPopMatrix(); glutSwapBuffers(); } void reset(void) { Center_X = START_X; Center_Y = START_Y; Center_Z = START_Z; T = 0; Radius = sqrt(START_X*START_X + START_Y*START_Y + START_Z * START_Z); return; } // Initialization routine. void setup(void) { reset(); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glClearColor(1.0, 1.0, 1.0, 0.0); } // OpenGL window reshape routine. void resize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0, 1.0, -1.0, 1.0, 1, 20.0); glMatrixMode(GL_MODELVIEW); return; } // some different motion functions. void LinearTransformation(float t) { Center_X = START_X + (END_X - START_X) * T; Center_Y = START_Y + (END_Y - START_Y) * T; Center_Z = START_Z + (END_Z - START_Z) * T; return; } // pulled out of function, we only calculate once and it is expensive. void InitAngles() { Theta = atan2(START_Y,START_X); float sign; if (START_X * START_Y > 0) { sign = 1.0; } else { sign = -1.0; } //Phi = atan2(sign * sqrt(START_X * START_X + START_Y*START_Y),START_Z); Phi = acos(START_Z/Radius); return; } void HelixTransformation(float t) { // this should stay in the routine Theta += 5 * D_THETA ; Center_X = Radius * cos(Theta); Center_Y = Radius * sin(Theta); Center_Z = START_Z + (END_Z - START_Z) * T; return; } void MovementFunction(int moveType) { if (T < 1.0) { T += 1.0/float(TOTAL_STEPS); switch(moveType) { case 0: LinearTransformation(T); break; case 2: HelixTransformation(T); break; } glutPostRedisplay(); glutTimerFunc(DELAY, MovementFunction, moveType); } else { cout << "Animation Done " << endl; } return; } // Keyboard input processing routine. void keyInput(unsigned char key, int x, int y) { switch (key) { case 'r': reset(); break; case 'h': T = 0; InitAngles(); MovementFunction(2); break; case 'l': T = 0; MovementFunction(0); break; case 27: case 'q': exit(0); break; default: break; } glutPostRedisplay(); } // Main routine. int main(int argc, char **argv) { glutInit(&argc, argv); glutInitContextVersion(3, 0); glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("Movement"); glutDisplayFunc(drawScene); glutReshapeFunc(resize); glutKeyboardFunc(keyInput); glewExperimental = GL_TRUE; glewInit(); setup(); glutMainLoop(); }