From: Brendan Hansen Date: Sun, 15 Nov 2020 23:18:37 +0000 (-0600) Subject: fixed color bug; added saving settings X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=4bfebeef2d27a306a088e9d178c57fc6be466ea8;p=csc718.git fixed color bug; added saving settings --- diff --git a/res/shaders/planet_frag.glsl b/res/shaders/planet_frag.glsl index eed53c3..1722372 100644 --- a/res/shaders/planet_frag.glsl +++ b/res/shaders/planet_frag.glsl @@ -2,11 +2,11 @@ precision mediump float; -uniform vec4 u_planet_colors[4]; +uniform vec3 u_planet_colors[32]; in float planet_color_idx; out vec4 fragColor; void main() { - fragColor = u_planet_colors[int(planet_color_idx)]; + fragColor = vec4(u_planet_colors[int(planet_color_idx)], 1.0); } diff --git a/src/settings.cpp b/src/settings.cpp index c6a51f3..7833841 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -14,6 +14,41 @@ load_settings_from_file(SimSettings* settings, const char* filename) void save_settings_to_file(SimSettings* settings, const char* filename) { + FILE* file = fopen(filename, "w"); + if (file == NULL) + { + logprint(LOG_LEVEL_ERROR, "Failed to open file '%s' for saving settings.", filename); + return; + } + defer { fclose(file); }; + + fprintf(file, "thread_count %d\n", settings->thread_count); + fprintf(file, "body_count %d\n", settings->body_count); + fprintf(file, "body_type_count %d\n", settings->body_type_count); + + foreach (i, 0, settings->body_type_count) + { + BodyColor c = settings->body_colors[i]; + fprintf(file, "body_color %d %f %f %f\n", i, c.r, c.g, c.b); + } + + foreach (i, 0, settings->body_type_count) + { + BodyMassRange r = settings->body_mass_range[i]; + fprintf(file, "body_mass_range %d %f %f\n", i, r.min, r.max); + } + + fprintf(file, "friction %f\n", settings->friction); + fprintf(file, "universe_scale %f\n", settings->universe_scale); + + foreach (i, 0, settings->body_type_count) + { + foreach (j, 0, settings->body_type_count) + { + BodyRelation br = settings->body_relations[i * settings->body_type_count + j]; + fprintf(file, "interaction %d %d %f %f\n", i, j, br.distance_range, br.max_force); + } + } } void @@ -25,12 +60,14 @@ generate_random_settings(SimSettings* settings) settings->body_type_count = num_body_types; settings->body_colors.init(num_body_types); + settings->body_colors.count = num_body_types; settings->body_colors[0] = { 1.0, 0.0, 0.0 }; settings->body_colors[1] = { 0.0, 1.0, 0.0 }; settings->body_colors[2] = { 0.0, 0.0, 1.0 }; settings->body_colors[3] = { 1.0, 1.0, 1.0 }; settings->body_mass_range.init(num_body_types); + settings->body_mass_range.count = num_body_types; settings->body_mass_range[0] = { 4.0, 6.0 }; settings->body_mass_range[1] = { 4.0, 6.0 }; settings->body_mass_range[2] = { 4.0, 6.0 }; @@ -43,11 +80,12 @@ generate_random_settings(SimSettings* settings) settings->thread_count = 4; settings->body_relations.init(num_body_types * num_body_types); + settings->body_relations.count = num_body_types * num_body_types; foreach (i, 0, num_body_types) { foreach (j, 0, num_body_types) { - settings->body_relations[i * num_body_types] = + settings->body_relations[i * num_body_types + j] = { .distance_range = randf(4.0f, 7.0f), .max_force = randf(-200.0f, 200.0f), diff --git a/src/sim.cpp b/src/sim.cpp index 75b4a46..fe49721 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -329,16 +329,9 @@ main(i32 argc, char* argv[]) load_shader(GL_FRAGMENT_SHADER, "res/shaders/planet_frag.glsl")); glUseProgram(body_program); - // :ArbitraryConstant - persist GLfloat planet_colors[4][4] = - { - { 1.0f, 0.0f, 0.0f, 1.0f }, - { 0.0f, 1.0f, 0.0f, 1.0f }, - { 0.0f, 0.0f, 1.0f, 1.0f }, - { 1.0f, 1.0f, 1.0f, 1.0f }, - }; GLuint planet_colors_loc = glGetUniformLocation(body_program, "u_planet_colors"); - glUniform4fv(planet_colors_loc, 4, (GLfloat *) planet_colors); + GLfloat* planet_colors = (GLfloat *) global_settings.body_colors.data; + glUniform3fv(planet_colors_loc, global_settings.body_type_count, planet_colors); auto state = alloc(); sim_state_init(state);