gears meshing, machines of gears, machines of MADNESS
[antikythera] / src / main.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <GL/glew.h>
5 #ifdef __APPLE__
6 #include <GLUT/glut.h>
7 #else
8 #include <GL/glut.h>
9 #endif
10 #include "machine.h"
11
12 bool init();
13 void cleanup();
14 void display();
15 void idle();
16 void draw_gears();
17 void reshape(int x, int y);
18 void keyb(unsigned char key, int x, int y);
19 void mouse(int bn, int st, int x, int y);
20 void motion(int x, int y);
21
22 static float cam_dist = 2;
23 static float cam_theta, cam_phi;
24 static int prev_mx, prev_my;
25 static bool bnstate[8];
26
27 static unsigned int start_time, prev_msec;
28 static Machine *machine;
29
30 int main(int argc, char **argv)
31 {
32         glutInitWindowSize(1024, 768);
33         glutInit(&argc, argv);
34         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
35         glutCreateWindow("Antikythera");
36
37         glutDisplayFunc(display);
38         glutIdleFunc(idle);
39         glutReshapeFunc(reshape);
40         glutKeyboardFunc(keyb);
41         glutMouseFunc(mouse);
42         glutMotionFunc(motion);
43
44         if(!init()) {
45                 return 1;
46         }
47         atexit(cleanup);
48
49         glutMainLoop();
50         return 0;
51 }
52
53 bool init()
54 {
55         glewInit();
56
57         glEnable(GL_DEPTH_TEST);
58         glEnable(GL_CULL_FACE);
59         glEnable(GL_LIGHTING);
60         glEnable(GL_LIGHT0);
61         glEnable(GL_NORMALIZE);
62
63         Mesh::use_custom_sdr_attr = false;
64
65         machine = new Machine;
66
67         const float pitch = 10.0f;
68
69         Gear *gear1 = new Gear;
70         gear1->pos = Vec3(-50, 0, 0);
71         gear1->set_teeth(16, pitch);
72         gear1->gen_mesh();
73         machine->add_gear(gear1);
74
75         Gear *gear2 = new Gear;
76         gear2->set_teeth(32, pitch);
77         gear2->pos = gear1->pos + Vec3(gear1->radius + gear2->radius - gear1->teeth_length * 0.75, 0, 0);
78         gear2->thickness = 5;
79         gear2->gen_mesh();
80         machine->add_gear(gear2);
81
82         Gear *gear3 = new Gear;
83         gear3->set_teeth(8, pitch);
84         gear3->pos = gear2->pos + Vec3(0, gear2->radius + gear3->radius - gear2->teeth_length * 0.75, 0);
85         gear3->gen_mesh();
86         machine->add_gear(gear3);
87
88         machine->add_motor(0, 1.0);
89         machine->calc_meshing();
90
91         start_time = glutGet(GLUT_ELAPSED_TIME);
92         return true;
93 }
94
95 void cleanup()
96 {
97         delete machine;
98 }
99
100 void display()
101 {
102         unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
103         float dt = (float)(msec - prev_msec) / 1000.0f;
104         prev_msec = msec;
105
106         machine->update(dt);
107
108         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
109
110         glMatrixMode(GL_MODELVIEW);
111         glLoadIdentity();
112         glTranslatef(0, 0, -cam_dist);
113         glRotatef(cam_phi, 1, 0, 0);
114         glRotatef(cam_theta, 0, 1, 0);
115
116         draw_gears();
117
118         glutSwapBuffers();
119         assert(glGetError() == GL_NO_ERROR);
120 }
121
122 void idle()
123 {
124         glutPostRedisplay();
125 }
126
127 void draw_gears()
128 {
129         /* world scale is in meters, gears are in millimeters, sclae by 1/1000 */
130         glPushMatrix();
131         glScalef(0.001, 0.001, 0.001);
132
133         machine->draw();
134
135         glPopMatrix();
136 }
137
138 void reshape(int x, int y)
139 {
140         glViewport(0, 0, x, y);
141
142         glMatrixMode(GL_PROJECTION);
143         glLoadIdentity();
144         gluPerspective(50.0, (float)x / (float)y, 0.05, 100.0);
145 }
146
147 void keyb(unsigned char key, int x, int y)
148 {
149         switch(key) {
150         case 27:
151                 exit(0);
152         }
153 }
154
155 void mouse(int bn, int st, int x, int y)
156 {
157         prev_mx = x;
158         prev_my = y;
159         bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
160 }
161
162 void motion(int x, int y)
163 {
164         int dx = x - prev_mx;
165         int dy = y - prev_my;
166         prev_mx = x;
167         prev_my = y;
168
169         if(!dx && !dy) return;
170
171         if(bnstate[0]) {
172                 cam_theta += dx * 0.5;
173                 cam_phi += dy * 0.5;
174
175                 if(cam_phi < -90) cam_phi = -90;
176                 if(cam_phi > 90) cam_phi = 90;
177                 glutPostRedisplay();
178         }
179         if(bnstate[2]) {
180                 cam_dist += dy * 0.05;
181                 if(cam_dist < 0.0) cam_dist = 0.0;
182                 glutPostRedisplay();
183         }
184 }