From 711b6ab7fe030ea9bafb237f0d1c3f053c8cff55 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Wed, 28 Mar 2018 11:46:21 +0200 Subject: [PATCH] intermediate commit --- src/modules/RenderUtils.cc | 66 ++++++++++++++++++++++++++++++++++++++ src/modules/RenderUtils.h | 5 +++ 2 files changed, 71 insertions(+) diff --git a/src/modules/RenderUtils.cc b/src/modules/RenderUtils.cc index 4d6efa1..107a717 100644 --- a/src/modules/RenderUtils.cc +++ b/src/modules/RenderUtils.cc @@ -717,6 +717,8 @@ bool AssetFile::Load(const char* filename) { gLog("Successfully loaded model '%s'", mFilename.c_str()); } + LoadBuffers(); + return result; } @@ -735,6 +737,69 @@ void AssetFile::DrawNodeGui(const tinygltf::Node& node) { } } +void AssetFile::LoadBuffers() { + for (int i = 0, n = mGLTFModel.bufferViews.size(); i < n; ++i) { + const tinygltf::BufferView &buffer_view = mGLTFModel.bufferViews[i]; + if (buffer_view.target == 0) { + gLog("Warning: buffer_view target at index %d is 0", i); + continue; + } + + const tinygltf::Buffer &buffer = mGLTFModel.buffers[buffer_view.buffer]; + GLuint buffer_id; + glGenBuffers(1, &buffer_id); + glBindBuffer(buffer_view.target, buffer_id); + if (buffer_view.name.size() > 0) { + gLog("Loading Buffer '%s': size %d offset %d", buffer_view.name.c_str(), buffer.data.size(), buffer_view.byteOffset); + } else { + gLog("Loading Buffer: size %d offset %d", buffer_view.name.c_str(), buffer.data.size(), buffer_view.byteOffset); + } + glBufferData(buffer_view.target, buffer_view.byteLength, + &buffer.data.at(0) + buffer_view.byteOffset, GL_STATIC_DRAW); + glBindBuffer(buffer_view.target, 0); + mBuffers.push_back(buffer_id); + } +} + +void AssetFile::DrawMesh(const tinygltf::Mesh &mesh, const Matrix44f &matrix) { +} + +void AssetFile::DrawNode(const tinygltf::Node &node, const Matrix44f &matrix) { + Matrix44f local_matrix = matrix; + if (node.matrix.size() == 16) { + Matrix44d mat44d = SimpleMath::Map (node.matrix.data(), 4, 4); + local_matrix *= mat44d + } else { + if (node.scale.size() == 3) { + local_matrix *= ScaleMat44(node.scale[0], node.scale[1], node.scale[2]); + } + + if node.rotation.size() == 4) { + local_matrix *= Quaternion(node.rotation[0], node.rotation[1], node.rotation[2], node.rotation[3]).toGLMatrix(); + } + + if node.translation.size() == 3) { + local_matrix *= TranslateMat44(node.translation[0], node.translation[1], node.translation[2]); + } + } + + DrawMesh (mGLTFModel.meshes[node.mesh], local_matrix); + + for (int i = 0; i < node.children.size(); ++i) { + DrawNode(model.nodes[node.children[i]]); + } +} + +void AssetFile::DrawModel() { + // todo: support non-default scenes + assert(mGLTFModel.defaultScene >= 0); + const tinygltf::Scene &scene = mGLTFModel.scenes[mGLTFModel.defaultScene]; + for (int i = 0; i < scene.nodes.size(); ++i) { + const tinygltf::Node &node = mGLTFModel.nodes[scene.nodes[i]]; + DrawNode(node, Matrix44f::Identity()); + } +} + void AssetFile::DrawGui() { ImGui::Text("File: %s", mFilename.c_str()); if (ImGui::TreeNode("Meshes")) { @@ -762,3 +827,4 @@ void AssetFile::DrawGui() { ImGui::TreePop(); } } + diff --git a/src/modules/RenderUtils.h b/src/modules/RenderUtils.h index 4a8dcdf..881141b 100644 --- a/src/modules/RenderUtils.h +++ b/src/modules/RenderUtils.h @@ -418,8 +418,13 @@ struct VertexArrayMesh { struct AssetFile { std::string mFilename; tinygltf::Model mGLTFModel; + std::vector mBuffers; bool Load(const char* filename); + void LoadBuffers(); + void DrawMesh(const tinygltf::Mesh &mesh, const Matrix44f &matrix); + void DrawNode(const tinygltf::Node &node, const Matrix44f &matrix); + void DrawModel(); void DrawNodeGui(const tinygltf::Node& node); void DrawGui(); };