From 09a819b2ef927adf5239a73f91cdfcefd6774688 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 20 May 2020 07:41:03 +0200 Subject: Added colors with 1D texture --- src/color.c | 30 ++++++++++++++++++++++-------- src/event.c | 17 ++++++++--------- src/shader.c | 37 +++++++++++++++++++++++-------------- src/state.c | 11 +++++++---- 4 files changed, 60 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/color.c b/src/color.c index 2ad4bad..24ca2f2 100644 --- a/src/color.c +++ b/src/color.c @@ -5,22 +5,36 @@ static Color color_hsl_to_rgb(ColorHSL color_hsl); -Color *color_palette_new(Color *palette, int iterations) +unsigned int color_texture_new(int count) { - ColorHSL hsl; + ColorHSL hsl; + Color *palette; + unsigned int texture; - palette = realloc(palette, sizeof(Color) * (iterations + 1)); + palette = malloc(sizeof(Color) * count); if (palette == NULL) - return NULL; - for (int i = 0; i < iterations; i++) + return 0; + for (int i = 0; i < count; i++) { - hsl.h = (int)(255.0 * ((double)i / (double)iterations)); + hsl.h = (uint8_t)(255.0 * ((double)i / (double)count)); hsl.s = 150; hsl.l = 127; palette[i] = color_hsl_to_rgb(hsl); } - palette[iterations].data = 0x0; - return palette; + for (int i = 0, j = count - 1; i < j; i++, j--) + { + Color tmp = palette[i]; + palette[i] = palette[j]; + palette[j] = tmp; + } + + GL_CALL(glGenTextures(1, &texture)); + GL_CALL(glBindTexture(GL_TEXTURE_1D, texture)); + GL_CALL(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)); + GL_CALL(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)); + GL_CALL(glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, count + 1, 0, GL_RGB, GL_UNSIGNED_BYTE, palette)); + free(palette); + return texture; } static Color color_hsl_to_rgb(ColorHSL color_hsl) diff --git a/src/event.c b/src/event.c index 18763b0..6fd6a8c 100644 --- a/src/event.c +++ b/src/event.c @@ -21,13 +21,13 @@ void event_handle(State *state) { case SDLK_r: state->iterations += 10; - /* state->palette = color_palette_new(state->palette, state->iterations); */ + /* state->texture = color_texture_new( state->iterations); */ break; case SDLK_e: state->iterations -= 10; if (state->iterations <= 0) state->iterations = 1; - /* state->palette = color_palette_new(state->palette, state->iterations); */ + /* state->texture = color_texture_new( state->iterations); */ break; case SDLK_UP: case SDLK_k: @@ -68,15 +68,14 @@ void event_handle(State *state) case SDL_WINDOWEVENT: if (e.window.event == SDL_WINDOWEVENT_RESIZED) { + double w_ratio = (double)state->width / (double)e.window.data1; + double h_ratio = (double)state->height / (double)e.window.data2; SDL_GL_GetDrawableSize(state->window, &state->width, &state->height); GL_CALL(glViewport(0, 0, state->width, state->height)); - /* double w_ratio = (double)width / (double)e.window.data1; */ - /* double h_ratio = (double)height / (double)e.window.data2; */ - /* */ - /* state->real_start /= w_ratio; */ - /* state->real_end /= w_ratio; */ - /* state->imag_start /= h_ratio; */ - /* state->imag_end /= h_ratio; */ + state->real_start /= w_ratio; + state->real_end /= w_ratio; + state->imag_start /= h_ratio; + state->imag_end /= h_ratio; } break; } diff --git a/src/shader.c b/src/shader.c index 0b4091d..68295ab 100644 --- a/src/shader.c +++ b/src/shader.c @@ -4,17 +4,15 @@ #define MANDEL_SHADER_FRAG_FILE "shader/fragment.glsl" static unsigned int st_compile(char *filepath, unsigned int type); +static int st_get_location(unsigned int shader_id, const char *name); bool shader_init(Shader *shader) { unsigned int shader_vert; unsigned int shader_frag; - shader_vert = st_compile(MANDEL_SHADER_VERT_FILE, GL_VERTEX_SHADER); - if (shader_vert == 0) - return false; - shader_frag = st_compile(MANDEL_SHADER_FRAG_FILE, GL_FRAGMENT_SHADER); - if (shader_frag == 0) + if ((shader_vert = st_compile(MANDEL_SHADER_VERT_FILE, GL_VERTEX_SHADER)) == 0 + || (shader_frag = st_compile(MANDEL_SHADER_FRAG_FILE, GL_FRAGMENT_SHADER)) == 0) return false; GL_CALL(shader->id = glCreateProgram()); @@ -25,17 +23,24 @@ bool shader_init(Shader *shader) GL_CALL(glDeleteShader(shader_vert)); GL_CALL(glDeleteShader(shader_frag)); - GL_CALL(shader->location.width = glGetUniformLocation(shader->id, "u_width")); - GL_CALL(shader->location.height = glGetUniformLocation(shader->id, "u_height")); - - GL_CALL(shader->location.real_start = glGetUniformLocation(shader->id, "u_real_start")); - GL_CALL(shader->location.real_end = glGetUniformLocation(shader->id, "u_real_end")); - GL_CALL(shader->location.imag_start = glGetUniformLocation(shader->id, "u_imag_start")); - GL_CALL(shader->location.imag_end = glGetUniformLocation(shader->id, "u_imag_end")); + if ((shader->location.width = st_get_location(shader->id, "u_width")) == -1 + || (shader->location.height = st_get_location(shader->id, "u_height")) == -1 + || (shader->location.real_start = st_get_location(shader->id, "u_real_start")) == -1 + || (shader->location.real_end = st_get_location(shader->id, "u_real_end")) == -1 + || (shader->location.imag_start = st_get_location(shader->id, "u_imag_start")) == -1 + || (shader->location.imag_end = st_get_location(shader->id, "u_imag_end")) == -1 + || (shader->location.iterations = st_get_location(shader->id, "u_iterations")) == -1 + || (shader->location.texture = st_get_location(shader->id, "u_texture")) == -1) + return false; + return true; +} - GL_CALL(shader->location.iterations = glGetUniformLocation(shader->id, "u_iterations")); +static int st_get_location(unsigned int shader_id, const char *name) +{ + int location; - return (true); + GL_CALL(location = glGetUniformLocation(shader_id, name)); + return location; } void shader_set_uniforms(Shader *shader, State *state) @@ -49,6 +54,10 @@ void shader_set_uniforms(Shader *shader, State *state) GL_CALL(glUniform1f(shader->location.imag_end, state->imag_end)); GL_CALL(glUniform1i(shader->location.iterations, state->iterations)); + + GL_CALL(glUniform1i(shader->location.texture, 0)); + GL_CALL(glActiveTexture(GL_TEXTURE0)); + GL_CALL(glBindTexture(GL_TEXTURE_1D, state->texture)); } static unsigned int st_compile(char *filepath, unsigned int type) diff --git a/src/state.c b/src/state.c index 3468464..37eeccc 100644 --- a/src/state.c +++ b/src/state.c @@ -24,6 +24,7 @@ bool state_init(State *state) perror(NULL); return false; } + SDL_GL_GetDrawableSize(state->window, &state->width, &state->height); GL_CALL(glViewport(0, 0, state->width, state->height)); @@ -45,10 +46,10 @@ bool state_init(State *state) GL_CALL(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0)); GL_CALL(glEnableVertexAttribArray(0)); - /* state->palette = color_palette_new(NULL, MANDEL_ITERATIONS); */ - /* if (state->palette == NULL) */ - /* return false; */ state->iterations = MANDEL_ITERATIONS; + state->texture = color_texture_new(1024); + if (state->texture == 0) + return false; state->real_start = -2.0; state->real_end = 2.0; state->imag_start = -2.0; @@ -77,7 +78,9 @@ void state_run(State *state) void state_quit(State *state) { - /* free(state->palette); */ + GL_CALL(glDeleteTextures(1, &state->texture)); + GL_CALL(glDeleteBuffers(1, &state->vertex_buf)); + GL_CALL(glDeleteVertexArrays(1, &state->vertex_array)); GL_CALL(glDeleteProgram(state->shader.id)); SDL_GL_DeleteContext(state->context); SDL_DestroyWindow(state->window); -- cgit