From 8cc6ed1f157c6131952f7830ea28b39888db6b11 Mon Sep 17 00:00:00 2001 From: nigel Date: Sat, 28 May 2022 21:19:16 +0200 Subject: [PATCH] Multiple changes to prepare for the basic render engine. * Rendering 2 cubes * per cube transform panels * Updated TODO.md * Updated README.md --- BarinkEngine/BarinkEngine.cpp | 71 ++-------- BarinkEngine/Include/BarinkEngine.h | 1 + BarinkEngine/Include/Graphics/Renderable.h | 14 +- BarinkEngine/Include/Graphics/Transform.h | 8 -- BarinkEngine/Include/Scene.h | 47 +++++++ BarinkEngine/Scene.cpp | 52 +++++++ BarinkEngine/graphics/ModelImporter.cpp | 2 - BarinkEngine/graphics/Renderable.cpp | 4 +- README.md | 27 +++- SandboxApplication/Sandbox.cpp | 149 +++++++++++++++++---- Screenshots/screen2.png | 3 + TODO.md | 2 +- 12 files changed, 265 insertions(+), 115 deletions(-) delete mode 100644 BarinkEngine/Include/Graphics/Transform.h create mode 100644 BarinkEngine/Include/Scene.h create mode 100644 BarinkEngine/Scene.cpp create mode 100644 Screenshots/screen2.png diff --git a/BarinkEngine/BarinkEngine.cpp b/BarinkEngine/BarinkEngine.cpp index d613cdb..c41e385 100644 --- a/BarinkEngine/BarinkEngine.cpp +++ b/BarinkEngine/BarinkEngine.cpp @@ -1,106 +1,53 @@ #include "BarinkEngine.h" #include -extern void Start(int argc, char* argv[]); +extern void Start(); extern void Update(); +extern void ImmediateGraphicsDraw(); extern void Stop(); using namespace BarinkEngine; -void DrawMyGUI(); -BarinkWindow* MainWindow; int main(int argc, char* argv[]) { // Startup services - MainWindow = new BarinkWindow(800, 600); + BarinkWindow MainWindow = BarinkWindow(800, 600); Renderer renderer = Renderer(); InputManager InputSystem = InputManager(); + InputSystem.attach(&MainWindow); - InputSystem.attach(MainWindow); - - GUIManager GUISystem = GUIManager(MainWindow); - - - + GUIManager GUISystem = GUIManager(&MainWindow); // First call to setup game - Start(argc, argv); - + Start(); // Runtime loop - while (!MainWindow->WindowShouldClose()) { + while (!MainWindow.WindowShouldClose()) { InputSystem.PollEvents(); Update(); - renderer.Render(); - - DrawMyGUI(); + ImmediateGraphicsDraw(); GUISystem.Render(); - - - - MainWindow->SwapBuffers(); + MainWindow.SwapBuffers(); } // Shutdown game - Stop(); // Shutdown Services - delete MainWindow; return 0; } -void WARN(std::string message) { - spdlog::warn(message); -} - - -void DrawMyGUI() { - ImGui::NewFrame(); - - ImGui::Begin("Transform"); - ImGui::Text("Cube"); - /* - ImGui::InputFloat3("Position:", (float*)nullptr); - ImGui::InputFloat3("Rotation:", (float*)nullptr); - ImGui::InputFloat3("Scale:", (float*)nullptr); - - */ - - ImGui::End(); - - ImGui::Begin("Camera"); - - //ImGui::SliderFloat("Zoom:", &NULL, 10, 190); - - ImGui::End(); - - - ImGui::Begin("Scripting!!"); - - //ImGui::InputTextMultiline("Lua Script", nullptr, 255); - //runCode = ImGui::Button("Run"); - - - ImGui::End(); - - - ImGui::ShowDemoWindow(); - -} - - diff --git a/BarinkEngine/Include/BarinkEngine.h b/BarinkEngine/Include/BarinkEngine.h index d377364..7251c27 100644 --- a/BarinkEngine/Include/BarinkEngine.h +++ b/BarinkEngine/Include/BarinkEngine.h @@ -14,6 +14,7 @@ #include "Input/InputManager.h" #include "Graphics/Renderer.h" #include "Graphics/GUI/GUIManager.h" +#include "Scene.h" void WARN(std::string message); diff --git a/BarinkEngine/Include/Graphics/Renderable.h b/BarinkEngine/Include/Graphics/Renderable.h index b7d4fd3..06e9695 100644 --- a/BarinkEngine/Include/Graphics/Renderable.h +++ b/BarinkEngine/Include/Graphics/Renderable.h @@ -1,26 +1,18 @@ #pragma once #include #include "Mesh.h" -#include "Transform.h" #include "Buffer.h" #include "VertexArray.h" +#include "Scene.h" -/* -#include -#include -#include - -*/ - -class Renderable { +class Renderable : public SceneNode { public: Buffer vertexBuffer; Buffer elementBuffer; VertexArray VAO; - Transform transform; ~Renderable(); - static Renderable Load(); + static Renderable* Load(); void Draw(); private: diff --git a/BarinkEngine/Include/Graphics/Transform.h b/BarinkEngine/Include/Graphics/Transform.h deleted file mode 100644 index 2d1b972..0000000 --- a/BarinkEngine/Include/Graphics/Transform.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include - -struct Transform { - glm::vec3 Position; - glm::vec3 Rotation; - glm::vec3 Scale; -}; \ No newline at end of file diff --git a/BarinkEngine/Include/Scene.h b/BarinkEngine/Include/Scene.h new file mode 100644 index 0000000..817c55d --- /dev/null +++ b/BarinkEngine/Include/Scene.h @@ -0,0 +1,47 @@ +#pragma once +#include +#include + +#include "glm/glm.hpp" + +/* +* 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(); + + Scene(std::string SceneName = "Default Scene"); + ~Scene(); + + +private: + SceneNode* root; + +}; + diff --git a/BarinkEngine/Scene.cpp b/BarinkEngine/Scene.cpp new file mode 100644 index 0000000..00c5ace --- /dev/null +++ b/BarinkEngine/Scene.cpp @@ -0,0 +1,52 @@ +#include "Scene.h" + + +SceneNode* SearchInChildren(SceneNode* root, std::string name ) { + + if (root->name == name) + return root; + + SceneNode* found = nullptr; + for (auto child : root->children) { + found = SearchInChildren(child, name); + } + return found; +} + + +SceneNode& Scene::GetSceneNode(std::string name) +{ + return *SearchInChildren(root, name); +} + + + +SceneNode& 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) +{ + children.push_back(&node); +} diff --git a/BarinkEngine/graphics/ModelImporter.cpp b/BarinkEngine/graphics/ModelImporter.cpp index d954a01..c2e68d8 100644 --- a/BarinkEngine/graphics/ModelImporter.cpp +++ b/BarinkEngine/graphics/ModelImporter.cpp @@ -94,8 +94,6 @@ BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene vertices.push_back(vector); } - //spdlog::info("{} == {}", mesh->mNumVertices, vertices.size()); - // Process Indices for (unsigned int i = 0; i < mesh->mNumFaces; i++) { aiFace face = mesh->mFaces[i]; diff --git a/BarinkEngine/graphics/Renderable.cpp b/BarinkEngine/graphics/Renderable.cpp index e186afe..7ccf110 100644 --- a/BarinkEngine/graphics/Renderable.cpp +++ b/BarinkEngine/graphics/Renderable.cpp @@ -3,9 +3,9 @@ -Renderable Renderable::Load() +Renderable* Renderable::Load() { - return Renderable(); + return new Renderable(); } Renderable::Renderable() diff --git a/README.md b/README.md index 7e591e5..25790b7 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,34 @@ ## Screenshots - + + ## Planning see [TODO](docs/TODO.md) +_NOTE:_ + +The planning is moving away from markdown in favor +of gitea Projects. New planning will be visible on [this page](https://git.barink.dev/Nigel/MyGameEngine/projects). +## Requirements +#### Software +* Premake +* Git +* C++ Compiler + ## Windows development workflow -__added soon__ +User premake to generate project files for the approperiate build method. +On Windows I assume you'll build with visual studio + +```bash +User:~$ premake vs2022 +``` ## Linux development workflow -__added soon__ \ No newline at end of file +Use premake to generate project files for the approperiate build method. +On Linux I assume you'll build with something like make. + +```bash +User@Machine:~$ premake gmake2 +``` \ No newline at end of file diff --git a/SandboxApplication/Sandbox.cpp b/SandboxApplication/Sandbox.cpp index 826f0be..90ab938 100644 --- a/SandboxApplication/Sandbox.cpp +++ b/SandboxApplication/Sandbox.cpp @@ -1,53 +1,148 @@ #include "BarinkEngine.h" +#include "imgui.h" using namespace BarinkEngine; Camera* cam; Renderable* Cube; -Shader* shader; +Renderable* Cube2; + +char* code = new char[254]; -void Start(int argc, char* argv[]) { - std::cout << "Hello start!" << std::endl; - +void PrintSceneTree(SceneNode& node, int depth ){ + // Indent name based on depth + std::cout << " "; + for (int i = 0; i < depth; i++) { + std::cout << "-"; + } + + std::cout << " " << node.name << std::endl; - cam = new Camera (glm::vec3(0.0f, 1.5f, -10.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f); - Cube = &Renderable::Load(); + depth++; + for (auto child : node.children) { + PrintSceneTree(*child, depth); + } +} - spdlog::info("==== Load Shader(s) ===="); - std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs"; - std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs"; - shader = new Shader(vertexShaderSource, fragmentShaderSource); +void Start() { + + 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); + + Cube = Renderable::Load(); + Cube2 = Renderable::Load(); + Cube->addChild(*Cube2); + + cam = new Camera(glm::vec3(0.0f, 1.5f, -10.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f); + + memset(code, '\0', 254); } + + +void transformWindow(Transform& transform, std::string PanelName) { + ImGui::Begin(PanelName.c_str()); + ImGui::InputFloat3("Position:", (float*)&transform.Position[0]); + ImGui::InputFloat3("Rotation:", (float*)&transform.Rotation[0]); + ImGui::InputFloat3("Scale:", (float*)&transform.Scale[0]); + ImGui::End(); + +} + +void ImmediateGraphicsDraw() { + ImGui::NewFrame(); + + + ImGui::Begin("Camera"); + + ImGui::SliderFloat("Zoom:", &cam->Zoom, 10, 190); + + ImGui::End(); + + + transformWindow(Cube->transform, "Transform (Cube)"); + + transformWindow(Cube2->transform, "Transform (Cube2)"); + + + ImGui::Begin("Scripting!!"); + + ImGui::InputTextMultiline("Lua Script", code, 255); + bool runCode = ImGui::Button("Run"); + + + ImGui::End(); + + + ImGui::ShowDemoWindow(); + +} + + + +glm::mat4 CalculateModelMat(Transform& transform) { + + glm::mat4 tran = glm::translate(glm::mat4(), transform.Position); + glm::mat4 scale = glm::scale(glm::mat4(), transform.Scale); + glm::mat4 rot = + glm::rotate(glm::mat4(), glm::radians(transform.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) * + glm::rotate(glm::mat4(), glm::radians(transform.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) * + glm::rotate(glm::mat4(), glm::radians(transform.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); + + + return tran * rot * scale; +} + + void Update() { - glm::mat4 tran = glm::translate(glm::mat4(), Cube->transform.Position); - glm::mat4 scale = glm::scale(glm::mat4(), Cube->transform.Scale); - glm::mat4 rot = - glm::rotate(glm::mat4(), glm::radians(Cube->transform.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) * - glm::rotate(glm::mat4(), glm::radians(Cube->transform.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) * - glm::rotate(glm::mat4(), glm::radians(Cube->transform.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); + + std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs"; + std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs"; + Shader shader = Shader(vertexShaderSource, fragmentShaderSource); - - glm::mat4 model = tran * rot * scale; + + glm::mat4 projection = glm::perspective(glm::radians(cam->Zoom), (800.0f / 600.0f), 0.001f, 100.0f); - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - shader->Use(); - shader->setUniformMat4("P", projection); - shader->setUniformMat4("M", model); - shader->setUniformMat4("V", cam->GetViewMatrix()); + shader.Use(); + shader.setUniformMat4("P", projection); + shader.setUniformMat4("M", CalculateModelMat(Cube->transform)); + shader.setUniformMat4("V", cam->GetViewMatrix()); + shader.setUniformVec3("MatColour", glm::vec3(1.0f, 0.0f, 0.0f)); Cube->Draw(); + shader.setUniformMat4("M", CalculateModelMat(Cube2->transform)); + shader.setUniformVec3("MatColour", glm::vec3(0.0f, 1.0f, 0.0f)); + + Cube2->Draw(); + } @@ -56,7 +151,9 @@ void Stop() { Cube->VAO.Delete(); Cube->elementBuffer.Delete(); + Cube2->VAO.Delete(); + Cube2->elementBuffer.Delete(); + + delete Cube2; delete Cube; - delete cam; - delete shader; } \ No newline at end of file diff --git a/Screenshots/screen2.png b/Screenshots/screen2.png new file mode 100644 index 0000000..62b7d5f --- /dev/null +++ b/Screenshots/screen2.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:daa76d1f8bb71e45c54e73145f4f313c31d55c60a7cc3e53653722f3a5dc239a +size 369689 diff --git a/TODO.md b/TODO.md index 1ff52d4..90ee5ee 100644 --- a/TODO.md +++ b/TODO.md @@ -13,7 +13,7 @@ Work on basic logging \ Input handling \ More shader work \ - Load FBX model files \ + Load FBX model files \ Basic Physics \ Running LUA \ Lua Scripting \