diff options
Diffstat (limited to 'fractal.c')
-rw-r--r-- | fractal.c | 199 |
1 files changed, 184 insertions, 15 deletions
@@ -27,7 +27,7 @@ #define SCREEN_WIDTH2 (SCREEN_WIDTH/2) #define SCREEN_HEIGHT 320 #define SCREEN_HEIGHT2 (SCREEN_HEIGHT/2) -#define MAX_GENERATION 5 +//#define MAX_GENERATION 5 static int quit=0; static SDL_Window *window = NULL; @@ -133,6 +133,67 @@ rgba32_t color_gen_mainlv(rgba32_t original, int gen, int max_gen) return c; } +rgba32_t color_gen_redish(rgba32_t original, int gen, int max_gen) +{ + double color_r = 20.0/255; + double color_g = 10.0/255; + double color_b = 14.0/255; + rgba32_t c = {color_r, color_g, color_b,0}; + + /* + c.r = 1.0*180/255 + (gen-max_gen)*color_r; + c.g = 1.0*100/255 + (gen-max_gen)*color_g; + c.b = 1.0*50/255 + (gen-max_gen)*color_b; + */ + c.r = 1.0*180/255 - (max_gen-gen)*color_r; + c.g = 1.0*30/255 - (max_gen-gen)*color_g; + c.b = 1.0*10/255 - (max_gen-gen)*color_b; + c.a = original.a; + + return c; +} + +rgba32_t color_gen_yellowish(rgba32_t original, int gen, int max_gen) +{ + double color_r = 10.0/255; + double color_g = 20.0/255; + double color_b = 14.0/255; + rgba32_t c = {color_r, color_g, color_b,0}; + + /* + c.r = 1.0*180/255 + (gen-max_gen)*color_r; + c.g = 1.0*100/255 + (gen-max_gen)*color_g; + c.b = 1.0*50/255 + (gen-max_gen)*color_b; + */ + c.r = 1.0*180/255 - (max_gen-gen)*color_r; + c.g = 1.0*160/255 - (max_gen-gen)*color_g; + c.b = 1.0*40/255 - (max_gen-gen)*color_b; + c.a = original.a; + + return c; +} + +rgba32_t color_switch(int pattern_id, rgba32_t original, int gen, int max_gen) +{ + switch(pattern_id) + { + case 0: + return color_gen_shade(original, gen, max_gen); + case 1: + return color_gen_shade_inv(original, gen, max_gen); + case 2: + return color_gen_mainlv(original, gen, max_gen); + case 3: + return color_gen_yellowish(original, gen, max_gen); + case 4: + return color_gen_redish(original, gen, max_gen); + default: + return color_gen_mainlv(original, gen, max_gen); + } + + return color_gen_mainlv(original, gen, max_gen); +} + void generate_koh(int gener, double size, int posx, int posy, double k) { int i; @@ -314,6 +375,27 @@ void RectPXcolor( int x, int y,int w, int h, rgba32_t c ) glDrawArrays ( GL_TRIANGLE_FAN, 0, 4 ); + glDeleteBuffers( 1, &vertexPosObject ); + glDeleteBuffers( 1, &colorBuffer ); + +} + +void koh_generation_inc() +{ + koh_set_config.max_generations += 1; + if (koh_set_config.max_generations >= MAX_LEVEL) + { + koh_set_config.max_generations = MAX_LEVEL-1; + } +} + +void koh_generation_dec() +{ + koh_set_config.max_generations -= 1; + if (koh_set_config.max_generations < 0) + { + koh_set_config.max_generations = 0; + } } void koh_pos_x_add(int x) @@ -336,6 +418,36 @@ void koh_pos_y_sub(int y) koh_set_config.y -= 10; } +void koh_pattern_next() +{ + koh_set_config.color_pattern += 1; + if (koh_set_config.color_pattern>4) + { + koh_set_config.color_pattern=2; + } +} + +void koh_pattern_prev() +{ + koh_set_config.color_pattern -= 1; + if (koh_set_config.color_pattern<0) + koh_set_config.color_pattern=0; +} + +void koh_size_dec() +{ + koh_set_config.size -= 25; + if (koh_set_config.size<50) + koh_set_config.size = 50; +} + +void koh_size_inc() +{ + koh_set_config.size += 25; + if (koh_set_config.size>400) + koh_set_config.size = 400; +} + rgba32_t start_col = {0.0,1.0,0.0,0.0}; #if __EMSCRIPTEN__ @@ -361,14 +473,16 @@ int main_tick() { { switch (event.key.keysym.sym) { - + case 81: case SDLK_DOWN: update_fractal = 1; - koh_pos_y_add(10); + koh_pos_y_sub(10); break; + + case 82: case SDLK_UP: update_fractal = 1; - koh_pos_y_sub(10); + koh_pos_y_add(10); break; case 80: case SDLK_LEFT: @@ -382,6 +496,54 @@ int main_tick() { printf("add 10\n"); koh_pos_x_add(10); break; + + case 61: + case 46: + case SDLK_KP_PLUS: + case SDLK_PLUS: + update_fractal=1; + koh_generation_inc(); + printf("plus\n"); + break; + + case SDLK_MINUS: + update_fractal = 1; + koh_generation_dec(); + break; + + case 21: + case SDLK_r: + update_fractal = 1; + koh_set_config.size = 100.0; + koh_set_config.x = SCREEN_WIDTH2-100/2; + koh_set_config.y = SCREEN_WIDTH2-100/2; + koh_set_config.k = 0.5f; + koh_set_config.max_generations = MAX_LEVEL-2; + koh_set_config.color_pattern = 0; + break; + + case 20: + case SDLK_q: + koh_pattern_prev(); + break; + + case 26: + case SDLK_w: + koh_pattern_next(); + break; + + case 4: + case SDLK_a: + update_fractal = 1; + koh_size_dec(); + break; + + case 22: + case SDLK_s: + update_fractal = 1; + koh_size_inc(); + break; + default: printf("Unknown key %d\n",event.key.keysym.sym); update_fractal = 1; @@ -389,12 +551,13 @@ int main_tick() { break; } } - printf("Poll event\n"); + //printf("Poll event\n"); } if (update_fractal == 1) { queue_init(); + printf("k=%f",koh_set_config.k); generate_koh( koh_set_config.max_generations, koh_set_config.size, @@ -410,10 +573,11 @@ int main_tick() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(shaderProgram); - for (i=0;i<=MAX_GENERATION;i++) + for (i=0;i<=koh_set_config.max_generations;i++) { //rgba32_t c = color_gen_shade(start_col, i, MAX_GENERATION); - rgba32_t c = color_gen_mainlv(start_col, i, MAX_GENERATION); + //rgba32_t c = color_gen_mainlv(start_col, i, MAX_LEVEL); + rgba32_t c = color_switch(koh_set_config.color_pattern, start_col, i, MAX_LEVEL); //static int once=0 //glColor(1.0f,0.0,0.0,0.0); for (j=0;j<queue_index;j++) @@ -425,18 +589,22 @@ int main_tick() { koh_node kn = queue_get(j); //printf("j:%d %d %d %d\n",j,kn.x, kn.y, kn.size); //RectPX(kn.x, kn.y, kn.size, kn.size); - //if (update_fractal == 1) + #ifndef __EMSCRIPTEN__ + if (update_fractal == 1) + #endif RectPXcolor(kn.x, kn.y, kn.size, kn.size, c); } } } - //if (update_fractal == 1) + #ifndef __EMSCRIPTEN__ + if (update_fractal == 1) + #endif { - printf("SWAP\n"); + //printf("SWAP\n"); SDL_GL_SwapWindow( window ); } //SDL_UpdateWindowSurface(window); - SDL_Delay( 20 ); + //SDL_Delay( 20 ); update_fractal=0; #if !__EMSCRIPTEN__ @@ -448,14 +616,15 @@ void main_loop() { #if __EMSCRIPTEN__ - emscripten_set_main_loop(main_tick, -1, 1); - //emscripten_set_main_loop(main_tick, 10, 1); + //emscripten_set_main_loop(main_tick, -1, 1); + //emscripten_set_main_loop(main_tick, 100, 1); + emscripten_set_main_loop(main_tick, 25, 1); #else while (0 == quit) { main_tick(); - printf("One tick\n"); + //printf("One tick\n"); } #endif } @@ -508,7 +677,7 @@ int main() koh_set_config.x = SCREEN_WIDTH2-frac_size/2; koh_set_config.y = SCREEN_WIDTH2-frac_size/2; koh_set_config.k = 0.5; - koh_set_config.max_generations = MAX_GENERATION; + koh_set_config.max_generations = MAX_LEVEL-2; generate_koh( koh_set_config.max_generations, |