implementing build_bvh_sah
[cyberay] / src / main.c
index 7ce59cb..b3b96cc 100644 (file)
@@ -1,8 +1,11 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
-#include <GL/glut.h>
 #include <cgmath/cgmath.h>
+#include "miniglut.h"
+#include "game.h"
+#include "level.h"
+#include "rt.h"
 
 enum {
        KEY_F1          = GLUT_KEY_F1 | 0x100,
@@ -41,13 +44,10 @@ static void skeydown(int key, int x, int y);
 static void skeyup(int key, int x, int y);
 static void mouse(int bn, int st, int x, int y);
 static void motion(int x, int y);
-
-
-static long start_time;
+static unsigned int nextpow2(unsigned int x);
 
 static float cam_theta, cam_phi;
-static cgm_vec3 cam_pos;
-static float pxform[16];
+static cgm_vec3 cam_pos = {0, 1.6, 0};
 
 static int mouse_x, mouse_y;
 static int bnstate[8];
@@ -62,11 +62,25 @@ static int keymap[NUM_INPUTS][2] = {
        {' ', 0}
 };
 
+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);
@@ -85,31 +99,54 @@ int main(int argc, char **argv)
        }
        atexit(cleanup);
 
+       start_time = glutGet(GLUT_ELAPSED_TIME);
+
        glutMainLoop();
        return 0;
 }
 
 static int init(void)
 {
+       if(!(tpool = tpool_create(0))) {
+               fprintf(stderr, "failed to create thread pool\n");
+               return -1;
+       }
+
        glEnable(GL_CULL_FACE);
 
-       glEnable(GL_DEPTH_TEST);
-       glEnable(GL_LIGHTING);
-       glEnable(GL_LIGHT0);
 
-       start_time = glutGet(GLUT_ELAPSED_TIME);
+       glGenTextures(1, &tex);
+       glBindTexture(GL_TEXTURE_2D, tex);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+       tex_intfmt = GL_RGB16F;
+
+       if(load_level(&lvl, "data/test.lvl") == -1) {
+               return -1;
+       }
        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);
+
+       tpool_destroy(tpool);
 }
 
-#define WALK_SPEED 1.0f
+#define WALK_SPEED 3.0f
 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;
@@ -125,36 +162,45 @@ 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(pxform);
-       cgm_mtranslate(pxform, cam_pos.x, cam_pos.y, cam_pos.z);
-       cgm_mrotate_y(pxform, cam_theta);
-       cgm_mrotate_x(pxform, cam_phi);
+       cgm_midentity(view_xform);
+       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)
 {
        update();
 
-       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-       glMatrixMode(GL_MODELVIEW);
-       glLoadMatrixf(pxform);
-
-       glFrontFace(GL_CW);
-       glutSolidTeapot(1.0);
-       glFrontFace(GL_CCW);
+       render();
+       glBindTexture(GL_TEXTURE_2D, tex);
+       glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, fb.width, fb.height, GL_RGB, GL_FLOAT, fb.pixels);
+       glEnable(GL_TEXTURE_2D);
+
+       glBegin(GL_QUADS);
+       glTexCoord2f(0, 1);
+       glVertex2f(-1, -1);
+       glTexCoord2f(1, 1);
+       glVertex2f(1, -1);
+       glTexCoord2f(1, 0);
+       glVertex2f(1, 1);
+       glTexCoord2f(0, 0);
+       glVertex2f(-1, 1);
+       glEnd();
 
        glutSwapBuffers();
        assert(glGetError() == GL_NO_ERROR);
+
+       nframes++;
 }
 
 static void idle(void)
@@ -164,9 +210,20 @@ static void idle(void)
 
 static void reshape(int x, int y)
 {
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
+       glViewport(0, 0, x, y);
+
+       if(x > tex_width || y > tex_height) {
+               tex_width = nextpow2(x);
+               tex_height = nextpow2(y);
+
+               glBindTexture(GL_TEXTURE_2D, tex);
+               glTexImage2D(GL_TEXTURE_2D, 0, tex_intfmt, tex_width, tex_height, 0, GL_RGB, GL_FLOAT, 0);
+       }
+       fbsize(x, y);
+       cgm_mscaling(tex_xform, (float)x / tex_width, (float)y / tex_height, 1.0f);
+
+       glMatrixMode(GL_TEXTURE);
+       glLoadMatrixf(tex_xform);
 }
 
 static void keyb(int key, int press)
@@ -218,10 +275,21 @@ 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;
        }
 }
+
+static unsigned int nextpow2(unsigned int x)
+{
+       x--;
+       x |= x >> 1;
+       x |= x >> 2;
+       x |= x >> 4;
+       x |= x >> 8;
+       x |= x >> 16;
+       return x + 1;
+}