foo
[voxscape] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include <GL/glut.h>
7 #include "glfb.h"
8 #include "voxscape.h"
9
10 int init(void);
11 void cleanup(void);
12 void display(void);
13 void idle(void);
14 void reshape(int x, int y);
15 void keyb(unsigned char key, int x, int y);
16
17 int win_width, win_height;
18
19 #define FB_W    640
20 #define FB_H    480
21 unsigned int fb[FB_W * FB_H];
22
23 struct voxscape *vox;
24
25
26 int main(int argc, char **argv)
27 {
28         glutInit(&argc, argv);
29         glutInitWindowSize(1280, 960);
30         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
31         glutCreateWindow("voxel landscape test");
32
33         glutDisplayFunc(display);
34         glutReshapeFunc(reshape);
35         glutKeyboardFunc(keyb);
36         glutIdleFunc(idle);
37
38         if(init() == -1) {
39                 return 1;
40         }
41         atexit(cleanup);
42
43         glutMainLoop();
44         return 0;
45 }
46
47
48 int init(void)
49 {
50         if(!(vox = vox_open("data/height.png", "data/color.png"))) {
51                 return -1;
52         }
53         vox_framebuf(vox, FB_W, FB_H, fb);
54         vox_proj(vox, 45, 5, 100);
55         vox_view(vox, 512 << 16, 512 << 16, 0);
56
57         glfb_setup(FB_W, FB_H, GLFB_RGBA32, FB_W * 4);
58         return 0;
59 }
60
61 void cleanup(void)
62 {
63         vox_free(vox);
64 }
65
66 void display(void)
67 {
68         vox_render(vox);
69
70         glfb_update(fb);
71         glfb_display();
72
73         glutSwapBuffers();
74         assert(glGetError() == GL_NO_ERROR);
75 }
76
77 void idle(void)
78 {
79         glutPostRedisplay();
80 }
81
82 void reshape(int x, int y)
83 {
84         glViewport(0, 0, x, y);
85
86         win_width = x;
87         win_height = y;
88 }
89
90 void keyb(unsigned char key, int x, int y)
91 {
92         switch(key) {
93         case 27:
94                 exit(0);
95
96         default:
97                 break;
98         }
99 }