voxfract

annotate src/main.c @ 0:8171de5a000b

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jun 2017 18:03:57 +0300
parents
children d0297d001505
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <math.h>
nuclear@0 4 #include <assert.h>
nuclear@0 5 #include <GL/glut.h>
nuclear@0 6 #include <metasurf.h>
nuclear@0 7
nuclear@0 8 struct quat {
nuclear@0 9 float x, y, z, w;
nuclear@0 10 };
nuclear@0 11
nuclear@0 12 int init(void);
nuclear@0 13 void cleanup(void);
nuclear@0 14 void display(void);
nuclear@0 15 void reshape(int x, int y);
nuclear@0 16 void keypress(unsigned char key, int x, int y);
nuclear@0 17 void mouse(int bn, int st, int x, int y);
nuclear@0 18 void motion(int x, int y);
nuclear@0 19
nuclear@0 20 float eval(struct metasurface *ms, float x, float y, float z);
nuclear@0 21 void vertex(struct metasurface *ms, float x, float y, float z);
nuclear@0 22
nuclear@0 23 int julia(struct quat *qres, struct quat *qprime, struct quat *q, struct quat *seed, int max_iter);
nuclear@0 24 float julia_dist(struct quat *z, struct quat *seed, int max_iter);
nuclear@0 25 void julia_grad(float *grad, float dist, struct quat *q, struct quat *seed, int max_iter);
nuclear@0 26
nuclear@0 27 void show_waitscr(void);
nuclear@0 28
nuclear@0 29 float cam_theta, cam_phi = 25, cam_dist = 5;
nuclear@0 30 int grid_size = 350;
nuclear@0 31 float grid_scale = 1.7;
nuclear@0 32 struct quat seed = {0.4, 0.0, 0.0, -0.8};
nuclear@0 33 int max_iter = 9;
nuclear@0 34
nuclear@0 35 struct metasurface *msurf;
nuclear@0 36 int dlist;
nuclear@0 37 FILE *fp;
nuclear@0 38
nuclear@0 39 int main(int argc, char **argv)
nuclear@0 40 {
nuclear@0 41 glutInit(&argc, argv);
nuclear@0 42 glutInitWindowSize(800, 600);
nuclear@0 43 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@0 44 glutCreateWindow("voxel fractals");
nuclear@0 45
nuclear@0 46 glutDisplayFunc(display);
nuclear@0 47 glutReshapeFunc(reshape);
nuclear@0 48 glutKeyboardFunc(keypress);
nuclear@0 49 glutMouseFunc(mouse);
nuclear@0 50 glutMotionFunc(motion);
nuclear@0 51
nuclear@0 52 if(init() == -1) {
nuclear@0 53 return 1;
nuclear@0 54 }
nuclear@0 55 atexit(cleanup);
nuclear@0 56
nuclear@0 57 glutMainLoop();
nuclear@0 58 return 0;
nuclear@0 59 }
nuclear@0 60
nuclear@0 61 int init(void)
nuclear@0 62 {
nuclear@0 63 glEnable(GL_DEPTH_TEST);
nuclear@0 64 glEnable(GL_CULL_FACE);
nuclear@0 65 glEnable(GL_LIGHTING);
nuclear@0 66 glEnable(GL_LIGHT0);
nuclear@0 67 /*glShadeModel(GL_FLAT);*/
nuclear@0 68
nuclear@0 69 fp = fopen("julia.obj", "wb");
nuclear@0 70
nuclear@0 71 if(!(msurf = msurf_create())) {
nuclear@0 72 return -1;
nuclear@0 73 }
nuclear@0 74 msurf_eval_func(msurf, eval);
nuclear@0 75 msurf_vertex_func(msurf, vertex);
nuclear@0 76 msurf_set_resolution(msurf, grid_size, grid_size, grid_size);
nuclear@0 77 msurf_set_threshold(msurf, 0.0);
nuclear@0 78 msurf_set_inside(msurf, MSURF_LESS);
nuclear@0 79
nuclear@0 80 show_waitscr();
nuclear@0 81
nuclear@0 82 dlist = glGenLists(1);
nuclear@0 83 glNewList(dlist, GL_COMPILE);
nuclear@0 84
nuclear@0 85 glBegin(GL_TRIANGLES);
nuclear@0 86 msurf_polygonize(msurf);
nuclear@0 87 glEnd();
nuclear@0 88
nuclear@0 89 glEndList();
nuclear@0 90
nuclear@0 91 if(fp) {
nuclear@0 92 fclose(fp);
nuclear@0 93 }
nuclear@0 94
nuclear@0 95 glClearColor(0.05, 0.05, 0.05, 1);
nuclear@0 96
nuclear@0 97 return 0;
nuclear@0 98 }
nuclear@0 99
nuclear@0 100 void cleanup(void)
nuclear@0 101 {
nuclear@0 102 msurf_free(msurf);
nuclear@0 103 }
nuclear@0 104
nuclear@0 105 void display(void)
nuclear@0 106 {
nuclear@0 107 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 108
nuclear@0 109 glMatrixMode(GL_MODELVIEW);
nuclear@0 110 glLoadIdentity();
nuclear@0 111 glTranslatef(0, 0, -cam_dist);
nuclear@0 112 glRotatef(cam_phi, 1, 0, 0);
nuclear@0 113 glRotatef(cam_theta, 0, 1, 0);
nuclear@0 114
nuclear@0 115 glCallList(dlist);
nuclear@0 116
nuclear@0 117 assert(glGetError() == GL_NO_ERROR);
nuclear@0 118 glutSwapBuffers();
nuclear@0 119 }
nuclear@0 120
nuclear@0 121 void reshape(int x, int y)
nuclear@0 122 {
nuclear@0 123 glViewport(0, 0, x, y);
nuclear@0 124 glMatrixMode(GL_PROJECTION);
nuclear@0 125 glLoadIdentity();
nuclear@0 126 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
nuclear@0 127 }
nuclear@0 128
nuclear@0 129 void keypress(unsigned char key, int x, int y)
nuclear@0 130 {
nuclear@0 131 switch(key) {
nuclear@0 132 case 27:
nuclear@0 133 exit(0);
nuclear@0 134 }
nuclear@0 135 }
nuclear@0 136
nuclear@0 137 int prev_x, prev_y;
nuclear@0 138 int bnstate[8];
nuclear@0 139
nuclear@0 140 void mouse(int bn, int st, int x, int y)
nuclear@0 141 {
nuclear@0 142 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
nuclear@0 143 prev_x = x;
nuclear@0 144 prev_y = y;
nuclear@0 145 }
nuclear@0 146
nuclear@0 147 void motion(int x, int y)
nuclear@0 148 {
nuclear@0 149 int dx = x - prev_x;
nuclear@0 150 int dy = y - prev_y;
nuclear@0 151 prev_x = x;
nuclear@0 152 prev_y = y;
nuclear@0 153
nuclear@0 154 if(!dx && !dy) return;
nuclear@0 155
nuclear@0 156 if(bnstate[0]) {
nuclear@0 157 cam_theta += dx * 0.5;
nuclear@0 158 cam_phi += dy * 0.5;
nuclear@0 159
nuclear@0 160 if(cam_phi < -90) cam_phi = -90;
nuclear@0 161 if(cam_phi > 90) cam_phi = 90;
nuclear@0 162 glutPostRedisplay();
nuclear@0 163 }
nuclear@0 164 if(bnstate[2]) {
nuclear@0 165 cam_dist += dy * 0.1;
nuclear@0 166
nuclear@0 167 if(cam_dist < 0.0) cam_dist = 0.0;
nuclear@0 168 glutPostRedisplay();
nuclear@0 169 }
nuclear@0 170 }
nuclear@0 171
nuclear@0 172 float eval(struct metasurface *ms, float x, float y, float z)
nuclear@0 173 {
nuclear@0 174 struct quat q;
nuclear@0 175
nuclear@0 176 q.w = grid_scale * x;
nuclear@0 177 q.x = grid_scale * y;
nuclear@0 178 q.y = grid_scale * z;
nuclear@0 179 q.z = 0.0f;
nuclear@0 180
nuclear@0 181 return julia_dist(&q, &seed, max_iter);
nuclear@0 182 }
nuclear@0 183
nuclear@0 184 void vertex(struct metasurface *ms, float x, float y, float z)
nuclear@0 185 {
nuclear@0 186 struct quat q;
nuclear@0 187 float dist;
nuclear@0 188 float norm[3];
nuclear@0 189 float norm_len, nscale = 1.0;
nuclear@0 190
nuclear@0 191 q.w = grid_scale * x;
nuclear@0 192 q.x = grid_scale * y;
nuclear@0 193 q.y = grid_scale * z;
nuclear@0 194 q.z = 0.0f;
nuclear@0 195
nuclear@0 196 dist = julia_dist(&q, &seed, max_iter);
nuclear@0 197
nuclear@0 198 julia_grad(norm, dist, &q, &seed, max_iter);
nuclear@0 199 norm_len = sqrt(norm[0] * norm[0] + norm[1] * norm[1] + norm[2] * norm[2]);
nuclear@0 200
nuclear@0 201 if(norm_len != 0.0f) {
nuclear@0 202 nscale = 1.0f / norm_len;
nuclear@0 203 }
nuclear@0 204
nuclear@0 205 glNormal3f(norm[0] * nscale, norm[1] * nscale, norm[2] * nscale);
nuclear@0 206 glVertex3f(x, y, z);
nuclear@0 207
nuclear@0 208 if(fp) {
nuclear@0 209 static int nverts;
nuclear@0 210
nuclear@0 211 fprintf(fp, "v %f %f %f\n", x, y, z);
nuclear@0 212
nuclear@0 213 if((++nverts) % 3 == 0) {
nuclear@0 214 fprintf(fp, "f %d %d %d\n", nverts - 2, nverts - 1, nverts);
nuclear@0 215 }
nuclear@0 216 }
nuclear@0 217 }
nuclear@0 218
nuclear@0 219 void quat_mul(struct quat *res, struct quat *a, struct quat *b)
nuclear@0 220 {
nuclear@0 221 float w = a->w * b->w - (a->x * b->x + a->y * b->y + a->z * b->z);
nuclear@0 222 float x = a->w * b->x + b->w * a->x + (a->y * b->z - a->z * b->y);
nuclear@0 223 float y = a->w * b->y + b->w * a->y + (a->z * b->x - a->x * b->z);
nuclear@0 224 res->z = a->w * b->z + b->w * a->z + (a->x * b->y - a->y * b->x);
nuclear@0 225 res->x = x;
nuclear@0 226 res->y = y;
nuclear@0 227 res->w = w;
nuclear@0 228 }
nuclear@0 229
nuclear@0 230 void quat_sq(struct quat *q)
nuclear@0 231 {
nuclear@0 232 float w = q->w * q->w - (q->x * q->x + q->y * q->y + q->z * q->z);
nuclear@0 233 float x = 2.0 * q->w * q->x;
nuclear@0 234 float y = 2.0 * q->w * q->y;
nuclear@0 235 q->z = 2.0 * q->w * q->z;
nuclear@0 236 q->x = x;
nuclear@0 237 q->y = y;
nuclear@0 238 q->w = w;
nuclear@0 239 }
nuclear@0 240
nuclear@0 241 float quat_lensq(struct quat *q)
nuclear@0 242 {
nuclear@0 243 return q->x * q->x + q->y * q->y + q->z * q->z + q->w * q->w;
nuclear@0 244 }
nuclear@0 245
nuclear@0 246 int julia(struct quat *qres, struct quat *qprime, struct quat *q, struct quat *seed, int max_iter)
nuclear@0 247 {
nuclear@0 248 int i;
nuclear@0 249
nuclear@0 250 *qres = *q;
nuclear@0 251 qprime->x = qprime->y = qprime->z = 0.0f;
nuclear@0 252 qprime->w = 1.0f;
nuclear@0 253
nuclear@0 254 for(i=0; i<max_iter; i++) {
nuclear@0 255 quat_mul(qprime, qres, qprime);
nuclear@0 256 qprime->x *= 2.0;
nuclear@0 257 qprime->y *= 2.0;
nuclear@0 258 qprime->z *= 2.0;
nuclear@0 259 qprime->w *= 2.0;
nuclear@0 260
nuclear@0 261 quat_sq(qres);
nuclear@0 262 qres->x += seed->x;
nuclear@0 263 qres->y += seed->y;
nuclear@0 264 qres->z += seed->z;
nuclear@0 265 qres->w += seed->w;
nuclear@0 266
nuclear@0 267 if(quat_lensq(qres) > 8.0) {
nuclear@0 268 return 0;
nuclear@0 269 }
nuclear@0 270 }
nuclear@0 271
nuclear@0 272 return 1;
nuclear@0 273 }
nuclear@0 274
nuclear@0 275 float julia_dist(struct quat *z, struct quat *seed, int max_iter)
nuclear@0 276 {
nuclear@0 277 struct quat qprime, q;
nuclear@0 278
nuclear@0 279 /* calc julia at z */
nuclear@0 280 /*int inside = */julia(&q, &qprime, z, seed, max_iter);
nuclear@0 281
nuclear@0 282 float lenq = sqrt(quat_lensq(&q));
nuclear@0 283 float lenqp = sqrt(quat_lensq(&qprime));
nuclear@0 284 return 0.5 * lenq * log(lenq) / lenqp;
nuclear@0 285 }
nuclear@0 286
nuclear@0 287 #define OFFS 1e-4
nuclear@0 288 void julia_grad(float *grad, float dist, struct quat *q, struct quat *seed, int max_iter)
nuclear@0 289 {
nuclear@0 290 struct quat qnext = *q;
nuclear@0 291 struct quat qprev = *q;
nuclear@0 292
nuclear@0 293 qnext.w += OFFS;
nuclear@0 294 qprev.w -= OFFS;
nuclear@0 295 grad[0] = julia_dist(&qnext, seed, max_iter) - julia_dist(&qprev, seed, max_iter);
nuclear@0 296 qnext.w = qprev.w = q->w;
nuclear@0 297
nuclear@0 298 qnext.x += OFFS;
nuclear@0 299 qprev.x -= OFFS;
nuclear@0 300 grad[1] = julia_dist(&qnext, seed, max_iter) - julia_dist(&qprev, seed, max_iter);
nuclear@0 301 qnext.x = qprev.x = q->x;
nuclear@0 302
nuclear@0 303 qnext.y += OFFS;
nuclear@0 304 qprev.y -= OFFS;
nuclear@0 305 grad[2] = julia_dist(&qnext, seed, max_iter) - julia_dist(&qprev, seed, max_iter);
nuclear@0 306 }
nuclear@0 307
nuclear@0 308 void show_waitscr(void)
nuclear@0 309 {
nuclear@0 310 const char *text = "Please wait, generating fractal...";
nuclear@0 311 glClear(GL_COLOR_BUFFER_BIT);
nuclear@0 312
nuclear@0 313 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 314 glDisable(GL_LIGHTING);
nuclear@0 315 glDisable(GL_DEPTH_TEST);
nuclear@0 316
nuclear@0 317 glMatrixMode(GL_PROJECTION);
nuclear@0 318 glPushMatrix();
nuclear@0 319 glLoadIdentity();
nuclear@0 320 glTranslatef(-0.75, 0, 0);
nuclear@0 321 glScalef(0.00075, 0.00075, 0.00075);
nuclear@0 322 glMatrixMode(GL_MODELVIEW);
nuclear@0 323
nuclear@0 324 glEnable(GL_LINE_SMOOTH);
nuclear@0 325 glEnable(GL_BLEND);
nuclear@0 326 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
nuclear@0 327
nuclear@0 328 glLineWidth(1.5);
nuclear@0 329
nuclear@0 330 glColor3f(1, 1, 1);
nuclear@0 331 while(*text) {
nuclear@0 332 glutStrokeCharacter(GLUT_STROKE_ROMAN, *text++);
nuclear@0 333 }
nuclear@0 334
nuclear@0 335 glMatrixMode(GL_PROJECTION);
nuclear@0 336 glPopMatrix();
nuclear@0 337
nuclear@0 338 glPopAttrib();
nuclear@0 339
nuclear@0 340 glutSwapBuffers();
nuclear@0 341 }