From b7e34654061df85ebe257afeb8dd44870a6c4de5 Mon Sep 17 00:00:00 2001 From: nigel Date: Fri, 8 Jul 2022 21:35:14 +0200 Subject: [PATCH] Editing the modelimporter to allow to create scene graphs --- .vscode/settings.json | 6 - .../Include/AssetManager/ModelImporter.h | 21 ++-- BarinkEngine/Include/Graphics/Buffer.h | 2 +- BarinkEngine/Include/Graphics/Mesh.h | 10 +- BarinkEngine/Include/Graphics/RenderSurface.h | 4 +- BarinkEngine/Include/Graphics/Renderable.h | 40 +++---- BarinkEngine/Include/Graphics/Transform.h | 11 ++ BarinkEngine/Include/Scene.h | 34 +----- BarinkEngine/Include/Scene/Node.h | 21 ++++ BarinkEngine/Include/Scene/SceneBuilder.h | 14 +++ BarinkEngine/Include/Scene/SceneManager.h | 15 +++ BarinkEngine/Include/Scene/SceneNodeTypes.h | 21 ++++ BarinkEngine/Scene.cpp | 62 ++++++----- BarinkEngine/graphics/Buffer.cpp | 12 +- BarinkEngine/graphics/ModelImporter.cpp | 105 +++++------------- BarinkEngine/graphics/Renderable.cpp | 83 ++++++-------- SandboxApplication/Sandbox.cpp | 32 +----- SandboxApplication/Util.cpp | 2 +- SandboxApplication/Util.h | 2 +- 19 files changed, 241 insertions(+), 256 deletions(-) delete mode 100644 .vscode/settings.json create mode 100644 BarinkEngine/Include/Graphics/Transform.h create mode 100644 BarinkEngine/Include/Scene/Node.h create mode 100644 BarinkEngine/Include/Scene/SceneBuilder.h create mode 100644 BarinkEngine/Include/Scene/SceneManager.h create mode 100644 BarinkEngine/Include/Scene/SceneNodeTypes.h diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index d23f8a5..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "cmake.configureOnOpen": true, - "files.associations": { - "iosfwd": "cpp" - } -} \ No newline at end of file diff --git a/BarinkEngine/Include/AssetManager/ModelImporter.h b/BarinkEngine/Include/AssetManager/ModelImporter.h index be98a83..a28bdae 100644 --- a/BarinkEngine/Include/AssetManager/ModelImporter.h +++ b/BarinkEngine/Include/AssetManager/ModelImporter.h @@ -10,17 +10,20 @@ #include #include +void ProcessVertices(aiMesh* mesh, std::vector& out_vertices); +void ProcessIndices(aiMesh* mesh, std::vector& out_indices); + + class ModelImporter { -private: - void ImportFBX(std::string path); - void ImportBlend(std::string path); - void ImportGLTF(std::string path); - void ImportOBJ(std::string path); - static BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene); - static std::vector ModelImporter::processNode(aiNode* node, const aiScene* scene); public: - void Import(std::string path); + std::vector Import(const std::string path); + +private: + + static BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene); + static std::vector ModelImporter::processNode(aiNode* node, const aiScene* scene); + + - static std::vector Test(); }; \ No newline at end of file diff --git a/BarinkEngine/Include/Graphics/Buffer.h b/BarinkEngine/Include/Graphics/Buffer.h index 2c5c5dc..196693f 100644 --- a/BarinkEngine/Include/Graphics/Buffer.h +++ b/BarinkEngine/Include/Graphics/Buffer.h @@ -1,6 +1,6 @@ #pragma once #include -class Buffer { +class GpuBuffer { private: unsigned int id; public: diff --git a/BarinkEngine/Include/Graphics/Mesh.h b/BarinkEngine/Include/Graphics/Mesh.h index d509ace..98d0b02 100644 --- a/BarinkEngine/Include/Graphics/Mesh.h +++ b/BarinkEngine/Include/Graphics/Mesh.h @@ -1,10 +1,11 @@ #pragma once #include #include +#include "VertexArray.h" +#include "Buffer.h" namespace BarinkEngine{ - struct Vertex { glm::vec3 vertices; glm::vec2 uv; @@ -13,8 +14,13 @@ namespace BarinkEngine{ class Mesh { public: std::vector vertices; - std::vector elements; + std::vector elements; + private: + GpuBuffer vertexBuffer; + GpuBuffer elementBuffer; + VertexArray VAO; + unsigned int UV_id; }; } \ No newline at end of file diff --git a/BarinkEngine/Include/Graphics/RenderSurface.h b/BarinkEngine/Include/Graphics/RenderSurface.h index 23cfbf9..47c6cb7 100644 --- a/BarinkEngine/Include/Graphics/RenderSurface.h +++ b/BarinkEngine/Include/Graphics/RenderSurface.h @@ -20,8 +20,8 @@ private: std::vector indices; - Buffer vertexBuffer; - Buffer elementBuffer; + GpuBuffer vertexBuffer; + GpuBuffer elementBuffer; VertexArray VAO; diff --git a/BarinkEngine/Include/Graphics/Renderable.h b/BarinkEngine/Include/Graphics/Renderable.h index e7c2ee9..3addd2c 100644 --- a/BarinkEngine/Include/Graphics/Renderable.h +++ b/BarinkEngine/Include/Graphics/Renderable.h @@ -1,37 +1,23 @@ #pragma once #include #include "Mesh.h" -#include "Buffer.h" + #include "Material.h" #include "Texture.h" -#include "VertexArray.h" + #include "Scene.h" +namespace BarinkEngine { -class Renderable : public SceneNode { -public: - /* - * NOTE: Should combine into a Mesh!! - */ - Buffer vertexBuffer; - Buffer elementBuffer; - //Buffer uv; - VertexArray VAO; + class Renderable { + public: + Mesh mesh; + Material* material; + Texture* texture; + Shader* shader; - GLuint UV_id; - Material* material; - Texture* texture; - - - Shader* shader; - - ~Renderable(); - - static Renderable* Load(); - void Draw(); - -private: - std::vector meshes; - Renderable(); -}; \ No newline at end of file + Renderable(); + ~Renderable(); + }; +} \ No newline at end of file diff --git a/BarinkEngine/Include/Graphics/Transform.h b/BarinkEngine/Include/Graphics/Transform.h new file mode 100644 index 0000000..7065ba6 --- /dev/null +++ b/BarinkEngine/Include/Graphics/Transform.h @@ -0,0 +1,11 @@ +#pragma once +#include +struct Transform { + glm::vec3 Position; + glm::vec3 Rotation; + glm::vec3 Scale; + + glm::mat4 ModelMatrix; +}; + + diff --git a/BarinkEngine/Include/Scene.h b/BarinkEngine/Include/Scene.h index 817c55d..e8f6818 100644 --- a/BarinkEngine/Include/Scene.h +++ b/BarinkEngine/Include/Scene.h @@ -2,46 +2,22 @@ #include #include -#include "glm/glm.hpp" - +#include "Graphics/Transform.h" +#include "Scene/Node.h" /* * Scene should be a description of a game world */ - -struct Transform { - glm::vec3 Position; - glm::vec3 Rotation; - glm::vec3 Scale; - - glm::mat4 ModelMatrix; -}; - - -class SceneNode { -public: - std::string name; - Transform transform; - SceneNode* parent; - std::vector children; - - - void addChild(SceneNode& node); - -}; - - class Scene { public: - SceneNode& GetSceneNode(std::string); - SceneNode& GetRoot(); + Node& GetSceneNode(std::string); + Node& GetRoot(); Scene(std::string SceneName = "Default Scene"); ~Scene(); - private: - SceneNode* root; + Node* root; }; diff --git a/BarinkEngine/Include/Scene/Node.h b/BarinkEngine/Include/Scene/Node.h new file mode 100644 index 0000000..1eda35b --- /dev/null +++ b/BarinkEngine/Include/Scene/Node.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include +#include "Graphics/Transform.h" + +class Node { + public: + std::string& name; + Node* parent; + std::vector children; + + void addChild(Node& node); + +}; + + + +class Group : Node { + Transform& transform; + +}; diff --git a/BarinkEngine/Include/Scene/SceneBuilder.h b/BarinkEngine/Include/Scene/SceneBuilder.h new file mode 100644 index 0000000..ab459de --- /dev/null +++ b/BarinkEngine/Include/Scene/SceneBuilder.h @@ -0,0 +1,14 @@ +#pragma once +#include "Graphics/Renderable.h" +#include "Scene/SceneNodeTypes.h" + +#include +/* +* Define a helper class to more easily build a proper scene +*/ +static class SceneBuilder { + + static Group* AddGroup(std::string name); + static SceneObject* AddVisual(std::string name, Renderable& object, glm::vec3 position ); + +}; \ No newline at end of file diff --git a/BarinkEngine/Include/Scene/SceneManager.h b/BarinkEngine/Include/Scene/SceneManager.h new file mode 100644 index 0000000..23520e6 --- /dev/null +++ b/BarinkEngine/Include/Scene/SceneManager.h @@ -0,0 +1,15 @@ +#pragma once +#include +#include +#include "Scene.h" +static class SceneManager { + +public: + static Scene* CreateScene(); + static Scene& GetScene(std::string name); + + +private: + static std::map + +}; \ No newline at end of file diff --git a/BarinkEngine/Include/Scene/SceneNodeTypes.h b/BarinkEngine/Include/Scene/SceneNodeTypes.h new file mode 100644 index 0000000..6e9a3f1 --- /dev/null +++ b/BarinkEngine/Include/Scene/SceneNodeTypes.h @@ -0,0 +1,21 @@ +#pragma once +#include "Graphics/Camera.h" +#include "Graphics/Renderable.h" +#include "Scene/Node.h" + +class SceneCamera : Group +{ + Camera& camera; + + +}; + + +class SceneObject : Group +{ + Renderable& renderable; + + + +}; + diff --git a/BarinkEngine/Scene.cpp b/BarinkEngine/Scene.cpp index 00c5ace..f197af1 100644 --- a/BarinkEngine/Scene.cpp +++ b/BarinkEngine/Scene.cpp @@ -1,12 +1,28 @@ #include "Scene.h" +void DeleteSubGraph(Node* tree); + +Scene::Scene(std::string sceneName) +{ + // Create a root node + root = new Node(); + root->name = sceneName; + +} + +Scene::~Scene() +{ + // Delete all nodes in the graph. + DeleteSubGraph(root); + +} -SceneNode* SearchInChildren(SceneNode* root, std::string name ) { +Node* SearchInChildren(Node* root, std::string name ) { if (root->name == name) return root; - SceneNode* found = nullptr; + Node* found = nullptr; for (auto child : root->children) { found = SearchInChildren(child, name); } @@ -14,39 +30,35 @@ SceneNode* SearchInChildren(SceneNode* root, std::string name ) { } -SceneNode& Scene::GetSceneNode(std::string name) +Node& Scene::GetSceneNode(std::string name) { return *SearchInChildren(root, name); } -SceneNode& Scene::GetRoot() +Node& Scene::GetRoot() { return *root; } -Scene::Scene(std::string sceneName) -{ - // Create a root node - root = new SceneNode(); - root->name = sceneName; - root->transform = Transform(); - - root->transform.Position = glm::vec3(0); - root->transform.Rotation = glm::vec3(0); - root->transform.Scale = glm::vec3(0); - - root->transform.ModelMatrix = glm::mat4(0); - -} - -Scene::~Scene() -{ - // Destruct scene! -} - -void SceneNode::addChild(SceneNode& node) +void Node::addChild(Node& node) { children.push_back(&node); } + + +void DeleteSubGraph(Node* tree) +{ + if (tree->children.size() == 0) { + delete tree; + return; + } + + for (auto child : tree->children) { + if (child->children.size() > 0) { + DeleteSubGraph(child); + } + } + +} \ No newline at end of file diff --git a/BarinkEngine/graphics/Buffer.cpp b/BarinkEngine/graphics/Buffer.cpp index 2a89581..0f0a23e 100644 --- a/BarinkEngine/graphics/Buffer.cpp +++ b/BarinkEngine/graphics/Buffer.cpp @@ -1,15 +1,15 @@ #include "Graphics/Buffer.h" -int Buffer::getBufferID() { +int GpuBuffer::getBufferID() { return id; } -void Buffer::createBuffer() { +void GpuBuffer::createBuffer() { glGenBuffers(1, (GLuint*) &id); } -void Buffer::setBufferData(void* data, size_t dataSize, bool elementBuffer = false ) { +void GpuBuffer::setBufferData(void* data, size_t dataSize, bool elementBuffer = false ) { if (elementBuffer) { glBufferData(GL_ELEMENT_ARRAY_BUFFER, dataSize, data, GL_STATIC_DRAW); @@ -21,7 +21,7 @@ void Buffer::setBufferData(void* data, size_t dataSize, bool elementBuffer = fal } -void Buffer::Bind(bool elementBuffer = false ) { +void GpuBuffer::Bind(bool elementBuffer = false ) { if (elementBuffer) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); @@ -32,7 +32,7 @@ void Buffer::Bind(bool elementBuffer = false ) { } } -void Buffer::Unbind(bool elementBuffer = false) { +void GpuBuffer::Unbind(bool elementBuffer = false) { if (elementBuffer) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } @@ -42,6 +42,6 @@ void Buffer::Unbind(bool elementBuffer = false) { } } -void Buffer::Delete() { +void GpuBuffer::Delete() { glDeleteBuffers(1, (GLuint*) &id); } \ No newline at end of file diff --git a/BarinkEngine/graphics/ModelImporter.cpp b/BarinkEngine/graphics/ModelImporter.cpp index e66528e..22a03a1 100644 --- a/BarinkEngine/graphics/ModelImporter.cpp +++ b/BarinkEngine/graphics/ModelImporter.cpp @@ -1,70 +1,21 @@ #include "AssetManager/ModelImporter.h" -void ModelImporter::ImportFBX(std::string path) +std::vector ModelImporter::Import(const std::string path) { - //spdlog::warn("ImportFBX not implemented!"); -} - -void ModelImporter::ImportBlend(std::string path) -{ - //spdlog::warn("ImportBlend not implemented!"); -} - -void ModelImporter::ImportGLTF(std::string path) -{ - //spdlog::warn("ImportGLTF not implemented!"); -} - -void ModelImporter::ImportOBJ(std::string path) -{ - //spdlog::warn("ImportOBJ not implemented!"); -} - -void ModelImporter::Import(std::string path) -{ - //spdlog::warn("Import not implemented!"); -} - -std::vector ModelImporter::Test() { - - /* - spdlog::info("====== Tiny GLTF ======"); - tinygltf::Model loadedModel; - tinygltf::TinyGLTF loader; - std::string error; - std::string warn; - bool ret = loader.LoadASCIIFromFile(&loadedModel, &error, &warn, "./Build/SandboxApplication/Debug/sponza.gltf"); - - if (!warn.empty()) - spdlog::warn("TinyGLTF Warning: {}", warn); - if (!error.empty()) - spdlog::error("TinyGLTF Error: {}", error); - if (!ret) { - spdlog::error("TinyGLTF Error: Failed to parse glTF"); - exit(-1); - } - - spdlog::info("Meshes in model: {}", loadedModel.meshes.size()); - spdlog::info("Primitives in mesh: {}", loadedModel.meshes[0].primitives.size()); - - */ - - - //spdlog::info("======= Assimp ======"); - Assimp::Importer importer; - const aiScene* scene = importer.ReadFile("build/SandboxApplication/Debug/Models/Cube.obj", aiProcess_Triangulate | aiProcess_FlipUVs); - + const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs); + aiNode* currentNode = scene->mRootNode; - return processNode(currentNode, scene); - - + return processNode(currentNode, scene); } -std::vector ModelImporter::processNode(aiNode* node, const aiScene* scene) { +std::vector ModelImporter::processNode(aiNode* node, const aiScene* scene) +{ + std::vector meshes; + for (unsigned int i = 0; i < node->mNumMeshes; i++) { aiMesh* mesh = scene->mMeshes[node->mMeshes[i]]; meshes.push_back(processMesh(mesh, scene)); @@ -85,6 +36,20 @@ BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene std::vector indices; std::vector vertices; + ProcessVertices(mesh, vertices); + + ProcessIndices(mesh, indices); + + BarinkEngine::Mesh result; + result.vertices = vertices; + result.elements = indices; + + + return result; + +} + +void ProcessVertices(aiMesh* mesh, std::vector& out_vertices) { // Process vertices for (unsigned int i = 0; i < mesh->mNumVertices; i++) { BarinkEngine::Vertex v{}; @@ -92,9 +57,9 @@ BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene vector.x = mesh->mVertices[i].x; vector.y = mesh->mVertices[i].y; vector.z = mesh->mVertices[i].z; - + v.vertices = vector; - + if (mesh->mTextureCoords[0]) { glm::vec2 texCoord; @@ -105,29 +70,19 @@ BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene v.uv = texCoord; } - - vertices.push_back(v); - } + out_vertices.push_back(v); + } +} + +void ProcessIndices(aiMesh* mesh, std::vector& out_indices) { // Process Indices for (unsigned int i = 0; i < mesh->mNumFaces; i++) { aiFace face = mesh->mFaces[i]; if (face.mNumIndices < 3) continue; for (unsigned int j = 0; j < face.mNumIndices; j++) { - indices.push_back(face.mIndices[j]); + out_indices.push_back(face.mIndices[j]); } } - - - - BarinkEngine::Mesh result; - - - result.vertices = vertices; - result.elements = indices; - - - return result; - } \ No newline at end of file diff --git a/BarinkEngine/graphics/Renderable.cpp b/BarinkEngine/graphics/Renderable.cpp index 5109edf..990136e 100644 --- a/BarinkEngine/graphics/Renderable.cpp +++ b/BarinkEngine/graphics/Renderable.cpp @@ -3,60 +3,51 @@ #include "PerfCounter.h" -Renderable* Renderable::Load() +BarinkEngine::Renderable::Renderable() { - return new Renderable(); -} - -Renderable::Renderable() -{ - meshes = ModelImporter::Test(); - - transform.Scale = glm::vec3(1.0f); - transform.Rotation = glm::vec3(0.0f, 0.0f, 0.0f); - transform.Position = glm::vec3(0.0f, 0.0f, 0.0f); - - VAO.Create(); - VAO.Bind(); - - - vertexBuffer.createBuffer(); - vertexBuffer.Bind(false); - vertexBuffer.setBufferData(&meshes[0].vertices[0], meshes[0].vertices.size() * sizeof(BarinkEngine::Vertex), false); - - elementBuffer.createBuffer(); - elementBuffer.Bind(true); - elementBuffer.setBufferData(&meshes[0].elements[0], meshes[0].elements.size() * sizeof(unsigned int), true); - - VAO.AttachAttribute(0, 3, sizeof(BarinkEngine::Vertex)); - - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(BarinkEngine::Vertex),(void* )offsetof(BarinkEngine::Vertex, vertices)); - glEnableVertexAttribArray(1); + /* - vertexBuffer.Unbind(false); + VAO.Create(); + VAO.Bind(); + + + vertexBuffer.createBuffer(); + vertexBuffer.Bind(false); + vertexBuffer.setBufferData(&meshes[0].vertices[0], meshes[0].vertices.size() * sizeof(BarinkEngine::Vertex), false); + + elementBuffer.createBuffer(); + elementBuffer.Bind(true); + elementBuffer.setBufferData(&meshes[0].elements[0], meshes[0].elements.size() * sizeof(unsigned int), true); + + VAO.AttachAttribute(0, 3, sizeof(BarinkEngine::Vertex)); + + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(BarinkEngine::Vertex),(void* )offsetof(BarinkEngine::Vertex, vertices)); + glEnableVertexAttribArray(1); + + //vertexBuffer.Unbind(false); - VAO.Unbind(); - + VAO.Unbind(); + */ } -Renderable::~Renderable() +BarinkEngine::Renderable::~Renderable() { - glDeleteBuffers(1, &UV_id); + // glDeleteBuffers(1, &UV_id); } -void Renderable::Draw() -{ - VAO.Bind(); - elementBuffer.Bind(true); +// Draw call Example !! +/* + VAO.Bind(); + elementBuffer.Bind(true); - glActiveTexture(GL_TEXTURE0); - glUniform1i(glGetUniformLocation(shader->id, "Texture"), GL_TEXTURE0); - texture->Bind(); + glActiveTexture(GL_TEXTURE0); + glUniform1i(glGetUniformLocation(shader->id, "Texture"), GL_TEXTURE0); + texture->Bind(); - ES->verts = meshes[0].vertices.size(); - ES->DC++; - glDrawElements(GL_TRIANGLES, static_cast(meshes[0].elements.size()), GL_UNSIGNED_INT, NULL); - VAO.Unbind(); - -} + ES->verts = meshes[0].vertices.size(); + ES->DC++; + glDrawElements(GL_TRIANGLES, static_cast(meshes[0].elements.size()), GL_UNSIGNED_INT, NULL); + VAO.Unbind(); + */ + diff --git a/SandboxApplication/Sandbox.cpp b/SandboxApplication/Sandbox.cpp index b52438f..a92fa6a 100644 --- a/SandboxApplication/Sandbox.cpp +++ b/SandboxApplication/Sandbox.cpp @@ -11,11 +11,11 @@ Camera* cam; Shader* shader; -Renderable* Cube; +BarinkEngine::Renderable* Cube; Material* matCube; Texture* textureCube; -Renderable* Cube2; +BarinkEngine::Renderable* Cube2; Material* matCube2; char* code = new char[254]; @@ -26,35 +26,15 @@ const std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs + /* * Runs once at startup * - USe to initialize the game/sandbox/demo */ void Start() { - /* - Building a very basic scene graph - */ - SceneNode MyCube = SceneNode(); - MyCube.name = "MyCube"; - - SceneNode MyBaby = SceneNode(); - MyBaby.name = "Baby"; - - SceneNode MySecondCube = SceneNode(); - MySecondCube.name = "MySecondCube"; - - - MyCube.addChild(MyBaby); - - - Scene scene = Scene("My awesome Game Scene"); - scene.GetRoot().addChild(MyCube); - scene.GetRoot().addChild(MySecondCube); - - - // Walk scene graph - PrintSceneTree(scene.GetRoot(),0); +// Walk scene graph +//PrintSceneTree(scene.GetRoot(),0); shader = new Shader(vertexShaderSource, fragmentShaderSource); @@ -69,7 +49,7 @@ void Start() { /* * load meshes */ - Cube = Renderable::Load(); + Cube = Cube2 = Renderable::Load(); Cube->addChild(*Cube2); diff --git a/SandboxApplication/Util.cpp b/SandboxApplication/Util.cpp index aa9560a..abb8e9d 100644 --- a/SandboxApplication/Util.cpp +++ b/SandboxApplication/Util.cpp @@ -1,6 +1,6 @@ #include "Util.h" -void PrintSceneTree(SceneNode& node, int depth) { +void PrintSceneTree(Node& node, int depth) { // Indent name based on depth std::cout << " "; for (int i = 0; i < depth; i++) { diff --git a/SandboxApplication/Util.h b/SandboxApplication/Util.h index 3118ad0..c831684 100644 --- a/SandboxApplication/Util.h +++ b/SandboxApplication/Util.h @@ -1,6 +1,6 @@ #pragma once #include "BarinkEngine.h" -void PrintSceneTree(SceneNode& node, int depth); +void PrintSceneTree(Node& node, int depth); glm::mat4 CalculateModelMat(Transform& transform); \ No newline at end of file