#include <assert.h>
#include <GL/glut.h>
#include "glfb.h"
+#include "voxscape.h"
+#include "lut.h"
+
+enum {
+ INP_FWD = 0x01,
+ INP_BACK = 0x02,
+ INP_LEFT = 0x04,
+ INP_RIGHT = 0x08,
+ INP_LTURN = 0x10,
+ INP_RTURN = 0x20
+};
int init(void);
+void cleanup(void);
void display(void);
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;
#define FB_H 480
unsigned int fb[FB_W * FB_H];
+int mouse_x, mouse_y, mwarp, mbstate[3];
+
+unsigned int input;
+int32_t pos[2], angle;
+
+struct voxscape *vox;
+
int main(int argc, char **argv)
{
glutCreateWindow("voxel landscape test");
glutDisplayFunc(display);
+ glutIdleFunc(idle);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyb);
- glutIdleFunc(idle);
+ glutKeyboardUpFunc(keyb_up);
+ glutMouseFunc(mouse);
+ glutMotionFunc(motion);
if(init() == -1) {
return 1;
}
+ atexit(cleanup);
glutMainLoop();
return 0;
int init(void)
{
- int i, j, xor, r, g, b;
- unsigned int *ptr;
-
- ptr = fb;
- for(i=0; i<FB_H; i++) {
- for(j=0; j<FB_W; j++) {
- xor = i ^ j;
- r = (xor >> 1) & 0xff;
- g = xor & 0xff;
- b = (xor << 1) & 0xff;
- *ptr++ = b | (g << 8) | (r << 16);
- }
- }
+ init_lut();
- win_width = glutGet(GLUT_WINDOW_WIDTH);
- win_height = glutGet(GLUT_WINDOW_HEIGHT);
+ pos[0] = 512 << 16;
+ pos[1] = 512 << 16;
+
+ if(!(vox = vox_open("data/height.png", "data/color.png"))) {
+ return -1;
+ }
+ vox_framebuf(vox, FB_W, FB_H, fb);
+ vox_proj(vox, 45, 1, 300);
glfb_setup(FB_W, FB_H, GLFB_RGBA32, FB_W * 4);
return 0;
}
+void cleanup(void)
+{
+ vox_free(vox);
+}
+
+#define WALK_SPEED 0x40000
+#define TURN_SPEED 0x100
+
+void update(void)
+{
+ int32_t fwd[2], right[2];
+
+ if(input & INP_LTURN) angle += TURN_SPEED;
+ if(input & INP_RTURN) angle -= TURN_SPEED;
+
+ fwd[0] = -SIN(angle);
+ fwd[1] = COS(angle);
+ 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, angle);
+}
+
void display(void)
{
+ update();
+
+ memset(fb, 0, sizeof fb);
+
+ vox_render(vox);
+ vox_sky_grad(vox, 0xcc77ff, 0x5588cc);
+
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)
case 27:
exit(0);
+ case 'w':
+ input |= INP_FWD;
+ break;
+ case 's':
+ input |= INP_BACK;
+ break;
+ case 'a':
+ input |= INP_LEFT;
+ break;
+ case 'd':
+ input |= INP_RIGHT;
+ break;
+ case 'q':
+ input |= INP_LTURN;
+ break;
+ case 'e':
+ input |= INP_RTURN;
+ break;
+
default:
break;
}
}
+
+void keyb_up(unsigned char key, int x, int y)
+{
+ switch(key) {
+ case 'w':
+ input &= ~INP_FWD;
+ break;
+ case 's':
+ input &= ~INP_BACK;
+ break;
+ case 'a':
+ input &= ~INP_LEFT;
+ break;
+ case 'd':
+ input &= ~INP_RIGHT;
+ break;
+ case 'q':
+ input &= ~INP_LTURN;
+ break;
+ case 'e':
+ input &= ~INP_RTURN;
+ break;
+
+ default:
+ 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]) {
+ angle -= dx << 6;
+ }
+}