lotza circles
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 19 Oct 2020 22:47:12 +0000 (17:47 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 19 Oct 2020 22:47:12 +0000 (17:47 -0500)
include/physics.h
include/utils.h
src/sim.cpp
src/utils.cpp

index 2210594a3a6c99d81f0093348afa5dde47e14569..881dfa4e91177fa59d428700e8353e6a25e58c30 100644 (file)
@@ -11,4 +11,4 @@ struct Body
     f32 mass;
 };
 
-#endif //PHYSICS_H
+#endif //PHYSICS_H
\ No newline at end of file
index 4c1584e8977228f62f2f503eb3dedfda023414da..15ebbdd93193147a6a9904ae6835887d11e24046 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef UTILS_H
 #define UTILS_H
 
+#include <malloc.h>
 #include <stdint.h> // 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.
 
 // 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 <typename T>
+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 {};
 
index 018fba650afc99dc07d647ec0135ae9cc37e4140..b8bbbc69bed327a772590c6bcf6b8ed7d337c5e4 100644 (file)
@@ -3,6 +3,7 @@
 #include <cstdio>
 #include <cstdlib>
 #include <cstdbool>
+#include <cassert>
 #include <malloc.h>
 #include <alloca.h>
 #include <unistd.h>
@@ -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<Body>(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;
index 141f25b9aac552f32f2c8eb20e1938316bc93475..dbaf8666d17188bde64d33e5e74a0d1e43cb4e90 100644 (file)
@@ -2,22 +2,31 @@
 #include <cstdarg>
 #include <cstdlib>
 
+#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