rayfract

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