// Peter Bui // CSE 40166 Computer Graphics (Fall 2010) // Lab 2: fun with OpenGL polygons #include "color.h" #include #include #include #include #include #include using namespace std; // Type definition ------------------------------------------------------------- typedef struct { GLfloat x; GLfloat y; } point; // Constants ------------------------------------------------------------------- #define NPRIMITIVES 6 int PRIMITIVES[NPRIMITIVES] = { GL_POLYGON, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP }; // Global variables ------------------------------------------------------------ static int WindowWidth = 640; static int WindowHeight = 480; static int BackgroundColor = COLOR_BLACK; static vector Points; // Initialize viewing ---------------------------------------------------------- void init() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-5.0, 10.0, -5.0, 10.0); glMatrixMode(GL_MODELVIEW); glClearColor(ColorTable[BackgroundColor][0], ColorTable[BackgroundColor][1], ColorTable[BackgroundColor][2], 1.0); glEnable(GL_POINT_SMOOTH); } // Read in points -------------------------------------------------------------- void read_points(const char *fname) { FILE *fs; point p; fs = fopen(fname, "r"); if (!fs) { fprintf(stderr, "polygons: could not open file %s\n", fname); exit(EXIT_FAILURE); } while (fscanf(fs, "%f %f", &p.x, &p.y) == 2) Points.push_back(p); fclose(fs); } // Display callback ------------------------------------------------------------ void display() { int ViewportWidth = WindowWidth*2 / NPRIMITIVES; int ViewportHeight = WindowHeight*3 / NPRIMITIVES; glClear(GL_COLOR_BUFFER_BIT); for (int p = 0; p < NPRIMITIVES; p++) { glViewport((p % 3) * ViewportWidth, (p < 3) ? ViewportHeight : 0, ViewportWidth, ViewportHeight); glBegin(PRIMITIVES[p]); { for (size_t i = 0; i < Points.size(); i++) { glColor3fv(color_rotate()); glVertex2f(Points[i].x, Points[i].y); } } glEnd(); } glFlush(); } // Show usage ------------------------------------------------------------------ void show_usage() { printf("usage: polygons [options] \n"); printf("General Options:\n"); printf(" -h Show this help message\n"); printf("\nWindow Options:\n"); printf(" -W Set window width\n"); printf(" -H Set window height\n"); } // Parse command line arguments ------------------------------------------------ void parse_command_line_arguments(int argc, char *argv[]) { int c; while ((c = getopt(argc, argv, "W:H:h")) >= 0) { switch (c) { case 'W': WindowWidth = strtol(optarg, NULL, 10); break; case 'H': WindowHeight = strtol(optarg, NULL, 10); break; case 'h': show_usage(); exit(EXIT_SUCCESS); break; default: fprintf(stderr, "polygons: unknown command line argument %c\n", c); show_usage(); exit(EXIT_FAILURE); break; } } if ((argc - optind) != 1) { show_usage(); exit(EXIT_FAILURE); } else { read_points(argv[optind]); } } // Main execution -------------------------------------------------------------- int main(int argc, char *argv[]) { glutInit(&argc, argv); parse_command_line_arguments(argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowSize(WindowWidth, WindowHeight); glutCreateWindow("polygons"); glutDisplayFunc(display); init(); glutMainLoop(); return (EXIT_SUCCESS); } // vim: sts=4 sw=4 ts=8 ft=cpp