pool allocation. not working. not sure why. will travel back in time...
This commit is contained in:
56
src/body.cpp
56
src/body.cpp
@@ -2,7 +2,6 @@
|
||||
#include <glad/glad.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
||||
#include "util.hpp"
|
||||
#include "body.hpp"
|
||||
@@ -17,29 +16,24 @@ enum class ParserState {
|
||||
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) {
|
||||
vector<float> verts;
|
||||
vector<int> faces;
|
||||
|
||||
ObjData data;
|
||||
if (auto it = cache.find(obj_filepath); it != cache.end()) {
|
||||
verts = it->second.first;
|
||||
faces = it->second.second;
|
||||
} else if (auto pair = _parse_obj(obj_filepath); pair.has_value()) {
|
||||
verts = pair.value().first;
|
||||
faces = pair.value().second;
|
||||
cache[obj_filepath] = pair.value();
|
||||
data = it->second;
|
||||
} else if (_parse_obj(&data, obj_filepath)) {
|
||||
cache[obj_filepath] = data;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Need a good validation check here. This is a stand in.
|
||||
if (verts.size() == 0 || faces.size() == 0) {
|
||||
printf(
|
||||
"Obj file appears incomplete or corrupted. Num verts %zu. Num Faces %zu.\n", verts.size(), faces.size());
|
||||
if (data.verts_len == 0 || data.faces_len == 0) {
|
||||
printf("Obj file appears incomplete or corrupted. Num verts %d. Num Faces %d.\n", data.verts_len,
|
||||
data.faces_len);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -55,22 +49,15 @@ bool load_body(Body* out_body, const char* obj_filepath) {
|
||||
glBindVertexArray(vao);
|
||||
|
||||
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);
|
||||
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);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
out_body->pose = glm::mat4(1);
|
||||
out_body->ebo = ebo;
|
||||
out_body->vao = vao;
|
||||
out_body->vbo = vbo;
|
||||
out_body->shader = 0;
|
||||
out_body->verts = verts;
|
||||
out_body->faces = faces;
|
||||
|
||||
*out_body = { .pose = glm::mat4(1), .ebo = ebo, .vao = vao, .vbo = vbo, .shader = 0, .data = data };
|
||||
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, "color", b.color);
|
||||
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) {
|
||||
@@ -87,7 +74,8 @@ void create_new_sphere(Body* b, float 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;
|
||||
if (!read_file(&source, obj_filepath)) {
|
||||
return {};
|
||||
@@ -103,8 +91,11 @@ optional<pair<vector<float>, vector<int>>> _parse_obj(const char* obj_filepath)
|
||||
}
|
||||
}
|
||||
|
||||
vector<float> verts;
|
||||
vector<int> faces;
|
||||
float* verts = (float*)malloc(sizeof(float) * num_verts * 3);
|
||||
int* faces = (int*)malloc(sizeof(int) * num_faces * 3);
|
||||
|
||||
int vert_i = 0;
|
||||
int face_i = 0;
|
||||
|
||||
// Get ready for the parsing loop
|
||||
ParserState state = ParserState::PREFIX;
|
||||
@@ -123,14 +114,14 @@ optional<pair<vector<float>, vector<int>>> _parse_obj(const char* obj_filepath)
|
||||
break;
|
||||
case ParserState::VERTEX:
|
||||
if (iswspace(source[i])) {
|
||||
verts.push_back(atof(&source[start]));
|
||||
verts[vert_i++] = atof(&source[start]);
|
||||
start = i + 1;
|
||||
}
|
||||
break;
|
||||
case ParserState::FACE:
|
||||
if (source[i] == '/') {
|
||||
state = ParserState::FACE_SKIP;
|
||||
faces.push_back(atoi(&source[start]) - 1);
|
||||
faces[face_i++] = atoi(&source[start]) - 1;
|
||||
}
|
||||
break;
|
||||
case ParserState::FACE_SKIP:
|
||||
@@ -144,4 +135,7 @@ optional<pair<vector<float>, vector<int>>> _parse_obj(const char* obj_filepath)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
*data = { .verts = verts, .verts_len = vert_i, .faces = faces, .faces_len = face_i };
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -66,6 +66,9 @@ int main() {
|
||||
}
|
||||
|
||||
start(800, 800);
|
||||
pointid id = create_point(0, 0, 0);
|
||||
set_color(id, 1, 1, 1);
|
||||
set_scale(id, 10);
|
||||
|
||||
while (true)
|
||||
;
|
||||
@@ -84,7 +87,8 @@ int main() {
|
||||
// vector<string> words = split_str(line, ' ');
|
||||
// if (!words.size() == 4)
|
||||
// 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];
|
||||
// float x = stof(words[1]);
|
||||
// float y = stof(words[2]);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <cassert>
|
||||
#include <windows.h>
|
||||
|
||||
#include <cstring>
|
||||
@@ -5,9 +6,7 @@
|
||||
#include <glm/ext/vector_float4.hpp>
|
||||
#include <profileapi.h>
|
||||
#include <synchapi.h>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
@@ -16,9 +15,7 @@
|
||||
#include <glm/matrix.hpp>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <winnt.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include "sync.hpp"
|
||||
#include "util.hpp"
|
||||
@@ -27,13 +24,18 @@
|
||||
#include "camera.hpp"
|
||||
#include "live_plotter.hpp"
|
||||
|
||||
#define LERP_MOVE_PERIOD_S 0.05
|
||||
#define STOP_WAIT_TIME_MS 500
|
||||
|
||||
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 {
|
||||
bool initialized;
|
||||
bool replaceable;
|
||||
glm::vec3 startloc;
|
||||
glm::vec3 targetloc;
|
||||
Body b;
|
||||
@@ -51,13 +53,11 @@ static Thread t;
|
||||
static Mutex m;
|
||||
static uint shader;
|
||||
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
|
||||
const char* vertex_filepath = "vertex.glsl";
|
||||
const char* fragment_filepath = "fragment.glsl";
|
||||
constexpr int point_buf_len
|
||||
= max_point_mem / sizeof(Point); // ~max_point_mem bytes of points. does not include vertex data for obj
|
||||
static Point* point_buf = (Point*)malloc(point_buf_len * sizeof(Point));
|
||||
static int point_buf_i = 0;
|
||||
|
||||
// Private foward decls
|
||||
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();
|
||||
|
||||
for (int i = 0; i < point_buf_len; i++) {
|
||||
point_buf[i].replaceable = true;
|
||||
}
|
||||
|
||||
if (!QueryPerformanceFrequency(&fr)) {
|
||||
printf("Failed to get the performance counter frequency.\n");
|
||||
return false;
|
||||
@@ -95,72 +99,81 @@ extern DllExport bool __cdecl start(int win_w, int win_h) {
|
||||
|
||||
extern DllExport bool __cdecl stop() {
|
||||
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;
|
||||
running = !success; // Consider the app still running if we didn't shut down properly...?
|
||||
|
||||
// TODO: Please wrap away win api synch stuff. thank you
|
||||
if (!running) dispose(m);
|
||||
if (!running)
|
||||
dispose(m);
|
||||
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);
|
||||
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,
|
||||
.replaceable = false,
|
||||
.startloc = glm::vec3(x, y, z),
|
||||
.targetloc = glm::vec3(x, y, z),
|
||||
.lifetime_s = INFINITY,
|
||||
.start_s = _time()
|
||||
.start_s = _time()
|
||||
};
|
||||
p->b.scale = 1.0f;
|
||||
p->b.color = glm::vec4(randf() + 0.1, randf() + 0.1, randf() + 0.1, 1);
|
||||
point_buf[slot].b.scale = 1.0f;
|
||||
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);
|
||||
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);
|
||||
Point* p = id_to_point[id];
|
||||
p->b.color = { r, g, b, p->b.color[3] };
|
||||
Point &p = point_buf[id % point_buf_len];
|
||||
p.b.color = { r, g, b, p.b.color[3] };
|
||||
unlock(m);
|
||||
}
|
||||
|
||||
extern DllExport void __cdecl set_scale(pointid id, float scale) {
|
||||
lock(m);
|
||||
Point* p = id_to_point[id];
|
||||
p->b.scale = scale;
|
||||
Point &p = point_buf[id % point_buf_len];
|
||||
p.b.scale = scale;
|
||||
unlock(m);
|
||||
}
|
||||
|
||||
extern DllExport void __cdecl update_point(pointid id, float x, float y, float z) {
|
||||
lock(m);
|
||||
Point* p = id_to_point[id];
|
||||
p->targetloc = { x, y, z };
|
||||
p->startloc = p->b.pose[3];
|
||||
p->start_s = _time();
|
||||
Point &p = point_buf[id % point_buf_len];
|
||||
p.targetloc = { x, y, z };
|
||||
p.startloc = p.b.pose[3];
|
||||
p.start_s = _time();
|
||||
unlock(m);
|
||||
}
|
||||
|
||||
extern DllExport void __cdecl set_lifetime(pointid id, float new_lifetime_s) {
|
||||
lock(m);
|
||||
Point* p = id_to_point[id];
|
||||
p->start_s = _time();
|
||||
p->lifetime_s = new_lifetime_s;
|
||||
Point &p = point_buf[id % point_buf_len];
|
||||
p.start_s = _time();
|
||||
p.lifetime_s = new_lifetime_s;
|
||||
unlock(m);
|
||||
}
|
||||
|
||||
extern DllExport void __cdecl clear_point(pointid id) {
|
||||
lock(m);
|
||||
Point* p = id_to_point[id];
|
||||
id_to_point.erase(id);
|
||||
point_to_id.erase(p);
|
||||
free(p);
|
||||
point_buf[id % point_buf_len].replaceable = true;
|
||||
unlock(m);
|
||||
}
|
||||
|
||||
@@ -192,7 +205,7 @@ void _scroll_cb(GLFWwindow* win, double xoffset, double yoffset) { zoom_camera(c
|
||||
|
||||
DWORD _win_thread(LPVOID 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.phi = glm::radians(30.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));
|
||||
|
||||
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
|
||||
if (!p->initialized) {
|
||||
create_new_sphere(&p->b, p->b.scale);
|
||||
p->b.shader = shader;
|
||||
p->b.pose = glm::translate(glm::mat4(1), p->startloc);
|
||||
p->initialized = true;
|
||||
if (!p.initialized) {
|
||||
create_new_sphere(&p.b, p.b.scale);
|
||||
p.b.shader = shader;
|
||||
p.b.pose = glm::translate(glm::mat4(1), p.startloc);
|
||||
p.initialized = true;
|
||||
}
|
||||
|
||||
// Perform fade
|
||||
double elapsed_time_s = _time() - p->start_s;
|
||||
float t = elapsed_time_s / p->lifetime_s;
|
||||
t = std::clamp(t, 0.0f, 1.0f);
|
||||
p->b.color.w = lerp(1.0, 0.0, t);
|
||||
double elapsed_time_s = _time() - p.start_s;
|
||||
float t = elapsed_time_s / p.lifetime_s;
|
||||
t = glm::clamp(t, 0.0f, 1.0f);
|
||||
p.b.color.w = lerp(1.0, 0.0, t);
|
||||
|
||||
// Lerp position
|
||||
t = elapsed_time_s / LERP_MOVE_PERIOD_S;
|
||||
t = std::clamp(t, 0.0f, 1.0f);
|
||||
p->b.pose[3] = glm::vec4(lerp(p->startloc, p->targetloc, t), 1);
|
||||
draw_body(p->b);
|
||||
t = elapsed_time_s / lerp_move_period_s;
|
||||
t = glm::clamp(t, 0.0f, 1.0f);
|
||||
p.b.pose[3] = glm::vec4(lerp(p.startloc, p.targetloc, t), 1);
|
||||
draw_body(p.b);
|
||||
}
|
||||
unlock(m);
|
||||
}
|
||||
@@ -290,4 +306,3 @@ double _time() {
|
||||
}
|
||||
return (double)timeticks.QuadPart / (double)fr.QuadPart;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user