Add instancing system
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
|
||||
#include "Spatial.h"
|
||||
#include "Transform.h"
|
||||
|
||||
class Camera : public Spatial {
|
||||
class Camera {
|
||||
public:
|
||||
Transform transform;
|
||||
Camera();
|
||||
void MakeCurrent();
|
||||
static Camera *GetCurrentCamera();
|
||||
|
||||
@@ -2,6 +2,24 @@
|
||||
|
||||
Name Light::GetType() const {return "Light";}
|
||||
|
||||
Light *Light::Duplicate() {
|
||||
Light *light = static_cast<Light*>(Spatial::Duplicate());
|
||||
light->color = color;
|
||||
light->ambient = ambient;
|
||||
light->diffuse = diffuse;
|
||||
light->specular = specular;
|
||||
|
||||
return light;
|
||||
}
|
||||
|
||||
Light *Light::Instance() {
|
||||
return static_cast<Light*>(Node::Instance());
|
||||
}
|
||||
|
||||
Light *Light::Create() {
|
||||
return new Light;
|
||||
}
|
||||
|
||||
Name DirectionalLight::GetType() const {return "DirectionalLight";}
|
||||
|
||||
DirectionalLight::DirectionalLight() {
|
||||
@@ -19,3 +37,18 @@ DirectionalLight::DirectionalLight(Vector3 direction, Vector3 color, cfloat ambi
|
||||
this->diffuse = diffuse;
|
||||
this->specular = specular;
|
||||
}
|
||||
|
||||
DirectionalLight *DirectionalLight::Create() {
|
||||
return new DirectionalLight;
|
||||
}
|
||||
|
||||
DirectionalLight *DirectionalLight::Duplicate() {
|
||||
DirectionalLight *directionalLight = static_cast<DirectionalLight*>(Light::Duplicate());
|
||||
directionalLight->direction = direction;
|
||||
|
||||
return directionalLight;
|
||||
}
|
||||
|
||||
DirectionalLight *DirectionalLight::Instance() {
|
||||
return static_cast<DirectionalLight*>(Node::Instance());
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ public:
|
||||
Vector3 color;
|
||||
cfloat ambient, diffuse, specular;
|
||||
virtual Name GetType() const;
|
||||
virtual Light *Create();
|
||||
virtual Light *Duplicate();
|
||||
virtual Light *Instance();
|
||||
};
|
||||
|
||||
class DirectionalLight : public Light {
|
||||
@@ -17,6 +20,9 @@ public:
|
||||
DirectionalLight();
|
||||
DirectionalLight(Vector3 direction, Vector3 color, cfloat ambient, cfloat diffuse, cfloat specular);
|
||||
virtual Name GetType() const;
|
||||
virtual DirectionalLight *Create();
|
||||
virtual DirectionalLight *Duplicate();
|
||||
virtual DirectionalLight *Instance();
|
||||
};
|
||||
|
||||
#endif /* LIGHT_H */
|
||||
|
||||
@@ -41,6 +41,16 @@ void SubMesh::Draw(Shader *shader) {
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
SubMesh *SubMesh::Duplicate() {
|
||||
SubMesh* submesh = new SubMesh();
|
||||
|
||||
submesh->VAO = VAO;
|
||||
submesh->indices = indices;
|
||||
submesh->material = material;
|
||||
|
||||
return submesh;
|
||||
}
|
||||
|
||||
Name Mesh::GetType() const {return "Mesh";}
|
||||
|
||||
Mesh::Mesh() {}
|
||||
@@ -51,9 +61,24 @@ Mesh::~Mesh() {
|
||||
}
|
||||
}
|
||||
|
||||
Mesh *Mesh::Create() {
|
||||
return new Mesh;
|
||||
}
|
||||
|
||||
Mesh *Mesh::Duplicate() {
|
||||
Mesh *dup = new Mesh(*this);
|
||||
return dup;
|
||||
Mesh *mesh = static_cast<Mesh*>(Spatial::Duplicate());
|
||||
// Duplicate submeshes
|
||||
mesh->submeshes = SubMeshList();
|
||||
|
||||
for (SubMesh *submesh : submeshes) {
|
||||
mesh->submeshes.push_back(submesh->Duplicate());
|
||||
}
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
Mesh *Mesh::Instance() {
|
||||
return static_cast<Mesh*>(Node::Instance());
|
||||
}
|
||||
|
||||
void Mesh::SetupMesh() {
|
||||
|
||||
@@ -26,6 +26,7 @@ public:
|
||||
IndexList indices;
|
||||
Material material;
|
||||
void Draw(Shader *shader);
|
||||
SubMesh *Duplicate();
|
||||
private:
|
||||
Id VAO, VBO, EBO;
|
||||
void SetupSubMesh();
|
||||
@@ -44,7 +45,9 @@ public:
|
||||
virtual bool IsDrawable() const {return true;}
|
||||
virtual void Draw(Shader *shader);
|
||||
virtual Name GetType() const;
|
||||
Mesh *Duplicate();
|
||||
virtual Mesh *Create();
|
||||
virtual Mesh *Duplicate();
|
||||
virtual Mesh *Instance();
|
||||
protected:
|
||||
SubMeshList submeshes;
|
||||
virtual void SetupMesh();
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "Node.h"
|
||||
#include "Util.h"
|
||||
|
||||
Name Node::GetType() const {return "Node";}
|
||||
|
||||
Node *Node::root = {new Node()};
|
||||
Node *Node::root = {Node().Instance()};
|
||||
Node *Node::GetRoot() {
|
||||
return root;
|
||||
}
|
||||
@@ -13,6 +14,35 @@ bool Node::IsTransformable() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
Node* Node::Create() {
|
||||
return new Node;
|
||||
}
|
||||
|
||||
Node* Node::Duplicate() {
|
||||
return Create();
|
||||
}
|
||||
|
||||
Node* Node::Instance() {
|
||||
if (not this->isPrefab) {
|
||||
Util::Die("Attempt to instance an instanced node!");
|
||||
}
|
||||
Node* instance = Duplicate();
|
||||
instance->isPrefab = false;
|
||||
instance->children = NodeList();
|
||||
instance->children.isPrefabList = false;
|
||||
for (Node *child : children) {
|
||||
instance->children.Append(child->Instance());
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NodeList::Append(Node *node) {
|
||||
if (this->isPrefabList and not node->isPrefab) {
|
||||
Util::Die("Attempt to add instanced node to prefab list!");
|
||||
}
|
||||
if (node->isPrefab and not this->isPrefabList) {
|
||||
Util::Die("Attempt to add prefab node to instanced list!");
|
||||
}
|
||||
push_back(node);
|
||||
}
|
||||
|
||||
10
core/Node.h
10
core/Node.h
@@ -9,6 +9,9 @@ class Node; // Forwards declare
|
||||
class NodeList : public std::vector<Node*> {
|
||||
public:
|
||||
void Append(Node *node);
|
||||
private:
|
||||
bool isPrefabList = true;
|
||||
friend class Node;
|
||||
};
|
||||
|
||||
class Node {
|
||||
@@ -16,11 +19,16 @@ public:
|
||||
NodeList children;
|
||||
static Node *GetRoot();
|
||||
virtual bool IsDrawable() const;
|
||||
virtual void Draw(){};
|
||||
virtual void Draw() {};
|
||||
virtual bool IsTransformable() const;
|
||||
virtual Name GetType() const;
|
||||
virtual Node* Create();
|
||||
virtual Node* Instance();
|
||||
virtual Node* Duplicate();
|
||||
private:
|
||||
static Node *root;
|
||||
bool isPrefab = true;
|
||||
friend class NodeList;
|
||||
};
|
||||
|
||||
#endif /* NODE_H */
|
||||
|
||||
@@ -63,6 +63,22 @@ Skybox::Skybox() {
|
||||
|
||||
Name Skybox::GetType() const { return "Skybox"; }
|
||||
|
||||
Skybox *Skybox::Create() {
|
||||
return new Skybox;
|
||||
}
|
||||
|
||||
Skybox *Skybox::Duplicate() {
|
||||
Skybox* skybox = static_cast<Skybox*>(Node::Duplicate());
|
||||
skybox->id = id;
|
||||
skybox->cube = cube;
|
||||
|
||||
return skybox;
|
||||
}
|
||||
|
||||
Skybox *Skybox::Instance() {
|
||||
return static_cast<Skybox*>(Node::Instance());
|
||||
}
|
||||
|
||||
Skybox *Skybox::FromFiles(const char *right, const char* left, const char* top, const char* bottom, const char* front, const char* back) {
|
||||
// HOCUS: https://learnopengl.com/Advanced-OpenGL/Cubemaps
|
||||
Skybox *sb = new Skybox();
|
||||
|
||||
@@ -11,6 +11,9 @@ class Skybox : public Node {
|
||||
public:
|
||||
Skybox();
|
||||
virtual Name GetType() const;
|
||||
virtual Skybox *Create();
|
||||
virtual Skybox *Duplicate();
|
||||
virtual Skybox *Instance();
|
||||
static Skybox *FromFiles(const char *right, const char* left, const char* top, const char* bottom, const char* front, const char* back);
|
||||
void DrawSkybox();
|
||||
Id id;
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
#include "Spatial.h"
|
||||
|
||||
Name Spatial::GetType() const {return "Spatial";}
|
||||
|
||||
Spatial *Spatial::Create() {
|
||||
return new Spatial;
|
||||
}
|
||||
|
||||
Spatial *Spatial::Duplicate() {
|
||||
Spatial *spatial = static_cast<Spatial*>(Node::Duplicate());
|
||||
spatial->transform = transform;
|
||||
|
||||
return spatial;
|
||||
}
|
||||
|
||||
Spatial *Spatial::Instance() {
|
||||
return static_cast<Spatial*>(Node::Instance());
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,9 @@ public:
|
||||
Transform transform;
|
||||
virtual bool IsTransformable() const { return true;}
|
||||
virtual Name GetType() const;
|
||||
virtual Spatial *Create();
|
||||
virtual Spatial *Duplicate();
|
||||
virtual Spatial *Instance();
|
||||
};
|
||||
|
||||
#endif /* SPATIAL_H */
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "types.h"
|
||||
class Node;
|
||||
#include "Node.h"
|
||||
|
||||
namespace Util {
|
||||
|
||||
Reference in New Issue
Block a user