rayfract

view src/rayfract.cc @ 3:bf1d56975cc9

- added visual studio project - removed vmath dependency
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 26 Oct 2010 09:52:57 +0300
parents 03022062c464
children e4349f5804b9
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.9, 0.95, 1.0);
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 glMatrixMode(GL_TEXTURE);
109 glPushMatrix();
110 glScalef(tex_scale.x, tex_scale.y, 1.0);
112 glBindTexture(GL_TEXTURE_2D, ray_tex);
113 glEnable(GL_TEXTURE_2D);
114 bind_program(sdr);
116 glBegin(GL_QUADS);
117 glColor3f(1, 1, 1);
118 glTexCoord2f(0, 1); glVertex2f(-1, -1);
119 glTexCoord2f(1, 1); glVertex2f(1, -1);
120 glTexCoord2f(1, 0); glVertex2f(1, 1);
121 glTexCoord2f(0, 0); glVertex2f(-1, 1);
122 glEnd();
124 bind_program(0);
125 glDisable(GL_TEXTURE_2D);
127 glMatrixMode(GL_TEXTURE);
128 glPopMatrix();
130 gui_draw();
132 glutSwapBuffers();
133 assert(glGetError() == GL_NO_ERROR);
134 }
136 void reshape(int x, int y)
137 {
138 glViewport(0, 0, x, y);
139 glMatrixMode(GL_PROJECTION);
140 glLoadIdentity();
141 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
143 if(ray_tex) {
144 glDeleteTextures(1, &ray_tex);
145 }
146 ray_tex = create_ray_texture(x, y, 50.0, &tex_scale);
147 }
150 void keyb(unsigned char key, int x, int y)
151 {
152 switch(key) {
153 case 27:
154 exit(0);
156 case '-':
157 if(iter > 1) {
158 iter--;
159 set_uniform_int(sdr, "iter", iter);
160 printf("iter: %d\n", iter);
161 glutPostRedisplay();
162 }
163 break;
165 case '=':
166 iter++;
167 set_uniform_int(sdr, "iter", iter);
168 printf("iter: %d\n", iter);
169 glutPostRedisplay();
170 break;
172 case ',':
173 err_thres -= 0.001;
174 set_uniform_float(sdr, "err_thres", err_thres);
175 printf("maximum error: %f\n", err_thres);
176 glutPostRedisplay();
177 break;
179 case '.':
180 err_thres += 0.001;
181 set_uniform_float(sdr, "err_thres", err_thres);
182 printf("maximum error: %f\n", err_thres);
183 glutPostRedisplay();
184 break;
186 case 's':
187 load_shader();
188 glutPostRedisplay();
189 break;
191 case 'r':
192 reflectivity = reflectivity > 0.0 ? 0.0 : 0.6;
193 set_uniform_float(sdr, "reflectivity", reflectivity);
194 glutPostRedisplay();
195 break;
197 case '`':
198 {
199 static bool vis = true;
200 vis = !vis;
201 gui_set_visible(vis);
202 glutPostRedisplay();
203 }
204 break;
205 }
207 utk::KeyboardEvent ev(key);
208 ev.pressed = true;
209 utk::event(&ev);
210 glutPostRedisplay();
211 }
213 void keyb_up(unsigned char key, int x, int y)
214 {
215 utk::KeyboardEvent ev(key);
216 ev.pressed = false;
217 utk::event(&ev);
218 glutPostRedisplay();
219 }
221 int bnstate[16];
223 int prev_x = -1, prev_y;
224 void mouse(int bn, int state, int x, int y)
225 {
226 utk::Container *utkroot = utk::get_root_widget();
228 if(utkroot->get_child_at(x, y) == utkroot) {
229 bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
230 if(state == GLUT_DOWN) {
231 if(bn == 3) {
232 cam_dist -= 0.1;
233 glutPostRedisplay();
234 if(cam_dist < 0) cam_dist = 0;
235 } else if(bn == 4) {
236 cam_dist += 0.1;
237 glutPostRedisplay();
238 } else {
239 prev_x = x;
240 prev_y = y;
241 }
242 } else {
243 prev_x = -1;
244 }
245 }
247 utk::MButtonEvent ev(bn, x, y);
248 ev.pressed = state == GLUT_DOWN;
249 utk::event(&ev);
250 glutPostRedisplay();
251 }
253 void motion(int x, int y)
254 {
255 if(bnstate[0]) {
256 cam_theta += (x - prev_x) * 0.5;
257 cam_phi += (y - prev_y) * 0.5;
259 if(cam_phi < -90) cam_phi = -90;
260 if(cam_phi > 90) cam_phi = 90;
262 glutPostRedisplay();
263 }
265 if(bnstate[1]) {
266 cam_y += (y - prev_y) * 0.1;
267 glutPostRedisplay();
268 }
270 if(bnstate[2]) {
271 cam_dist += (y - prev_y) * 0.1;
272 glutPostRedisplay();
273 }
275 prev_x = x;
276 prev_y = y;
278 utk::MMotionEvent ev(x, y);
279 utk::event(&ev);
280 glutPostRedisplay();
281 }
283 void passive_motion(int x, int y)
284 {
285 utk::MMotionEvent ev(x, y);
286 utk::event(&ev);
287 //glutPostRedisplay();
288 }
290 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
291 {
292 unsigned int tex;
293 int tex_xsz = round_pow2(xsz);
294 int tex_ysz = round_pow2(ysz);
295 float *teximg, *dir;
297 teximg = new float[3 * tex_xsz * tex_ysz];
298 dir = teximg;
300 for(int i=0; i<tex_ysz; i++) {
301 for(int j=0; j<tex_xsz; j++) {
302 if(j < xsz && i < ysz) {
303 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
304 dir[0] = rdir.x;
305 dir[1] = rdir.y;
306 dir[2] = rdir.z;
307 } else {
308 dir[0] = dir[1] = 0.0f;
309 dir[2] = 1.0f;
310 }
312 dir += 3;
313 }
314 }
316 glGenTextures(1, &tex);
317 glBindTexture(GL_TEXTURE_2D, tex);
318 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
319 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
320 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
321 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
322 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, teximg);
323 delete [] teximg;
325 if(tex_scale) {
326 tex_scale->x = (float)xsz / (float)tex_xsz;
327 tex_scale->y = (float)ysz / (float)tex_ysz;
328 }
329 return tex;
330 }
332 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
333 {
334 float vfov = M_PI * vfov_deg / 180.0;
335 float aspect = (float)w / (float)h;
337 float ysz = 2.0;
338 float xsz = aspect * ysz;
340 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
341 float py = 1.0 - ((float)y / (float)h) * ysz;
342 float pz = 1.0 / tan(0.5 * vfov);
344 float mag = sqrt(px * px + py * py + pz * pz);
346 return Vector3(px / mag, py / mag, pz / mag);
347 }
349 static int round_pow2(int x)
350 {
351 x--;
352 x = (x >> 1) | x;
353 x = (x >> 2) | x;
354 x = (x >> 4) | x;
355 x = (x >> 8) | x;
356 x = (x >> 16) | x;
357 return x + 1;
358 }