voxfract

diff 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 diff
     1.1 --- a/src/main.c	Wed Jun 14 18:03:57 2017 +0300
     1.2 +++ b/src/main.c	Thu Jun 15 05:10:00 2017 +0300
     1.3 @@ -22,15 +22,15 @@
     1.4  
     1.5  int julia(struct quat *qres, struct quat *qprime, struct quat *q, struct quat *seed, int max_iter);
     1.6  float julia_dist(struct quat *z, struct quat *seed, int max_iter);
     1.7 -void julia_grad(float *grad, float dist, struct quat *q, struct quat *seed, int max_iter);
     1.8 +void julia_grad(float *grad, struct quat *q, struct quat *seed, int max_iter);
     1.9  
    1.10  void show_waitscr(void);
    1.11  
    1.12  float cam_theta, cam_phi = 25, cam_dist = 5;
    1.13 -int grid_size = 350;
    1.14 +int grid_size = 300;
    1.15  float grid_scale = 1.7;
    1.16  struct quat seed = {0.4, 0.0, 0.0, -0.8};
    1.17 -int max_iter = 9;
    1.18 +int max_iter = 10;
    1.19  
    1.20  struct metasurface *msurf;
    1.21  int dlist;
    1.22 @@ -38,6 +38,41 @@
    1.23  
    1.24  int main(int argc, char **argv)
    1.25  {
    1.26 +	int i;
    1.27 +
    1.28 +	for(i=1; i<argc; i++) {
    1.29 +		if(argv[i][0] == '-') {
    1.30 +			if(argv[i][2] != 0) {
    1.31 +				fprintf(stderr, "invalid option: %s\n", argv[i]);
    1.32 +				return 1;
    1.33 +			}
    1.34 +			switch(argv[i][1]) {
    1.35 +			case 'g':
    1.36 +				if((grid_size = atoi(argv[++i])) <= 0) {
    1.37 +					fprintf(stderr, "invalid grid size value: %s\n", argv[i]);
    1.38 +					return 1;
    1.39 +				}
    1.40 +				break;
    1.41 +
    1.42 +			case 'i':
    1.43 +				if((max_iter = atoi(argv[++i])) <= 0) {
    1.44 +					fprintf(stderr, "invalid iterations count: %s\n", argv[i]);
    1.45 +					return 1;
    1.46 +				}
    1.47 +				break;
    1.48 +
    1.49 +			default:
    1.50 +				fprintf(stderr, "invalid option: %s\n", argv[i]);
    1.51 +				return 1;
    1.52 +			}
    1.53 +		} else {
    1.54 +			fprintf(stderr, "unexpected argument: %s\n", argv[i]);
    1.55 +			return 1;
    1.56 +		}
    1.57 +	}
    1.58 +	printf("grid size: %d\n", grid_size);
    1.59 +	printf("max iter: %d\n", max_iter);
    1.60 +
    1.61  	glutInit(&argc, argv);
    1.62  	glutInitWindowSize(800, 600);
    1.63  	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    1.64 @@ -128,9 +163,20 @@
    1.65  
    1.66  void keypress(unsigned char key, int x, int y)
    1.67  {
    1.68 +	static int flat;
    1.69 +
    1.70  	switch(key) {
    1.71  	case 27:
    1.72  		exit(0);
    1.73 +
    1.74 +	case 's':
    1.75 +		flat = !flat;
    1.76 +		if(flat) {
    1.77 +			glShadeModel(GL_FLAT);
    1.78 +		} else {
    1.79 +			glShadeModel(GL_SMOOTH);
    1.80 +		}
    1.81 +		glutPostRedisplay();
    1.82  	}
    1.83  }
    1.84  
    1.85 @@ -184,7 +230,6 @@
    1.86  void vertex(struct metasurface *ms, float x, float y, float z)
    1.87  {
    1.88  	struct quat q;
    1.89 -	float dist;
    1.90  	float norm[3];
    1.91  	float norm_len, nscale = 1.0;
    1.92  
    1.93 @@ -193,13 +238,11 @@
    1.94  	q.y = grid_scale * z;
    1.95  	q.z = 0.0f;
    1.96  
    1.97 -	dist = julia_dist(&q, &seed, max_iter);
    1.98 -
    1.99 -	julia_grad(norm, dist, &q, &seed, max_iter);
   1.100 +	julia_grad(norm, &q, &seed, max_iter);
   1.101  	norm_len = sqrt(norm[0] * norm[0] + norm[1] * norm[1] + norm[2] * norm[2]);
   1.102  
   1.103 -	if(norm_len != 0.0f) {
   1.104 -		nscale = 1.0f / norm_len;
   1.105 +	if(norm_len != 0.0) {
   1.106 +		nscale = 1.0 / norm_len;
   1.107  	}
   1.108  
   1.109  	glNormal3f(norm[0] * nscale, norm[1] * nscale, norm[2] * nscale);
   1.110 @@ -284,8 +327,8 @@
   1.111  	return 0.5 * lenq * log(lenq) / lenqp;
   1.112  }
   1.113  
   1.114 -#define OFFS 1e-4
   1.115 -void julia_grad(float *grad, float dist, struct quat *q, struct quat *seed, int max_iter)
   1.116 +#define OFFS 1e-5
   1.117 +void julia_grad(float *grad, struct quat *q, struct quat *seed, int max_iter)
   1.118  {
   1.119  	struct quat qnext = *q;
   1.120  	struct quat qprev = *q;