fractorb

diff src/main.c @ 3:f440ecffc45a

trying to draw the orbit in the shader
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 26 Nov 2017 14:49:34 +0200
parents 03e8b9a5031d
children
line diff
     1.1 --- a/src/main.c	Mon Nov 20 03:53:02 2017 +0200
     1.2 +++ b/src/main.c	Sun Nov 26 14:49:34 2017 +0200
     1.3 @@ -8,6 +8,17 @@
     1.4  #include <GL/glut.h>
     1.5  #include "sdr.h"
     1.6  
     1.7 +enum {
     1.8 +	SDR_LINE_DIST_BIT = 1,
     1.9 +	SDR_PLOT_ORBIT_BIT = 2
    1.10 +};
    1.11 +const char *sdrdef[] = {
    1.12 +	"#define LINE_DIST\n",
    1.13 +	"#define PLOT_ORBIT\n"
    1.14 +};
    1.15 +#define NUM_SDR_BITS	(sizeof sdrdef / sizeof *sdrdef)
    1.16 +#define NUM_SHADERS		(1 << NUM_SDR_BITS)
    1.17 +
    1.18  int init(void);
    1.19  void cleanup(void);
    1.20  void disp(void);
    1.21 @@ -21,11 +32,12 @@
    1.22  static int win_width = 1280, win_height = 800;
    1.23  static float win_aspect;
    1.24  static int mouse_x = 640, mouse_y = 400;
    1.25 -static unsigned int prog_mbrot;
    1.26 +static unsigned int prog_mbrot[NUM_SHADERS];
    1.27  
    1.28  static float view_center[2] = {0.7, 0.0};
    1.29  static float view_scale = 1.2;
    1.30  
    1.31 +static unsigned int sdrflags;
    1.32  
    1.33  int main(int argc, char **argv)
    1.34  {
    1.35 @@ -53,17 +65,57 @@
    1.36  
    1.37  int init(void)
    1.38  {
    1.39 +	int i, j;
    1.40 +	unsigned int vs = 0, ps[NUM_SHADERS] = {0};
    1.41 +
    1.42  	glewInit();
    1.43  
    1.44 -	if(!(prog_mbrot = create_program_load("vertex.glsl", "mbrot.glsl"))) {
    1.45 +	if(!(vs = load_vertex_shader("vertex.glsl"))) {
    1.46  		return -1;
    1.47  	}
    1.48 +
    1.49 +	for(i=0; i<NUM_SHADERS; i++) {
    1.50 +		clear_shader_header(GL_FRAGMENT_SHADER);
    1.51 +		for(j=0; j<NUM_SDR_BITS; j++) {
    1.52 +			if(i & (1 << j)) {
    1.53 +				add_shader_header(GL_FRAGMENT_SHADER, sdrdef[j]);
    1.54 +			}
    1.55 +		}
    1.56 +
    1.57 +		if(!(ps[i] = load_pixel_shader("mbrot.glsl"))) {
    1.58 +			goto err;
    1.59 +		}
    1.60 +
    1.61 +		if(!(prog_mbrot[i] = create_program_link(vs, ps[i], 0))) {
    1.62 +			goto err;
    1.63 +		}
    1.64 +	}
    1.65  	return 0;
    1.66 +
    1.67 +err:
    1.68 +	for(i=0; i<NUM_SHADERS; i++) {
    1.69 +		if(prog_mbrot[i]) {
    1.70 +			free_program(prog_mbrot[i]);
    1.71 +			prog_mbrot[i] = 0;
    1.72 +		}
    1.73 +		if(ps[i]) {
    1.74 +			free_shader(ps[i]);
    1.75 +		}
    1.76 +	}
    1.77 +	if(vs) {
    1.78 +		free_shader(vs);
    1.79 +	}
    1.80 +	return -1;
    1.81  }
    1.82  
    1.83  void cleanup(void)
    1.84  {
    1.85 -	free_program(prog_mbrot);
    1.86 +	int i;
    1.87 +
    1.88 +	for(i=0; i<NUM_SHADERS; i++) {
    1.89 +		free_program(prog_mbrot[i]);
    1.90 +		prog_mbrot[i] = 0;
    1.91 +	}
    1.92  }
    1.93  
    1.94  void pixel_to_complex(float *res, float px, float py)
    1.95 @@ -83,10 +135,10 @@
    1.96  
    1.97  	pixel_to_complex(seed, mouse_x, mouse_y);
    1.98  
    1.99 -	set_uniform_float2(prog_mbrot, "seed", seed[0], seed[1]);
   1.100 -	set_uniform_float(prog_mbrot, "view_scale", view_scale);
   1.101 -	set_uniform_float2(prog_mbrot, "view_center", view_center[0], view_center[1]);
   1.102 -	glUseProgram(prog_mbrot);
   1.103 +	set_uniform_float2(prog_mbrot[sdrflags], "seed", seed[0], seed[1]);
   1.104 +	set_uniform_float(prog_mbrot[sdrflags], "view_scale", view_scale);
   1.105 +	set_uniform_float2(prog_mbrot[sdrflags], "view_center", view_center[0], view_center[1]);
   1.106 +	glUseProgram(prog_mbrot[sdrflags]);
   1.107  
   1.108  	glBegin(GL_QUADS);
   1.109  	for(i=0; i<4; i++) {
   1.110 @@ -131,6 +183,16 @@
   1.111  		}
   1.112  		break;
   1.113  
   1.114 +	case ' ':
   1.115 +		sdrflags ^= SDR_LINE_DIST_BIT;
   1.116 +		glutPostRedisplay();
   1.117 +		break;
   1.118 +
   1.119 +	case 'o':
   1.120 +		sdrflags ^= SDR_PLOT_ORBIT_BIT;
   1.121 +		glutPostRedisplay();
   1.122 +		break;
   1.123 +
   1.124  	case '`':
   1.125  		screenshot();
   1.126  		break;