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