voxfract

view src/main.c @ 1:d0297d001505

some command line options
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 15 Jun 2017 05:10:00 +0300
parents 8171de5a000b
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
5 #include <GL/glut.h>
6 #include <metasurf.h>
8 struct quat {
9 float x, y, z, w;
10 };
12 int init(void);
13 void cleanup(void);
14 void display(void);
15 void reshape(int x, int y);
16 void keypress(unsigned char key, int x, int y);
17 void mouse(int bn, int st, int x, int y);
18 void motion(int x, int y);
20 float eval(struct metasurface *ms, float x, float y, float z);
21 void vertex(struct metasurface *ms, float x, float y, float z);
23 int julia(struct quat *qres, struct quat *qprime, struct quat *q, struct quat *seed, int max_iter);
24 float julia_dist(struct quat *z, struct quat *seed, int max_iter);
25 void julia_grad(float *grad, struct quat *q, struct quat *seed, int max_iter);
27 void show_waitscr(void);
29 float cam_theta, cam_phi = 25, cam_dist = 5;
30 int grid_size = 300;
31 float grid_scale = 1.7;
32 struct quat seed = {0.4, 0.0, 0.0, -0.8};
33 int max_iter = 10;
35 struct metasurface *msurf;
36 int dlist;
37 FILE *fp;
39 int main(int argc, char **argv)
40 {
41 int i;
43 for(i=1; i<argc; i++) {
44 if(argv[i][0] == '-') {
45 if(argv[i][2] != 0) {
46 fprintf(stderr, "invalid option: %s\n", argv[i]);
47 return 1;
48 }
49 switch(argv[i][1]) {
50 case 'g':
51 if((grid_size = atoi(argv[++i])) <= 0) {
52 fprintf(stderr, "invalid grid size value: %s\n", argv[i]);
53 return 1;
54 }
55 break;
57 case 'i':
58 if((max_iter = atoi(argv[++i])) <= 0) {
59 fprintf(stderr, "invalid iterations count: %s\n", argv[i]);
60 return 1;
61 }
62 break;
64 default:
65 fprintf(stderr, "invalid option: %s\n", argv[i]);
66 return 1;
67 }
68 } else {
69 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
70 return 1;
71 }
72 }
73 printf("grid size: %d\n", grid_size);
74 printf("max iter: %d\n", max_iter);
76 glutInit(&argc, argv);
77 glutInitWindowSize(800, 600);
78 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
79 glutCreateWindow("voxel fractals");
81 glutDisplayFunc(display);
82 glutReshapeFunc(reshape);
83 glutKeyboardFunc(keypress);
84 glutMouseFunc(mouse);
85 glutMotionFunc(motion);
87 if(init() == -1) {
88 return 1;
89 }
90 atexit(cleanup);
92 glutMainLoop();
93 return 0;
94 }
96 int init(void)
97 {
98 glEnable(GL_DEPTH_TEST);
99 glEnable(GL_CULL_FACE);
100 glEnable(GL_LIGHTING);
101 glEnable(GL_LIGHT0);
102 /*glShadeModel(GL_FLAT);*/
104 fp = fopen("julia.obj", "wb");
106 if(!(msurf = msurf_create())) {
107 return -1;
108 }
109 msurf_eval_func(msurf, eval);
110 msurf_vertex_func(msurf, vertex);
111 msurf_set_resolution(msurf, grid_size, grid_size, grid_size);
112 msurf_set_threshold(msurf, 0.0);
113 msurf_set_inside(msurf, MSURF_LESS);
115 show_waitscr();
117 dlist = glGenLists(1);
118 glNewList(dlist, GL_COMPILE);
120 glBegin(GL_TRIANGLES);
121 msurf_polygonize(msurf);
122 glEnd();
124 glEndList();
126 if(fp) {
127 fclose(fp);
128 }
130 glClearColor(0.05, 0.05, 0.05, 1);
132 return 0;
133 }
135 void cleanup(void)
136 {
137 msurf_free(msurf);
138 }
140 void display(void)
141 {
142 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
144 glMatrixMode(GL_MODELVIEW);
145 glLoadIdentity();
146 glTranslatef(0, 0, -cam_dist);
147 glRotatef(cam_phi, 1, 0, 0);
148 glRotatef(cam_theta, 0, 1, 0);
150 glCallList(dlist);
152 assert(glGetError() == GL_NO_ERROR);
153 glutSwapBuffers();
154 }
156 void reshape(int x, int y)
157 {
158 glViewport(0, 0, x, y);
159 glMatrixMode(GL_PROJECTION);
160 glLoadIdentity();
161 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
162 }
164 void keypress(unsigned char key, int x, int y)
165 {
166 static int flat;
168 switch(key) {
169 case 27:
170 exit(0);
172 case 's':
173 flat = !flat;
174 if(flat) {
175 glShadeModel(GL_FLAT);
176 } else {
177 glShadeModel(GL_SMOOTH);
178 }
179 glutPostRedisplay();
180 }
181 }
183 int prev_x, prev_y;
184 int bnstate[8];
186 void mouse(int bn, int st, int x, int y)
187 {
188 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
189 prev_x = x;
190 prev_y = y;
191 }
193 void motion(int x, int y)
194 {
195 int dx = x - prev_x;
196 int dy = y - prev_y;
197 prev_x = x;
198 prev_y = y;
200 if(!dx && !dy) return;
202 if(bnstate[0]) {
203 cam_theta += dx * 0.5;
204 cam_phi += dy * 0.5;
206 if(cam_phi < -90) cam_phi = -90;
207 if(cam_phi > 90) cam_phi = 90;
208 glutPostRedisplay();
209 }
210 if(bnstate[2]) {
211 cam_dist += dy * 0.1;
213 if(cam_dist < 0.0) cam_dist = 0.0;
214 glutPostRedisplay();
215 }
216 }
218 float eval(struct metasurface *ms, float x, float y, float z)
219 {
220 struct quat q;
222 q.w = grid_scale * x;
223 q.x = grid_scale * y;
224 q.y = grid_scale * z;
225 q.z = 0.0f;
227 return julia_dist(&q, &seed, max_iter);
228 }
230 void vertex(struct metasurface *ms, float x, float y, float z)
231 {
232 struct quat q;
233 float norm[3];
234 float norm_len, nscale = 1.0;
236 q.w = grid_scale * x;
237 q.x = grid_scale * y;
238 q.y = grid_scale * z;
239 q.z = 0.0f;
241 julia_grad(norm, &q, &seed, max_iter);
242 norm_len = sqrt(norm[0] * norm[0] + norm[1] * norm[1] + norm[2] * norm[2]);
244 if(norm_len != 0.0) {
245 nscale = 1.0 / norm_len;
246 }
248 glNormal3f(norm[0] * nscale, norm[1] * nscale, norm[2] * nscale);
249 glVertex3f(x, y, z);
251 if(fp) {
252 static int nverts;
254 fprintf(fp, "v %f %f %f\n", x, y, z);
256 if((++nverts) % 3 == 0) {
257 fprintf(fp, "f %d %d %d\n", nverts - 2, nverts - 1, nverts);
258 }
259 }
260 }
262 void quat_mul(struct quat *res, struct quat *a, struct quat *b)
263 {
264 float w = a->w * b->w - (a->x * b->x + a->y * b->y + a->z * b->z);
265 float x = a->w * b->x + b->w * a->x + (a->y * b->z - a->z * b->y);
266 float y = a->w * b->y + b->w * a->y + (a->z * b->x - a->x * b->z);
267 res->z = a->w * b->z + b->w * a->z + (a->x * b->y - a->y * b->x);
268 res->x = x;
269 res->y = y;
270 res->w = w;
271 }
273 void quat_sq(struct quat *q)
274 {
275 float w = q->w * q->w - (q->x * q->x + q->y * q->y + q->z * q->z);
276 float x = 2.0 * q->w * q->x;
277 float y = 2.0 * q->w * q->y;
278 q->z = 2.0 * q->w * q->z;
279 q->x = x;
280 q->y = y;
281 q->w = w;
282 }
284 float quat_lensq(struct quat *q)
285 {
286 return q->x * q->x + q->y * q->y + q->z * q->z + q->w * q->w;
287 }
289 int julia(struct quat *qres, struct quat *qprime, struct quat *q, struct quat *seed, int max_iter)
290 {
291 int i;
293 *qres = *q;
294 qprime->x = qprime->y = qprime->z = 0.0f;
295 qprime->w = 1.0f;
297 for(i=0; i<max_iter; i++) {
298 quat_mul(qprime, qres, qprime);
299 qprime->x *= 2.0;
300 qprime->y *= 2.0;
301 qprime->z *= 2.0;
302 qprime->w *= 2.0;
304 quat_sq(qres);
305 qres->x += seed->x;
306 qres->y += seed->y;
307 qres->z += seed->z;
308 qres->w += seed->w;
310 if(quat_lensq(qres) > 8.0) {
311 return 0;
312 }
313 }
315 return 1;
316 }
318 float julia_dist(struct quat *z, struct quat *seed, int max_iter)
319 {
320 struct quat qprime, q;
322 /* calc julia at z */
323 /*int inside = */julia(&q, &qprime, z, seed, max_iter);
325 float lenq = sqrt(quat_lensq(&q));
326 float lenqp = sqrt(quat_lensq(&qprime));
327 return 0.5 * lenq * log(lenq) / lenqp;
328 }
330 #define OFFS 1e-5
331 void julia_grad(float *grad, struct quat *q, struct quat *seed, int max_iter)
332 {
333 struct quat qnext = *q;
334 struct quat qprev = *q;
336 qnext.w += OFFS;
337 qprev.w -= OFFS;
338 grad[0] = julia_dist(&qnext, seed, max_iter) - julia_dist(&qprev, seed, max_iter);
339 qnext.w = qprev.w = q->w;
341 qnext.x += OFFS;
342 qprev.x -= OFFS;
343 grad[1] = julia_dist(&qnext, seed, max_iter) - julia_dist(&qprev, seed, max_iter);
344 qnext.x = qprev.x = q->x;
346 qnext.y += OFFS;
347 qprev.y -= OFFS;
348 grad[2] = julia_dist(&qnext, seed, max_iter) - julia_dist(&qprev, seed, max_iter);
349 }
351 void show_waitscr(void)
352 {
353 const char *text = "Please wait, generating fractal...";
354 glClear(GL_COLOR_BUFFER_BIT);
356 glPushAttrib(GL_ENABLE_BIT);
357 glDisable(GL_LIGHTING);
358 glDisable(GL_DEPTH_TEST);
360 glMatrixMode(GL_PROJECTION);
361 glPushMatrix();
362 glLoadIdentity();
363 glTranslatef(-0.75, 0, 0);
364 glScalef(0.00075, 0.00075, 0.00075);
365 glMatrixMode(GL_MODELVIEW);
367 glEnable(GL_LINE_SMOOTH);
368 glEnable(GL_BLEND);
369 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
371 glLineWidth(1.5);
373 glColor3f(1, 1, 1);
374 while(*text) {
375 glutStrokeCharacter(GLUT_STROKE_ROMAN, *text++);
376 }
378 glMatrixMode(GL_PROJECTION);
379 glPopMatrix();
381 glPopAttrib();
383 glutSwapBuffers();
384 }