lazy meshing
[antikythera] / src / main.cc
index c4696f6..6fc85ba 100644 (file)
@@ -7,32 +7,36 @@
 #else
 #include <GL/glut.h>
 #endif
-#include "gear.h"
+#include "app.h"
+#include "machine.h"
 
 bool init();
 void cleanup();
 void display();
+void idle();
 void draw_gears();
 void reshape(int x, int y);
 void keyb(unsigned char key, int x, int y);
 void mouse(int bn, int st, int x, int y);
 void motion(int x, int y);
 
-static float cam_dist = 2;
+static float cam_dist = 0.5;
 static float cam_theta, cam_phi;
 static int prev_mx, prev_my;
 static bool bnstate[8];
 
-static Gear *test_gear;
+static unsigned int start_time, prev_msec;
+static Machine *machine;
 
 int main(int argc, char **argv)
 {
-       glutInit(&argc, argv);
        glutInitWindowSize(1024, 768);
-       glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
+       glutInit(&argc, argv);
+       glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
        glutCreateWindow("Antikythera");
 
        glutDisplayFunc(display);
+       glutIdleFunc(idle);
        glutReshapeFunc(reshape);
        glutKeyboardFunc(keyb);
        glutMouseFunc(mouse);
@@ -51,6 +55,7 @@ bool init()
 {
        glewInit();
 
+       glEnable(GL_MULTISAMPLE);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        glEnable(GL_LIGHTING);
@@ -59,19 +64,48 @@ bool init()
 
        Mesh::use_custom_sdr_attr = false;
 
-       test_gear = new Gear;
-       test_gear->gen_mesh();
+       machine = new Machine;
+
+       const float pitch = 10.0f;
+
+       Gear *gear1 = new Gear;
+       gear1->pos = Vec3(-50, 0, 0);
+       gear1->set_teeth(16, pitch);
+       gear1->gen_mesh();
+       machine->add_gear(gear1);
+
+       Gear *gear2 = new Gear;
+       gear2->set_teeth(32, pitch);
+       gear2->pos = gear1->pos + Vec3(gear1->radius + gear2->radius - gear1->teeth_length * 0.75, 0, 0);
+       gear2->thickness = 5;
+       gear2->gen_mesh();
+       machine->add_gear(gear2);
+
+       Gear *gear3 = new Gear;
+       gear3->set_teeth(8, pitch);
+       gear3->pos = gear2->pos + Vec3(0, gear2->radius + gear3->radius - gear2->teeth_length * 0.75, 0);
+       gear3->gen_mesh();
+       machine->add_gear(gear3);
+
+       machine->add_motor(0, 1.0);
 
+       start_time = glutGet(GLUT_ELAPSED_TIME);
        return true;
 }
 
 void cleanup()
 {
-       delete test_gear;
+       delete machine;
 }
 
 void display()
 {
+       unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
+       float dt = (float)(msec - prev_msec) / 1000.0f;
+       prev_msec = msec;
+
+       machine->update(dt);
+
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        glMatrixMode(GL_MODELVIEW);
@@ -86,13 +120,18 @@ void display()
        assert(glGetError() == GL_NO_ERROR);
 }
 
+void idle()
+{
+       glutPostRedisplay();
+}
+
 void draw_gears()
 {
-       /* world scale is in meters, gears are in millimeters, sclae by 1/1000 */
+       /* world scale is in meters, gears are in millimeters, scale by 1/1000 */
        glPushMatrix();
        glScalef(0.001, 0.001, 0.001);
 
-       test_gear->draw();
+       machine->draw();
 
        glPopMatrix();
 }
@@ -103,7 +142,7 @@ void reshape(int x, int y)
 
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
-       gluPerspective(50.0, (float)x / (float)y, 0.05, 100.0);
+       gluPerspective(50.0, (float)x / (float)y, 0.01, 100.0);
 }
 
 void keyb(unsigned char key, int x, int y)
@@ -111,6 +150,11 @@ void keyb(unsigned char key, int x, int y)
        switch(key) {
        case 27:
                exit(0);
+
+       case 'w':
+               opt_gear_wireframe = !opt_gear_wireframe;
+               glutPostRedisplay();
+               break;
        }
 }
 
@@ -139,7 +183,7 @@ void motion(int x, int y)
                glutPostRedisplay();
        }
        if(bnstate[2]) {
-               cam_dist += dy * 0.05;
+               cam_dist += dy * 0.01;
                if(cam_dist < 0.0) cam_dist = 0.0;
                glutPostRedisplay();
        }