bbe64fd6738ea7b008f1cf82fd3287845335b5ef
[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 #include "lut.h"
10
11 enum {
12         INP_FWD         = 0x01,
13         INP_BACK        = 0x02,
14         INP_LEFT        = 0x04,
15         INP_RIGHT       = 0x08,
16         INP_LTURN       = 0x10,
17         INP_RTURN       = 0x20
18 };
19
20 int init(void);
21 void cleanup(void);
22 void display(void);
23 void idle(void);
24 void reshape(int x, int y);
25 void keyb(unsigned char key, int x, int y);
26 void keyb_up(unsigned char key, int x, int y);
27 void mouse(int bn, int st, int x, int y);
28 void motion(int x, int y);
29
30 int win_width, win_height;
31
32 #define FB_W    640
33 #define FB_H    480
34 unsigned int fb[FB_W * FB_H];
35
36 int mouse_x, mouse_y, mwarp, mbstate[3];
37 int hfilt = VOX_LINEAR, cfilt = VOX_LINEAR;
38
39 unsigned int input;
40 int32_t pos[2], theta, phi;
41 int horizon;
42
43 struct voxscape *vox;
44
45 #define COLOR_HORIZON   0xcc77ff
46 #define COLOR_ZENITH    0x5588cc
47
48
49 int main(int argc, char **argv)
50 {
51         glutInit(&argc, argv);
52         glutInitWindowSize(1280, 960);
53         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
54         glutCreateWindow("voxel landscape test");
55
56         glutDisplayFunc(display);
57         glutIdleFunc(idle);
58         glutReshapeFunc(reshape);
59         glutKeyboardFunc(keyb);
60         glutKeyboardUpFunc(keyb_up);
61         glutMouseFunc(mouse);
62         glutMotionFunc(motion);
63
64         if(init() == -1) {
65                 return 1;
66         }
67         atexit(cleanup);
68
69         glutMainLoop();
70         return 0;
71 }
72
73
74 int init(void)
75 {
76         init_lut();
77
78         pos[0] = 512 << 16;
79         pos[1] = 512 << 16;
80
81         if(!(vox = vox_open("data/height.png", "data/color.png"))) {
82                 return -1;
83         }
84         vox_framebuf(vox, FB_W, FB_H, fb);
85         vox_proj(vox, 140, 45, 1, 300);
86         vox_fog(vox, 260, COLOR_HORIZON);
87         vox_filter(vox, hfilt, cfilt);
88
89         glfb_setup(FB_W, FB_H, GLFB_RGBA32, FB_W * 4);
90         return 0;
91 }
92
93 void cleanup(void)
94 {
95         vox_free(vox);
96 }
97
98 #define WALK_SPEED      0x40000
99 #define TURN_SPEED      0x100
100
101 void update(void)
102 {
103         int32_t fwd[2], right[2];
104
105         if(input & INP_LTURN) theta += TURN_SPEED;
106         if(input & INP_RTURN) theta -= TURN_SPEED;
107
108         fwd[0] = -SIN(theta);
109         fwd[1] = COS(theta);
110         right[0] = fwd[1];
111         right[1] = -fwd[0];
112
113         if(input & INP_FWD) {
114                 pos[0] += fwd[0];
115                 pos[1] += fwd[1];
116         }
117         if(input & INP_BACK) {
118                 pos[0] -= fwd[0];
119                 pos[1] -= fwd[1];
120         }
121         if(input & INP_RIGHT) {
122                 pos[0] += right[0];
123                 pos[1] += right[1];
124         }
125         if(input & INP_LEFT) {
126                 pos[0] -= right[0];
127                 pos[1] -= right[1];
128         }
129
130         vox_view(vox, pos[0], pos[1], -30, theta, phi);
131 }
132
133 void display(void)
134 {
135         update();
136
137         vox_render(vox);
138         vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
139
140         glfb_update(fb);
141         glfb_display();
142
143         glutSwapBuffers();
144         assert(glGetError() == GL_NO_ERROR);
145
146         if(mbstate[0]) {
147                 mwarp = 1;
148                 glutWarpPointer(win_width / 2, win_height / 2);
149         }
150 }
151
152 void idle(void)
153 {
154         glutPostRedisplay();
155 }
156
157 void reshape(int x, int y)
158 {
159         glViewport(0, 0, x, y);
160
161         win_width = x;
162         win_height = y;
163 }
164
165 void keyb(unsigned char key, int x, int y)
166 {
167         switch(key) {
168         case 27:
169                 exit(0);
170
171         case 'w':
172                 input |= INP_FWD;
173                 break;
174         case 's':
175                 input |= INP_BACK;
176                 break;
177         case 'a':
178                 input |= INP_LEFT;
179                 break;
180         case 'd':
181                 input |= INP_RIGHT;
182                 break;
183         case 'q':
184                 input |= INP_LTURN;
185                 break;
186         case 'e':
187                 input |= INP_RTURN;
188                 break;
189
190         case 'h':
191                 hfilt ^= 1;
192                 printf("filtering: height(%s) color(%s)\n", hfilt ? "linear" : "nearest",
193                                 cfilt ? "linear" : "nearest");
194                 vox_filter(vox, hfilt, cfilt);
195                 break;
196
197         case 'c':
198                 cfilt ^= 1;
199                 vox_filter(vox, hfilt, cfilt);
200                 printf("filtering: height(%s) color(%s)\n", hfilt ? "linear" : "nearest",
201                                 cfilt ? "linear" : "nearest");
202                 break;
203
204         default:
205                 break;
206         }
207 }
208
209 void keyb_up(unsigned char key, int x, int y)
210 {
211         switch(key) {
212         case 'w':
213                 input &= ~INP_FWD;
214                 break;
215         case 's':
216                 input &= ~INP_BACK;
217                 break;
218         case 'a':
219                 input &= ~INP_LEFT;
220                 break;
221         case 'd':
222                 input &= ~INP_RIGHT;
223                 break;
224         case 'q':
225                 input &= ~INP_LTURN;
226                 break;
227         case 'e':
228                 input &= ~INP_RTURN;
229                 break;
230
231         default:
232                 break;
233         }
234 }
235
236 void mouse(int bn, int st, int x, int y)
237 {
238         int bidx = bn - GLUT_LEFT_BUTTON;
239
240         if(bidx < 3) {
241                 mbstate[bidx] = st == GLUT_DOWN;
242         }
243         mouse_x = x;
244         mouse_y = y;
245
246         if(st == GLUT_DOWN) {
247                 glutSetCursor(GLUT_CURSOR_NONE);
248         } else {
249                 glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
250         }
251 }
252
253 void motion(int x, int y)
254 {
255         int dx = x - mouse_x;
256         int dy = y - mouse_y;
257         mouse_x = x;
258         mouse_y = y;
259
260         if(mwarp) {
261                 mwarp = 0;
262                 return;
263         }
264         if(!(dx | dy)) return;
265
266         if(mbstate[0]) {
267                 theta -= dx << 6;
268                 phi += dy << 8;
269                 if(phi < -0x18000) phi = -0x18000;
270                 if(phi > 0x18000) phi = 0x18000;
271         }
272 }