1
2#include <stdio.h>
3#include <stdlib.h>
4#include <math.h>
5#include "glut_wrap.h"
6
7static int Win;
8static int WinWidth = 400, WinHeight = 400;
9static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
10static GLboolean Anim = GL_FALSE;
11
12static int CurVert = 0, MoveAll = 0;
13static GLfloat Verts[3][2];
14
15
16static void
17Idle(void)
18{
19   Xrot += 3.0;
20   Yrot += 4.0;
21   Zrot += 2.0;
22   glutPostRedisplay();
23}
24
25
26static void
27Draw(void)
28{
29   glClear(GL_COLOR_BUFFER_BIT);
30
31   glBegin(GL_POLYGON);
32   glVertex2fv(Verts[0]);
33   glVertex2fv(Verts[1]);
34   glVertex2fv(Verts[2]);
35   glEnd();
36
37   glutSwapBuffers();
38}
39
40
41static void
42Reshape(int width, int height)
43{
44   WinWidth = width;
45   WinHeight = height;
46   glViewport(0, 0, width, height);
47   glMatrixMode(GL_PROJECTION);
48   glLoadIdentity();
49   glMatrixMode(GL_MODELVIEW);
50   glLoadIdentity();
51}
52
53
54static void
55RotVerts(void)
56{
57   float tx = Verts[0][0], ty = Verts[0][1];
58   Verts[0][0] = Verts[1][0];   Verts[0][1] = Verts[1][1];
59   Verts[1][0] = Verts[2][0];   Verts[1][1] = Verts[2][1];
60   Verts[2][0] = tx;            Verts[2][1] = ty;
61}
62
63static void
64Save(void)
65{
66   int i;
67   FILE *f = fopen("verts.txt", "w");
68   for (i = 0; i < 3; i++)
69      fprintf(f, "%f %f\n", Verts[i][0], Verts[i][1]);
70   fclose(f);
71   printf("Saved data\n");
72}
73
74static void
75Restore(void)
76{
77   int i;
78   FILE *f = fopen("verts.txt", "r");
79   if (f) {
80      printf("Restoring data\n");
81      for (i = 0; i < 3; i++) {
82         int n =fscanf(f, "%f %f\n", &Verts[i][0], &Verts[i][1]);
83         (void) n;
84      }
85      fclose(f);
86   }
87}
88
89static void
90Key(unsigned char key, int x, int y)
91{
92   (void) x;
93   (void) y;
94   switch (key) {
95   case 'r':
96      RotVerts();
97      break;
98   case 'a':
99      MoveAll = 1;
100      break;
101   case '0':
102      MoveAll = 0;
103      CurVert = 0;
104      break;
105   case '1':
106      MoveAll = 0;
107      CurVert = 1;
108      break;
109   case '2':
110      MoveAll = 0;
111      CurVert = 2;
112      break;
113   case 's':
114      Save();
115      break;
116   case 27:
117      glutDestroyWindow(Win);
118      exit(0);
119      break;
120   }
121   glutPostRedisplay();
122}
123
124static void
125move(float dx, float dy)
126{
127   int i;
128
129   dx *= 0.05;
130   dy *= 0.05;
131   if (MoveAll) {
132      for (i = 0; i < 3; i++) {
133         Verts[i][0] += dx;
134         Verts[i][1] += dy;
135      }
136   }
137   else {
138      Verts[CurVert][0] += dx;
139      Verts[CurVert][1] += dy;
140   }
141
142   printf("\n");
143   for (i = 0; i < 3; i++) {
144      printf("%f %f\n", Verts[i][0], Verts[i][1]);
145   }
146}
147
148
149static void
150SpecialKey(int key, int x, int y)
151{
152   switch (key) {
153   case GLUT_KEY_UP:
154      move(0, +1);
155      break;
156   case GLUT_KEY_DOWN:
157      move(0, -1);
158      break;
159   case GLUT_KEY_LEFT:
160      move(-1, 0);
161      break;
162   case GLUT_KEY_RIGHT:
163      move(1, 0);
164      break;
165   }
166   glutPostRedisplay();
167}
168
169
170static void
171Init(void)
172{
173   Verts[0][0] = 0.0; Verts[0][1] = 0.0;
174   Verts[1][0] = 1.2; Verts[1][1] = -0.5;
175   Verts[2][0] = 0.6; Verts[2][1] = -1.5;
176
177   Restore();
178
179   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
180}
181
182
183int
184main(int argc, char *argv[])
185{
186   glutInit(&argc, argv);
187   glutInitWindowSize(WinWidth, WinHeight);
188   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
189   Win = glutCreateWindow(argv[0]);
190   glutReshapeFunc(Reshape);
191   glutKeyboardFunc(Key);
192   glutSpecialFunc(SpecialKey);
193   glutDisplayFunc(Draw);
194   if (Anim)
195      glutIdleFunc(Idle);
196   Init();
197   glutMainLoop();
198   return 0;
199}
200