rayfract

annotate 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
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@5 34 Vector3 color(0.75, 0.75, 0.75);
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@4 108 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
nuclear@4 109 set_uniform_float(sdr, "reflectivity", reflectivity);
nuclear@4 110 set_uniform_int(sdr, "iter", iter);
nuclear@4 111 set_uniform_float(sdr, "err_thres", err_thres);
nuclear@5 112 set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
nuclear@4 113
nuclear@0 114 glMatrixMode(GL_TEXTURE);
nuclear@0 115 glPushMatrix();
nuclear@0 116 glScalef(tex_scale.x, tex_scale.y, 1.0);
nuclear@0 117
nuclear@0 118 glBindTexture(GL_TEXTURE_2D, ray_tex);
nuclear@0 119 glEnable(GL_TEXTURE_2D);
nuclear@0 120 bind_program(sdr);
nuclear@0 121
nuclear@0 122 glBegin(GL_QUADS);
nuclear@0 123 glColor3f(1, 1, 1);
nuclear@0 124 glTexCoord2f(0, 1); glVertex2f(-1, -1);
nuclear@0 125 glTexCoord2f(1, 1); glVertex2f(1, -1);
nuclear@0 126 glTexCoord2f(1, 0); glVertex2f(1, 1);
nuclear@0 127 glTexCoord2f(0, 0); glVertex2f(-1, 1);
nuclear@0 128 glEnd();
nuclear@0 129
nuclear@0 130 bind_program(0);
nuclear@0 131 glDisable(GL_TEXTURE_2D);
nuclear@0 132
nuclear@0 133 glMatrixMode(GL_TEXTURE);
nuclear@0 134 glPopMatrix();
nuclear@0 135
nuclear@1 136 gui_draw();
nuclear@1 137
nuclear@0 138 glutSwapBuffers();
nuclear@0 139 assert(glGetError() == GL_NO_ERROR);
nuclear@0 140 }
nuclear@0 141
nuclear@0 142 void reshape(int x, int y)
nuclear@0 143 {
nuclear@0 144 glViewport(0, 0, x, y);
nuclear@0 145 glMatrixMode(GL_PROJECTION);
nuclear@0 146 glLoadIdentity();
nuclear@0 147 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
nuclear@0 148
nuclear@5 149 imtk_set_viewport(x, y);
nuclear@5 150
nuclear@0 151 if(ray_tex) {
nuclear@0 152 glDeleteTextures(1, &ray_tex);
nuclear@0 153 }
nuclear@0 154 ray_tex = create_ray_texture(x, y, 50.0, &tex_scale);
nuclear@0 155 }
nuclear@0 156
nuclear@0 157
nuclear@0 158 void keyb(unsigned char key, int x, int y)
nuclear@0 159 {
nuclear@0 160 switch(key) {
nuclear@0 161 case 27:
nuclear@0 162 exit(0);
nuclear@0 163
nuclear@0 164 case '-':
nuclear@0 165 if(iter > 1) {
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 }
nuclear@0 171 break;
nuclear@0 172
nuclear@0 173 case '=':
nuclear@0 174 iter++;
nuclear@0 175 set_uniform_int(sdr, "iter", iter);
nuclear@0 176 printf("iter: %d\n", iter);
nuclear@0 177 glutPostRedisplay();
nuclear@0 178 break;
nuclear@0 179
nuclear@0 180 case ',':
nuclear@0 181 err_thres -= 0.001;
nuclear@0 182 set_uniform_float(sdr, "err_thres", err_thres);
nuclear@0 183 printf("maximum error: %f\n", err_thres);
nuclear@0 184 glutPostRedisplay();
nuclear@0 185 break;
nuclear@0 186
nuclear@0 187 case '.':
nuclear@0 188 err_thres += 0.001;
nuclear@0 189 set_uniform_float(sdr, "err_thres", err_thres);
nuclear@0 190 printf("maximum error: %f\n", err_thres);
nuclear@0 191 glutPostRedisplay();
nuclear@0 192 break;
nuclear@0 193
nuclear@0 194 case 's':
nuclear@0 195 load_shader();
nuclear@0 196 glutPostRedisplay();
nuclear@0 197 break;
nuclear@1 198
nuclear@1 199 case 'r':
nuclear@1 200 reflectivity = reflectivity > 0.0 ? 0.0 : 0.6;
nuclear@1 201 set_uniform_float(sdr, "reflectivity", reflectivity);
nuclear@1 202 glutPostRedisplay();
nuclear@1 203 break;
nuclear@1 204
nuclear@1 205 case '`':
nuclear@1 206 {
nuclear@1 207 static bool vis = true;
nuclear@1 208 vis = !vis;
nuclear@1 209 gui_set_visible(vis);
nuclear@1 210 glutPostRedisplay();
nuclear@1 211 }
nuclear@1 212 break;
nuclear@5 213
nuclear@5 214 case '\n':
nuclear@5 215 case '\r':
nuclear@5 216 printf("(%.3f %+.3fi %+.3fj %+.3fk) i:%d err: %.4f cam(theta: %.2f phi: %.2f rad: %.2f)\n", seed.w,
nuclear@5 217 seed.x, seed.y, seed.z, iter, err_thres, cam_theta, cam_phi, cam_dist);
nuclear@5 218 break;
nuclear@0 219 }
nuclear@1 220
nuclear@4 221 imtk_inp_key(key, 1);
nuclear@1 222 glutPostRedisplay();
nuclear@1 223 }
nuclear@1 224
nuclear@1 225 void keyb_up(unsigned char key, int x, int y)
nuclear@1 226 {
nuclear@4 227 imtk_inp_key(key, 0);
nuclear@1 228 glutPostRedisplay();
nuclear@0 229 }
nuclear@0 230
nuclear@0 231 int bnstate[16];
nuclear@0 232
nuclear@0 233 int prev_x = -1, prev_y;
nuclear@0 234 void mouse(int bn, int state, int x, int y)
nuclear@0 235 {
nuclear@4 236 int mod;
nuclear@1 237
nuclear@4 238 mod = glutGetModifiers();
nuclear@4 239
nuclear@4 240 if(mod) {
nuclear@1 241 bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
nuclear@1 242 if(state == GLUT_DOWN) {
nuclear@1 243 if(bn == 3) {
nuclear@1 244 cam_dist -= 0.1;
nuclear@1 245 glutPostRedisplay();
nuclear@1 246 if(cam_dist < 0) cam_dist = 0;
nuclear@1 247 } else if(bn == 4) {
nuclear@1 248 cam_dist += 0.1;
nuclear@1 249 glutPostRedisplay();
nuclear@1 250 } else {
nuclear@1 251 prev_x = x;
nuclear@1 252 prev_y = y;
nuclear@1 253 }
nuclear@0 254 } else {
nuclear@1 255 prev_x = -1;
nuclear@0 256 }
nuclear@4 257 } else {
nuclear@4 258 imtk_inp_mouse(bn, state == GLUT_DOWN ? 1 : 0);
nuclear@0 259 }
nuclear@1 260 glutPostRedisplay();
nuclear@0 261 }
nuclear@0 262
nuclear@0 263 void motion(int x, int y)
nuclear@0 264 {
nuclear@0 265 if(bnstate[0]) {
nuclear@0 266 cam_theta += (x - prev_x) * 0.5;
nuclear@0 267 cam_phi += (y - prev_y) * 0.5;
nuclear@0 268
nuclear@0 269 if(cam_phi < -90) cam_phi = -90;
nuclear@0 270 if(cam_phi > 90) cam_phi = 90;
nuclear@0 271
nuclear@0 272 glutPostRedisplay();
nuclear@0 273 }
nuclear@0 274
nuclear@0 275 if(bnstate[1]) {
nuclear@0 276 cam_y += (y - prev_y) * 0.1;
nuclear@0 277 glutPostRedisplay();
nuclear@0 278 }
nuclear@0 279
nuclear@0 280 if(bnstate[2]) {
nuclear@0 281 cam_dist += (y - prev_y) * 0.1;
nuclear@0 282 glutPostRedisplay();
nuclear@0 283 }
nuclear@0 284
nuclear@0 285 prev_x = x;
nuclear@0 286 prev_y = y;
nuclear@1 287
nuclear@4 288 imtk_inp_motion(x, y);
nuclear@1 289 glutPostRedisplay();
nuclear@1 290 }
nuclear@1 291
nuclear@1 292 void passive_motion(int x, int y)
nuclear@1 293 {
nuclear@4 294 imtk_inp_motion(x, y);
nuclear@4 295 glutPostRedisplay();
nuclear@0 296 }
nuclear@0 297
nuclear@0 298 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
nuclear@0 299 {
nuclear@0 300 unsigned int tex;
nuclear@0 301 int tex_xsz = round_pow2(xsz);
nuclear@0 302 int tex_ysz = round_pow2(ysz);
nuclear@0 303 float *teximg, *dir;
nuclear@0 304
nuclear@0 305 teximg = new float[3 * tex_xsz * tex_ysz];
nuclear@0 306 dir = teximg;
nuclear@0 307
nuclear@0 308 for(int i=0; i<tex_ysz; i++) {
nuclear@0 309 for(int j=0; j<tex_xsz; j++) {
nuclear@0 310 if(j < xsz && i < ysz) {
nuclear@0 311 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
nuclear@0 312 dir[0] = rdir.x;
nuclear@0 313 dir[1] = rdir.y;
nuclear@0 314 dir[2] = rdir.z;
nuclear@0 315 } else {
nuclear@0 316 dir[0] = dir[1] = 0.0f;
nuclear@0 317 dir[2] = 1.0f;
nuclear@0 318 }
nuclear@0 319
nuclear@0 320 dir += 3;
nuclear@0 321 }
nuclear@0 322 }
nuclear@0 323
nuclear@0 324 glGenTextures(1, &tex);
nuclear@0 325 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@0 326 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@0 327 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@0 328 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
nuclear@0 329 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
nuclear@0 330 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, teximg);
nuclear@0 331 delete [] teximg;
nuclear@0 332
nuclear@0 333 if(tex_scale) {
nuclear@0 334 tex_scale->x = (float)xsz / (float)tex_xsz;
nuclear@0 335 tex_scale->y = (float)ysz / (float)tex_ysz;
nuclear@0 336 }
nuclear@0 337 return tex;
nuclear@0 338 }
nuclear@0 339
nuclear@0 340 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
nuclear@0 341 {
nuclear@0 342 float vfov = M_PI * vfov_deg / 180.0;
nuclear@0 343 float aspect = (float)w / (float)h;
nuclear@0 344
nuclear@0 345 float ysz = 2.0;
nuclear@0 346 float xsz = aspect * ysz;
nuclear@0 347
nuclear@0 348 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
nuclear@0 349 float py = 1.0 - ((float)y / (float)h) * ysz;
nuclear@0 350 float pz = 1.0 / tan(0.5 * vfov);
nuclear@0 351
nuclear@0 352 float mag = sqrt(px * px + py * py + pz * pz);
nuclear@0 353
nuclear@0 354 return Vector3(px / mag, py / mag, pz / mag);
nuclear@0 355 }
nuclear@0 356
nuclear@0 357 static int round_pow2(int x)
nuclear@0 358 {
nuclear@0 359 x--;
nuclear@0 360 x = (x >> 1) | x;
nuclear@0 361 x = (x >> 2) | x;
nuclear@0 362 x = (x >> 4) | x;
nuclear@0 363 x = (x >> 8) | x;
nuclear@0 364 x = (x >> 16) | x;
nuclear@0 365 return x + 1;
nuclear@0 366 }