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 "module.h"
#include <vector>
#include <set>
#include <cstddef>
#include "shadersource.h"
#include "material.h"
@ -38,7 +39,7 @@ public:
private:
ShaderSource* shaderSources;
std::vector<Shader*> shaders;
std::set<Shader*> shaders;
std::vector<unsigned int> geometryFlagList;
std::vector<unsigned int> lightFlagList;
const FrameBuffer* renderTarget;

View File

@ -56,7 +56,7 @@ public:
glm::mat4 getProjectionMatrix() {return projectionMatrix;}
glm::mat4 getViewMatrix() {return viewMatrix;}
// 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);
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.
*/
Mesh();
virtual ~Mesh();
~Mesh();
/**
* OpenGL methods :

View File

@ -2,7 +2,8 @@
#include "mesh.h"
#include <set>
std::vector<unsigned int> getMeshTypes()
std::vector<unsigned int> Scene::getMeshTypes()
{
std::set<unsigned int> typesSet;
std::vector<unsigned int> types;
@ -15,3 +16,67 @@ std::vector<unsigned int> getMeshTypes()
types.push_back(type);
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 <vector>
#include <unordered_map>
#include "light.h"
class Camera;
@ -26,6 +27,8 @@ struct GeometryNode
{
Mesh* mesh;
glm::mat4 modelMatrix;
GeometryNode(Mesh* m, glm::mat4 transform) : mesh(m), modelMatrix(transform) {}
};
class Scene
@ -38,21 +41,22 @@ public:
Pipeline* getPipeline() {return m_pipeline;}
virtual SceneIterator<Light*>* getLights() = 0;
virtual SceneIterator<GeometryNode*>* getGeometry() = 0;
virtual SceneIterator<Light*> getLights(unsigned int flags) = 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 :
template <class T>
class ArraySceneIterator : public SceneIterator<T>
class ArrayIterator : public SceneIterator<T>
{
std::vector<T> &vec;
unsigned int id;
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 T operator*() {return vec[id];}
virtual bool isValid() {return id < vec.size();}
@ -61,22 +65,28 @@ public:
class ArrayScene : public Scene
{
protected:
std::vector<Light*> lights;
std::vector<GeometryNode*> geometry;
union Node
{
Light* light;
GeometryNode* geometry;
};
std::unordered_map<std::vector<Node>*> nodes;
std::vector<GeometryNode*> allocations;
public:
ArrayScene() : Scene() {}
~ArrayScene();
void setPipeline(Pipeline *pipeline) {m_pipeline = pipeline;}
void clearLights() {lights.clear();}
void clearEntities() {geometry.clear();}
void clearScene() {clearLights(); clearEntities();}
void addMesh(GeometryNode* node) {geometry.push_back(node);}
void clearScene();
void addMesh(GeometryNode* node);
void addMesh(Mesh* m, glm::mat4 transform);
void addLight(Light* myLight) {lights.push_back(myLight);}
Mesh* getMesh(int id) {return geometry[id]->mesh;}
SceneIterator<Light*>* getLights() {return new ArraySceneIterator<Light*>(lights);}
SceneIterator<GeometryNode*>* getGeometry() {return new ArraySceneIterator<GeometryNode*>(geometry);}
SceneIterator<Light*> getLights(unsigned int flags);
SceneIterator<GeometryNode*> getGeometry(unsigned int flags);
};
#endif // SCENE_H