From 68236052a61d5ea306703e00a0cb34a604d4e461 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 23 Oct 2020 12:23:52 -0500 Subject: [PATCH] circles now have various colors --- include/physics.h | 2 ++ res/shaders/planet_frag.glsl | 3 ++- res/shaders/planet_vert.glsl | 6 ++++++ src/sim.cpp | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/include/physics.h b/include/physics.h index 3c77e6c..1044d84 100644 --- a/include/physics.h +++ b/include/physics.h @@ -12,6 +12,8 @@ struct Body V2f post_update_vel; f32 mass; + + u8 color_idx; }; void body_calculate_move(Body* body, const Array other_bodies, f64 dt); diff --git a/res/shaders/planet_frag.glsl b/res/shaders/planet_frag.glsl index 863ee86..6363dc4 100644 --- a/res/shaders/planet_frag.glsl +++ b/res/shaders/planet_frag.glsl @@ -2,8 +2,9 @@ precision mediump float; +in vec4 planet_color; out vec4 fragColor; void main() { - fragColor = vec4(1.0, 0.0, 1.0, 1.0); + fragColor = planet_color; } diff --git a/res/shaders/planet_vert.glsl b/res/shaders/planet_vert.glsl index 4c306ce..83962d9 100644 --- a/res/shaders/planet_vert.glsl +++ b/res/shaders/planet_vert.glsl @@ -5,9 +5,15 @@ precision mediump float; layout(location = 0) in vec2 a_shape_pos; layout(location = 1) in vec2 a_obj_pos; layout(location = 2) in float a_mass; +layout(location = 3) in float a_planet_color_idx; + +uniform vec4 u_planet_colors[4]; +out vec4 planet_color; uniform mat4 u_proj; void main() { gl_Position = u_proj * vec4(a_shape_pos * a_mass + a_obj_pos, 0, 1); + + planet_color = u_planet_colors[int(a_planet_color_idx)]; } \ No newline at end of file diff --git a/src/sim.cpp b/src/sim.cpp index f2ba131..244442b 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -278,8 +278,19 @@ main(i32 argc, char* argv[]) mat4_ortho(&ortho_mat, 0, 800, 0, 800, 0.0f, 100.0f); GLuint ortho_mat_loc = glGetUniformLocation(program, "u_proj"); + logprint(LOG_LEVEL_INFO, "Projection loc: %d", ortho_mat_loc); glUniformMatrix4fv(ortho_mat_loc, 1, false, (f32 *) ortho_mat); + GLuint planet_colors_loc = glGetUniformLocation(program, "u_planet_colors"); + logprint(LOG_LEVEL_INFO, "Planet color loc: %d", planet_colors_loc); + GLfloat planet_colors[16] = { + 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, + }; + glUniform4fv(planet_colors_loc, 4, planet_colors); + auto state = alloc(); // NOTE(Brendan): Need to initialize the array since it does not get constructed because I refuse to use the 'new' keyword. alloc uses malloc under the hood and cannot initialize the result. state->bodies.init(); @@ -291,6 +302,8 @@ main(i32 argc, char* argv[]) tmp_body.pos = V2f{ randf(0, 800), randf(0, 800) }; tmp_body.vel = V2f{ randf(-50.0f, 50.0f), randf(-50.0f, 50.0f) }; tmp_body.mass = randf(5.0f, 25.0f); + tmp_body.color_idx = rand() % 4; + logprint(LOG_LEVEL_INFO, "Body color idx: %d", tmp_body.color_idx); state->bodies.push(tmp_body); } @@ -303,10 +316,13 @@ main(i32 argc, char* argv[]) glBufferData(GL_ARRAY_BUFFER, sizeof(Body) * state->bodies.count, state->bodies.data, GL_STREAM_DRAW); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); + glEnableVertexAttribArray(3); glVertexAttribDivisor(1, 1); glVertexAttribDivisor(2, 1); + glVertexAttribDivisor(3, 1); glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof(Body), (void *) offsetof(Body, pos.x)); glVertexAttribPointer(2, 1, GL_FLOAT, false, sizeof(Body), (void *) offsetof(Body, mass)); + glVertexAttribPointer(3, 1, GL_BYTE, false, sizeof(Body), (void *) offsetof(Body, color_idx)); glBindBuffer(GL_ARRAY_BUFFER, -1); } -- 2.25.1