Recently, I started wrapping Mesh
es and Transform
classes into new ones, but by doing this I came upon a problem: I needed to render all primitives, or meshes with a renderer. After studying a some game engines (especially the JME3 engine), I came up with this node-based rendering. It's really not complete yet, but functional. I would like to ask you to point out any relevant changes I could make.
Entity.h
#pragma once
#include <glm/glm.hpp>
#include <vector>
#include "../render/Transform.h"
class Entity {
public:
Entity (
const glm::vec3& position = glm::vec3(0.0, 0.0, 0.0),
const glm::vec3& rotation = glm::vec3(0.0, 0.0, 0.0),
const glm::vec3& scale = glm::vec3(1.0, 1.0, 1.0)
)
:
m_transform(position, rotation, scale)
{}
Entity (
const Transform& transform
)
:
m_transform(transform)
{}
virtual ~Entity(){}
virtual void Render(const Shader& shader) = 0;
virtual void Update(){}
void SetTransform(const Transform& transform) { m_transform = transform; }
Transform GetTransform() const { return m_transform; }
void SetPosition(const glm::vec3& position) { m_transform.SetPos(position); }
void SetRotation(const glm::vec3& rotation) { m_transform.SetRot(rotation); }
void SetScale(const glm::vec3& scale) { m_transform.SetScale(scale); }
glm::vec3 GetPosition() const { return m_transform.GetPos(); }
glm::vec3 GetRotation() const { return m_transform.GetRot(); }
glm::vec3 GetScale() const { return m_transform.GetScale(); }
protected:
Transform m_transform;
};
Spatial.h:
#pragma once
#include "Entity.h"
#include "../render/Material.h"
class Spatial : public Entity
{
public:
Spatial (
Mesh mesh,
const glm::vec3& position = glm::vec3(0.0, 0.0, 0.0),
const glm::vec3& rotation = glm::vec3(0.0, 0.0, 0.0),
const glm::vec3& scale = glm::vec3(1.0, 1.0, 1.0)
)
:
Entity(position, rotation, scale),
m_mesh(mesh),
m_material(CreateTexture("res/images/White.png"))
{}
virtual ~Spatial(){}
virtual void Render(const Shader& shader){
m_material.Bind(shader);
m_mesh->Render();
}
virtual void Update(){}
Mesh GetMesh() const { return m_mesh; }
void SetMesh(const Mesh& mesh) { m_mesh = mesh; }
Material GetMaterial() const { return m_material; }
void SetMaterial(const Material& material) { m_material = material; }
protected:
Mesh m_mesh;
Material m_material;
};
Node.h:
#pragma once
#include <vector>
#include <iostream>
#include <cassert>
#include "Entity.h"
#include "Spatial.h"
class Node : public Entity{
public:
Node(){}
~Node(){
m_components.clear();
}
void AttachChild(Entity* component){
assert((Entity*)this != component);
m_components.push_back(component);
}
void DetachAllChildren(){
m_components.clear();
}
void DetachChild(unsigned index){
m_components.erase(m_components.begin() + index);
}
void Render(const Shader& shader){
for(auto it = std::begin(m_components); it != std::end(m_components); ++it){
(*it)->Render(shader);
}
}
protected:
std::vector<Entity*> m_components;
};
main.cpp:
int main(int argc, char **args){
Node rootNode;
Node node;
Spatial spatial = Spatial(CreateMesh("res/models/Suzy.obj"));
Spatial spatial2 = Spatial(CreateMesh("res/models/Suzy.obj"));
rootNode.AttachChild(&spatial2);
node.AttachChild(&spatial1);
rootNode.AttachChild(node);
///Some kind of renderer will render here; haven't implemented that yet...
}
spatial1
variable which is never declared. \$\endgroup\$