rayfract

view src/rayfract.cc @ 5:48e0e7d33d9e

foo
author John Tsiombikas <nuclear@siggraph.org>
date Sat, 28 May 2011 22:31:07 +0300
parents e4349f5804b9
children 8a9aa21b32cf
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 void disp();
12 void reshape(int x, int y);
13 void keyb(unsigned char key, int x, int y);
14 void keyb_up(unsigned char key, int x, int y);
15 void mouse(int bn, int state, int x, int y);
16 void motion(int x, int y);
17 void passive_motion(int x, int y);
19 int load_shader();
20 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale = 0);
21 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
22 static int round_pow2(int x);
24 float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
25 float cam_y = 0;
27 unsigned int sdr;
28 unsigned int ray_tex;
29 Vector2 tex_scale;
30 Vector4 seed;
31 float err_thres = 0.0075;
32 int iter = 10;
33 float reflectivity = 0.2;
34 Vector3 color(0.75, 0.75, 0.75);
36 int main(int argc, char **argv)
37 {
38 int xsz, ysz;
40 seed = Vector4(0.4, 0.0, 0.0, -0.8);
42 glutInitWindowSize(640, 480);
43 glutInit(&argc, argv);
44 glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
45 glutCreateWindow("Raytraced Fractals");
46 xsz = glutGet(GLUT_WINDOW_WIDTH);
47 ysz = glutGet(GLUT_WINDOW_HEIGHT);
49 glutDisplayFunc(disp);
50 glutReshapeFunc(reshape);
51 glutKeyboardFunc(keyb);
52 glutKeyboardUpFunc(keyb_up);
53 glutMouseFunc(mouse);
54 glutMotionFunc(motion);
55 glutPassiveMotionFunc(passive_motion);
57 glEnable(GL_DEPTH_TEST);
58 glEnable(GL_LIGHTING);
59 glEnable(GL_LIGHT0);
60 glEnable(GL_CULL_FACE);
62 glewInit();
64 if(load_shader() == -1) {
65 return 1;
66 }
68 if(gui_init(xsz, ysz) == -1) {
69 return 1;
70 }
72 glutMainLoop();
73 return 0;
74 }
76 int load_shader()
77 {
78 if(sdr) {
79 free_program(sdr);
80 }
82 if(!(sdr = create_program_load("sdr/sdr.v.glsl", "sdr/julia.p.glsl"))) {
83 return -1;
84 }
85 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
86 set_uniform_float(sdr, "err_thres", err_thres);
87 set_uniform_int(sdr, "iter", iter);
88 set_uniform_float(sdr, "reflectivity", reflectivity);
89 set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
91 return 0;
92 }
94 void disp()
95 {
96 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
98 glMatrixMode(GL_MODELVIEW);
99 glLoadIdentity();
100 glRotatef(cam_theta, 0, 1, 0);
101 glRotatef(cam_phi, 1, 0, 0);
102 glTranslatef(0, 0, -cam_dist);
104 float lpos[] = {-1, 1, 3, 0};
105 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
108 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
109 set_uniform_float(sdr, "reflectivity", reflectivity);
110 set_uniform_int(sdr, "iter", iter);
111 set_uniform_float(sdr, "err_thres", err_thres);
112 set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
114 glMatrixMode(GL_TEXTURE);
115 glPushMatrix();
116 glScalef(tex_scale.x, tex_scale.y, 1.0);
118 glBindTexture(GL_TEXTURE_2D, ray_tex);
119 glEnable(GL_TEXTURE_2D);
120 bind_program(sdr);
122 glBegin(GL_QUADS);
123 glColor3f(1, 1, 1);
124 glTexCoord2f(0, 1); glVertex2f(-1, -1);
125 glTexCoord2f(1, 1); glVertex2f(1, -1);
126 glTexCoord2f(1, 0); glVertex2f(1, 1);
127 glTexCoord2f(0, 0); glVertex2f(-1, 1);
128 glEnd();
130 bind_program(0);
131 glDisable(GL_TEXTURE_2D);
133 glMatrixMode(GL_TEXTURE);
134 glPopMatrix();
136 gui_draw();
138 glutSwapBuffers();
139 assert(glGetError() == GL_NO_ERROR);
140 }
142 void reshape(int x, int y)
143 {
144 glViewport(0, 0, x, y);
145 glMatrixMode(GL_PROJECTION);
146 glLoadIdentity();
147 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
149 imtk_set_viewport(x, y);
151 if(ray_tex) {
152 glDeleteTextures(1, &ray_tex);
153 }
154 ray_tex = create_ray_texture(x, y, 50.0, &tex_scale);
155 }
158 void keyb(unsigned char key, int x, int y)
159 {
160 switch(key) {
161 case 27:
162 exit(0);
164 case '-':
165 if(iter > 1) {
166 iter--;
167 set_uniform_int(sdr, "iter", iter);
168 printf("iter: %d\n", iter);
169 glutPostRedisplay();
170 }
171 break;
173 case '=':
174 iter++;
175 set_uniform_int(sdr, "iter", iter);
176 printf("iter: %d\n", iter);
177 glutPostRedisplay();
178 break;
180 case ',':
181 err_thres -= 0.001;
182 set_uniform_float(sdr, "err_thres", err_thres);
183 printf("maximum error: %f\n", err_thres);
184 glutPostRedisplay();
185 break;
187 case '.':
188 err_thres += 0.001;
189 set_uniform_float(sdr, "err_thres", err_thres);
190 printf("maximum error: %f\n", err_thres);
191 glutPostRedisplay();
192 break;
194 case 's':
195 load_shader();
196 glutPostRedisplay();
197 break;
199 case 'r':
200 reflectivity = reflectivity > 0.0 ? 0.0 : 0.6;
201 set_uniform_float(sdr, "reflectivity", reflectivity);
202 glutPostRedisplay();
203 break;
205 case '`':
206 {
207 static bool vis = true;
208 vis = !vis;
209 gui_set_visible(vis);
210 glutPostRedisplay();
211 }
212 break;
214 case '\n':
215 case '\r':
216 printf("(%.3f %+.3fi %+.3fj %+.3fk) i:%d err: %.4f cam(theta: %.2f phi: %.2f rad: %.2f)\n", seed.w,
217 seed.x, seed.y, seed.z, iter, err_thres, cam_theta, cam_phi, cam_dist);
218 break;
219 }
221 imtk_inp_key(key, 1);
222 glutPostRedisplay();
223 }
225 void keyb_up(unsigned char key, int x, int y)
226 {
227 imtk_inp_key(key, 0);
228 glutPostRedisplay();
229 }
231 int bnstate[16];
233 int prev_x = -1, prev_y;
234 void mouse(int bn, int state, int x, int y)
235 {
236 int mod;
238 mod = glutGetModifiers();
240 if(mod) {
241 bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
242 if(state == GLUT_DOWN) {
243 if(bn == 3) {
244 cam_dist -= 0.1;
245 glutPostRedisplay();
246 if(cam_dist < 0) cam_dist = 0;
247 } else if(bn == 4) {
248 cam_dist += 0.1;
249 glutPostRedisplay();
250 } else {
251 prev_x = x;
252 prev_y = y;
253 }
254 } else {
255 prev_x = -1;
256 }
257 } else {
258 imtk_inp_mouse(bn, state == GLUT_DOWN ? 1 : 0);
259 }
260 glutPostRedisplay();
261 }
263 void motion(int x, int y)
264 {
265 if(bnstate[0]) {
266 cam_theta += (x - prev_x) * 0.5;
267 cam_phi += (y - prev_y) * 0.5;
269 if(cam_phi < -90) cam_phi = -90;
270 if(cam_phi > 90) cam_phi = 90;
272 glutPostRedisplay();
273 }
275 if(bnstate[1]) {
276 cam_y += (y - prev_y) * 0.1;
277 glutPostRedisplay();
278 }
280 if(bnstate[2]) {
281 cam_dist += (y - prev_y) * 0.1;
282 glutPostRedisplay();
283 }
285 prev_x = x;
286 prev_y = y;
288 imtk_inp_motion(x, y);
289 glutPostRedisplay();
290 }
292 void passive_motion(int x, int y)
293 {
294 imtk_inp_motion(x, y);
295 glutPostRedisplay();
296 }
298 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
299 {
300 unsigned int tex;
301 int tex_xsz = round_pow2(xsz);
302 int tex_ysz = round_pow2(ysz);
303 float *teximg, *dir;
305 teximg = new float[3 * tex_xsz * tex_ysz];
306 dir = teximg;
308 for(int i=0; i<tex_ysz; i++) {
309 for(int j=0; j<tex_xsz; j++) {
310 if(j < xsz && i < ysz) {
311 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
312 dir[0] = rdir.x;
313 dir[1] = rdir.y;
314 dir[2] = rdir.z;
315 } else {
316 dir[0] = dir[1] = 0.0f;
317 dir[2] = 1.0f;
318 }
320 dir += 3;
321 }
322 }
324 glGenTextures(1, &tex);
325 glBindTexture(GL_TEXTURE_2D, tex);
326 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
327 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
328 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
329 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
330 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, teximg);
331 delete [] teximg;
333 if(tex_scale) {
334 tex_scale->x = (float)xsz / (float)tex_xsz;
335 tex_scale->y = (float)ysz / (float)tex_ysz;
336 }
337 return tex;
338 }
340 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
341 {
342 float vfov = M_PI * vfov_deg / 180.0;
343 float aspect = (float)w / (float)h;
345 float ysz = 2.0;
346 float xsz = aspect * ysz;
348 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
349 float py = 1.0 - ((float)y / (float)h) * ysz;
350 float pz = 1.0 / tan(0.5 * vfov);
352 float mag = sqrt(px * px + py * py + pz * pz);
354 return Vector3(px / mag, py / mag, pz / mag);
355 }
357 static int round_pow2(int x)
358 {
359 x--;
360 x = (x >> 1) | x;
361 x = (x >> 2) | x;
362 x = (x >> 4) | x;
363 x = (x >> 8) | x;
364 x = (x >> 16) | x;
365 return x + 1;
366 }