#include #include #include #include #include #include #include int width = 400; int height = 400; int shape = 0; // Anti-aliased Point Size Info GLfloat ps_range[2]; GLfloat ps_gran; GLfloat ps = 5.0; // Anti-aliased Line Width Info GLfloat lw_range[2]; GLfloat lw_gran; GLfloat lw = 5.0; double drand () { return rand() / (double)(RAND_MAX + 1); } void draw () { // Clear buffers glClear(GL_COLOR_BUFFER_BIT); // Reset viewport glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Draw current shape switch(shape) { case 0: glBegin(GL_POINTS); break; case 1: glBegin(GL_LINES); break; case 2: glBegin(GL_LINE_STRIP); break; case 3: glBegin(GL_LINE_LOOP); break; case 4: glBegin(GL_TRIANGLES); break; case 5: glBegin(GL_TRIANGLE_STRIP); break; case 6: glBegin(GL_TRIANGLE_FAN); break; case 7: glBegin(GL_QUADS); break; case 8: glBegin(GL_QUAD_STRIP); break; case 9: glBegin(GL_POLYGON); break; }; // Draw random verticies for (int i = 0; i < 24; i++) { glColor3f(drand(), drand(), drand()); glVertex3f(drand()*width, drand()*height, 0); } glEnd(); // Put buffer on screen glutSwapBuffers(); } void keys (unsigned char key, int x, int y) { GLboolean b; GLint k; switch (key) { // Exit Program case 'q': case 'Q': case 27: exit(0); break; // Toggle Line Anti-aliasing case 'l': glGetBooleanv(GL_LINE_SMOOTH, &b); if (b) glDisable(GL_LINE_SMOOTH); else glEnable(GL_LINE_SMOOTH); break; // Toggle Point Anti-aliasing case 'p': glGetBooleanv(GL_POINT_SMOOTH, &b); if (b) glDisable(GL_POINT_SMOOTH); else glEnable(GL_POINT_SMOOTH); break; // Toggle the Shading Model case 's': glGetIntegerv(GL_SHADE_MODEL, &k); if (k == GL_SMOOTH) glShadeModel(GL_FLAT); else glShadeModel(GL_SMOOTH); break; // Cycle Current Shape case '[': shape = --shape % 10; break; case ']': shape = ++shape % 10; break; // Cycle Line/Point Size case '+': case '=': if (shape == 0 && ps + 1.0 <= ps_range[1]) ps += 1.0; else if (shape > 0 && lw + 1.0 <= lw_range[1]) lw += 1.0; glPointSize(ps); glLineWidth(lw); break; case '-': case '_': if (shape == 0 && ps - 1.0 >= ps_range[0]) ps -= 1.0; else if (shape > 0 && lw - 1.0 >= lw_range[0]) lw -= 1.0; glPointSize(ps); glLineWidth(lw); break; }; // Force OpenGL to redraw screen glutPostRedisplay(); } int main (int argc, char **argv) { // Setup OpenGL glutInit(&argc, argv); glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE); glutInitWindowSize(width, height); glutCreateWindow("Shapes"); // Setup callbacks glutDisplayFunc(draw); glutKeyboardFunc(keys); // Setup viewport glMatrixMode(GL_PROJECTION); glOrtho(0, width, 0, height, -1000, 1000); // Change background color glClearColor(0, 0, 0, 0); // Collect Point Size Info glGetFloatv(GL_POINT_SIZE_RANGE, ps_range); // Collect Line Width Info glGetFloatv(GL_LINE_WIDTH_RANGE, lw_range); // Setup Point Size glPointSize(ps); // Setup Line Width glLineWidth(lw); // Setup random srand(time(NULL)); // Give control to OpenGL glutMainLoop(); return 0; }