Skip to content

Commit 1f6000e

Browse files
committed
Backend minor improvement and other changes
1 parent 5293e36 commit 1f6000e

63 files changed

Lines changed: 412 additions & 751 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,5 @@ lint/tmp/
211211
*.cache
212212
*.spv
213213

214-
asset/
214+
asset/
215+
imgui.ini

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/Tessellation)
140140
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/Deferred)
141141
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/ModelBase)
142142
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/ModelViewer)
143-
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/ShadowVolume)
144143
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/SimpleReflection)
145144
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/Forest)
146145
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/OcclusionCulling)

Common/Importer/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake" )
55
INCLUDE(assimp)
66

77
ADD_LIBRARY(gl-sample-common-asset-importer ${SOURCE_IMPORTER_FILES} ${HEADER_IMPORTER_FILES})
8-
TARGET_LINK_LIBRARIES(gl-sample-common-asset-importer PUBLIC fmt fragcore-imageloader glCommon assimp)
8+
TARGET_LINK_LIBRARIES(gl-sample-common-asset-importer PUBLIC fmt fragcore fragcore-imageloader glCommon assimp)
99

1010
TARGET_INCLUDE_DIRECTORIES(gl-sample-common-asset-importer PUBLIC
1111
${CMAKE_CURRENT_SOURCE_DIR}

Common/Importer/ImportHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <ProceduralGeometry.h>
99
#include <cstdint>
1010
#include <exception>
11-
#include <half.hpp>
11+
//#include <half.hpp>
1212
#include <iostream>
1313
#include <ostream>
1414

Common/Importer/ModelImporter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,14 @@ void ModelImporter::initNodeRoot(const aiNode *ai_node, NodeObject *parent) {
311311
aiVector3f position, scale;
312312
aiQuaternion rotation;
313313

314-
// TODO: fix pool.
315-
314+
// TODO: fix pool for classes.
316315
NodeObject *pobject = new NodeObject(); // nodePool.obtain();
317316
//*pobject = NodeObject();
318317

318+
/* Assigned parent. */
319319
if (parent) {
320320
pobject->parent = parent;
321-
// parent->childrens.addChild(ITree<node_object_t *> *pchild)
321+
pobject->parent_index = this->nodes.size();
322322
} else {
323323
pobject->parent = nullptr;
324324
}

Common/Importer/ModelImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ using NodeObject = struct node_object_t : public AssetObject {
151151
std::vector<unsigned int> geometryObjectIndex;
152152
std::vector<unsigned int> materialIndex;
153153

154+
int parent_index = -1;
154155
struct node_object_t *parent = nullptr;
155-
fragcore::ITree<struct node_object_t *> childrens;
156156
};
157157

158158
using MeshData = struct mesh_data_t : public AssetObject {

Common/Scene/Node.cpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,57 @@
11
#include "Node.h"
2+
#include <glm/ext/matrix_transform.hpp>
23
#include <glm/ext/quaternion_common.hpp>
34
#include <glm/fwd.hpp>
5+
#include <glm/gtc/quaternion.hpp>
46
#include <glm/gtx/quaternion.hpp>
57

68
using namespace glsample;
79

810
void Node::setPosition(const glm::vec3 &position) noexcept {
11+
912
const glm::vec3 position_offset = position - this->getPosition();
13+
1014
for (int x = 0; x < this->getNumChildren(); x++) {
1115

1216
Node *node = dynamic_cast<Node *>(this->getChild(x));
13-
node->setPosition(node->getPosition() + position);
17+
node->setPosition(node->getPosition() + position_offset);
1418
}
19+
1520
TransformGLM::setPosition(position);
1621
}
1722
glm::vec3 Node::getPosition() noexcept { return TransformGLM::getPosition(); }
1823
const glm::vec3 &Node::getPosition() const noexcept { return TransformGLM::getPosition(); }
1924

20-
void Node::setScale(const glm::vec3 &scale) noexcept { TransformGLM::setPosition(scale); }
25+
void Node::setScale(const glm::vec3 &scale) noexcept {
26+
27+
const glm::vec3 scale_offset = scale - getScale();
28+
29+
TransformGLM::setScale(scale);
30+
}
2131
glm::vec3 Node::getScale() const noexcept { return TransformGLM::getScale(); }
2232

2333
const glm::quat &Node::getRotation() const noexcept { return TransformGLM::getRotation(); }
2434
void Node::setRotation(const glm::quat &quat) noexcept { TransformGLM::setRotation(quat); }
2535

2636
Node *Node::parent() const noexcept { return dynamic_cast<Node *>(this->getParent()); }
2737

38+
glm::mat4 Node::getGlobalMatrix() const noexcept {
39+
glm::mat4 model(1);
40+
model = glm::translate(model, this->getPosition());
41+
model = model * glm::toMat4(this->getRotation());
42+
model = glm::scale(model, this->getScale());
43+
return model;
44+
}
45+
glm::mat4 Node::getLocalMatrix() const noexcept {
46+
glm::mat4 model(1);
47+
model = glm::translate(model, this->getLocalPosition());
48+
model = model * glm::toMat4(this->getLocalRotation());
49+
model = glm::scale(model, this->getLocalScale());
50+
return model;
51+
}
52+
2853
glm::mat4 Node::getViewMatrix() const noexcept { return glm::translate(glm::mat4(1), -this->getPosition()); }
54+
glm::mat4 Node::getLocalViewMatrix() const noexcept { return glm::translate(glm::mat4(1), -this->getLocalPosition()); }
2955
glm::mat4 Node::getRotationMatrix() const noexcept {
3056
glm::quat rotation = glm::quatLookAt(glm::normalize(this->forward()), glm::normalize(this->up()));
3157
return glm::toMat4(rotation);
@@ -36,6 +62,7 @@ void Node::setLocalPosition(const glm::vec3 &localPosition) noexcept {
3662
Node *parent = this->parent();
3763

3864
const glm::vec3 global_position = parent ? parent->getPosition() : glm::vec3(0);
65+
3966
this->setPosition(global_position + localPosition);
4067
}
4168
void Node::setLocalScale(const glm::vec3 &localScale) noexcept {
@@ -46,20 +73,28 @@ void Node::setLocalScale(const glm::vec3 &localScale) noexcept {
4673
}
4774
void Node::setLocalRotation(const glm::quat &localRotation) noexcept {
4875
Node *parent = this->parent();
76+
if (parent) {
77+
}
4978

5079
const glm::quat global_rotation = parent ? parent->getRotation() : glm::quat();
5180
this->setRotation(global_rotation * localRotation);
5281
}
5382

5483
glm::vec3 Node::getLocalPosition() const noexcept {
5584
Node *parent = this->parent();
56-
glm::vec3 parent_position = parent ? parent->getPosition() : glm::vec3(0);
57-
return parent_position - this->getPosition();
85+
if (parent) {
86+
return this->getPosition() - parent->getPosition();
87+
}
88+
89+
return this->getPosition();
5890
}
91+
5992
glm::vec3 Node::getLocalScale() const noexcept {
6093
Node *parent = this->parent();
61-
glm::vec3 parent_position = parent ? parent->getScale() : glm::vec3(0);
62-
return parent_position - this->getScale();
94+
if (parent) {
95+
return this->getScale() - parent->getScale();
96+
}
97+
return this->getScale();
6398
}
6499
glm::quat Node::getLocalRotation() const noexcept {
65100
Node *parent = this->parent();

Common/Scene/Node.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ namespace glsample {
5454
void setLocalScale(const glm::vec3 &localScale) noexcept;
5555
void setLocalRotation(const glm::quat &localRotation) noexcept;
5656

57+
glm::mat4 getGlobalMatrix() const noexcept;
58+
glm::mat4 getLocalMatrix() const noexcept;
59+
5760
glm::mat4 getViewMatrix() const noexcept;
61+
glm::mat4 getLocalViewMatrix() const noexcept;
5862
glm::mat4 getRotationMatrix() const noexcept;
5963
glm::mat4 getViewTranslationMatrix() const noexcept;
6064

Common/Scene/Scene.cpp

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ namespace glsample {
288288
NodeData *baseNodeData = this->stageNodeDataRobin.buffers[getRoundRobinIndex()];
289289
size_t node_index = 0;
290290
auto copyQueue = renderQueue; // TODO: fix performance
291-
for (const NodeObject *node : copyQueue) {
292-
baseNodeData[node_index++].model = node->modelGlobalTransform;
291+
for (const Node *node : copyQueue) {
292+
baseNodeData[node_index++].model = node->getGlobalMatrix(); // modelGlobalTransform;
293293
}
294294

295295
/* Update Materials. */
@@ -406,7 +406,7 @@ namespace glsample {
406406
/* */
407407
// TODO: multi thread
408408
const size_t num_threads = 6;
409-
static std::vector<std::vector<NodeObject *>> objects(num_threads, std::vector<NodeObject *>());
409+
static std::vector<std::vector<Node *>> objects(num_threads, std::vector<Node *>());
410410
for (size_t i = 0; i < objects.size(); i++) {
411411
objects[i].clear();
412412
objects[i].reserve(1024);
@@ -415,7 +415,7 @@ namespace glsample {
415415
#pragma omp parallel for collapse(1) num_threads(num_threads)
416416
for (size_t node_index = 0; node_index < this->getNodes().size(); node_index++) {
417417

418-
NodeObject *node = this->getNodes()[node_index];
418+
Node *node = this->getNodes()[node_index];
419419

420420
/* Check if any of the meshes are visable. */
421421
for (size_t mesh_index = 0; mesh_index < node->geometryObjectIndex.size(); mesh_index++) {
@@ -458,11 +458,11 @@ namespace glsample {
458458
}
459459

460460
for (size_t i = 0; i < objects.size(); i++) {
461-
visableNodes.insert(visableNodes.end(), objects[i].begin(), objects[i].end());
461+
this->visableNodes.insert(this->visableNodes.end(), objects[i].begin(), objects[i].end());
462462
}
463463

464464
} else {
465-
visableNodes = this->getNodes();
465+
this->visableNodes = this->getNodes();
466466
}
467467
}
468468

@@ -516,10 +516,10 @@ namespace glsample {
516516
/* */
517517
for (auto it = order.begin(); it != order.end(); it++) {
518518
// RenderQueue renderQueue = (*it).first;
519-
std::deque<const NodeObject *> nodeQueue = this->renderBucketQueue[(*it)];
519+
std::deque<const Node *> nodeQueue = this->renderBucketQueue[(*it)];
520520
for (auto itQ = nodeQueue.begin(); itQ != nodeQueue.end(); itQ++) {
521521

522-
const NodeObject *node = (*itQ);
522+
const Node *node = (*itQ);
523523
// this->debugDrawer->addAABB(node->bound.aabb, glm::vec4(0.3f, 1.0f 0.3f, 1.0f));
524524
}
525525
}
@@ -596,30 +596,30 @@ namespace glsample {
596596
/* */
597597
for (auto it = order.begin(); it != order.end(); it++) {
598598
// RenderQueue renderQueue = (*it).first;
599-
std::deque<const NodeObject *> nodeQueue = this->renderBucketQueue[(*it)];
599+
std::deque<const Node *> nodeQueue = this->renderBucketQueue[(*it)];
600600

601601
/* */
602602
const std::string domain = fmt::format("{}", (int)(*it));
603603

604604
glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 1, domain.size(), domain.data());
605605
for (auto itQ = nodeQueue.begin(); itQ != nodeQueue.end(); itQ++) {
606-
const NodeObject *node = (*itQ);
606+
const Node *node = (*itQ);
607607
this->renderNode(node);
608608
}
609609
glPopDebugGroup();
610610
}
611611

612612
for (auto it = this->renderQueue.begin(); it != this->renderQueue.end(); it++) {
613-
const NodeObject *node = (*it);
613+
const Node *node = (*it);
614614
// this->renderNode(node);
615615
}
616616

617617
/* */
618618
if (this->debugMode & DebugMode::Wireframe) {
619619
/* */
620-
for (const NodeObject *node : this->renderQueue) {
620+
for (const Node *node : this->renderQueue) {
621621
/* */
622-
// const NodeObject *node = this->renderQueue[x];
622+
// const Node *node = this->renderQueue[x];
623623
// this->renderNode(node);
624624
}
625625
}
@@ -744,7 +744,7 @@ namespace glsample {
744744
}
745745
}
746746

747-
void Scene::renderNode(const NodeObject *node) {
747+
void Scene::renderNode(const Node *node) {
748748

749749
/* Update binding offset. */
750750
if ((currentNodeIndex % this->UBOStructure.max_node_per_binding) == 0) {
@@ -783,7 +783,8 @@ namespace glsample {
783783
this->bindMaterial(&material);
784784
}
785785

786-
const MeshObject &refMesh = this->refGeometry[node->geometryObjectIndex[geo_index]];
786+
const int mesh_index = node->geometryObjectIndex[geo_index];
787+
const MeshObject &refMesh = this->refGeometry[mesh_index];
787788
glBindVertexArray(refMesh.vao);
788789

789790
/* Material, model matrix. */
@@ -808,15 +809,15 @@ namespace glsample {
808809
for (size_t x = 0; x < this->visableNodes.size(); x++) {
809810

810811
/* */
811-
const NodeObject *node = this->visableNodes[x];
812+
const Node *node = this->visableNodes[x];
812813
if (node->materialIndex.empty()) {
813814
continue;
814815
}
815816

816817
/* */
817-
const bool validIndex = node->materialIndex[0] < this->materials.size();
818+
const bool validMaterialIndex = node->materialIndex[0] < this->materials.size();
818819

819-
if (validIndex) {
820+
if (validMaterialIndex) {
820821

821822
const MaterialObject *material = &this->materials[node->materialIndex[0]];
822823
assert(material);
@@ -832,7 +833,7 @@ namespace glsample {
832833
}
833834

834835
} else {
835-
std::cerr << "Invalid Material " << node->name << std::endl;
836+
std::cerr << "Invalid Material " << node->getName() << std::endl;
836837
}
837838
}
838839

@@ -947,49 +948,38 @@ namespace glsample {
947948

948949
for (size_t node_index = 0; node_index < nodes.size(); node_index++) {
949950

951+
Node *currentNode = nodes[node_index];
952+
950953
if (node_index == 0) {
951954
ImGui::SetNextItemOpen(true, ImGuiCond_Once);
952955
}
953956

954957
ImGui::PushID(node_index);
955958

956-
if (nodes[node_index]->parent) {
957-
ImGui::TextUnformatted(nodes[node_index]->parent->name.c_str());
959+
if (currentNode->getParent()) {
960+
ImGui::TextUnformatted(currentNode->getParent()->ptr()->getName().c_str());
958961
}
959962

960-
ImGui::TextUnformatted(nodes[node_index]->name.c_str());
961-
if (ImGui::DragFloat3("Position", &nodes[node_index]->localPosition[0])) {
962-
const glm::mat4 globaMat = nodes[node_index]->parent == nullptr
963-
? glm::mat4(1)
964-
: nodes[node_index]->parent->modelGlobalTransform;
965-
nodes[node_index]->modelGlobalTransform =
966-
glm::translate(globaMat, nodes[node_index]->localPosition);
963+
ImGui::TextUnformatted(currentNode->getName().c_str());
964+
965+
glm::vec3 localPosition = currentNode->getLocalPosition();
966+
if (ImGui::DragFloat3("Position", &localPosition[0])) {
967+
currentNode->setLocalPosition(localPosition);
967968
}
968969

969-
if (ImGui::DragFloat4("Rotation (Quat)", &nodes[node_index]->localRotation[0])) {
970-
nodes[node_index]->localRotation = glm::normalize(nodes[node_index]->localRotation);
971-
const glm::mat4 globaMat = nodes[node_index]->parent == nullptr
972-
? glm::mat4(1)
973-
: nodes[node_index]->parent->modelGlobalTransform;
974-
975-
nodes[node_index]->modelGlobalTransform =
976-
glm::translate(globaMat, nodes[node_index]->localPosition) *
977-
glm::mat4_cast(nodes[node_index]->localRotation);
970+
glm::quat quat_local_rotation = currentNode->getLocalRotation();
971+
if (ImGui::DragFloat4("Rotation (Quat)", &quat_local_rotation[0])) {
972+
currentNode->setLocalRotation(quat_local_rotation);
978973
}
979974

980-
if (ImGui::DragFloat3("Rotation (Eular)", &nodes[node_index]->localRotation[0])) {
975+
glm::vec3 eular_rotation = currentNode->getRotationEular();
976+
if (ImGui::DragFloat3("Rotation (Eular)", &eular_rotation[0])) {
977+
currentNode->setRotationEular(eular_rotation);
981978
}
982979

983-
if (ImGui::DragFloat3("Scale", &nodes[node_index]->localScale[0])) {
984-
985-
const glm::mat4 globaMat = nodes[node_index]->parent == nullptr
986-
? glm::mat4(1)
987-
: nodes[node_index]->parent->modelGlobalTransform;
988-
989-
nodes[node_index]->modelGlobalTransform =
990-
glm::translate(globaMat, nodes[node_index]->localPosition) *
991-
glm::mat4_cast(nodes[node_index]->localRotation) *
992-
glm::scale(nodes[node_index]->localScale);
980+
glm::vec3 local_scale = currentNode->getLocalScale();
981+
if (ImGui::DragFloat3("Scale", &local_scale[0])) {
982+
currentNode->setLocalScale(local_scale);
993983
}
994984
ImGui::Separator();
995985

0 commit comments

Comments
 (0)