rayfract
view src/rayfract.cc @ 10:1496aae2e7d4
- simplified build by including dependences in the source tree
- added make dep tracking
- added mingw cross-build rules
- added readme & licence
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 31 Jul 2023 18:58:56 +0300 |
parents | 628e7084a482 |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include "GL/glew.h"
7 #include <GL/glut.h>
8 #include "sdr.h"
9 #include "gui.h"
10 #include "vmath.h"
12 #define DEG_TO_RAD(x) (M_PI * (x) / 180.0)
14 void disp();
15 void render();
16 void idle();
17 void reshape(int x, int y);
18 void keyb(unsigned char key, int x, int y);
19 void keyb_up(unsigned char key, int x, int y);
20 void mouse(int bn, int state, int x, int y);
21 void motion(int x, int y);
22 void passive_motion(int x, int y);
23 void sball_motion(int x, int y, int z);
24 void sball_rot(int x, int y, int z);
25 void sball_button(int bn, int state);
28 int load_shader();
29 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale = 0);
30 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
31 static int round_pow2(int x);
33 int parse_opt(int argc, char **argv);
36 float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
37 float cam_x, cam_y, cam_z;
39 unsigned int sdr;
40 unsigned int ray_tex;
41 Vector2 tex_scale;
42 Vector4 seed;
43 float err_thres = 0.005;
44 int iter = 10;
45 float reflectivity = 0.2;
46 Vector3 color(0.75, 0.75, 0.75);
48 int use_stereo, benchmark_mode;
49 float eye_sep = 0.5;
51 int main(int argc, char **argv)
52 {
53 int xsz, ysz;
55 seed = Vector4(0.4, 0.0, 0.0, -0.8);
57 glutInitWindowSize(640, 480);
58 glutInit(&argc, argv);
60 if(parse_opt(argc, argv) == -1) {
61 return 1;
62 }
64 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | (use_stereo ? GLUT_STEREO : 0));
65 glutCreateWindow("Raytraced Fractals");
66 xsz = glutGet(GLUT_WINDOW_WIDTH);
67 ysz = glutGet(GLUT_WINDOW_HEIGHT);
69 glutDisplayFunc(disp);
70 glutReshapeFunc(reshape);
71 glutKeyboardFunc(keyb);
72 glutKeyboardUpFunc(keyb_up);
73 glutMouseFunc(mouse);
74 glutMotionFunc(motion);
75 glutPassiveMotionFunc(passive_motion);
76 glutSpaceballMotionFunc(sball_motion);
77 glutSpaceballRotateFunc(sball_rot);
78 glutSpaceballButtonFunc(sball_button);
79 if(benchmark_mode) {
80 glutIdleFunc(idle);
81 }
83 glewInit();
85 glEnable(GL_CULL_FACE);
87 if(load_shader() == -1) {
88 return 1;
89 }
91 if(gui_init(xsz, ysz) == -1) {
92 return 1;
93 }
95 glutMainLoop();
96 return 0;
97 }
99 int load_shader()
100 {
101 if(sdr) {
102 free_program(sdr);
103 }
105 if(!(sdr = create_program_load("sdr/sdr.v.glsl", "sdr/julia.p.glsl"))) {
106 return -1;
107 }
108 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
109 set_uniform_float(sdr, "err_thres", err_thres);
110 set_uniform_int(sdr, "iter", iter);
111 set_uniform_float(sdr, "reflectivity", reflectivity);
112 set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
114 return 0;
115 }
117 void disp()
118 {
119 glMatrixMode(GL_MODELVIEW);
120 glLoadIdentity();
121 glTranslatef(cam_x, cam_y, -cam_z);
122 glRotatef(cam_theta, 0, 1, 0);
123 glRotatef(cam_phi, 1, 0, 0);
124 glTranslatef(0, 0, -cam_dist);
126 float lpos[] = {-1, 1, 3, 0};
127 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
130 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
131 set_uniform_float(sdr, "reflectivity", reflectivity);
132 set_uniform_int(sdr, "iter", iter);
133 set_uniform_float(sdr, "err_thres", err_thres);
134 set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
136 glBindTexture(GL_TEXTURE_2D, ray_tex);
139 if(use_stereo) {
140 glDrawBuffer(GL_BACK_LEFT);
141 set_uniform_float(sdr, "eye_offs", -eye_sep / 2.0);
142 } else {
143 set_uniform_float(sdr, "eye_offs", 0);
144 }
146 render();
148 if(use_stereo) {
149 glDrawBuffer(GL_BACK_RIGHT);
151 set_uniform_float(sdr, "eye_offs", eye_sep / 2.0);
152 render();
153 }
155 glutSwapBuffers();
156 assert(glGetError() == GL_NO_ERROR);
157 }
159 void idle()
160 {
161 glutPostRedisplay();
162 }
164 void render()
165 {
166 glMatrixMode(GL_TEXTURE);
167 glPushMatrix();
168 glScalef(tex_scale.x, tex_scale.y, 1.0);
170 glEnable(GL_TEXTURE_2D);
171 bind_program(sdr);
173 glBegin(GL_QUADS);
174 glColor3f(1, 1, 1);
175 glTexCoord2f(0, 1); glVertex2f(-1, -1);
176 glTexCoord2f(1, 1); glVertex2f(1, -1);
177 glTexCoord2f(1, 0); glVertex2f(1, 1);
178 glTexCoord2f(0, 0); glVertex2f(-1, 1);
179 glEnd();
181 bind_program(0);
182 glDisable(GL_TEXTURE_2D);
184 glMatrixMode(GL_TEXTURE);
185 glPopMatrix();
187 gui_draw();
188 }
190 void reshape(int x, int y)
191 {
192 glViewport(0, 0, x, y);
193 glMatrixMode(GL_PROJECTION);
194 glLoadIdentity();
195 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
197 imtk_set_viewport(x, y);
199 if(ray_tex) {
200 glDeleteTextures(1, &ray_tex);
201 }
202 ray_tex = create_ray_texture(x, y, 50.0, &tex_scale);
203 }
206 void keyb(unsigned char key, int x, int y)
207 {
208 switch(key) {
209 case 27:
210 exit(0);
212 case '-':
213 if(iter > 1) {
214 iter--;
215 set_uniform_int(sdr, "iter", iter);
216 printf("iter: %d\n", iter);
217 glutPostRedisplay();
218 }
219 break;
221 case '=':
222 iter++;
223 set_uniform_int(sdr, "iter", iter);
224 printf("iter: %d\n", iter);
225 glutPostRedisplay();
226 break;
228 case ',':
229 err_thres -= 0.001;
230 set_uniform_float(sdr, "err_thres", err_thres);
231 printf("maximum error: %f\n", err_thres);
232 glutPostRedisplay();
233 break;
235 case '.':
236 err_thres += 0.001;
237 set_uniform_float(sdr, "err_thres", err_thres);
238 printf("maximum error: %f\n", err_thres);
239 glutPostRedisplay();
240 break;
242 case 's':
243 load_shader();
244 glutPostRedisplay();
245 break;
247 case 'S':
248 use_stereo = !use_stereo;
249 glutPostRedisplay();
250 break;
252 case 'r':
253 reflectivity = reflectivity > 0.0 ? 0.0 : 0.6;
254 set_uniform_float(sdr, "reflectivity", reflectivity);
255 glutPostRedisplay();
256 break;
258 case '`':
259 {
260 static bool vis = true;
261 vis = !vis;
262 gui_set_visible(vis);
263 glutPostRedisplay();
264 }
265 break;
267 case '[':
268 eye_sep -= 0.1;
269 printf("eye separation: %f\n", eye_sep);
270 glutPostRedisplay();
271 break;
273 case ']':
274 eye_sep += 0.1;
275 printf("eye separation: %f\n", eye_sep);
276 glutPostRedisplay();
277 break;
280 case '\n':
281 case '\r':
282 printf("(%.3f %+.3fi %+.3fj %+.3fk) i:%d err: %.4f cam(theta: %.2f phi: %.2f rad: %.2f)\n", seed.w,
283 seed.x, seed.y, seed.z, iter, err_thres, cam_theta, cam_phi, cam_dist);
284 break;
285 }
287 imtk_inp_key(key, 1);
288 glutPostRedisplay();
289 }
291 void keyb_up(unsigned char key, int x, int y)
292 {
293 imtk_inp_key(key, 0);
294 glutPostRedisplay();
295 }
297 int bnstate[16];
299 int prev_x = -1, prev_y;
300 void mouse(int bn, int state, int x, int y)
301 {
302 int mod = glutGetModifiers();
304 if(mod) {
305 bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
306 if(state == GLUT_DOWN) {
307 if(bn == 3) {
308 cam_dist -= 0.1;
309 glutPostRedisplay();
310 if(cam_dist < 0) cam_dist = 0;
311 } else if(bn == 4) {
312 cam_dist += 0.1;
313 glutPostRedisplay();
314 } else {
315 prev_x = x;
316 prev_y = y;
317 }
318 } else {
319 prev_x = -1;
320 }
321 } else {
322 imtk_inp_mouse(bn, state == GLUT_DOWN ? 1 : 0);
323 }
324 glutPostRedisplay();
325 }
327 void motion(int x, int y)
328 {
329 if(bnstate[0]) {
330 cam_theta += (x - prev_x) * 0.5;
331 cam_phi += (y - prev_y) * 0.5;
333 if(cam_phi < -90) cam_phi = -90;
334 if(cam_phi > 90) cam_phi = 90;
336 glutPostRedisplay();
337 }
339 if(bnstate[1]) {
340 cam_y += (y - prev_y) * 0.1;
341 glutPostRedisplay();
342 }
344 if(bnstate[2]) {
345 cam_dist += (y - prev_y) * 0.1;
346 if(cam_dist < 0.0) cam_dist = 0.0;
347 glutPostRedisplay();
348 }
350 prev_x = x;
351 prev_y = y;
353 imtk_inp_motion(x, y);
354 glutPostRedisplay();
355 }
357 void passive_motion(int x, int y)
358 {
359 imtk_inp_motion(x, y);
360 glutPostRedisplay();
361 }
363 void sball_motion(int x, int y, int z)
364 {
365 float dx = (float)x * 0.0015f;
366 float dy = (float)y * 0.0015f;
367 float dz = -(float)z * 0.001f;
368 float angle = -DEG_TO_RAD(cam_theta);
370 cam_x += cos(angle) * dx + sin(angle) * dz;
371 cam_z += -sin(angle) * dx + cos(angle) * dz;
372 cam_y += dy;
374 glutPostRedisplay();
375 }
377 void sball_rot(int x, int y, int z)
378 {
379 cam_theta += -y / 15.0;
380 cam_phi += -x / 15.0;
381 glutPostRedisplay();
382 }
384 void sball_button(int bn, int state)
385 {
386 if(state == GLUT_DOWN) {
387 switch(bn) {
388 case 0:
389 /* TODO reset */
390 break;
392 default:
393 break;
394 }
395 }
396 }
399 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
400 {
401 unsigned int tex;
402 int tex_xsz = round_pow2(xsz);
403 int tex_ysz = round_pow2(ysz);
404 float *teximg, *dir;
406 teximg = new float[3 * tex_xsz * tex_ysz];
407 dir = teximg;
409 for(int i=0; i<tex_ysz; i++) {
410 for(int j=0; j<tex_xsz; j++) {
411 if(j < xsz && i < ysz) {
412 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
413 dir[0] = rdir.x;
414 dir[1] = rdir.y;
415 dir[2] = rdir.z;
416 } else {
417 dir[0] = dir[1] = 0.0f;
418 dir[2] = 1.0f;
419 }
421 dir += 3;
422 }
423 }
425 glGenTextures(1, &tex);
426 glBindTexture(GL_TEXTURE_2D, tex);
427 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
428 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
429 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
430 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
431 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, teximg);
432 delete [] teximg;
434 if(tex_scale) {
435 tex_scale->x = (float)xsz / (float)tex_xsz;
436 tex_scale->y = (float)ysz / (float)tex_ysz;
437 }
438 return tex;
439 }
441 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
442 {
443 float vfov = M_PI * vfov_deg / 180.0;
444 float aspect = (float)w / (float)h;
446 float ysz = 2.0;
447 float xsz = aspect * ysz;
449 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
450 float py = 1.0 - ((float)y / (float)h) * ysz;
451 float pz = 1.0 / tan(0.5 * vfov);
453 float mag = sqrt(px * px + py * py + pz * pz);
455 return Vector3(px / mag, py / mag, pz / mag);
456 }
458 static int round_pow2(int x)
459 {
460 x--;
461 x = (x >> 1) | x;
462 x = (x >> 2) | x;
463 x = (x >> 4) | x;
464 x = (x >> 8) | x;
465 x = (x >> 16) | x;
466 return x + 1;
467 }
469 int parse_opt(int argc, char **argv)
470 {
471 int i;
473 for(i=1; i<argc; i++) {
474 if(argv[i][0] == '-' && argv[i][2] == 0) {
475 switch(argv[i][1]) {
476 case 's':
477 use_stereo = !use_stereo;
478 break;
480 case 'b':
481 benchmark_mode = 1;
482 break;
484 default:
485 goto invalid;
486 }
487 } else {
488 goto invalid;
489 }
490 }
492 return 0;
494 invalid:
495 fprintf(stderr, "invalid option: %s\n", argv[i]);
496 return -1;
497 }