rayfract

view src/rayfract.cc @ 6:8a9aa21b32cf

added spaceball support through GLUT
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 18 Jun 2011 01:04:25 +0300
parents 48e0e7d33d9e
children dfe7c98cf373
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
5 #include <GL/glew.h>
6 #include <GL/glut.h>
7 #include "sdr.h"
8 #include "gui.h"
9 #include "vmath.h"
11 #define DEG_TO_RAD(x) (M_PI * (x) / 180.0)
13 void disp();
14 void reshape(int x, int y);
15 void keyb(unsigned char key, int x, int y);
16 void keyb_up(unsigned char key, int x, int y);
17 void mouse(int bn, int state, int x, int y);
18 void motion(int x, int y);
19 void passive_motion(int x, int y);
20 void sball_motion(int x, int y, int z);
21 void sball_rot(int x, int y, int z);
22 void sball_button(int bn, int state);
25 int load_shader();
26 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale = 0);
27 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
28 static int round_pow2(int x);
30 float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
31 float cam_x, cam_y, cam_z;
33 unsigned int sdr;
34 unsigned int ray_tex;
35 Vector2 tex_scale;
36 Vector4 seed;
37 float err_thres = 0.0075;
38 int iter = 10;
39 float reflectivity = 0.2;
40 Vector3 color(0.75, 0.75, 0.75);
42 int main(int argc, char **argv)
43 {
44 int xsz, ysz;
46 seed = Vector4(0.4, 0.0, 0.0, -0.8);
48 glutInitWindowSize(640, 480);
49 glutInit(&argc, argv);
50 glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
51 glutCreateWindow("Raytraced Fractals");
52 xsz = glutGet(GLUT_WINDOW_WIDTH);
53 ysz = glutGet(GLUT_WINDOW_HEIGHT);
55 glutDisplayFunc(disp);
56 glutReshapeFunc(reshape);
57 glutKeyboardFunc(keyb);
58 glutKeyboardUpFunc(keyb_up);
59 glutMouseFunc(mouse);
60 glutMotionFunc(motion);
61 glutPassiveMotionFunc(passive_motion);
62 glutSpaceballMotionFunc(sball_motion);
63 glutSpaceballRotateFunc(sball_rot);
64 glutSpaceballButtonFunc(sball_button);
66 glEnable(GL_DEPTH_TEST);
67 glEnable(GL_LIGHTING);
68 glEnable(GL_LIGHT0);
69 glEnable(GL_CULL_FACE);
71 glewInit();
73 if(load_shader() == -1) {
74 return 1;
75 }
77 if(gui_init(xsz, ysz) == -1) {
78 return 1;
79 }
81 glutMainLoop();
82 return 0;
83 }
85 int load_shader()
86 {
87 if(sdr) {
88 free_program(sdr);
89 }
91 if(!(sdr = create_program_load("sdr/sdr.v.glsl", "sdr/julia.p.glsl"))) {
92 return -1;
93 }
94 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
95 set_uniform_float(sdr, "err_thres", err_thres);
96 set_uniform_int(sdr, "iter", iter);
97 set_uniform_float(sdr, "reflectivity", reflectivity);
98 set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
100 return 0;
101 }
103 void disp()
104 {
105 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
107 glMatrixMode(GL_MODELVIEW);
108 glLoadIdentity();
109 glTranslatef(cam_x, cam_y, -cam_z);
110 glRotatef(cam_theta, 0, 1, 0);
111 glRotatef(cam_phi, 1, 0, 0);
112 glTranslatef(0, 0, -cam_dist);
114 float lpos[] = {-1, 1, 3, 0};
115 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
118 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
119 set_uniform_float(sdr, "reflectivity", reflectivity);
120 set_uniform_int(sdr, "iter", iter);
121 set_uniform_float(sdr, "err_thres", err_thres);
122 set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
124 glMatrixMode(GL_TEXTURE);
125 glPushMatrix();
126 glScalef(tex_scale.x, tex_scale.y, 1.0);
128 glBindTexture(GL_TEXTURE_2D, ray_tex);
129 glEnable(GL_TEXTURE_2D);
130 bind_program(sdr);
132 glBegin(GL_QUADS);
133 glColor3f(1, 1, 1);
134 glTexCoord2f(0, 1); glVertex2f(-1, -1);
135 glTexCoord2f(1, 1); glVertex2f(1, -1);
136 glTexCoord2f(1, 0); glVertex2f(1, 1);
137 glTexCoord2f(0, 0); glVertex2f(-1, 1);
138 glEnd();
140 bind_program(0);
141 glDisable(GL_TEXTURE_2D);
143 glMatrixMode(GL_TEXTURE);
144 glPopMatrix();
146 gui_draw();
148 glutSwapBuffers();
149 assert(glGetError() == GL_NO_ERROR);
150 }
152 void reshape(int x, int y)
153 {
154 glViewport(0, 0, x, y);
155 glMatrixMode(GL_PROJECTION);
156 glLoadIdentity();
157 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
159 imtk_set_viewport(x, y);
161 if(ray_tex) {
162 glDeleteTextures(1, &ray_tex);
163 }
164 ray_tex = create_ray_texture(x, y, 50.0, &tex_scale);
165 }
168 void keyb(unsigned char key, int x, int y)
169 {
170 switch(key) {
171 case 27:
172 exit(0);
174 case '-':
175 if(iter > 1) {
176 iter--;
177 set_uniform_int(sdr, "iter", iter);
178 printf("iter: %d\n", iter);
179 glutPostRedisplay();
180 }
181 break;
183 case '=':
184 iter++;
185 set_uniform_int(sdr, "iter", iter);
186 printf("iter: %d\n", iter);
187 glutPostRedisplay();
188 break;
190 case ',':
191 err_thres -= 0.001;
192 set_uniform_float(sdr, "err_thres", err_thres);
193 printf("maximum error: %f\n", err_thres);
194 glutPostRedisplay();
195 break;
197 case '.':
198 err_thres += 0.001;
199 set_uniform_float(sdr, "err_thres", err_thres);
200 printf("maximum error: %f\n", err_thres);
201 glutPostRedisplay();
202 break;
204 case 's':
205 load_shader();
206 glutPostRedisplay();
207 break;
209 case 'r':
210 reflectivity = reflectivity > 0.0 ? 0.0 : 0.6;
211 set_uniform_float(sdr, "reflectivity", reflectivity);
212 glutPostRedisplay();
213 break;
215 case '`':
216 {
217 static bool vis = true;
218 vis = !vis;
219 gui_set_visible(vis);
220 glutPostRedisplay();
221 }
222 break;
224 case '\n':
225 case '\r':
226 printf("(%.3f %+.3fi %+.3fj %+.3fk) i:%d err: %.4f cam(theta: %.2f phi: %.2f rad: %.2f)\n", seed.w,
227 seed.x, seed.y, seed.z, iter, err_thres, cam_theta, cam_phi, cam_dist);
228 break;
229 }
231 imtk_inp_key(key, 1);
232 glutPostRedisplay();
233 }
235 void keyb_up(unsigned char key, int x, int y)
236 {
237 imtk_inp_key(key, 0);
238 glutPostRedisplay();
239 }
241 int bnstate[16];
243 int prev_x = -1, prev_y;
244 void mouse(int bn, int state, int x, int y)
245 {
246 int mod;
248 mod = glutGetModifiers();
250 if(mod) {
251 bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
252 if(state == GLUT_DOWN) {
253 if(bn == 3) {
254 cam_dist -= 0.1;
255 glutPostRedisplay();
256 if(cam_dist < 0) cam_dist = 0;
257 } else if(bn == 4) {
258 cam_dist += 0.1;
259 glutPostRedisplay();
260 } else {
261 prev_x = x;
262 prev_y = y;
263 }
264 } else {
265 prev_x = -1;
266 }
267 } else {
268 imtk_inp_mouse(bn, state == GLUT_DOWN ? 1 : 0);
269 }
270 glutPostRedisplay();
271 }
273 void motion(int x, int y)
274 {
275 if(bnstate[0]) {
276 cam_theta += (x - prev_x) * 0.5;
277 cam_phi += (y - prev_y) * 0.5;
279 if(cam_phi < -90) cam_phi = -90;
280 if(cam_phi > 90) cam_phi = 90;
282 glutPostRedisplay();
283 }
285 if(bnstate[1]) {
286 cam_y += (y - prev_y) * 0.1;
287 glutPostRedisplay();
288 }
290 if(bnstate[2]) {
291 cam_dist += (y - prev_y) * 0.1;
292 if(cam_dist < 0.0) cam_dist = 0.0;
293 glutPostRedisplay();
294 }
296 prev_x = x;
297 prev_y = y;
299 imtk_inp_motion(x, y);
300 glutPostRedisplay();
301 }
303 void passive_motion(int x, int y)
304 {
305 imtk_inp_motion(x, y);
306 glutPostRedisplay();
307 }
309 void sball_motion(int x, int y, int z)
310 {
311 float dx = (float)x * 0.0015f;
312 float dy = (float)y * 0.0015f;
313 float dz = -(float)z * 0.001f;
314 float angle = -DEG_TO_RAD(cam_theta);
316 cam_x += cos(angle) * dx + sin(angle) * dz;
317 cam_z += -sin(angle) * dx + cos(angle) * dz;
318 cam_y += dy;
320 glutPostRedisplay();
321 }
323 void sball_rot(int x, int y, int z)
324 {
325 cam_theta += -y / 15.0;
326 cam_phi += -x / 15.0;
327 glutPostRedisplay();
328 }
330 void sball_button(int bn, int state)
331 {
332 if(state == GLUT_DOWN) {
333 switch(bn) {
334 case 0:
335 /* TODO reset */
336 break;
338 default:
339 break;
340 }
341 }
342 }
345 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
346 {
347 unsigned int tex;
348 int tex_xsz = round_pow2(xsz);
349 int tex_ysz = round_pow2(ysz);
350 float *teximg, *dir;
352 teximg = new float[3 * tex_xsz * tex_ysz];
353 dir = teximg;
355 for(int i=0; i<tex_ysz; i++) {
356 for(int j=0; j<tex_xsz; j++) {
357 if(j < xsz && i < ysz) {
358 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
359 dir[0] = rdir.x;
360 dir[1] = rdir.y;
361 dir[2] = rdir.z;
362 } else {
363 dir[0] = dir[1] = 0.0f;
364 dir[2] = 1.0f;
365 }
367 dir += 3;
368 }
369 }
371 glGenTextures(1, &tex);
372 glBindTexture(GL_TEXTURE_2D, tex);
373 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
374 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
375 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
376 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
377 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, teximg);
378 delete [] teximg;
380 if(tex_scale) {
381 tex_scale->x = (float)xsz / (float)tex_xsz;
382 tex_scale->y = (float)ysz / (float)tex_ysz;
383 }
384 return tex;
385 }
387 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
388 {
389 float vfov = M_PI * vfov_deg / 180.0;
390 float aspect = (float)w / (float)h;
392 float ysz = 2.0;
393 float xsz = aspect * ysz;
395 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
396 float py = 1.0 - ((float)y / (float)h) * ysz;
397 float pz = 1.0 / tan(0.5 * vfov);
399 float mag = sqrt(px * px + py * py + pz * pz);
401 return Vector3(px / mag, py / mag, pz / mag);
402 }
404 static int round_pow2(int x)
405 {
406 x--;
407 x = (x >> 1) | x;
408 x = (x >> 2) | x;
409 x = (x >> 4) | x;
410 x = (x >> 8) | x;
411 x = (x >> 16) | x;
412 return x + 1;
413 }