//********************************************************************************/ // NOTE: this example is a modified version of the one discussed in class. // In lecture, both cart and wheels were modeled with functions. // Here: cart is a function, but wheel is still ad isplay list. // // OpenGL program illustartes: 2D modeling, animation, interaction. // // OpenGL program that draws a simple cart consisting of two red wheels // positioned at the ends of a green axis. // The program uses hierarchical modeling to model the cart. // A wheel is modeled by a display list. The cart is modeled in a function. // The wheels are spinning continuously, each around its own center. // The cart is stationary (unless move event occurs). // Events: // move on right mouse button // // Models the 2D cart by a hierarchical display list CART. Called from myinit(). // The tree model is as follows // axis // / \ // /T1.R1 \T2.R2 // WHEEL WHEEL // // where // WHEEL is a component defined in a display list. // T1.R1 denotes the composition of a rotation, R1, followed by a transpation T1 // // File: circSpin.cpp // Date: 2/16/2001 /*********************************************************************************/ #include #include #include #include // display lists identifiers #define WHEEL 1 // modeling constants #define PI 3.141592 #define NUM_SLICES 24 #define WHEEL_RAD 20.0 #define HALF_AXIS_LENGTH 75.0 #define SPIN_SPEED 10 #define MOVE_SPEED 0.5 // globals GLdouble theta = 10; // rotation angle per wheel GLdouble translate_x = 0.0; // x-translation on motion event GLdouble translate_y = 0.0; // y-translation on motion event GLsizei wnd_w = 500; // display window width GLsizei wnd_h = 500; // display window height //prototypes void myinit(); // OpenGL init void display(); // display callback void myreshape(GLsizei, GLsizei); // reshape callback void mouse_motion(int, int); // motion callback void idle(); // idle callback void drawUnitCircle(int num_slices); // approximate unit circle void defineParts(void); // defines the display lists /****************/ /***** MAIN *****/ /****************/ int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("spinning wheels cart, drag with mouse"); myinit(); // register callbacks glutReshapeFunc(myreshape); glutMotionFunc(mouse_motion); glutDisplayFunc(display); glutIdleFunc(idle); glutMainLoop(); return 0; } /********************************/ /***** FUNCTION DEFINITIONS *****/ /********************************/ /*********************************************************************************/ // OpenGL init /*********************************************************************************/ void myinit() { glClearColor(1.0,1.0,1.0,0.0); // white glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-225.0, 225.0, -225, 225.0); // origin at center of 500x500 clip window glMatrixMode(GL_MODELVIEW); defineParts(); } /*********************************************************************************/ // Approximates the unit circle with a polyline going through num_slices points /*********************************************************************************/ void drawUnitCircle(int num_slices) { double thetaincr=2*PI/num_slices; // the angle per sector double th=0.0; // angle used to generate // the angle used to generate the points // approximate unit circle with this glBegin(GL_LINE_LOOP); for (int i=0; i