pool allocation. not working. not sure why. will travel back in time...

This commit is contained in:
2025-09-05 11:21:34 -05:00
parent 75e72ba9ca
commit 1c7a9900be
10 changed files with 145 additions and 109 deletions

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="DebugDLL|Win32"> <ProjectConfiguration Include="DebugDLL|Win32">
@@ -186,7 +186,7 @@
<Command>xcopy /y "$(ProjectDir)src\shaders\vertex.glsl" "$(OutDir)"</Command> <Command>xcopy /y "$(ProjectDir)src\shaders\vertex.glsl" "$(OutDir)"</Command>
</PostBuildEvent> </PostBuildEvent>
<PostBuildEvent> <PostBuildEvent>
<Command>xcopy /y "$(ProjectDir)poses.csv" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\vertex.glsl" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\fragment.glsl" "$(OutDir)"</Command> <Command>xcopy /y "$(ProjectDir)assets\Icosphere.obj" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)assets\poses.csv" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\vertex.glsl" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\fragment.glsl" "$(OutDir)"</Command>
</PostBuildEvent> </PostBuildEvent>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugDLL|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugDLL|x64'">
@@ -209,7 +209,7 @@
<Command>xcopy /y "$(ProjectDir)src\shaders\vertex.glsl" "$(OutDir)"</Command> <Command>xcopy /y "$(ProjectDir)src\shaders\vertex.glsl" "$(OutDir)"</Command>
</PostBuildEvent> </PostBuildEvent>
<PostBuildEvent> <PostBuildEvent>
<Command>xcopy /y "$(ProjectDir)poses.csv" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\vertex.glsl" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\fragment.glsl" "$(OutDir)"</Command> <Command>xcopy /y "$(ProjectDir)assets\Icosphere.obj" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)assets\poses.csv" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\vertex.glsl" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\fragment.glsl" "$(OutDir)"</Command>
</PostBuildEvent> </PostBuildEvent>
<PreBuildEvent> <PreBuildEvent>
<Command> <Command>
@@ -233,7 +233,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
<PostBuildEvent> <PostBuildEvent>
<Command>xcopy /y "$(ProjectDir)poses.csv" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\vertex.glsl" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\fragment.glsl" "$(OutDir)"</Command> <Command>xcopy /y "$(ProjectDir)assets\Icosphere.obj" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)assets\poses.csv" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\vertex.glsl" "$(OutDir)" &amp;&amp; xcopy /y "$(ProjectDir)src\shaders\fragment.glsl" "$(OutDir)"</Command>
</PostBuildEvent> </PostBuildEvent>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
@@ -242,16 +242,16 @@
<ClCompile Include="src\glad.c" /> <ClCompile Include="src\glad.c" />
<ClCompile Include="src\live_plotter.cpp" /> <ClCompile Include="src\live_plotter.cpp" />
<ClCompile Include="src\shaders.cpp" /> <ClCompile Include="src\shaders.cpp" />
<ClCompile Include="util.cpp" /> <ClCompile Include="src\util.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="inc\body.hpp" /> <ClInclude Include="inc\body.hpp" />
<ClInclude Include="inc\camera.hpp" /> <ClInclude Include="inc\camera.hpp" />
<ClInclude Include="inc\camera_poses.hpp" /> <ClInclude Include="inc\camera_poses.hpp" />
<ClInclude Include="inc\live_plotter.hpp" /> <ClInclude Include="inc\live_plotter.hpp" />
<ClInclude Include="inc\logger.hpp" />
<ClInclude Include="inc\shaders.hpp" /> <ClInclude Include="inc\shaders.hpp" />
<ClInclude Include="inc\util.hpp" /> <ClInclude Include="inc\util.hpp" />
<ClInclude Include="logger.hpp" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@@ -30,7 +30,7 @@
<ClCompile Include="src\live_plotter.cpp"> <ClCompile Include="src\live_plotter.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="util.cpp"> <ClCompile Include="src\util.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
@@ -53,7 +53,7 @@
<ClInclude Include="inc\util.hpp"> <ClInclude Include="inc\util.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="logger.hpp"> <ClInclude Include="inc\logger.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>

View File

@@ -20,7 +20,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\demo.cpp" /> <ClCompile Include="src\demo.cpp" />
<ClCompile Include="util.cpp" /> <ClCompile Include="src\util.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="LivePlotter.vcxproj"> <ProjectReference Include="LivePlotter.vcxproj">

View File

@@ -18,7 +18,7 @@
<ClCompile Include="src\demo.cpp"> <ClCompile Include="src\demo.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="util.cpp"> <ClCompile Include="src\util.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>

View File

@@ -1,4 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project> </Project>

View File

@@ -5,16 +5,22 @@
#include <string> #include <string>
#include "util.hpp" #include "util.hpp"
struct ObjData {
float* verts;
int verts_len;
int* faces;
int faces_len;
};
struct Body { struct Body {
glm::mat4 pose; glm::mat4 pose;
glm::vec4 color;
float scale; float scale;
uint ebo; uint ebo;
uint vao; uint vao;
uint vbo; uint vbo;
uint shader; uint shader;
std::vector<float> verts; ObjData data;
std::vector<int> faces;
glm::vec4 color;
}; };
bool load_body(Body* out_body, std::string filepath); bool load_body(Body* out_body, std::string filepath);

View File

@@ -15,9 +15,15 @@ typedef LARGE_INTEGER TimeSpan;
typedef DWORD (WINAPI *ThreadFunc)(_In_ LPVOID lpParameter); typedef DWORD (WINAPI *ThreadFunc)(_In_ LPVOID lpParameter);
typedef LPVOID ThreadArg; typedef LPVOID ThreadArg;
const TimeSpan infinite_ts = LLONG_MAX; const TimeSpan infinite_ts = { .QuadPart = LLONG_MAX };
static const LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq); LARGE_INTEGER _init_freq() {
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
return freq;
}
static LARGE_INTEGER freq = _init_freq();
#endif #endif
Thread make_thread(ThreadFunc t, ThreadArg a); Thread make_thread(ThreadFunc t, ThreadArg a);
@@ -60,7 +66,7 @@ Thread make_thread(ThreadFunc f, ThreadArg a) {
} }
void join(Thread t) { void join(Thread t) {
WaitForSingleObject(t, infinite_ts); WaitForSingleObject(t, INFINITE);
} }
Mutex make_mutex() { Mutex make_mutex() {
@@ -92,7 +98,11 @@ ConditionVar make_condition_var() {
} }
void wait(ConditionVar &c, Mutex &m, TimeSpan ts) { void wait(ConditionVar &c, Mutex &m, TimeSpan ts) {
SleepConditionVariableCS(&c, &m, ts); if (ts.QuadPart == infinite_ts.QuadPart) {
SleepConditionVariableCS(&c, &m, INFINITE);
} else {
SleepConditionVariableCS(&c, &m, static_cast<DWORD>(to_ms(ts)));
}
} }
void wake_one(ConditionVar &c) { void wake_one(ConditionVar &c) {
@@ -112,7 +122,7 @@ Semaphore make_semaphore(int initial, int max) {
} }
void wait(Semaphore &s) { void wait(Semaphore &s) {
WaitForSingleObject(s, infinite_ts); WaitForSingleObject(s, INFINITE);
} }
void post(Semaphore &s) { void post(Semaphore &s) {

View File

@@ -2,7 +2,6 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <map> #include <map>
#include <string> #include <string>
#include <optional>
#include "util.hpp" #include "util.hpp"
#include "body.hpp" #include "body.hpp"
@@ -17,29 +16,24 @@ enum class ParserState {
FACE_SKIP, FACE_SKIP,
}; };
static map<string, pair<vector<float>, vector<int>>> cache; static map<string, ObjData> cache;
optional<pair<vector<float>, vector<int>>> _parse_obj(const char* obj_filepath); bool _parse_obj(ObjData* data, const char* obj_filepath);
bool load_body(Body* out_body, const char* obj_filepath) { bool load_body(Body* out_body, const char* obj_filepath) {
vector<float> verts; ObjData data;
vector<int> faces;
if (auto it = cache.find(obj_filepath); it != cache.end()) { if (auto it = cache.find(obj_filepath); it != cache.end()) {
verts = it->second.first; data = it->second;
faces = it->second.second; } else if (_parse_obj(&data, obj_filepath)) {
} else if (auto pair = _parse_obj(obj_filepath); pair.has_value()) { cache[obj_filepath] = data;
verts = pair.value().first;
faces = pair.value().second;
cache[obj_filepath] = pair.value();
} else { } else {
return false; return false;
} }
// Need a good validation check here. This is a stand in. // Need a good validation check here. This is a stand in.
if (verts.size() == 0 || faces.size() == 0) { if (data.verts_len == 0 || data.faces_len == 0) {
printf( printf("Obj file appears incomplete or corrupted. Num verts %d. Num Faces %d.\n", data.verts_len,
"Obj file appears incomplete or corrupted. Num verts %zu. Num Faces %zu.\n", verts.size(), faces.size()); data.faces_len);
return false; return false;
} }
@@ -55,22 +49,15 @@ bool load_body(Body* out_body, const char* obj_filepath) {
glBindVertexArray(vao); glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(float), &verts[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, data.verts_len * sizeof(float), data.verts, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, faces.size() * sizeof(int), &faces[0], GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, data.faces_len * sizeof(int), &data.faces, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
out_body->pose = glm::mat4(1); *out_body = { .pose = glm::mat4(1), .ebo = ebo, .vao = vao, .vbo = vbo, .shader = 0, .data = data };
out_body->ebo = ebo;
out_body->vao = vao;
out_body->vbo = vbo;
out_body->shader = 0;
out_body->verts = verts;
out_body->faces = faces;
return true; return true;
} }
@@ -79,7 +66,7 @@ void draw_body(const Body& b) {
set_uniform(b.shader, "global_t", glm::scale(b.pose, glm::vec3(b.scale))); set_uniform(b.shader, "global_t", glm::scale(b.pose, glm::vec3(b.scale)));
set_uniform(b.shader, "color", b.color); set_uniform(b.shader, "color", b.color);
glBindVertexArray(b.vao); glBindVertexArray(b.vao);
glDrawElements(GL_TRIANGLES, b.faces.size(), GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, b.data.faces_len, GL_UNSIGNED_INT, 0);
} }
void create_new_sphere(Body* b, float scale) { void create_new_sphere(Body* b, float scale) {
@@ -87,7 +74,8 @@ void create_new_sphere(Body* b, float scale) {
b->scale = scale; b->scale = scale;
} }
optional<pair<vector<float>, vector<int>>> _parse_obj(const char* obj_filepath) { // I need to write a good obj file parser at some point. This is basically garbage
bool _parse_obj(ObjData* data, const char* obj_filepath) {
string source; string source;
if (!read_file(&source, obj_filepath)) { if (!read_file(&source, obj_filepath)) {
return {}; return {};
@@ -103,8 +91,11 @@ optional<pair<vector<float>, vector<int>>> _parse_obj(const char* obj_filepath)
} }
} }
vector<float> verts; float* verts = (float*)malloc(sizeof(float) * num_verts * 3);
vector<int> faces; int* faces = (int*)malloc(sizeof(int) * num_faces * 3);
int vert_i = 0;
int face_i = 0;
// Get ready for the parsing loop // Get ready for the parsing loop
ParserState state = ParserState::PREFIX; ParserState state = ParserState::PREFIX;
@@ -123,14 +114,14 @@ optional<pair<vector<float>, vector<int>>> _parse_obj(const char* obj_filepath)
break; break;
case ParserState::VERTEX: case ParserState::VERTEX:
if (iswspace(source[i])) { if (iswspace(source[i])) {
verts.push_back(atof(&source[start])); verts[vert_i++] = atof(&source[start]);
start = i + 1; start = i + 1;
} }
break; break;
case ParserState::FACE: case ParserState::FACE:
if (source[i] == '/') { if (source[i] == '/') {
state = ParserState::FACE_SKIP; state = ParserState::FACE_SKIP;
faces.push_back(atoi(&source[start]) - 1); faces[face_i++] = atoi(&source[start]) - 1;
} }
break; break;
case ParserState::FACE_SKIP: case ParserState::FACE_SKIP:
@@ -144,4 +135,7 @@ optional<pair<vector<float>, vector<int>>> _parse_obj(const char* obj_filepath)
continue; continue;
} }
} }
*data = { .verts = verts, .verts_len = vert_i, .faces = faces, .faces_len = face_i };
return true;
} }

View File

@@ -66,6 +66,9 @@ int main() {
} }
start(800, 800); start(800, 800);
pointid id = create_point(0, 0, 0);
set_color(id, 1, 1, 1);
set_scale(id, 10);
while (true) while (true)
; ;
@@ -84,7 +87,8 @@ int main() {
// vector<string> words = split_str(line, ' '); // vector<string> words = split_str(line, ' ');
// if (!words.size() == 4) // if (!words.size() == 4)
// return NULL; // return NULL;
// printf("Received: %s, %s, %s, %s\n", words[0], words[1], words[2], words[3]); // echo for debugging // printf("Received: %s, %s, %s, %s\n", words[0].c_str(), words[1].c_str(), words[2].c_str(),
// words[3].c_str()); // echo for debugging
// string name = words[0]; // string name = words[0];
// float x = stof(words[1]); // float x = stof(words[1]);
// float y = stof(words[2]); // float y = stof(words[2]);

View File

@@ -1,3 +1,4 @@
#include <cassert>
#include <windows.h> #include <windows.h>
#include <cstring> #include <cstring>
@@ -5,9 +6,7 @@
#include <glm/ext/vector_float4.hpp> #include <glm/ext/vector_float4.hpp>
#include <profileapi.h> #include <profileapi.h>
#include <synchapi.h> #include <synchapi.h>
#include <vector>
#include <map> #include <map>
#include <set>
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/ext/matrix_clip_space.hpp> #include <glm/ext/matrix_clip_space.hpp>
@@ -16,9 +15,7 @@
#include <glm/matrix.hpp> #include <glm/matrix.hpp>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#include <winnt.h> #include <winnt.h>
#include <algorithm>
#include "sync.hpp" #include "sync.hpp"
#include "util.hpp" #include "util.hpp"
@@ -27,13 +24,18 @@
#include "camera.hpp" #include "camera.hpp"
#include "live_plotter.hpp" #include "live_plotter.hpp"
#define LERP_MOVE_PERIOD_S 0.05
#define STOP_WAIT_TIME_MS 500
using namespace sync; using namespace sync;
// Constants
const char* vertex_filepath = "vertex.glsl";
const char* fragment_filepath = "fragment.glsl";
const float lerp_move_period_s = 0.05;
const int stop_wait_time_ms = 500;
const int max_point_mem = 10 << 20; // bytes
struct Point { struct Point {
bool initialized; bool initialized;
bool replaceable;
glm::vec3 startloc; glm::vec3 startloc;
glm::vec3 targetloc; glm::vec3 targetloc;
Body b; Body b;
@@ -51,13 +53,11 @@ static Thread t;
static Mutex m; static Mutex m;
static uint shader; static uint shader;
static LARGE_INTEGER fr; static LARGE_INTEGER fr;
static pointid next_id = 0;
static std::map<uint, Point*> id_to_point; // Use a pool allocator possibly later
static std::map<Point*, uint> point_to_id;
// Constants constexpr int point_buf_len
const char* vertex_filepath = "vertex.glsl"; = max_point_mem / sizeof(Point); // ~max_point_mem bytes of points. does not include vertex data for obj
const char* fragment_filepath = "fragment.glsl"; static Point* point_buf = (Point*)malloc(point_buf_len * sizeof(Point));
static int point_buf_i = 0;
// Private foward decls // Private foward decls
void _resize_cb(GLFWwindow* win, int w, int h); void _resize_cb(GLFWwindow* win, int w, int h);
@@ -77,6 +77,10 @@ extern DllExport bool __cdecl start(int win_w, int win_h) {
m = make_mutex(); m = make_mutex();
for (int i = 0; i < point_buf_len; i++) {
point_buf[i].replaceable = true;
}
if (!QueryPerformanceFrequency(&fr)) { if (!QueryPerformanceFrequency(&fr)) {
printf("Failed to get the performance counter frequency.\n"); printf("Failed to get the performance counter frequency.\n");
return false; return false;
@@ -95,72 +99,81 @@ extern DllExport bool __cdecl start(int win_w, int win_h) {
extern DllExport bool __cdecl stop() { extern DllExport bool __cdecl stop() {
stop_flag = true; stop_flag = true;
DWORD res = WaitForSingleObject(t, STOP_WAIT_TIME_MS); DWORD res = WaitForSingleObject(t, stop_wait_time_ms);
bool success = res == WAIT_OBJECT_0; bool success = res == WAIT_OBJECT_0;
running = !success; // Consider the app still running if we didn't shut down properly...? running = !success; // Consider the app still running if we didn't shut down properly...?
// TODO: Please wrap away win api synch stuff. thank you // TODO: Please wrap away win api synch stuff. thank you
if (!running) dispose(m); if (!running)
dispose(m);
return success; return success;
} }
extern DllExport pointid __cdecl create_point(float x, float y, float z) { extern DllExport pointid __cdecl create_point(float x, float y, float z) {
lock(m); lock(m);
Point* p = (Point*)malloc(sizeof(Point));
*p = { // This is stupid. Please fix
int slot = -1;
int start_loc = point_buf_i;
do {
if (point_buf[point_buf_i % point_buf_len].replaceable) {
slot = point_buf_i % point_buf_len;
break;
}
point_buf_i++;
} while (point_buf_i != start_loc);
assert(slot > -1);
point_buf[slot] = {
.initialized = false, .initialized = false,
.replaceable = false,
.startloc = glm::vec3(x, y, z), .startloc = glm::vec3(x, y, z),
.targetloc = glm::vec3(x, y, z), .targetloc = glm::vec3(x, y, z),
.lifetime_s = INFINITY, .lifetime_s = INFINITY,
.start_s = _time() .start_s = _time()
}; };
p->b.scale = 1.0f; point_buf[slot].b.scale = 1.0f;
p->b.color = glm::vec4(randf() + 0.1, randf() + 0.1, randf() + 0.1, 1); point_buf[slot].b.color = glm::vec4(randf() + 0.1, randf() + 0.1, randf() + 0.1, 1);
pointid id = next_id++;
id_to_point[id] = p;
point_to_id[p] = id;
unlock(m); unlock(m);
return id; return slot;
} }
extern DllExport void __cdecl set_color(pointid id, float r, float g, float b) { extern DllExport void __cdecl set_color(pointid id, float r, float g, float b) {
lock(m); lock(m);
Point* p = id_to_point[id]; Point &p = point_buf[id % point_buf_len];
p->b.color = { r, g, b, p->b.color[3] }; p.b.color = { r, g, b, p.b.color[3] };
unlock(m); unlock(m);
} }
extern DllExport void __cdecl set_scale(pointid id, float scale) { extern DllExport void __cdecl set_scale(pointid id, float scale) {
lock(m); lock(m);
Point* p = id_to_point[id]; Point &p = point_buf[id % point_buf_len];
p->b.scale = scale; p.b.scale = scale;
unlock(m); unlock(m);
} }
extern DllExport void __cdecl update_point(pointid id, float x, float y, float z) { extern DllExport void __cdecl update_point(pointid id, float x, float y, float z) {
lock(m); lock(m);
Point* p = id_to_point[id]; Point &p = point_buf[id % point_buf_len];
p->targetloc = { x, y, z }; p.targetloc = { x, y, z };
p->startloc = p->b.pose[3]; p.startloc = p.b.pose[3];
p->start_s = _time(); p.start_s = _time();
unlock(m); unlock(m);
} }
extern DllExport void __cdecl set_lifetime(pointid id, float new_lifetime_s) { extern DllExport void __cdecl set_lifetime(pointid id, float new_lifetime_s) {
lock(m); lock(m);
Point* p = id_to_point[id]; Point &p = point_buf[id % point_buf_len];
p->start_s = _time(); p.start_s = _time();
p->lifetime_s = new_lifetime_s; p.lifetime_s = new_lifetime_s;
unlock(m); unlock(m);
} }
extern DllExport void __cdecl clear_point(pointid id) { extern DllExport void __cdecl clear_point(pointid id) {
lock(m); lock(m);
Point* p = id_to_point[id]; point_buf[id % point_buf_len].replaceable = true;
id_to_point.erase(id);
point_to_id.erase(p);
free(p);
unlock(m); unlock(m);
} }
@@ -192,7 +205,7 @@ void _scroll_cb(GLFWwindow* win, double xoffset, double yoffset) { zoom_camera(c
DWORD _win_thread(LPVOID args) { DWORD _win_thread(LPVOID args) {
glm::vec2* winsize = (glm::vec2*)args; glm::vec2* winsize = (glm::vec2*)args;
camera = make_camera({ 0, 0, 0 }, 3000); camera = make_camera({ 0, 0, 0 }, 10);
camera.theta = glm::radians(90.0f); camera.theta = glm::radians(90.0f);
camera.phi = glm::radians(30.0f); camera.phi = glm::radians(30.0f);
viewport = make_viewport(winsize->x, winsize->y, 45.0f); viewport = make_viewport(winsize->x, winsize->y, 45.0f);
@@ -255,26 +268,29 @@ void _refresh_win() {
set_uniform(shader, "projection_t", camera_to_projection(viewport)); set_uniform(shader, "projection_t", camera_to_projection(viewport));
if (trylock(m)) { if (trylock(m)) {
for (const auto& [id, p] : id_to_point) { for (int i = 0; i < point_buf_len; i++) {
Point &p = point_buf[i];
if (p.replaceable) continue;
// Initialize if not done so yet // Initialize if not done so yet
if (!p->initialized) { if (!p.initialized) {
create_new_sphere(&p->b, p->b.scale); create_new_sphere(&p.b, p.b.scale);
p->b.shader = shader; p.b.shader = shader;
p->b.pose = glm::translate(glm::mat4(1), p->startloc); p.b.pose = glm::translate(glm::mat4(1), p.startloc);
p->initialized = true; p.initialized = true;
} }
// Perform fade // Perform fade
double elapsed_time_s = _time() - p->start_s; double elapsed_time_s = _time() - p.start_s;
float t = elapsed_time_s / p->lifetime_s; float t = elapsed_time_s / p.lifetime_s;
t = std::clamp(t, 0.0f, 1.0f); t = glm::clamp(t, 0.0f, 1.0f);
p->b.color.w = lerp(1.0, 0.0, t); p.b.color.w = lerp(1.0, 0.0, t);
// Lerp position // Lerp position
t = elapsed_time_s / LERP_MOVE_PERIOD_S; t = elapsed_time_s / lerp_move_period_s;
t = std::clamp(t, 0.0f, 1.0f); t = glm::clamp(t, 0.0f, 1.0f);
p->b.pose[3] = glm::vec4(lerp(p->startloc, p->targetloc, t), 1); p.b.pose[3] = glm::vec4(lerp(p.startloc, p.targetloc, t), 1);
draw_body(p->b); draw_body(p.b);
} }
unlock(m); unlock(m);
} }
@@ -290,4 +306,3 @@ double _time() {
} }
return (double)timeticks.QuadPart / (double)fr.QuadPart; return (double)timeticks.QuadPart / (double)fr.QuadPart;
} }