rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@3
|
3 #include <math.h>
|
nuclear@0
|
4 #include <assert.h>
|
nuclear@0
|
5 #include <GL/glew.h>
|
nuclear@0
|
6 #include <GL/glut.h>
|
nuclear@0
|
7 #include "sdr.h"
|
nuclear@1
|
8 #include "gui.h"
|
nuclear@3
|
9 #include "vmath.h"
|
nuclear@0
|
10
|
nuclear@0
|
11 void disp();
|
nuclear@0
|
12 void reshape(int x, int y);
|
nuclear@0
|
13 void keyb(unsigned char key, int x, int y);
|
nuclear@1
|
14 void keyb_up(unsigned char key, int x, int y);
|
nuclear@0
|
15 void mouse(int bn, int state, int x, int y);
|
nuclear@0
|
16 void motion(int x, int y);
|
nuclear@1
|
17 void passive_motion(int x, int y);
|
nuclear@0
|
18
|
nuclear@0
|
19 int load_shader();
|
nuclear@0
|
20 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale = 0);
|
nuclear@0
|
21 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
|
nuclear@0
|
22 static int round_pow2(int x);
|
nuclear@0
|
23
|
nuclear@0
|
24 float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
|
nuclear@0
|
25 float cam_y = 0;
|
nuclear@0
|
26
|
nuclear@0
|
27 unsigned int sdr;
|
nuclear@0
|
28 unsigned int ray_tex;
|
nuclear@0
|
29 Vector2 tex_scale;
|
nuclear@0
|
30 Vector4 seed;
|
nuclear@0
|
31 float err_thres = 0.0075;
|
nuclear@0
|
32 int iter = 10;
|
nuclear@1
|
33 float reflectivity = 0.2;
|
nuclear@1
|
34 Vector3 color(0.9, 0.95, 1.0);
|
nuclear@0
|
35
|
nuclear@0
|
36 int main(int argc, char **argv)
|
nuclear@0
|
37 {
|
nuclear@0
|
38 int xsz, ysz;
|
nuclear@0
|
39
|
nuclear@0
|
40 seed = Vector4(0.4, 0.0, 0.0, -0.8);
|
nuclear@0
|
41
|
nuclear@0
|
42 glutInitWindowSize(640, 480);
|
nuclear@0
|
43 glutInit(&argc, argv);
|
nuclear@0
|
44 glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
|
nuclear@0
|
45 glutCreateWindow("Raytraced Fractals");
|
nuclear@0
|
46 xsz = glutGet(GLUT_WINDOW_WIDTH);
|
nuclear@0
|
47 ysz = glutGet(GLUT_WINDOW_HEIGHT);
|
nuclear@0
|
48
|
nuclear@0
|
49 glutDisplayFunc(disp);
|
nuclear@0
|
50 glutReshapeFunc(reshape);
|
nuclear@0
|
51 glutKeyboardFunc(keyb);
|
nuclear@1
|
52 glutKeyboardUpFunc(keyb_up);
|
nuclear@0
|
53 glutMouseFunc(mouse);
|
nuclear@0
|
54 glutMotionFunc(motion);
|
nuclear@1
|
55 glutPassiveMotionFunc(passive_motion);
|
nuclear@0
|
56
|
nuclear@0
|
57 glEnable(GL_DEPTH_TEST);
|
nuclear@0
|
58 glEnable(GL_LIGHTING);
|
nuclear@0
|
59 glEnable(GL_LIGHT0);
|
nuclear@0
|
60 glEnable(GL_CULL_FACE);
|
nuclear@0
|
61
|
nuclear@0
|
62 glewInit();
|
nuclear@0
|
63
|
nuclear@0
|
64 if(load_shader() == -1) {
|
nuclear@0
|
65 return 1;
|
nuclear@0
|
66 }
|
nuclear@0
|
67
|
nuclear@1
|
68 if(gui_init(xsz, ysz) == -1) {
|
nuclear@1
|
69 return 1;
|
nuclear@1
|
70 }
|
nuclear@1
|
71
|
nuclear@0
|
72 glutMainLoop();
|
nuclear@0
|
73 return 0;
|
nuclear@0
|
74 }
|
nuclear@0
|
75
|
nuclear@0
|
76 int load_shader()
|
nuclear@0
|
77 {
|
nuclear@0
|
78 if(sdr) {
|
nuclear@0
|
79 free_program(sdr);
|
nuclear@0
|
80 }
|
nuclear@0
|
81
|
nuclear@0
|
82 if(!(sdr = create_program_load("sdr/sdr.v.glsl", "sdr/julia.p.glsl"))) {
|
nuclear@0
|
83 return -1;
|
nuclear@0
|
84 }
|
nuclear@0
|
85 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
|
nuclear@0
|
86 set_uniform_float(sdr, "err_thres", err_thres);
|
nuclear@0
|
87 set_uniform_int(sdr, "iter", iter);
|
nuclear@1
|
88 set_uniform_float(sdr, "reflectivity", reflectivity);
|
nuclear@1
|
89 set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
|
nuclear@1
|
90
|
nuclear@0
|
91 return 0;
|
nuclear@0
|
92 }
|
nuclear@0
|
93
|
nuclear@0
|
94 void disp()
|
nuclear@0
|
95 {
|
nuclear@0
|
96 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
97
|
nuclear@0
|
98 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
99 glLoadIdentity();
|
nuclear@0
|
100 glRotatef(cam_theta, 0, 1, 0);
|
nuclear@0
|
101 glRotatef(cam_phi, 1, 0, 0);
|
nuclear@0
|
102 glTranslatef(0, 0, -cam_dist);
|
nuclear@0
|
103
|
nuclear@0
|
104 float lpos[] = {-1, 1, 3, 0};
|
nuclear@0
|
105 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
|
nuclear@0
|
106
|
nuclear@0
|
107
|
nuclear@0
|
108 glMatrixMode(GL_TEXTURE);
|
nuclear@0
|
109 glPushMatrix();
|
nuclear@0
|
110 glScalef(tex_scale.x, tex_scale.y, 1.0);
|
nuclear@0
|
111
|
nuclear@0
|
112 glBindTexture(GL_TEXTURE_2D, ray_tex);
|
nuclear@0
|
113 glEnable(GL_TEXTURE_2D);
|
nuclear@0
|
114 bind_program(sdr);
|
nuclear@0
|
115
|
nuclear@0
|
116 glBegin(GL_QUADS);
|
nuclear@0
|
117 glColor3f(1, 1, 1);
|
nuclear@0
|
118 glTexCoord2f(0, 1); glVertex2f(-1, -1);
|
nuclear@0
|
119 glTexCoord2f(1, 1); glVertex2f(1, -1);
|
nuclear@0
|
120 glTexCoord2f(1, 0); glVertex2f(1, 1);
|
nuclear@0
|
121 glTexCoord2f(0, 0); glVertex2f(-1, 1);
|
nuclear@0
|
122 glEnd();
|
nuclear@0
|
123
|
nuclear@0
|
124 bind_program(0);
|
nuclear@0
|
125 glDisable(GL_TEXTURE_2D);
|
nuclear@0
|
126
|
nuclear@0
|
127 glMatrixMode(GL_TEXTURE);
|
nuclear@0
|
128 glPopMatrix();
|
nuclear@0
|
129
|
nuclear@1
|
130 gui_draw();
|
nuclear@1
|
131
|
nuclear@0
|
132 glutSwapBuffers();
|
nuclear@0
|
133 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
134 }
|
nuclear@0
|
135
|
nuclear@0
|
136 void reshape(int x, int y)
|
nuclear@0
|
137 {
|
nuclear@0
|
138 glViewport(0, 0, x, y);
|
nuclear@0
|
139 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
140 glLoadIdentity();
|
nuclear@0
|
141 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
|
nuclear@0
|
142
|
nuclear@0
|
143 if(ray_tex) {
|
nuclear@0
|
144 glDeleteTextures(1, &ray_tex);
|
nuclear@0
|
145 }
|
nuclear@0
|
146 ray_tex = create_ray_texture(x, y, 50.0, &tex_scale);
|
nuclear@0
|
147 }
|
nuclear@0
|
148
|
nuclear@0
|
149
|
nuclear@0
|
150 void keyb(unsigned char key, int x, int y)
|
nuclear@0
|
151 {
|
nuclear@0
|
152 switch(key) {
|
nuclear@0
|
153 case 27:
|
nuclear@0
|
154 exit(0);
|
nuclear@0
|
155
|
nuclear@0
|
156 case '-':
|
nuclear@0
|
157 if(iter > 1) {
|
nuclear@0
|
158 iter--;
|
nuclear@0
|
159 set_uniform_int(sdr, "iter", iter);
|
nuclear@0
|
160 printf("iter: %d\n", iter);
|
nuclear@0
|
161 glutPostRedisplay();
|
nuclear@0
|
162 }
|
nuclear@0
|
163 break;
|
nuclear@0
|
164
|
nuclear@0
|
165 case '=':
|
nuclear@0
|
166 iter++;
|
nuclear@0
|
167 set_uniform_int(sdr, "iter", iter);
|
nuclear@0
|
168 printf("iter: %d\n", iter);
|
nuclear@0
|
169 glutPostRedisplay();
|
nuclear@0
|
170 break;
|
nuclear@0
|
171
|
nuclear@0
|
172 case ',':
|
nuclear@0
|
173 err_thres -= 0.001;
|
nuclear@0
|
174 set_uniform_float(sdr, "err_thres", err_thres);
|
nuclear@0
|
175 printf("maximum error: %f\n", err_thres);
|
nuclear@0
|
176 glutPostRedisplay();
|
nuclear@0
|
177 break;
|
nuclear@0
|
178
|
nuclear@0
|
179 case '.':
|
nuclear@0
|
180 err_thres += 0.001;
|
nuclear@0
|
181 set_uniform_float(sdr, "err_thres", err_thres);
|
nuclear@0
|
182 printf("maximum error: %f\n", err_thres);
|
nuclear@0
|
183 glutPostRedisplay();
|
nuclear@0
|
184 break;
|
nuclear@0
|
185
|
nuclear@0
|
186 case 's':
|
nuclear@0
|
187 load_shader();
|
nuclear@0
|
188 glutPostRedisplay();
|
nuclear@0
|
189 break;
|
nuclear@1
|
190
|
nuclear@1
|
191 case 'r':
|
nuclear@1
|
192 reflectivity = reflectivity > 0.0 ? 0.0 : 0.6;
|
nuclear@1
|
193 set_uniform_float(sdr, "reflectivity", reflectivity);
|
nuclear@1
|
194 glutPostRedisplay();
|
nuclear@1
|
195 break;
|
nuclear@1
|
196
|
nuclear@1
|
197 case '`':
|
nuclear@1
|
198 {
|
nuclear@1
|
199 static bool vis = true;
|
nuclear@1
|
200 vis = !vis;
|
nuclear@1
|
201 gui_set_visible(vis);
|
nuclear@1
|
202 glutPostRedisplay();
|
nuclear@1
|
203 }
|
nuclear@1
|
204 break;
|
nuclear@0
|
205 }
|
nuclear@1
|
206
|
nuclear@1
|
207 utk::KeyboardEvent ev(key);
|
nuclear@1
|
208 ev.pressed = true;
|
nuclear@1
|
209 utk::event(&ev);
|
nuclear@1
|
210 glutPostRedisplay();
|
nuclear@1
|
211 }
|
nuclear@1
|
212
|
nuclear@1
|
213 void keyb_up(unsigned char key, int x, int y)
|
nuclear@1
|
214 {
|
nuclear@1
|
215 utk::KeyboardEvent ev(key);
|
nuclear@1
|
216 ev.pressed = false;
|
nuclear@1
|
217 utk::event(&ev);
|
nuclear@1
|
218 glutPostRedisplay();
|
nuclear@0
|
219 }
|
nuclear@0
|
220
|
nuclear@0
|
221 int bnstate[16];
|
nuclear@0
|
222
|
nuclear@0
|
223 int prev_x = -1, prev_y;
|
nuclear@0
|
224 void mouse(int bn, int state, int x, int y)
|
nuclear@0
|
225 {
|
nuclear@1
|
226 utk::Container *utkroot = utk::get_root_widget();
|
nuclear@1
|
227
|
nuclear@1
|
228 if(utkroot->get_child_at(x, y) == utkroot) {
|
nuclear@1
|
229 bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
|
nuclear@1
|
230 if(state == GLUT_DOWN) {
|
nuclear@1
|
231 if(bn == 3) {
|
nuclear@1
|
232 cam_dist -= 0.1;
|
nuclear@1
|
233 glutPostRedisplay();
|
nuclear@1
|
234 if(cam_dist < 0) cam_dist = 0;
|
nuclear@1
|
235 } else if(bn == 4) {
|
nuclear@1
|
236 cam_dist += 0.1;
|
nuclear@1
|
237 glutPostRedisplay();
|
nuclear@1
|
238 } else {
|
nuclear@1
|
239 prev_x = x;
|
nuclear@1
|
240 prev_y = y;
|
nuclear@1
|
241 }
|
nuclear@0
|
242 } else {
|
nuclear@1
|
243 prev_x = -1;
|
nuclear@0
|
244 }
|
nuclear@0
|
245 }
|
nuclear@1
|
246
|
nuclear@1
|
247 utk::MButtonEvent ev(bn, x, y);
|
nuclear@1
|
248 ev.pressed = state == GLUT_DOWN;
|
nuclear@1
|
249 utk::event(&ev);
|
nuclear@1
|
250 glutPostRedisplay();
|
nuclear@0
|
251 }
|
nuclear@0
|
252
|
nuclear@0
|
253 void motion(int x, int y)
|
nuclear@0
|
254 {
|
nuclear@0
|
255 if(bnstate[0]) {
|
nuclear@0
|
256 cam_theta += (x - prev_x) * 0.5;
|
nuclear@0
|
257 cam_phi += (y - prev_y) * 0.5;
|
nuclear@0
|
258
|
nuclear@0
|
259 if(cam_phi < -90) cam_phi = -90;
|
nuclear@0
|
260 if(cam_phi > 90) cam_phi = 90;
|
nuclear@0
|
261
|
nuclear@0
|
262 glutPostRedisplay();
|
nuclear@0
|
263 }
|
nuclear@0
|
264
|
nuclear@0
|
265 if(bnstate[1]) {
|
nuclear@0
|
266 cam_y += (y - prev_y) * 0.1;
|
nuclear@0
|
267 glutPostRedisplay();
|
nuclear@0
|
268 }
|
nuclear@0
|
269
|
nuclear@0
|
270 if(bnstate[2]) {
|
nuclear@0
|
271 cam_dist += (y - prev_y) * 0.1;
|
nuclear@0
|
272 glutPostRedisplay();
|
nuclear@0
|
273 }
|
nuclear@0
|
274
|
nuclear@0
|
275 prev_x = x;
|
nuclear@0
|
276 prev_y = y;
|
nuclear@1
|
277
|
nuclear@1
|
278 utk::MMotionEvent ev(x, y);
|
nuclear@1
|
279 utk::event(&ev);
|
nuclear@1
|
280 glutPostRedisplay();
|
nuclear@1
|
281 }
|
nuclear@1
|
282
|
nuclear@1
|
283 void passive_motion(int x, int y)
|
nuclear@1
|
284 {
|
nuclear@1
|
285 utk::MMotionEvent ev(x, y);
|
nuclear@1
|
286 utk::event(&ev);
|
nuclear@1
|
287 //glutPostRedisplay();
|
nuclear@0
|
288 }
|
nuclear@0
|
289
|
nuclear@0
|
290 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
|
nuclear@0
|
291 {
|
nuclear@0
|
292 unsigned int tex;
|
nuclear@0
|
293 int tex_xsz = round_pow2(xsz);
|
nuclear@0
|
294 int tex_ysz = round_pow2(ysz);
|
nuclear@0
|
295 float *teximg, *dir;
|
nuclear@0
|
296
|
nuclear@0
|
297 teximg = new float[3 * tex_xsz * tex_ysz];
|
nuclear@0
|
298 dir = teximg;
|
nuclear@0
|
299
|
nuclear@0
|
300 for(int i=0; i<tex_ysz; i++) {
|
nuclear@0
|
301 for(int j=0; j<tex_xsz; j++) {
|
nuclear@0
|
302 if(j < xsz && i < ysz) {
|
nuclear@0
|
303 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
|
nuclear@0
|
304 dir[0] = rdir.x;
|
nuclear@0
|
305 dir[1] = rdir.y;
|
nuclear@0
|
306 dir[2] = rdir.z;
|
nuclear@0
|
307 } else {
|
nuclear@0
|
308 dir[0] = dir[1] = 0.0f;
|
nuclear@0
|
309 dir[2] = 1.0f;
|
nuclear@0
|
310 }
|
nuclear@0
|
311
|
nuclear@0
|
312 dir += 3;
|
nuclear@0
|
313 }
|
nuclear@0
|
314 }
|
nuclear@0
|
315
|
nuclear@0
|
316 glGenTextures(1, &tex);
|
nuclear@0
|
317 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@0
|
318 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
nuclear@0
|
319 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
nuclear@0
|
320 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
nuclear@0
|
321 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
nuclear@0
|
322 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, teximg);
|
nuclear@0
|
323 delete [] teximg;
|
nuclear@0
|
324
|
nuclear@0
|
325 if(tex_scale) {
|
nuclear@0
|
326 tex_scale->x = (float)xsz / (float)tex_xsz;
|
nuclear@0
|
327 tex_scale->y = (float)ysz / (float)tex_ysz;
|
nuclear@0
|
328 }
|
nuclear@0
|
329 return tex;
|
nuclear@0
|
330 }
|
nuclear@0
|
331
|
nuclear@0
|
332 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
|
nuclear@0
|
333 {
|
nuclear@0
|
334 float vfov = M_PI * vfov_deg / 180.0;
|
nuclear@0
|
335 float aspect = (float)w / (float)h;
|
nuclear@0
|
336
|
nuclear@0
|
337 float ysz = 2.0;
|
nuclear@0
|
338 float xsz = aspect * ysz;
|
nuclear@0
|
339
|
nuclear@0
|
340 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
|
nuclear@0
|
341 float py = 1.0 - ((float)y / (float)h) * ysz;
|
nuclear@0
|
342 float pz = 1.0 / tan(0.5 * vfov);
|
nuclear@0
|
343
|
nuclear@0
|
344 float mag = sqrt(px * px + py * py + pz * pz);
|
nuclear@0
|
345
|
nuclear@0
|
346 return Vector3(px / mag, py / mag, pz / mag);
|
nuclear@0
|
347 }
|
nuclear@0
|
348
|
nuclear@0
|
349 static int round_pow2(int x)
|
nuclear@0
|
350 {
|
nuclear@0
|
351 x--;
|
nuclear@0
|
352 x = (x >> 1) | x;
|
nuclear@0
|
353 x = (x >> 2) | x;
|
nuclear@0
|
354 x = (x >> 4) | x;
|
nuclear@0
|
355 x = (x >> 8) | x;
|
nuclear@0
|
356 x = (x >> 16) | x;
|
nuclear@0
|
357 return x + 1;
|
nuclear@0
|
358 }
|