// Peter Bui // CSE 40166 Computer Graphics (Fall 2010) // Example 19: drawables #include "drawable.h" #include "drawable_circle.h" #include "drawable_star.h" #include "drawable_initials.h" #include #include #include #include #ifdef __APPLE__ #include #else #include #endif using namespace std; // Constants ------------------------------------------------------------------- #define WHEEL_UP 3 #define WHEEL_DOWN 4 // Global variables ------------------------------------------------------------ static GLint WindowWidth = 640; static GLint WindowHeight = 480; static Drawable *CurrentDrawable = NULL; static Drawable *StaticCircle = NULL; static Drawable *StaticStar = NULL; static Drawable *StaticInitials = NULL; static list Drawables; // Initialize scene ------------------------------------------------------------ void init_scene() { glViewport(0, 0, WindowWidth, WindowHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, WindowWidth, 0, WindowHeight); glMatrixMode(GL_MODELVIEW); glutSetCursor(GLUT_CURSOR_NONE); StaticCircle = new Circle(); StaticStar = new Star(); StaticInitials = new Initials(); CurrentDrawable = StaticCircle; } // Display callback ------------------------------------------------------------ void display() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); for (list::iterator it = Drawables.begin(); it != Drawables.end(); it++) (*it)->draw(); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); CurrentDrawable->draw(); glutSwapBuffers(); } // Reshape callback ------------------------------------------------------------ void reshape(GLsizei nw, GLsizei nh) { WindowWidth = nw; WindowHeight = nh; init_scene(); } // Keyboard callback ----------------------------------------------------------- void keyboard(unsigned char key, int x, int y) { switch (key) { case 'q': case 'Q': exit(EXIT_SUCCESS); break; case '1': CurrentDrawable = StaticCircle; break; case '2': CurrentDrawable = StaticStar; break; case '3': CurrentDrawable = StaticInitials; break; case 'w': break; } glutPostRedisplay(); } // Mouse callback -------------------------------------------------------------- void mouse(int button, int state, int x, int y) { CurrentDrawable->x = x; CurrentDrawable->y = WindowHeight - y; if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { Drawable *d = NULL; if (CurrentDrawable == StaticCircle) d = new Circle(x, WindowHeight - y); else if (CurrentDrawable == StaticStar) d = new Star(x, WindowHeight - y); else if (CurrentDrawable == StaticInitials) d = new Initials(x, WindowHeight - y); else d = NULL; if (d) Drawables.push_back(d); } if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { for (list::iterator it = Drawables.begin(); it != Drawables.end(); it++) delete *it; Drawables.clear(); } if (button == WHEEL_UP && state == GLUT_DOWN) { if (CurrentDrawable == StaticCircle) CurrentDrawable = StaticStar; else if (CurrentDrawable == StaticStar) CurrentDrawable = StaticInitials; else CurrentDrawable = StaticCircle; } if (button == WHEEL_DOWN && state == GLUT_DOWN) { if (CurrentDrawable == StaticCircle) CurrentDrawable = StaticInitials; else if (CurrentDrawable == StaticStar) CurrentDrawable = StaticCircle; else CurrentDrawable = StaticStar; } glutPostRedisplay(); } // Passive motion callback ----------------------------------------------------- void passive_motion(int x, int y) { CurrentDrawable->x = x; CurrentDrawable->y = WindowHeight - y; glutPostRedisplay(); } // Main execution -------------------------------------------------------------- int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowSize(WindowWidth, WindowHeight); glutCreateWindow("drawables"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMouseFunc(mouse); glutPassiveMotionFunc(passive_motion); init_scene(); glutMainLoop(); return (EXIT_SUCCESS); } // vim: set sts=4 sw=4 ts=8 ft=cpp: --------------------------------------------