ninja scene refactoring
This commit is contained in:
parent
c2039b331c
commit
c3b5a356fe
@ -4,6 +4,7 @@
|
|||||||
#include "glew.h"
|
#include "glew.h"
|
||||||
#include "module.h"
|
#include "module.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include "shadersource.h"
|
#include "shadersource.h"
|
||||||
#include "material.h"
|
#include "material.h"
|
||||||
@ -38,7 +39,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
ShaderSource* shaderSources;
|
ShaderSource* shaderSources;
|
||||||
std::vector<Shader*> shaders;
|
std::set<Shader*> shaders;
|
||||||
std::vector<unsigned int> geometryFlagList;
|
std::vector<unsigned int> geometryFlagList;
|
||||||
std::vector<unsigned int> lightFlagList;
|
std::vector<unsigned int> lightFlagList;
|
||||||
const FrameBuffer* renderTarget;
|
const FrameBuffer* renderTarget;
|
||||||
|
@ -56,7 +56,7 @@ public:
|
|||||||
glm::mat4 getProjectionMatrix() {return projectionMatrix;}
|
glm::mat4 getProjectionMatrix() {return projectionMatrix;}
|
||||||
glm::mat4 getViewMatrix() {return viewMatrix;}
|
glm::mat4 getViewMatrix() {return viewMatrix;}
|
||||||
// does nothing, just required for inheriting Camera
|
// does nothing, just required for inheriting Camera
|
||||||
void resize(int width, int height) {}
|
void resize(int width, int height) {}
|
||||||
|
|
||||||
static unsigned int getFlags(Light* l);
|
static unsigned int getFlags(Light* l);
|
||||||
private:
|
private:
|
||||||
|
@ -77,8 +77,7 @@ public:
|
|||||||
* @brief Mesh builds an empty mesh, to be renderable, a mesh must have vertices and a material.
|
* @brief Mesh builds an empty mesh, to be renderable, a mesh must have vertices and a material.
|
||||||
*/
|
*/
|
||||||
Mesh();
|
Mesh();
|
||||||
|
~Mesh();
|
||||||
virtual ~Mesh();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OpenGL methods :
|
* OpenGL methods :
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
#include "mesh.h"
|
#include "mesh.h"
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
std::vector<unsigned int> getMeshTypes()
|
|
||||||
|
std::vector<unsigned int> Scene::getMeshTypes()
|
||||||
{
|
{
|
||||||
std::set<unsigned int> typesSet;
|
std::set<unsigned int> typesSet;
|
||||||
std::vector<unsigned int> types;
|
std::vector<unsigned int> types;
|
||||||
@ -15,3 +16,67 @@ std::vector<unsigned int> getMeshTypes()
|
|||||||
types.push_back(type);
|
types.push_back(type);
|
||||||
return types;
|
return types;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<unsigned int> Scene::getLightTypes()
|
||||||
|
{
|
||||||
|
std::set<unsigned int> typesSet;
|
||||||
|
std::vector<unsigned int> types;
|
||||||
|
for(SceneIterator<Light*>* lightIt = scene->getLights();
|
||||||
|
lightIt->isValid(); lightIt->next())
|
||||||
|
{
|
||||||
|
typesSet.emplace(getFlags(geometryIt->getItem()));
|
||||||
|
}
|
||||||
|
for(unsigned int type : typesSet)
|
||||||
|
types.push_back(type);
|
||||||
|
return types;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayScene::~ArrayScene()
|
||||||
|
{
|
||||||
|
clearScene();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArrayScene::addMesh(GeometryNode* node)
|
||||||
|
{
|
||||||
|
Node n;
|
||||||
|
n.geometry = node;
|
||||||
|
unsigned int flags = node->mesh->getFlags();
|
||||||
|
if(nodes.count(flags) > 0)
|
||||||
|
nodes[m->getFlags()]->push_back(n);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::vector<Node> *vec = new std::vector<Node>();
|
||||||
|
vec->push_back(n);
|
||||||
|
nodes[m->getFlags()] = vec;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArrayScene::addMesh(Mesh* m, glm::mat4 transform)
|
||||||
|
{
|
||||||
|
GeometryNode *gn = new GeometryNode(m, transform);
|
||||||
|
allocations.push_back(gn);
|
||||||
|
addMesh(gn);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArrayScene::clearScene()
|
||||||
|
{
|
||||||
|
for(auto it : nodes)
|
||||||
|
delete it;
|
||||||
|
for(GeometryNode *gn : allocations)
|
||||||
|
delete gn;
|
||||||
|
allocations.clear();
|
||||||
|
nodes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
SceneIterator<Light*> ArrayScene::getLights(unsigned int flags)
|
||||||
|
{
|
||||||
|
if(nodes.count(flags))
|
||||||
|
return ArrayIterator<Light*>(*(nodes[flags]));
|
||||||
|
}
|
||||||
|
|
||||||
|
SceneIterator<GeometryNode*> ArrayScene::getGeometry(unsigned int flags)
|
||||||
|
{
|
||||||
|
if(nodes.count(flags))
|
||||||
|
return ArrayIterator<GeometryNode*>(*(nodes[flags]));
|
||||||
|
}
|
||||||
|
|
||||||
|
38
src/scene.h
38
src/scene.h
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <glm/mat4x4.hpp>
|
#include <glm/mat4x4.hpp>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
#include "light.h"
|
#include "light.h"
|
||||||
|
|
||||||
class Camera;
|
class Camera;
|
||||||
@ -26,6 +27,8 @@ struct GeometryNode
|
|||||||
{
|
{
|
||||||
Mesh* mesh;
|
Mesh* mesh;
|
||||||
glm::mat4 modelMatrix;
|
glm::mat4 modelMatrix;
|
||||||
|
|
||||||
|
GeometryNode(Mesh* m, glm::mat4 transform) : mesh(m), modelMatrix(transform) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Scene
|
class Scene
|
||||||
@ -38,21 +41,22 @@ public:
|
|||||||
|
|
||||||
Pipeline* getPipeline() {return m_pipeline;}
|
Pipeline* getPipeline() {return m_pipeline;}
|
||||||
|
|
||||||
virtual SceneIterator<Light*>* getLights() = 0;
|
virtual SceneIterator<Light*> getLights(unsigned int flags) = 0;
|
||||||
virtual SceneIterator<GeometryNode*>* getGeometry() = 0;
|
virtual SceneIterator<GeometryNode*> getGeometry(unsigned int flags) = 0;
|
||||||
|
|
||||||
std::vector<unsigned int> getMeshTypes();
|
virtual void getMeshTypes(std::vector<unsigned int> &meshTypes);
|
||||||
|
virtual void getLightTypes(std::vector<unsigned int> &lightTypes);
|
||||||
};
|
};
|
||||||
|
|
||||||
// A basic implementation :
|
// A basic implementation :
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
class ArraySceneIterator : public SceneIterator<T>
|
class ArrayIterator : public SceneIterator<T>
|
||||||
{
|
{
|
||||||
std::vector<T> &vec;
|
std::vector<T> &vec;
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
public:
|
public:
|
||||||
ArraySceneIterator(std::vector<T> &myVec, int myId=0) : vec(myVec), id(myId) {}
|
ArrayIterator(std::vector<T> &myVec, int myId=0) : vec(myVec), id(myId) {}
|
||||||
virtual SceneIterator<T>& operator++() {++id; return *this;}
|
virtual SceneIterator<T>& operator++() {++id; return *this;}
|
||||||
virtual T operator*() {return vec[id];}
|
virtual T operator*() {return vec[id];}
|
||||||
virtual bool isValid() {return id < vec.size();}
|
virtual bool isValid() {return id < vec.size();}
|
||||||
@ -61,22 +65,28 @@ public:
|
|||||||
class ArrayScene : public Scene
|
class ArrayScene : public Scene
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
std::vector<Light*> lights;
|
union Node
|
||||||
std::vector<GeometryNode*> geometry;
|
{
|
||||||
|
Light* light;
|
||||||
|
GeometryNode* geometry;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unordered_map<std::vector<Node>*> nodes;
|
||||||
|
|
||||||
|
std::vector<GeometryNode*> allocations;
|
||||||
public:
|
public:
|
||||||
ArrayScene() : Scene() {}
|
ArrayScene() : Scene() {}
|
||||||
|
~ArrayScene();
|
||||||
|
|
||||||
void setPipeline(Pipeline *pipeline) {m_pipeline = pipeline;}
|
void setPipeline(Pipeline *pipeline) {m_pipeline = pipeline;}
|
||||||
|
void clearScene();
|
||||||
void clearLights() {lights.clear();}
|
void addMesh(GeometryNode* node);
|
||||||
void clearEntities() {geometry.clear();}
|
void addMesh(Mesh* m, glm::mat4 transform);
|
||||||
void clearScene() {clearLights(); clearEntities();}
|
|
||||||
void addMesh(GeometryNode* node) {geometry.push_back(node);}
|
|
||||||
void addLight(Light* myLight) {lights.push_back(myLight);}
|
void addLight(Light* myLight) {lights.push_back(myLight);}
|
||||||
Mesh* getMesh(int id) {return geometry[id]->mesh;}
|
Mesh* getMesh(int id) {return geometry[id]->mesh;}
|
||||||
|
|
||||||
SceneIterator<Light*>* getLights() {return new ArraySceneIterator<Light*>(lights);}
|
SceneIterator<Light*> getLights(unsigned int flags);
|
||||||
SceneIterator<GeometryNode*>* getGeometry() {return new ArraySceneIterator<GeometryNode*>(geometry);}
|
SceneIterator<GeometryNode*> getGeometry(unsigned int flags);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SCENE_H
|
#endif // SCENE_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user