multisampling and shit
[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 | GLUT_MULTISAMPLE);
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_MULTISAMPLE);
58         glEnable(GL_DEPTH_TEST);
59         glEnable(GL_CULL_FACE);
60         glEnable(GL_LIGHTING);
61         glEnable(GL_LIGHT0);
62         glEnable(GL_NORMALIZE);
63
64         Mesh::use_custom_sdr_attr = false;
65
66         machine = new Machine;
67
68         const float pitch = 10.0f;
69
70         Gear *gear1 = new Gear;
71         gear1->pos = Vec3(-50, 0, 0);
72         gear1->set_teeth(16, pitch);
73         gear1->gen_mesh();
74         machine->add_gear(gear1);
75
76         Gear *gear2 = new Gear;
77         gear2->set_teeth(32, pitch);
78         gear2->pos = gear1->pos + Vec3(gear1->radius + gear2->radius - gear1->teeth_length * 0.75, 0, 0);
79         gear2->thickness = 5;
80         gear2->gen_mesh();
81         machine->add_gear(gear2);
82
83         Gear *gear3 = new Gear;
84         gear3->set_teeth(8, pitch);
85         gear3->pos = gear2->pos + Vec3(0, gear2->radius + gear3->radius - gear2->teeth_length * 0.75, 0);
86         gear3->gen_mesh();
87         machine->add_gear(gear3);
88
89         machine->add_motor(0, 1.0);
90         machine->calc_meshing();
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, sclae 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.05, 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 }
155
156 void mouse(int bn, int st, int x, int y)
157 {
158         prev_mx = x;
159         prev_my = y;
160         bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
161 }
162
163 void motion(int x, int y)
164 {
165         int dx = x - prev_mx;
166         int dy = y - prev_my;
167         prev_mx = x;
168         prev_my = y;
169
170         if(!dx && !dy) return;
171
172         if(bnstate[0]) {
173                 cam_theta += dx * 0.5;
174                 cam_phi += dy * 0.5;
175
176                 if(cam_phi < -90) cam_phi = -90;
177                 if(cam_phi > 90) cam_phi = 90;
178                 glutPostRedisplay();
179         }
180         if(bnstate[2]) {
181                 cam_dist += dy * 0.01;
182                 if(cam_dist < 0.0) cam_dist = 0.0;
183                 glutPostRedisplay();
184         }
185 }