primary rays without jitter
[cyberay] / src / main.c
index 1f4d3ac..d49c311 100644 (file)
@@ -1,9 +1,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
-#include <GL/glut.h>
 #include <cgmath/cgmath.h>
-#include "mesh.h"
+#include "miniglut.h"
+#include "level.h"
+#include "rt.h"
 
 enum {
        KEY_F1          = GLUT_KEY_F1 | 0x100,
@@ -42,13 +43,12 @@ 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 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 float pxform[16];
 
 static int mouse_x, mouse_y;
 static int bnstate[8];
@@ -63,7 +63,13 @@ static int keymap[NUM_INPUTS][2] = {
        {' ', 0}
 };
 
-static struct scenefile scn;
+static struct level lvl;
+
+static unsigned int tex;
+static int tex_width, tex_height;
+static int tex_intfmt;
+static float tex_xform[16];
+
 
 int main(int argc, char **argv)
 {
@@ -94,13 +100,27 @@ int main(int argc, char **argv)
 
 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);
+       */
 
-       if(load_scenefile(&scn, "data/testlvl.obj") == -1) {
+       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;
        }
 
@@ -110,7 +130,11 @@ static int init(void)
 
 static void cleanup(void)
 {
-       destroy_scenefile(&scn);
+       destroy_level(&lvl);
+
+       glDeleteTextures(1, &tex);
+
+       tpool_destroy(tpool);
 }
 
 #define WALK_SPEED 3.0f
@@ -142,35 +166,40 @@ static void update(void)
        cam_pos.x += cos(cam_theta) * vright + sin(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_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);
 }
 
 static void display(void)
 {
-       struct mesh *mesh;
-
        update();
 
+       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, 0);
+       glVertex2f(-1, -1);
+       glTexCoord2f(1, 0);
+       glVertex2f(1, -1);
+       glTexCoord2f(1, 1);
+       glVertex2f(1, 1);
+       glTexCoord2f(0, 1);
+       glVertex2f(-1, 1);
+       glEnd();
+
+       /*
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        glMatrixMode(GL_MODELVIEW);
-       glLoadMatrixf(pxform);
-
-       mesh = scn.meshlist;
-       while(mesh) {
-               float col[4];
-               col[0] = mesh->mtl.color.x;
-               col[1] = mesh->mtl.color.y;
-               col[2] = mesh->mtl.color.z;
-               col[3] = 1.0f;
-               glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
-
-               draw_mesh(mesh);
-               mesh = mesh->next;
-       }
+       glLoadMatrixf(view_xform);
+
+       draw_level(&lvl);
+       */
 
        glutSwapBuffers();
        assert(glGetError() == GL_NO_ERROR);
@@ -183,9 +212,28 @@ 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);
-       glLoadIdentity();
-       gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
+       glLoadMatrixf(proj);
+       */
+
+       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)
@@ -244,3 +292,14 @@ static void motion(int x, int y)
                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;
+}