Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit ada22d4

Browse files
committed
tried threading for loading OBJs
1 parent d4401eb commit ada22d4

File tree

5 files changed

+131
-37
lines changed

5 files changed

+131
-37
lines changed

engine.exe

12 KB
Binary file not shown.

imgui.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ RefScale=13
241241
Column 0 Sort=0v
242242

243243
[Docking][Data]
244-
DockSpace ID=0x382E4429 Window=0x260A4489 Pos=241,160 Size=1424,865 Split=X
244+
DockSpace ID=0x382E4429 Window=0x260A4489 Pos=57,110 Size=1424,865 Split=X
245245
DockNode ID=0x00000013 Parent=0x382E4429 SizeRef=250,865 Split=Y Selected=0x0B397EC5
246246
DockNode ID=0x00000015 Parent=0x00000013 SizeRef=184,227 Selected=0x0B397EC5
247247
DockNode ID=0x00000016 Parent=0x00000013 SizeRef=184,636 Split=Y Selected=0x4746B4B8

include/BEngine/engine.cpp

Lines changed: 104 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,58 @@ void Mesh::loadOBJ(const std::string* objSource) {
759759
updateGPU();
760760
}
761761

762+
void Mesh::updateGPU() {
763+
764+
vao.bind();
765+
delete vbo;
766+
delete ebo;
767+
vbo = new VBO(vertices);
768+
ebo = new EBO(indices);
769+
770+
vbo->linkVertexAttrib(0, 3, GL_FLOAT, sizeof(Vertex), (void*)0);
771+
vbo->linkVertexAttrib(1, 3, GL_FLOAT, sizeof(Vertex), (void*)(3 * sizeof(float)));
772+
vbo->linkVertexAttrib(2, 3, GL_FLOAT, sizeof(Vertex), (void*)(6 * sizeof(float)));
773+
vbo->linkVertexAttrib(3, 2, GL_FLOAT, sizeof(Vertex), (void*)(9 * sizeof(float)));
774+
775+
Message(0, "MESH", "OBJ loaded successfully", "STRING", 1);
776+
}
777+
778+
void Mesh::updateGPUAsync() {
779+
if (!ready) return;
780+
781+
std::lock_guard<std::mutex> lock(parseMutex);
782+
if (!parseData.has_value()) return;
783+
784+
auto& [verts, inds] = *parseData;
785+
786+
vao.bind();
787+
delete vbo;
788+
delete ebo;
789+
vbo = new VBO(vertices);
790+
ebo = new EBO(indices);
791+
792+
vbo->linkVertexAttrib(0, 3, GL_FLOAT, sizeof(Vertex), (void*)0);
793+
vbo->linkVertexAttrib(1, 3, GL_FLOAT, sizeof(Vertex), (void*)(3 * sizeof(float)));
794+
vbo->linkVertexAttrib(2, 3, GL_FLOAT, sizeof(Vertex), (void*)(6 * sizeof(float)));
795+
vbo->linkVertexAttrib(3, 2, GL_FLOAT, sizeof(Vertex), (void*)(9 * sizeof(float)));
796+
797+
Message(0, "MESH", "OBJ loaded successfully", "STRING", 1);
798+
799+
parseData.reset();
800+
ready = false;
801+
802+
}
803+
804+
int GetOrAddVertex(std::vector<Vertex>& vertices, const Vertex& v) {
805+
for (size_t i = 0; i < vertices.size(); i++) {
806+
if (memcmp(&vertices[i], &v, sizeof(Vertex)) == 0) {
807+
return static_cast<int>(i);
808+
}
809+
}
810+
vertices.push_back(v);
811+
return static_cast<int>(vertices.size() - 1);
812+
}
813+
762814
void Mesh::parseOBJString(const std::string* objSource, std::vector<Vertex>& outVerts, std::vector<GLuint>& outIndices) {
763815

764816
std::vector<glm::vec3> positions;
@@ -851,33 +903,6 @@ void Mesh::parseOBJString(const std::string* objSource, std::vector<Vertex>& out
851903
}
852904
}
853905

854-
void Mesh::updateGPU() {
855-
856-
vao.bind();
857-
delete vbo;
858-
delete ebo;
859-
vbo = new VBO(vertices);
860-
ebo = new EBO(indices);
861-
862-
vbo->linkVertexAttrib(0, 3, GL_FLOAT, sizeof(Vertex), (void*)0);
863-
vbo->linkVertexAttrib(1, 3, GL_FLOAT, sizeof(Vertex), (void*)(3 * sizeof(float)));
864-
vbo->linkVertexAttrib(2, 3, GL_FLOAT, sizeof(Vertex), (void*)(6 * sizeof(float)));
865-
vbo->linkVertexAttrib(3, 2, GL_FLOAT, sizeof(Vertex), (void*)(9 * sizeof(float)));
866-
867-
Message(0, "MESH", "OBJ loaded successfully", "STRING", 1);
868-
869-
}
870-
871-
int Mesh::GetOrAddVertex(std::vector<Vertex>& vertices, const Vertex& v) {
872-
for (size_t i = 0; i < vertices.size(); i++) {
873-
if (memcmp(&vertices[i], &v, sizeof(Vertex)) == 0) {
874-
return static_cast<int>(i);
875-
}
876-
}
877-
vertices.push_back(v);
878-
return static_cast<int>(vertices.size() - 1);
879-
}
880-
881906
void Mesh::draw(Shader& shader, const glm::mat4& modelMatrix) {
882907
shader.activate();
883908
vao.bind();
@@ -946,6 +971,58 @@ void Mesh::makePreview(Framebuffer& fb, Shader& shader, glm::vec2 rotation, bool
946971
fb.unbind();
947972
}
948973

974+
void Mesh::loadOBJAsync(const std::string& objPath) {
975+
if (parsing) return;
976+
977+
parsing = true;
978+
ready = false;
979+
980+
std::thread([this, objPath]() {
981+
std::vector<Vertex> verts;
982+
std::vector<GLuint> inds;
983+
984+
std::ifstream file(objPath);
985+
if (!file.is_open()) {
986+
Message(2, "MESH", "Failed to open OBJ file " + objPath, objPath.c_str(), 1);
987+
return;
988+
}
989+
990+
std::string source((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
991+
992+
parseOBJString(&source, verts, inds);
993+
994+
{
995+
std::lock_guard<std::mutex> lock(parseMutex);
996+
parseData = std::make_pair(std::move(verts), std::move(inds));
997+
}
998+
999+
parsing = false;
1000+
ready = true;
1001+
}).detach();
1002+
}
1003+
1004+
void Mesh::loadOBJAsync(const std::string* objSource) {
1005+
if (parsing) return;
1006+
1007+
parsing = true;
1008+
ready = false;
1009+
1010+
std::thread([this, objSource]() {
1011+
std::vector<Vertex> verts;
1012+
std::vector<GLuint> inds;
1013+
1014+
parseOBJString(objSource, verts, inds);
1015+
1016+
{
1017+
std::lock_guard<std::mutex> lock(parseMutex);
1018+
parseData = std::make_pair(std::move(verts), std::move(inds));
1019+
}
1020+
1021+
parsing = false;
1022+
ready = true;
1023+
}).detach();
1024+
}
1025+
9491026
Mesh::~Mesh() {
9501027
delete vbo;
9511028
delete ebo;

include/BEngine/engine.hpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
#include <filesystem>
2424
#include <cctype>
2525

26+
#include <thread>
27+
#include <atomic>
28+
#include <mutex>
29+
#include <optional>
30+
2631
#include "BEngine/engine_default.hpp"
2732

2833
namespace BE {
@@ -303,16 +308,24 @@ class Mesh {
303308
void loadData(const std::vector<Vertex>& verts, const std::vector<GLuint>& inds);
304309
void loadOBJ(const std::string& objPath);
305310
void loadOBJ(const std::string* objSource);
311+
void loadOBJAsync(const std::string& objPath);
312+
void loadOBJAsync(const std::string* objSource);
313+
314+
void updateGPU();
315+
void updateGPUAsync();
306316

307317
~Mesh();
308318

309319
void draw(Shader& shader, const glm::mat4& modelMatrix = glm::mat4(1));
310320
void makePreview(Framebuffer& fb, Shader& shader, glm::vec2 rotation, bool cull = false);
311321

312-
public:
313322
void parseOBJString(const std::string* objSource, std::vector<Vertex>& outVerts, std::vector<GLuint>& outIndices);
314-
void updateGPU();
315-
int GetOrAddVertex(std::vector<Vertex>& vertices, const Vertex& v);
323+
324+
std::mutex parseMutex;
325+
std::optional<std::pair<std::vector<Vertex>, std::vector<GLuint>>> parseData;
326+
std::atomic<bool> parsing{false};
327+
std::atomic<bool> ready{false};
328+
316329
};
317330

318331
struct CreatedShader {

include/BEngine/engine_editor.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ void Editor::beginFrame() {
9797

9898
ImGui::DockSpace(ImGui::GetID("MyDockSpace"), ImVec2(0.0f, 0.0f));
9999
ImGui::End();
100-
101-
if (glfwGetKey(engine->getWindow(), GLFW_KEY_W)) currentGizmoOperation = ImGuizmo::TRANSLATE;
102-
if (glfwGetKey(engine->getWindow(), GLFW_KEY_E)) currentGizmoOperation = ImGuizmo::ROTATE;
103-
if (glfwGetKey(engine->getWindow(), GLFW_KEY_R)) currentGizmoOperation = ImGuizmo::SCALE;
104-
if (glfwGetKey(engine->getWindow(), GLFW_KEY_T)) currentGizmoOperation = ImGuizmo::UNIVERSAL;
105100
}
106101

107102
void Editor::showPanels() {
@@ -217,6 +212,11 @@ void Editor::Viewport() {
217212

218213
if (ImGui::IsWindowHovered()) {
219214
engine->editorCamera.inputs();
215+
216+
if (glfwGetKey(engine->getWindow(), GLFW_KEY_W)) currentGizmoOperation = ImGuizmo::TRANSLATE;
217+
if (glfwGetKey(engine->getWindow(), GLFW_KEY_E)) currentGizmoOperation = ImGuizmo::ROTATE;
218+
if (glfwGetKey(engine->getWindow(), GLFW_KEY_R)) currentGizmoOperation = ImGuizmo::SCALE;
219+
if (glfwGetKey(engine->getWindow(), GLFW_KEY_T)) currentGizmoOperation = ImGuizmo::UNIVERSAL;
220220
} else {
221221
engine->editorCamera.scrollDelta = glm::vec2(0,0);
222222
}
@@ -228,7 +228,6 @@ void Editor::Viewport() {
228228
bool resizing = ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) && ImGui::IsMouseDown(ImGuiMouseButton_Left);
229229
if (!resizing) engine->viewport->resize(viewportSize.x, viewportSize.y);
230230

231-
// if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows)) engine->viewport->camera->handleInputs(engine->getWindow(), engine->ts.dt);
232231
ImGui::Image((void*)(intptr_t) engine->viewport->getColorTexture(), viewportSize, ImVec2(0, 1), ImVec2(1, 0));
233232

234233
ImVec2 relativePos = ImVec2(mousePos.x - viewportPos.x, mousePos.y - viewportPos.y);
@@ -260,6 +259,7 @@ void Editor::Viewport() {
260259
);
261260

262261
if (ImGuizmo::IsUsing()) {
262+
263263
glm::vec3 translation, rotation, scale;
264264
ImGuizmo::DecomposeMatrixToComponents(
265265
glm::value_ptr(t.model),
@@ -401,6 +401,10 @@ void Editor::Resources() {
401401

402402
ImGui::SeparatorText("Meshes");
403403
for (auto& [key, mesh] : engine->resources().meshes) {
404+
if (mesh->ready) {
405+
mesh->updateGPUAsync();
406+
}
407+
404408
if (!MatchesSearch(key) && !MatchesSearch("Meshes")) continue;
405409

406410
if (ImGui::Selectable(std::string(key + "##Mesh").c_str())) {}

0 commit comments

Comments
 (0)