Working on semi proper API to build a scene

This commit is contained in:
Nigel Barink 2022-07-09 21:22:50 +02:00
parent b7e3465406
commit f8b390923e
9 changed files with 116 additions and 55 deletions

View File

@ -1,7 +1,8 @@
#pragma once
#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define TINYGLTF_IMPLEMENTATION
#define TINYGLTF_NO_EXTERNAL_IMAGE
#include "Graphics/Mesh.h"
@ -9,6 +10,7 @@
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <string>
#include "Scene/SceneNodeTypes.h"
void ProcessVertices(aiMesh* mesh, std::vector<BarinkEngine::Vertex>& out_vertices);
void ProcessIndices(aiMesh* mesh, std::vector<unsigned int>& out_indices);
@ -17,7 +19,9 @@ void ProcessIndices(aiMesh* mesh, std::vector<unsigned int>& out_indices);
class ModelImporter {
public:
std::vector<BarinkEngine::Mesh> Import(const std::string path);
SceneObject* Import(const std::string path);
private:

View File

@ -5,6 +5,7 @@
class Node {
public:
Node(std::string& name);
std::string& name;
Node* parent;
std::vector<Node*> children;
@ -15,7 +16,9 @@ class Node {
class Group : Node {
class Group : public Node {
public:
Group(std::string& name);
Transform& transform;
};

View File

@ -5,11 +5,14 @@
static class SceneManager {
public:
static Scene* CreateScene();
static Scene& GetScene(std::string name);
static Scene* CreateScene(const std::string& name );
static Scene& GetScene(const std::string& name);
static void LoadScene(Scene& scene);
private:
static std::map<std::string , Scene*>
static Scene* CurrentScene;
static std::map<std::string, Scene*> Scenes;
};

View File

@ -3,18 +3,19 @@
#include "Graphics/Renderable.h"
#include "Scene/Node.h"
class SceneCamera : Group
class SceneCamera : public Group
{
public:
Camera& camera;
SceneCamera();
};
class SceneObject : Group
class SceneObject : public Group
{
public:
Renderable& renderable;
SceneObject();
};

View File

@ -1,10 +1,11 @@
#include "Scene.h"
#include "Scene/Node.h"
void DeleteSubGraph(Node* tree);
Scene::Scene(std::string sceneName)
{
// Create a root node
root = new Node();
root = new Group(sceneName);
root->name = sceneName;
}
@ -42,6 +43,8 @@ Node& Scene::GetRoot()
return *root;
}
void Node::addChild(Node& node)
{
children.push_back(&node);

View File

@ -0,0 +1,7 @@
#include "Scene/Node.h"
Node::Node(std::string& name)
: name(name), parent(nullptr), children(std::vector<Node*>()) {}
Group::Group(std::string& name )
: Node(name), transform(Transform()) {}

View File

@ -0,0 +1,18 @@
#include "Scene/SceneManager.h"
Scene* SceneManager::CreateScene(const std::string& name)
{
SceneManager::Scenes[name] = new Scene(name);
return &SceneManager::GetScene(name);
}
Scene& SceneManager::GetScene(const std::string& name)
{
return *SceneManager::Scenes[name];
}
void SceneManager::LoadScene( Scene& scene)
{
CurrentScene = &scene;
}

View File

@ -1,14 +1,20 @@
#include "AssetManager/ModelImporter.h"
std::vector<BarinkEngine::Mesh> ModelImporter::Import(const std::string path)
SceneObject* ModelImporter::Import(const std::string path)
{
SceneObject* root = new SceneObject();
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
aiNode* currentNode = scene->mRootNode;
return processNode(currentNode, scene);
std::vector<BarinkEngine::Mesh> meshes = processNode(currentNode, scene);
return root;
}
std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene)

View File

@ -1,4 +1,8 @@
#include "BarinkEngine.h"
#include "Scene\SceneManager.h"
#include "Scene\SceneNodeTypes.h"
#include "AssetManager/ModelImporter.h"
#include "imgui.h"
#include "GUI.h"
#include "Util.h"
@ -11,12 +15,12 @@ Camera* cam;
Shader* shader;
BarinkEngine::Renderable* Cube;
Material* matCube;
Texture* textureCube;
//BarinkEngine::Renderable* Cube;
//Material* matCube;
//Texture* textureCube;
BarinkEngine::Renderable* Cube2;
Material* matCube2;
//BarinkEngine::Renderable* Cube2;
//Material* matCube2;
char* code = new char[254];
@ -25,6 +29,8 @@ const std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs";
const std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs";
ModelImporter* MI = new ModelImporter();
/*
@ -33,35 +39,47 @@ const std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs
*/
void Start() {
// Walk scene graph
//PrintSceneTree(scene.GetRoot(),0);
// Create a level and load it as the current level
auto Level1 = SceneManager::CreateScene("TestLevel");
SceneManager::LoadScene(*Level1);
// Create a cube node
Level1->GetRoot().addChild(*(MI->Import("build/SandboxApplication/Debug/Models")));
// Build a basic test scene
// NOTE: This will later be done through an editor
// Walk scene graph
PrintSceneTree(Level1->GetRoot(),0);
shader = new Shader(vertexShaderSource, fragmentShaderSource);
textureCube = new Texture("build/SandboxApplication/Debug/Textures/wall.jpg");
//textureCube = new Texture("build/SandboxApplication/Debug/Textures/wall.jpg");
matCube = new Material(*shader);
matCube->Color = glm::vec3(1.0, 0.0, 0.0);
//matCube = new Material(*shader);
//matCube->Color = glm::vec3(1.0, 0.0, 0.0);
matCube2 = new Material(*shader);
matCube2->Color = glm::vec3(0.0, 1.0f, 0.0);
//matCube2 = new Material(*shader);
//matCube2->Color = glm::vec3(0.0, 1.0f, 0.0);
/*
* load meshes
*/
Cube =
Cube2 = Renderable::Load();
Cube->addChild(*Cube2);
//Cube =
//Cube2 = Renderable::Load();
//Cube->addChild(*Cube2);
Cube->shader = shader;
Cube2->shader = shader;
//Cube->shader = shader;
//Cube2->shader = shader;
Cube->texture = textureCube;
Cube2->texture = textureCube;
//Cube->texture = textureCube;
//Cube2->texture = textureCube;
Cube2->transform.Position = glm::vec3(-9.0f, 0.0f, 0.0f);
//Cube2->transform.Position = glm::vec3(-9.0f, 0.0f, 0.0f);
Cube->transform.Position = glm::vec3(-8.0f, 0.0f, -2.0f);
//Cube->transform.Position = glm::vec3(-8.0f, 0.0f, -2.0f);
cam = new Camera(glm::vec3(0.0f, 1.5f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
@ -90,12 +108,12 @@ void ImmediateGraphicsDraw() {
CameraTool(cam);
ScriptingTool(code);
transformWindow(Cube->transform, "Transform (Cube)");
//transformWindow(Cube->transform, "Transform (Cube)");
transformWindow(Cube2->transform, "Transform (Cube2)");
//transformWindow(Cube2->transform, "Transform (Cube2)");
materialWindow(*matCube, "Material Cube");
materialWindow(*matCube2, "Material Cube2");
//materialWindow(*matCube, "Material Cube");
//materialWindow(*matCube2, "Material Cube2");
@ -107,8 +125,6 @@ void ImmediateGraphicsDraw() {
*/
void Update()
{
/*
* NOTE: this needs to move to the renderer
* Render code should not appear in the sandbox file
@ -119,16 +135,16 @@ void Update()
shader->Use();
shader->setUniformMat4("P", projection);
shader->setUniformMat4("M", CalculateModelMat(Cube->transform));
//shader->setUniformMat4("M", CalculateModelMat(Cube->transform));
shader->setUniformMat4("V", cam->GetViewMatrix());
matCube->Apply();
//matCube->Apply();
Cube->Draw();
//Cube->Draw();
shader->setUniformMat4("M", CalculateModelMat(Cube2->transform));
matCube2->Apply();
//shader->setUniformMat4("M", CalculateModelMat(Cube2->transform));
// matCube2->Apply();
Cube2->Draw();
//Cube2->Draw();
}
@ -138,17 +154,17 @@ void Update()
*/
void Stop() {
// Cleanup
Cube->VAO.Delete();
Cube->elementBuffer.Delete();
//Cube->VAO.Delete();
//Cube->elementBuffer.Delete();
Cube2->VAO.Delete();
Cube2->elementBuffer.Delete();
//Cube2->VAO.Delete();
//Cube2->elementBuffer.Delete();
delete Cube2;
delete Cube;
//delete Cube2;
//delete Cube;
delete matCube;
delete matCube2;
//delete matCube;
//delete matCube2;
delete shader;
}