ninja scene refactoring

This commit is contained in:
Anselme 2016-04-01 16:59:21 +02:00
parent c2039b331c
commit c3b5a356fe
5 changed files with 94 additions and 19 deletions

View File

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

View File

@ -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:

View File

@ -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 :

View File

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

View File

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