implementing build_bvh_sah
[cyberay] / src / main.c
index d49c311..b3b96cc 100644 (file)
@@ -3,6 +3,7 @@
 #include <assert.h>
 #include <cgmath/cgmath.h>
 #include "miniglut.h"
+#include "game.h"
 #include "level.h"
 #include "rt.h"
 
@@ -45,10 +46,8 @@ static void mouse(int bn, int st, int x, int y);
 static void motion(int x, int y);
 static unsigned int nextpow2(unsigned int x);
 
-static long start_time;
-
 static float cam_theta, cam_phi;
-static cgm_vec3 cam_pos = {0, -1.6, 0};
+static cgm_vec3 cam_pos = {0, 1.6, 0};
 
 static int mouse_x, mouse_y;
 static int bnstate[8];
@@ -63,19 +62,25 @@ static int keymap[NUM_INPUTS][2] = {
        {' ', 0}
 };
 
-static struct level lvl;
-
 static unsigned int tex;
 static int tex_width, tex_height;
 static int tex_intfmt;
 static float tex_xform[16];
 
+static unsigned long nframes;
+static unsigned long start_time;
+
 
 int main(int argc, char **argv)
 {
        glutInit(&argc, argv);
-       glutInitWindowSize(1280, 800);
-       glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
+
+       if(init_options(argc, argv) == -1) {
+               return 1;
+       }
+
+       glutInitWindowSize(opt.width, opt.height);
+       glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
        glutCreateWindow("cyberay");
 
        glutDisplayFunc(display);
@@ -94,6 +99,8 @@ int main(int argc, char **argv)
        }
        atexit(cleanup);
 
+       start_time = glutGet(GLUT_ELAPSED_TIME);
+
        glutMainLoop();
        return 0;
 }
@@ -107,11 +114,6 @@ static int init(void)
 
        glEnable(GL_CULL_FACE);
 
-       /*
-       glEnable(GL_DEPTH_TEST);
-       glEnable(GL_LIGHTING);
-       glEnable(GL_LIGHT0);
-       */
 
        glGenTextures(1, &tex);
        glBindTexture(GL_TEXTURE_2D, tex);
@@ -123,13 +125,16 @@ static int init(void)
        if(load_level(&lvl, "data/test.lvl") == -1) {
                return -1;
        }
-
-       start_time = glutGet(GLUT_ELAPSED_TIME);
        return 0;
 }
 
 static void cleanup(void)
 {
+       float tsec;
+
+       tsec = (glutGet(GLUT_ELAPSED_TIME) - start_time) / 1000.0f;
+       printf("avg framerate: %.2f fps\n", (float)nframes / tsec);
+
        destroy_level(&lvl);
 
        glDeleteTextures(1, &tex);
@@ -141,7 +146,7 @@ static void cleanup(void)
 static void update(void)
 {
        static unsigned int prev_upd;
-       unsigned int msec;
+       unsigned long msec;
        float dt, vfwd, vright;
 
        msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
@@ -157,19 +162,19 @@ static void update(void)
                vfwd += WALK_SPEED * dt;
        }
        if(inpstate[INP_RIGHT]) {
-               vright -= WALK_SPEED * dt;
+               vright += WALK_SPEED * dt;
        }
        if(inpstate[INP_LEFT]) {
-               vright += WALK_SPEED * dt;
+               vright -= WALK_SPEED * dt;
        }
 
        cam_pos.x += cos(cam_theta) * vright + sin(cam_theta) * vfwd;
-       cam_pos.z += sin(cam_theta) * vright - cos(cam_theta) * vfwd;
+       cam_pos.z += -sin(cam_theta) * vright + cos(cam_theta) * vfwd;
 
        cgm_midentity(view_xform);
-       cgm_mtranslate(view_xform, cam_pos.x, cam_pos.y, cam_pos.z);
-       cgm_mrotate_y(view_xform, cam_theta);
        cgm_mrotate_x(view_xform, cam_phi);
+       cgm_mrotate_y(view_xform, cam_theta);
+       cgm_mtranslate(view_xform, cam_pos.x, cam_pos.y, cam_pos.z);
 }
 
 static void display(void)
@@ -182,27 +187,20 @@ static void display(void)
        glEnable(GL_TEXTURE_2D);
 
        glBegin(GL_QUADS);
-       glTexCoord2f(0, 0);
+       glTexCoord2f(0, 1);
        glVertex2f(-1, -1);
-       glTexCoord2f(1, 0);
-       glVertex2f(1, -1);
        glTexCoord2f(1, 1);
+       glVertex2f(1, -1);
+       glTexCoord2f(1, 0);
        glVertex2f(1, 1);
-       glTexCoord2f(0, 1);
+       glTexCoord2f(0, 0);
        glVertex2f(-1, 1);
        glEnd();
 
-       /*
-       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-       glMatrixMode(GL_MODELVIEW);
-       glLoadMatrixf(view_xform);
-
-       draw_level(&lvl);
-       */
-
        glutSwapBuffers();
        assert(glGetError() == GL_NO_ERROR);
+
+       nframes++;
 }
 
 static void idle(void)
@@ -213,14 +211,6 @@ static void idle(void)
 static void reshape(int x, int y)
 {
        glViewport(0, 0, x, y);
-       /*
-       float proj[16];
-
-       cgm_mperspective(proj, cgm_deg_to_rad(50.0f), (float)x / (float)y, 0.5, 500.0);
-
-       glMatrixMode(GL_PROJECTION);
-       glLoadMatrixf(proj);
-       */
 
        if(x > tex_width || y > tex_height) {
                tex_width = nextpow2(x);
@@ -285,8 +275,8 @@ static void motion(int x, int y)
        if(!(dx | dy)) return;
 
        if(bnstate[0]) {
-               cam_theta += dx * 0.01;
-               cam_phi += dy * 0.01;
+               cam_theta -= dx * 0.01;
+               cam_phi -= dy * 0.01;
 
                if(cam_phi < -M_PI) cam_phi = -M_PI;
                if(cam_phi > M_PI) cam_phi = M_PI;