From dcc7db76256a8e1465934f34331390b13022cd84 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 19 Oct 2020 17:47:12 -0500 Subject: [PATCH] lotza circles --- include/physics.h | 2 +- include/utils.h | 14 ++++++++++++++ src/sim.cpp | 34 +++++++++++++++++++++++++++++++--- src/utils.cpp | 15 ++++++++++++--- 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/include/physics.h b/include/physics.h index 2210594..881dfa4 100644 --- a/include/physics.h +++ b/include/physics.h @@ -11,4 +11,4 @@ struct Body f32 mass; }; -#endif //PHYSICS_H +#endif //PHYSICS_H \ No newline at end of file diff --git a/include/utils.h b/include/utils.h index 4c1584e..15ebbdd 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,6 +1,7 @@ #ifndef UTILS_H #define UTILS_H +#include #include // NOTE(Brendan): Only for intptr_t // NOTE(Brendan): Hacky way to get the offset of a struct member. offsetof() is the standard way in C to get it, but it is not guarenteed to be defined in all C implementations. @@ -13,9 +14,22 @@ // NOTE(Brendan): This is useful in many situations and I believe it cleans up the code by making simple, counter based for loops easier to recognize at a glance. #define foreach(var, lo, hi) for (i32 var = lo; var < hi; var++) +#define For(iterable) for (auto& it : iterable) [[noreturn]] void panic_and_die(const char* msg, ...); +// NOTE(Brendan): The fact that I have to define this hear solidifies the stupidity of C++ in my mind. +template +T* alloc(int32_t count) +{ + T* res = (T *) malloc(sizeof(T) * count); + return res; +} + +float randf(); +float randf(float max); +float randf(float min, float max); + #ifndef defer struct defer_dummy {}; diff --git a/src/sim.cpp b/src/sim.cpp index 018fba6..b8bbbc6 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -162,8 +163,8 @@ create_circle_mesh() foreach (i, 0, CIRCLE_POINT_COUNT) { f32 t = (f32) i / (f32) CIRCLE_POINT_COUNT; - circle_points[i].x = cos(t * 2 * PI) * 40.0f + 400.0f; - circle_points[i].y = sin(t * 2 * PI) * 40.0f + 200.0f; + circle_points[i].x = cos(t * 2 * PI) / 2.0f; + circle_points[i].y = sin(t * 2 * PI) / 2.0f; } GLuint vertex_buffer; @@ -204,7 +205,7 @@ draw() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindVertexArray(circle_mesh); - glDrawElements(GL_TRIANGLE_FAN, CIRCLE_POINT_COUNT, GL_UNSIGNED_BYTE, 0); + glDrawElementsInstanced(GL_TRIANGLE_FAN, CIRCLE_POINT_COUNT, GL_UNSIGNED_BYTE, 0, 128); glBindVertexArray(-1); glfwSwapBuffers(window); @@ -252,6 +253,33 @@ main(i32 argc, char* argv[]) GLuint ortho_mat_loc = glGetUniformLocation(program, "u_proj"); glUniformMatrix4fv(ortho_mat_loc, 1, false, (f32 *) ortho_mat); + auto bodies = alloc(128); + foreach(i, 0, 128) + { + bodies[i].pos = V2f{ randf(0, 800), randf(0, 800) }; + bodies[i].vel = V2f{ 0.0f, 0.0f }; + bodies[i].mass = randf(10.0f, 50.0f); + } + + { + glBindVertexArray(circle_mesh); + // defer { glBindVertexArray(-1); }; + + GLuint body_buffer; + glGenBuffers(1, &body_buffer); + glBindBuffer(GL_ARRAY_BUFFER, body_buffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(Body) * 128, bodies, GL_STREAM_DRAW); + glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); + glVertexAttribDivisor(1, 1); + glVertexAttribDivisor(2, 1); + glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof(Body), (void *) offset_of(Body, pos.x)); + glVertexAttribPointer(2, 1, GL_FLOAT, false, sizeof(Body), (void *) offset_of(Body, mass)); + glBindBuffer(GL_ARRAY_BUFFER, -1); + + // glBindVertexArray(-1); + } + loop(); return 0; diff --git a/src/utils.cpp b/src/utils.cpp index 141f25b..dbaf866 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -2,22 +2,31 @@ #include #include +#include "utils.h" #include "log.h" void panic_and_die(const char* msg, ...) { puts("************ PANIC ************"); - + va_list va; va_start(va, msg); logvprint(LOG_LEVEL_ERROR, msg, va); va_end(va); - + #ifdef DEBUG // NOTE: This allows for a debugger to stop here. __asm("int $3"); #endif - + exit(1); } + +float randf() { return randf(0.0f, 1.0f); } +float randf(float max) { return randf(0.0f, max); } +float +randf(float min, float max) +{ + return (float) rand() * (max - min) / (float) RAND_MAX + min; +} \ No newline at end of file -- 2.25.1