assimp
[laserbrain_demo] / src / main.cc
index 7efc114..b170095 100644 (file)
@@ -8,32 +8,43 @@
 #include <GL/glut.h>
 #endif
 #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 = 0.5;
-static float cam_theta, cam_phi;
+#include "sdr.h"
+#include "shadow.h"
+#include "texture.h"
+#include "mesh.h"
+#include "meshgen.h"
+
+static bool init();
+static void cleanup();
+static void display();
+static void idle();
+static void draw_scene();
+static void reshape(int x, int y);
+static void keyb(unsigned char key, int x, int y);
+static void mouse(int bn, int st, int x, int y);
+static void motion(int x, int y);
+static void passive_motion(int x, int y);
+
+static int win_width, win_height;
+
+static float cam_dist = 0.25;
+static float cam_theta, cam_phi = 20;
 static int prev_mx, prev_my;
 static bool bnstate[8];
 
+static Mat4 view_matrix;
+
 static unsigned int start_time, prev_msec;
-static Machine *machine;
+
+static TextureSet texman;
+
 
 int main(int argc, char **argv)
 {
        glutInitWindowSize(1024, 768);
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
-       glutCreateWindow("Antikythera");
+       glutCreateWindow("demo");
 
        glutDisplayFunc(display);
        glutIdleFunc(idle);
@@ -41,6 +52,7 @@ int main(int argc, char **argv)
        glutKeyboardFunc(keyb);
        glutMouseFunc(mouse);
        glutMotionFunc(motion);
+       glutPassiveMotionFunc(passive_motion);
 
        if(!init()) {
                return 1;
@@ -51,7 +63,7 @@ int main(int argc, char **argv)
        return 0;
 }
 
-bool init()
+static bool init()
 {
        glewInit();
 
@@ -59,114 +71,117 @@ bool init()
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        glEnable(GL_LIGHTING);
-       glEnable(GL_LIGHT0);
        glEnable(GL_NORMALIZE);
 
        Mesh::use_custom_sdr_attr = false;
 
-       machine = new Machine;
-
-       const float pitch = 10.0f;
+       float ambient[] = {0.1, 0.1, 0.1, 0.0};
+       glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
 
-       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);
-       machine->calc_meshing();
+       glUseProgram(0);
 
        start_time = glutGet(GLUT_ELAPSED_TIME);
        return true;
 }
 
-void cleanup()
+static void cleanup()
 {
-       delete machine;
+       texman.clear();
 }
 
-void display()
+static void update(float dt)
+{
+       texman.update();
+}
+
+static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
+{
+       unsigned int lt = GL_LIGHT0 + idx;
+       float posv[] = { pos.x, pos.y, pos.z, 1 };
+       float colv[] = { color.x, color.y, color.z, 1 };
+
+       glEnable(lt);
+       glLightfv(lt, GL_POSITION, posv);
+       glLightfv(lt, GL_DIFFUSE, colv);
+       glLightfv(lt, GL_SPECULAR, colv);
+}
+
+static 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);
 
+       view_matrix = Mat4::identity;
+       view_matrix.pre_translate(0, 0, -cam_dist);
+       view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0);
+       view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0);
+
        glMatrixMode(GL_MODELVIEW);
-       glLoadIdentity();
-       glTranslatef(0, 0, -cam_dist);
-       glRotatef(cam_phi, 1, 0, 0);
-       glRotatef(cam_theta, 0, 1, 0);
+       glLoadMatrixf(view_matrix[0]);
+
+       static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
+       set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
+       set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
+       set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
 
-       draw_gears();
+       update(dt);
+
+       draw_scene();
 
        glutSwapBuffers();
        assert(glGetError() == GL_NO_ERROR);
 }
 
-void idle()
+static void idle()
 {
        glutPostRedisplay();
 }
 
-void draw_gears()
+static void draw_scene()
 {
-       /* world scale is in meters, gears are in millimeters, scale by 1/1000 */
-       glPushMatrix();
-       glScalef(0.001, 0.001, 0.001);
-
-       machine->draw();
-
-       glPopMatrix();
+       glBegin(GL_QUADS);
+       glNormal3f(0, 1, 0);
+       glVertex3f(-30, -10, 30);
+       glVertex3f(30, -10, 30);
+       glVertex3f(30, -10, -30);
+       glVertex3f(-30, -10, -30);
+       glEnd();
 }
 
-void reshape(int x, int y)
+static void reshape(int x, int y)
 {
+       win_width = x;
+       win_height = y;
+
        glViewport(0, 0, x, y);
 
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
-       gluPerspective(50.0, (float)x / (float)y, 0.01, 100.0);
+       gluPerspective(50.0, (float)x / (float)y, 0.01, 50.0);
 }
 
-void keyb(unsigned char key, int x, int y)
+static 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;
        }
 }
 
-void mouse(int bn, int st, int x, int y)
+static void mouse(int bn, int st, int x, int y)
 {
+       int bidx = bn - GLUT_LEFT_BUTTON;
+       bool down = st == GLUT_DOWN;
+
        prev_mx = x;
        prev_my = y;
-       bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
+       bnstate[bidx] = down;
 }
 
-void motion(int x, int y)
+static void motion(int x, int y)
 {
        int dx = x - prev_mx;
        int dy = y - prev_my;
@@ -189,3 +204,9 @@ void motion(int x, int y)
                glutPostRedisplay();
        }
 }
+
+static void passive_motion(int x, int y)
+{
+       prev_mx = x;
+       prev_my = y;
+}