/* This program gives a basic introduction to "animation" */ # include # include # include # include void DrawBoxAt(float x, float y) { const float size = 1.0f; const float x1 = x - size; const float x2 = x + size; const float y1 = y - size; const float y2 = y + size; glVertex2f(x1, y1); glVertex2f(x2, y1); glVertex2f(x2, y1); glVertex2f(x2, y2); glVertex2f(x2, y2); glVertex2f(x1, y2); glVertex2f(x1, y2); glVertex2f(x1, y1); } void UpdateLeftPalm(float *x) { unsigned int time = GetTickCount(); float theta = time * 0.01f; float x1 = -25.0f; float x2 = -15.0f; float w = 0.5f * sinf(theta) + 0.5f; *x = w*x1 + x2*(1.0f - w); } void Draw(void) { # define PARTS_COUNT 13 float points[PARTS_COUNT][2] = {// x y 0.0f, 30.0f, //head 0.0f, 20.0f, //neck -5.0f, 18.0f, //left_shoulder 5.0f, 18.0f, //right_shoulder -18.0f, 15.0f, //left_elbow 12.0f, 5.0f, //right_elbow 0.0f, 0.0f, //waist -25.0f, 30.0f, //left_palm 20.0f, -5.0f, //right_palm -10.0f, -15.0f, //left_knee 10.0f, -15.0f, //right_knee -15.0f, -30.0f, //left_foot 15.0f, -30.0f, //right_foot }; const int head = 0; const int neck = 1; const int left_shoulder = 2; const int right_shoulder = 3; const int left_elbow = 4; const int right_elbow = 5; const int waist = 6; const int left_palm = 7; const int right_palm = 8; const int left_knee = 9; const int right_knee = 10; const int left_foot = 11; const int right_foot = 12; int adjacency[PARTS_COUNT][PARTS_COUNT]; int loop1, loop2; for(loop1 = 0; loop1 < PARTS_COUNT; loop1++) for(loop2 = 0; loop2 < PARTS_COUNT; loop2++) adjacency[loop1][loop2] = 0; # define Connect(a, b) adjacency[a][b] = adjacency[b][a] = 1 Connect(head, neck); Connect(neck, left_shoulder); Connect(neck, right_shoulder); Connect(neck, waist); Connect(left_elbow, left_shoulder); Connect(left_elbow, left_palm); Connect(right_elbow, right_shoulder); Connect(right_elbow, right_palm); Connect(waist, left_knee); Connect(waist, right_knee); Connect(left_knee, left_foot); Connect(right_knee, right_foot); // r g b a glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); UpdateLeftPalm(&points[left_palm][0]); glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_LINES); for(loop1 = 0; loop1 < PARTS_COUNT; loop1++) { DrawBoxAt(points[loop1][0], points[loop1][1]); for(loop2 = 0; loop2 < loop1; loop2++) { if(adjacency[loop1][loop2]) { glVertex2f(points[loop1][0], points[loop1][1]); glVertex2f(points[loop2][0], points[loop2][1]); } } } glEnd(); glutSwapBuffers(); } void OnWindowResize(int width, int height) { //See how the output changes if you change left and top int left = 0; int bottom = 0; glViewport(left, bottom, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-width/2.0, width/2.0, -height/2.0, height/2.0); } void OnKeyPress(unsigned char key, int x, int y) { # define ESC_Key 27 switch (key) { case ESC_Key: exit(0); break; } //printf("\nThe mouse is @..(%d, %d)\n", x, y); } int main(int argc, char** argv) { const int left = 0; const int top = 0; const int width = 256; const int height = 256; const char* windowTitle = "Hasta La Vista Baby!!!"; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(width, height); glutInitWindowPosition(left, top); glutCreateWindow(windowTitle); glutDisplayFunc(Draw); glutReshapeFunc(OnWindowResize); glutKeyboardFunc(OnKeyPress); //NEW FUNCTION in this tutorial's main function glutIdleFunc(Draw); glutMainLoop(); return 0; } /* EXERCISE 1: Restructure the code so that..... the connections are not done again and again inside the Draw HINT: Make the points and adjacency matrix global... so that they are calculated only once EXERCISE 2: Draw the head with a bigger box and try to have more details like eyes and mouth */ // // MORAL of this tutorial // ~~~~~~~~~~~~~~~~~~~~~~~~ // All that waves is not friend. //