Editing the modelimporter to allow to create scene graphs

This commit is contained in:
Nigel Barink 2022-07-08 21:35:14 +02:00
parent 85f9c78adf
commit b7e3465406
19 changed files with 241 additions and 256 deletions

View File

@ -1,6 +0,0 @@
{
"cmake.configureOnOpen": true,
"files.associations": {
"iosfwd": "cpp"
}
}

View File

@ -10,17 +10,20 @@
#include <assimp/postprocess.h>
#include <string>
void ProcessVertices(aiMesh* mesh, std::vector<BarinkEngine::Vertex>& out_vertices);
void ProcessIndices(aiMesh* mesh, std::vector<unsigned int>& 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<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene);
public:
void Import(std::string path);
std::vector<BarinkEngine::Mesh> Import(const std::string path);
private:
static BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene);
static std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene);
static std::vector<BarinkEngine::Mesh> Test();
};

View File

@ -1,6 +1,6 @@
#pragma once
#include <glad/glad.h>
class Buffer {
class GpuBuffer {
private:
unsigned int id;
public:

View File

@ -1,10 +1,11 @@
#pragma once
#include <vector>
#include <glm/glm.hpp>
#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<Vertex> vertices;
std::vector<unsigned int > elements;
std::vector<unsigned int> elements;
private:
GpuBuffer vertexBuffer;
GpuBuffer elementBuffer;
VertexArray VAO;
unsigned int UV_id;
};
}

View File

@ -20,8 +20,8 @@ private:
std::vector<unsigned int > indices;
Buffer vertexBuffer;
Buffer elementBuffer;
GpuBuffer vertexBuffer;
GpuBuffer elementBuffer;
VertexArray VAO;

View File

@ -1,37 +1,23 @@
#pragma once
#include <vector>
#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<BarinkEngine::Mesh> meshes;
Renderable();
};
Renderable();
~Renderable();
};
}

View File

@ -0,0 +1,11 @@
#pragma once
#include <glm/glm.hpp>
struct Transform {
glm::vec3 Position;
glm::vec3 Rotation;
glm::vec3 Scale;
glm::mat4 ModelMatrix;
};

View File

@ -2,46 +2,22 @@
#include <string>
#include <vector>
#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<SceneNode*> 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;
};

View File

@ -0,0 +1,21 @@
#pragma once
#include <string>
#include <vector>
#include "Graphics/Transform.h"
class Node {
public:
std::string& name;
Node* parent;
std::vector<Node*> children;
void addChild(Node& node);
};
class Group : Node {
Transform& transform;
};

View File

@ -0,0 +1,14 @@
#pragma once
#include "Graphics/Renderable.h"
#include "Scene/SceneNodeTypes.h"
#include <glm/glm.hpp>
/*
* 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 );
};

View File

@ -0,0 +1,15 @@
#pragma once
#include <string>
#include <map>
#include "Scene.h"
static class SceneManager {
public:
static Scene* CreateScene();
static Scene& GetScene(std::string name);
private:
static std::map<std::string , Scene*>
};

View File

@ -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;
};

View File

@ -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);
}
}
}

View File

@ -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);
}

View File

@ -1,70 +1,21 @@
#include "AssetManager/ModelImporter.h"
void ModelImporter::ImportFBX(std::string path)
std::vector<BarinkEngine::Mesh> 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<BarinkEngine::Mesh> 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<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene) {
std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene)
{
std::vector<BarinkEngine::Mesh> 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<unsigned int> indices;
std::vector<BarinkEngine::Vertex> vertices;
ProcessVertices(mesh, vertices);
ProcessIndices(mesh, indices);
BarinkEngine::Mesh result;
result.vertices = vertices;
result.elements = indices;
return result;
}
void ProcessVertices(aiMesh* mesh, std::vector<BarinkEngine::Vertex>& 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<unsigned int>& 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;
}

View File

@ -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<unsigned int>(meshes[0].elements.size()), GL_UNSIGNED_INT, NULL);
VAO.Unbind();
}
ES->verts = meshes[0].vertices.size();
ES->DC++;
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(meshes[0].elements.size()), GL_UNSIGNED_INT, NULL);
VAO.Unbind();
*/

View File

@ -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);

View File

@ -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++) {

View File

@ -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);