diff options
Diffstat (limited to 'fractal.c')
-rw-r--r-- | fractal.c | 97 |
1 files changed, 88 insertions, 9 deletions
@@ -43,6 +43,9 @@ typedef struct rgba32_t int queue_index=0; koh_node koh_queue[QUEUE_SIZE]; +koh_config koh_set_config; + + void queue_init() { @@ -242,6 +245,7 @@ GLuint fragmentShader; GLuint shaderProgram; GLint posAttrib; + void RectPX( int x, int y,int w, int h ) { GLfloat x1 = (1.0f*x-SCREEN_WIDTH2)/SCREEN_WIDTH2; @@ -268,6 +272,7 @@ void RectPX( int x, int y,int w, int h ) } + void RectPXcolor( int x, int y,int w, int h, rgba32_t c ) { GLfloat x1 = (1.0f*x-SCREEN_WIDTH2)/SCREEN_WIDTH2; @@ -289,11 +294,12 @@ void RectPXcolor( int x, int y,int w, int h, rgba32_t c ) glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject); glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices), vVertices, GL_STATIC_DRAW); - printf("!!!x1 %f y1 %f x2 %f y2 %f\n",x1,y1,x2,y2); + //printf("!!!x1 %f y1 %f x2 %f y2 %f\n",x1,y1,x2,y2); glGenBuffers(1, &colorBuffer); glBindBuffer(GL_ARRAY_BUFFER, colorBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(cVertices), cVertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject); glVertexAttribPointer(0 /* ? */, 3, GL_FLOAT, GL_FALSE, 0, 0); @@ -310,6 +316,26 @@ void RectPXcolor( int x, int y,int w, int h, rgba32_t c ) } +void koh_pos_x_add(int x) +{ + koh_set_config.x += 10; +} + +void koh_pos_x_sub(int x) +{ + koh_set_config.x -= 10; +} + +void koh_pos_y_add(int y) +{ + koh_set_config.y += 10; +} + +void koh_pos_y_sub(int y) +{ + koh_set_config.y -= 10; +} + rgba32_t start_col = {0.0,1.0,0.0,0.0}; #if __EMSCRIPTEN__ @@ -321,7 +347,7 @@ int main_tick() { int update_fractal=0; SDL_Event event; SDL_StartTextInput(); - while (SDL_PollEvent(&event)) + while (SDL_PollEvent(&event) != 0) { switch (event.type) @@ -335,23 +361,51 @@ int main_tick() { { switch (event.key.keysym.sym) { + case SDLK_DOWN: update_fractal = 1; + koh_pos_y_add(10); break; case SDLK_UP: update_fractal = 1; + koh_pos_y_sub(10); break; + case 80: case SDLK_LEFT: + koh_pos_x_sub(10); update_fractal = 1; + printf("sub 10\n"); break; + case 79: case SDLK_RIGHT: update_fractal = 1; + printf("add 10\n"); + koh_pos_x_add(10); break; + default: + printf("Unknown key %d\n",event.key.keysym.sym); + update_fractal = 1; } + break; } } + printf("Poll event\n"); + } + + if (update_fractal == 1) + { + queue_init(); + generate_koh( + koh_set_config.max_generations, + koh_set_config.size, + koh_set_config.x, + koh_set_config.y, + koh_set_config.k + ); + printf("Regenerated fractal\n"); } + //glClear( GL_COLOR_BUFFER_BIT ); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(shaderProgram); @@ -369,14 +423,21 @@ int main_tick() { if (koh_queue[j].generaton == i) { koh_node kn = queue_get(j); - printf("j:%d %d %d %d\n",j,kn.x, kn.y, kn.size); + //printf("j:%d %d %d %d\n",j,kn.x, kn.y, kn.size); //RectPX(kn.x, kn.y, kn.size, kn.size); - RectPXcolor(kn.x, kn.y, kn.size, kn.size, c); + //if (update_fractal == 1) + RectPXcolor(kn.x, kn.y, kn.size, kn.size, c); } } } - SDL_GL_SwapWindow( window ); - SDL_Delay( 100 ); + //if (update_fractal == 1) + { + printf("SWAP\n"); + SDL_GL_SwapWindow( window ); + } + //SDL_UpdateWindowSurface(window); + SDL_Delay( 20 ); + update_fractal=0; #if !__EMSCRIPTEN__ return 0; @@ -416,7 +477,11 @@ int main() renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_SetRenderDrawColor(renderer, 0xff, 0xaa, 0xaa, 0xaa); SDL_GLContext gl_context = SDL_GL_CreateContext(window); - SDL_GL_SetSwapInterval( 1 ); + SDL_GL_SetSwapInterval( 0 ); + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); + //init OpenGL @@ -437,8 +502,21 @@ int main() printf( "Error initializing OpenGL!\n" ); } + //set initial values const int frac_size=100; - generate_koh(MAX_GENERATION, frac_size, SCREEN_WIDTH2-frac_size/2, SCREEN_HEIGHT2-frac_size/2, 0.5); + koh_set_config.size = 100; + 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; + + generate_koh( + koh_set_config.max_generations, + koh_set_config.size, + koh_set_config.x, + koh_set_config.y, + koh_set_config.k + ); //prepare shaders vertexShader = glCreateShader(GL_VERTEX_SHADER); @@ -479,10 +557,11 @@ int main() glLinkProgram(shaderProgram); glUseProgram(shaderProgram); - posAttrib = glGetAttribLocation(shaderProgram, "vertexPosition"); + posAttrib = glGetAttribLocation(shaderProgram, "position"); glEnableVertexAttribArray(posAttrib); //changed from 2 to 3 glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0); main_loop(); |