|
| 1 | +#include <math.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <GL/glut.h> |
| 5 | +#include "geometry.h" |
| 6 | +#include "solid.h" |
| 7 | + |
| 8 | +#define INITIAL_CAPACITY_POINTS 100 |
| 9 | +#define DIVISION_NUMBER 60 |
| 10 | +#define WINDOW_HEIGHT 600 |
| 11 | + |
| 12 | +static point_t *pt = NULL; |
| 13 | +static unsigned numpoints = 0; |
| 14 | +static unsigned capacity = INITIAL_CAPACITY_POINTS; |
| 15 | +static FILE *file_out = NULL; |
| 16 | + |
| 17 | +void push_point(const double x, const double y) |
| 18 | +{ |
| 19 | + if(numpoints >= capacity) { |
| 20 | + capacity *= 2; |
| 21 | + if((pt = realloc(pt, capacity * sizeof(point_t))) == NULL) { |
| 22 | + error_printf("out of memory\n"); |
| 23 | + exit(EXIT_FAILURE); |
| 24 | + } |
| 25 | + } |
| 26 | + pt[numpoints].x = x; |
| 27 | + pt[numpoints].y = y; |
| 28 | + numpoints++; |
| 29 | +} |
| 30 | + |
| 31 | +void discard_top() |
| 32 | +{ |
| 33 | + if(numpoints == 0) return; |
| 34 | + numpoints--; |
| 35 | +} |
| 36 | + |
| 37 | +void display(void) |
| 38 | +{ |
| 39 | + glClear(GL_COLOR_BUFFER_BIT); |
| 40 | + // Draw points |
| 41 | + glLineWidth(3.0); |
| 42 | + glBegin(GL_POINTS); |
| 43 | + for(unsigned i = 0; i < numpoints; ++i) glVertex2d(pt[i].x, pt[i].y); |
| 44 | + glEnd(); |
| 45 | + // Draw lines |
| 46 | + glLineWidth(1.0); |
| 47 | + glBegin(GL_LINE_STRIP); |
| 48 | + for(unsigned i = 0; i < numpoints; ++i) glVertex2d(pt[i].x, pt[i].y); |
| 49 | + glEnd(); |
| 50 | + glFlush(); |
| 51 | +} |
| 52 | + |
| 53 | +void handle_mouse(const int btn, const int state, const int x, const int y) |
| 54 | +{ |
| 55 | + if(state != GLUT_DOWN) return; |
| 56 | + switch(btn) { |
| 57 | + case GLUT_LEFT_BUTTON: |
| 58 | + push_point(x, y); |
| 59 | + break; |
| 60 | + case GLUT_RIGHT_BUTTON: |
| 61 | + discard_top(); |
| 62 | + break; |
| 63 | + default: break; |
| 64 | + } |
| 65 | + glutPostRedisplay(); |
| 66 | +} |
| 67 | + |
| 68 | +void to_revolution() |
| 69 | +{ |
| 70 | + init_geometry(); |
| 71 | + shape_t *s = new_shape(); |
| 72 | + for(unsigned i = 0; i < numpoints; ++i) { |
| 73 | + add_vertex(s, new_vertex(pt[i].x, WINDOW_HEIGHT-pt[i].y, 0)); |
| 74 | + } |
| 75 | + new_revolution(s, DIVISION_NUMBER); |
| 76 | + flushOBJ(file_out); |
| 77 | + finalize_geometry(); |
| 78 | +} |
| 79 | + |
| 80 | +void handle_keyboard(const unsigned char key, const int x, const int y) |
| 81 | +{ |
| 82 | +#define KEY_ESC 27 |
| 83 | +#define KEY_ENTER 13 |
| 84 | + switch (key) { |
| 85 | + case KEY_ESC: |
| 86 | + case KEY_ENTER: |
| 87 | + to_revolution(); |
| 88 | + exit(EXIT_SUCCESS); |
| 89 | + default: |
| 90 | + break; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void reshape(const int w, const int h) |
| 95 | +{ |
| 96 | + glViewport(0, 0, w, h); |
| 97 | + glMatrixMode(GL_PROJECTION); |
| 98 | + glLoadIdentity(); |
| 99 | + glOrtho(0.0, (float)w, (float)h, 0.0, -10.0, 10.0); |
| 100 | + glMatrixMode(GL_MODELVIEW); |
| 101 | +} |
| 102 | + |
| 103 | +int main(int argc, char *argv[]) |
| 104 | +{ |
| 105 | + if(argc < 2) { |
| 106 | + error_printf("missing file argument\n"); |
| 107 | + fprintf(stderr, "usage : %s output_filename\n", argv[0]); |
| 108 | + exit(EXIT_FAILURE); |
| 109 | + } |
| 110 | + if((pt = malloc(capacity * sizeof(point_t))) == NULL) { |
| 111 | + error_printf("out of memory\n"); |
| 112 | + exit(EXIT_FAILURE); |
| 113 | + } |
| 114 | + if((file_out = fopen(argv[1], "w")) == NULL) { |
| 115 | + error_printf("failed to open %s for write\n"); |
| 116 | + FREE(pt); |
| 117 | + exit(EXIT_FAILURE); |
| 118 | + } |
| 119 | + glutInitWindowSize(600, WINDOW_HEIGHT); |
| 120 | + glutInitWindowPosition(10, 10); |
| 121 | + glutInit(&argc, argv); |
| 122 | + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); |
| 123 | + glutCreateWindow(argv[0]); |
| 124 | + glClearColor(1.0, 1.0, 1.0, 0.0); |
| 125 | + glColor3f(0.0, 0.0, 0.0); |
| 126 | + glPointSize(7.0); |
| 127 | + glutDisplayFunc(display); |
| 128 | + glutReshapeFunc(reshape); |
| 129 | + glutMouseFunc(handle_mouse); |
| 130 | + glutKeyboardFunc(handle_keyboard); |
| 131 | + glutMainLoop(); |
| 132 | + return 0; |
| 133 | +} |
| 134 | + |
0 commit comments