porting back some changes from the GBA version
[voxscape] / src / main.c
index f690a0e..bbe64fd 100644 (file)
@@ -24,6 +24,8 @@ void idle(void);
 void reshape(int x, int y);
 void keyb(unsigned char key, int x, int y);
 void keyb_up(unsigned char key, int x, int y);
+void mouse(int bn, int st, int x, int y);
+void motion(int x, int y);
 
 int win_width, win_height;
 
@@ -31,11 +33,18 @@ int win_width, win_height;
 #define FB_H   480
 unsigned int fb[FB_W * FB_H];
 
+int mouse_x, mouse_y, mwarp, mbstate[3];
+int hfilt = VOX_LINEAR, cfilt = VOX_LINEAR;
+
 unsigned int input;
-int32_t pos[2], angle;// = 0x4000;
+int32_t pos[2], theta, phi;
+int horizon;
 
 struct voxscape *vox;
 
+#define COLOR_HORIZON  0xcc77ff
+#define COLOR_ZENITH   0x5588cc
+
 
 int main(int argc, char **argv)
 {
@@ -45,10 +54,12 @@ int main(int argc, char **argv)
        glutCreateWindow("voxel landscape test");
 
        glutDisplayFunc(display);
+       glutIdleFunc(idle);
        glutReshapeFunc(reshape);
        glutKeyboardFunc(keyb);
        glutKeyboardUpFunc(keyb_up);
-       glutIdleFunc(idle);
+       glutMouseFunc(mouse);
+       glutMotionFunc(motion);
 
        if(init() == -1) {
                return 1;
@@ -71,8 +82,9 @@ int init(void)
                return -1;
        }
        vox_framebuf(vox, FB_W, FB_H, fb);
-       vox_proj(vox, 45, 1, 200);
-       vox_view(vox, pos[0], pos[1], angle);
+       vox_proj(vox, 140, 45, 1, 300);
+       vox_fog(vox, 260, COLOR_HORIZON);
+       vox_filter(vox, hfilt, cfilt);
 
        glfb_setup(FB_W, FB_H, GLFB_RGBA32, FB_W * 4);
        return 0;
@@ -88,31 +100,53 @@ void cleanup(void)
 
 void update(void)
 {
-       if(input & INP_FWD) pos[1] += WALK_SPEED;
-       if(input & INP_BACK) pos[1] -= WALK_SPEED;
-       if(input & INP_LEFT) pos[0] -= WALK_SPEED;
-       if(input & INP_RIGHT) pos[0] += WALK_SPEED;
+       int32_t fwd[2], right[2];
 
-       if(input & INP_LTURN) angle += TURN_SPEED;
-       if(input & INP_RTURN) angle -= TURN_SPEED;
+       if(input & INP_LTURN) theta += TURN_SPEED;
+       if(input & INP_RTURN) theta -= TURN_SPEED;
 
-       vox_view(vox, pos[0], pos[1], angle);
+       fwd[0] = -SIN(theta);
+       fwd[1] = COS(theta);
+       right[0] = fwd[1];
+       right[1] = -fwd[0];
+
+       if(input & INP_FWD) {
+               pos[0] += fwd[0];
+               pos[1] += fwd[1];
+       }
+       if(input & INP_BACK) {
+               pos[0] -= fwd[0];
+               pos[1] -= fwd[1];
+       }
+       if(input & INP_RIGHT) {
+               pos[0] += right[0];
+               pos[1] += right[1];
+       }
+       if(input & INP_LEFT) {
+               pos[0] -= right[0];
+               pos[1] -= right[1];
+       }
+
+       vox_view(vox, pos[0], pos[1], -30, theta, phi);
 }
 
 void display(void)
 {
        update();
 
-       memset(fb, 0, sizeof fb);
-
        vox_render(vox);
-       vox_sky_grad(vox, 0xcc77ff, 0x5588cc);
+       vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
 
        glfb_update(fb);
        glfb_display();
 
        glutSwapBuffers();
        assert(glGetError() == GL_NO_ERROR);
+
+       if(mbstate[0]) {
+               mwarp = 1;
+               glutWarpPointer(win_width / 2, win_height / 2);
+       }
 }
 
 void idle(void)
@@ -153,6 +187,20 @@ void keyb(unsigned char key, int x, int y)
                input |= INP_RTURN;
                break;
 
+       case 'h':
+               hfilt ^= 1;
+               printf("filtering: height(%s) color(%s)\n", hfilt ? "linear" : "nearest",
+                               cfilt ? "linear" : "nearest");
+               vox_filter(vox, hfilt, cfilt);
+               break;
+
+       case 'c':
+               cfilt ^= 1;
+               vox_filter(vox, hfilt, cfilt);
+               printf("filtering: height(%s) color(%s)\n", hfilt ? "linear" : "nearest",
+                               cfilt ? "linear" : "nearest");
+               break;
+
        default:
                break;
        }
@@ -184,3 +232,41 @@ void keyb_up(unsigned char key, int x, int y)
                break;
        }
 }
+
+void mouse(int bn, int st, int x, int y)
+{
+       int bidx = bn - GLUT_LEFT_BUTTON;
+
+       if(bidx < 3) {
+               mbstate[bidx] = st == GLUT_DOWN;
+       }
+       mouse_x = x;
+       mouse_y = y;
+
+       if(st == GLUT_DOWN) {
+               glutSetCursor(GLUT_CURSOR_NONE);
+       } else {
+               glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
+       }
+}
+
+void motion(int x, int y)
+{
+       int dx = x - mouse_x;
+       int dy = y - mouse_y;
+       mouse_x = x;
+       mouse_y = y;
+
+       if(mwarp) {
+               mwarp = 0;
+               return;
+       }
+       if(!(dx | dy)) return;
+
+       if(mbstate[0]) {
+               theta -= dx << 6;
+               phi += dy << 8;
+               if(phi < -0x18000) phi = -0x18000;
+               if(phi > 0x18000) phi = 0x18000;
+       }
+}