primary rays without jitter
[cyberay] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <cgmath/cgmath.h>
5 #include "miniglut.h"
6 #include "level.h"
7 #include "rt.h"
8
9 enum {
10         KEY_F1          = GLUT_KEY_F1 | 0x100,
11         KEY_F2          = GLUT_KEY_F2 | 0x100,
12         KEY_F3          = GLUT_KEY_F3 | 0x100,
13         KEY_F4          = GLUT_KEY_F4 | 0x100,
14         KEY_F5          = GLUT_KEY_F5 | 0x100,
15         KEY_F6          = GLUT_KEY_F6 | 0x100,
16         KEY_F7          = GLUT_KEY_F7 | 0x100,
17         KEY_F8          = GLUT_KEY_F8 | 0x100,
18         KEY_F9          = GLUT_KEY_F9 | 0x100,
19         KEY_F10         = GLUT_KEY_F10 | 0x100,
20         KEY_F11         = GLUT_KEY_F11 | 0x100,
21         KEY_F12         = GLUT_KEY_F12 | 0x100,
22         KEY_LEFT        = GLUT_KEY_LEFT | 0x100,
23         KEY_UP          = GLUT_KEY_UP | 0x100,
24         KEY_RIGHT       = GLUT_KEY_RIGHT | 0x100,
25         KEY_DOWN        = GLUT_KEY_DOWN | 0x100,
26         KEY_PGUP        = GLUT_KEY_PAGE_UP | 0x100,
27         KEY_PGDN        = GLUT_KEY_PAGE_DOWN | 0x100,
28         KEY_HOME        = GLUT_KEY_HOME | 0x100,
29         KEY_END         = GLUT_KEY_END | 0x100,
30         KEY_INS         = GLUT_KEY_INSERT | 0x100
31 };
32
33 enum { INP_FWD, INP_BACK, INP_RIGHT, INP_LEFT, INP_FIRE, NUM_INPUTS };
34
35 static int init(void);
36 static void cleanup(void);
37 static void display(void);
38 static void idle(void);
39 static void reshape(int x, int y);
40 static void keydown(unsigned char key, int x, int y);
41 static void keyup(unsigned char key, int x, int y);
42 static void skeydown(int key, int x, int y);
43 static void skeyup(int key, int x, int y);
44 static void mouse(int bn, int st, int x, int y);
45 static void motion(int x, int y);
46 static unsigned int nextpow2(unsigned int x);
47
48 static long start_time;
49
50 static float cam_theta, cam_phi;
51 static cgm_vec3 cam_pos = {0, -1.6, 0};
52
53 static int mouse_x, mouse_y;
54 static int bnstate[8];
55
56 static int inpstate[NUM_INPUTS];
57
58 static int keymap[NUM_INPUTS][2] = {
59         {'w', KEY_UP},
60         {'s', KEY_DOWN},
61         {'d', KEY_RIGHT},
62         {'a', KEY_LEFT},
63         {' ', 0}
64 };
65
66 static struct level lvl;
67
68 static unsigned int tex;
69 static int tex_width, tex_height;
70 static int tex_intfmt;
71 static float tex_xform[16];
72
73
74 int main(int argc, char **argv)
75 {
76         glutInit(&argc, argv);
77         glutInitWindowSize(1280, 800);
78         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
79         glutCreateWindow("cyberay");
80
81         glutDisplayFunc(display);
82         glutIdleFunc(idle);
83         glutReshapeFunc(reshape);
84         glutKeyboardFunc(keydown);
85         glutKeyboardUpFunc(keyup);
86         glutSpecialFunc(skeydown);
87         glutSpecialUpFunc(skeyup);
88         glutMouseFunc(mouse);
89         glutMotionFunc(motion);
90         glutPassiveMotionFunc(motion);
91
92         if(init() == -1) {
93                 return 1;
94         }
95         atexit(cleanup);
96
97         glutMainLoop();
98         return 0;
99 }
100
101 static int init(void)
102 {
103         if(!(tpool = tpool_create(0))) {
104                 fprintf(stderr, "failed to create thread pool\n");
105                 return -1;
106         }
107
108         glEnable(GL_CULL_FACE);
109
110         /*
111         glEnable(GL_DEPTH_TEST);
112         glEnable(GL_LIGHTING);
113         glEnable(GL_LIGHT0);
114         */
115
116         glGenTextures(1, &tex);
117         glBindTexture(GL_TEXTURE_2D, tex);
118         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
119         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
120
121         tex_intfmt = GL_RGB16F;
122
123         if(load_level(&lvl, "data/test.lvl") == -1) {
124                 return -1;
125         }
126
127         start_time = glutGet(GLUT_ELAPSED_TIME);
128         return 0;
129 }
130
131 static void cleanup(void)
132 {
133         destroy_level(&lvl);
134
135         glDeleteTextures(1, &tex);
136
137         tpool_destroy(tpool);
138 }
139
140 #define WALK_SPEED 3.0f
141 static void update(void)
142 {
143         static unsigned int prev_upd;
144         unsigned int msec;
145         float dt, vfwd, vright;
146
147         msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
148         dt = (float)(msec - prev_upd) / 1000.0f;
149         prev_upd = msec;
150
151         vfwd = vright = 0;
152
153         if(inpstate[INP_FWD]) {
154                 vfwd -= WALK_SPEED * dt;
155         }
156         if(inpstate[INP_BACK]) {
157                 vfwd += WALK_SPEED * dt;
158         }
159         if(inpstate[INP_RIGHT]) {
160                 vright -= WALK_SPEED * dt;
161         }
162         if(inpstate[INP_LEFT]) {
163                 vright += WALK_SPEED * dt;
164         }
165
166         cam_pos.x += cos(cam_theta) * vright + sin(cam_theta) * vfwd;
167         cam_pos.z += sin(cam_theta) * vright - cos(cam_theta) * vfwd;
168
169         cgm_midentity(view_xform);
170         cgm_mtranslate(view_xform, cam_pos.x, cam_pos.y, cam_pos.z);
171         cgm_mrotate_y(view_xform, cam_theta);
172         cgm_mrotate_x(view_xform, cam_phi);
173 }
174
175 static void display(void)
176 {
177         update();
178
179         render();
180         glBindTexture(GL_TEXTURE_2D, tex);
181         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, fb.width, fb.height, GL_RGB, GL_FLOAT, fb.pixels);
182         glEnable(GL_TEXTURE_2D);
183
184         glBegin(GL_QUADS);
185         glTexCoord2f(0, 0);
186         glVertex2f(-1, -1);
187         glTexCoord2f(1, 0);
188         glVertex2f(1, -1);
189         glTexCoord2f(1, 1);
190         glVertex2f(1, 1);
191         glTexCoord2f(0, 1);
192         glVertex2f(-1, 1);
193         glEnd();
194
195         /*
196         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
197
198         glMatrixMode(GL_MODELVIEW);
199         glLoadMatrixf(view_xform);
200
201         draw_level(&lvl);
202         */
203
204         glutSwapBuffers();
205         assert(glGetError() == GL_NO_ERROR);
206 }
207
208 static void idle(void)
209 {
210         glutPostRedisplay();
211 }
212
213 static void reshape(int x, int y)
214 {
215         glViewport(0, 0, x, y);
216         /*
217         float proj[16];
218
219         cgm_mperspective(proj, cgm_deg_to_rad(50.0f), (float)x / (float)y, 0.5, 500.0);
220
221         glMatrixMode(GL_PROJECTION);
222         glLoadMatrixf(proj);
223         */
224
225         if(x > tex_width || y > tex_height) {
226                 tex_width = nextpow2(x);
227                 tex_height = nextpow2(y);
228
229                 glBindTexture(GL_TEXTURE_2D, tex);
230                 glTexImage2D(GL_TEXTURE_2D, 0, tex_intfmt, tex_width, tex_height, 0, GL_RGB, GL_FLOAT, 0);
231         }
232         fbsize(x, y);
233         cgm_mscaling(tex_xform, (float)x / tex_width, (float)y / tex_height, 1.0f);
234
235         glMatrixMode(GL_TEXTURE);
236         glLoadMatrixf(tex_xform);
237 }
238
239 static void keyb(int key, int press)
240 {
241         int i;
242
243         for(i=0; i<NUM_INPUTS; i++) {
244                 if(keymap[i][0] == key || keymap[i][1] == key) {
245                         inpstate[i] = press;
246                 }
247         }
248 }
249
250 static void keydown(unsigned char key, int x, int y)
251 {
252         if(key == 27) exit(0);
253         keyb(key, 1);
254 }
255
256 static void keyup(unsigned char key, int x, int y)
257 {
258         keyb(key, 0);
259 }
260
261 static void skeydown(int key, int x, int y)
262 {
263         keyb(key | 0x100, 1);
264 }
265
266 static void skeyup(int key, int x, int y)
267 {
268         keyb(key | 0x100, 0);
269 }
270
271 static void mouse(int bn, int st, int x, int y)
272 {
273         mouse_x = x;
274         mouse_y = y;
275         bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN ? 1 : 0;
276 }
277
278 static void motion(int x, int y)
279 {
280         int dx = x - mouse_x;
281         int dy = y - mouse_y;
282         mouse_x = x;
283         mouse_y = y;
284
285         if(!(dx | dy)) return;
286
287         if(bnstate[0]) {
288                 cam_theta += dx * 0.01;
289                 cam_phi += dy * 0.01;
290
291                 if(cam_phi < -M_PI) cam_phi = -M_PI;
292                 if(cam_phi > M_PI) cam_phi = M_PI;
293         }
294 }
295
296 static unsigned int nextpow2(unsigned int x)
297 {
298         x--;
299         x |= x >> 1;
300         x |= x >> 2;
301         x |= x >> 4;
302         x |= x >> 8;
303         x |= x >> 16;
304         return x + 1;
305 }