sparrow renderer now uses resourcebase
This commit is contained in:
parent
c534c7b4e7
commit
5393bbfa1b
@ -7,7 +7,7 @@
|
||||
class Material
|
||||
{
|
||||
public:
|
||||
Material(Shader* myShader) : shader(myShader) {}
|
||||
Material(Shader* myShader = NULL) : shader(myShader) {}
|
||||
Shader* getShader() {return shader;}
|
||||
|
||||
virtual void bindAttributes() = 0;
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "skyboxmodule.h"
|
||||
#include "entity.h"
|
||||
#include "phongmodule.h"
|
||||
#include "resourcebase.h"
|
||||
#include "utils.h"
|
||||
#include "lights.h"
|
||||
#include "texture.h"
|
||||
@ -33,47 +34,57 @@ MyGLWidget::~MyGLWidget()
|
||||
delete(controller);
|
||||
}
|
||||
|
||||
void MyGLWidget::buildScene()
|
||||
void MyGLWidget::loadResources()
|
||||
{
|
||||
// init camera
|
||||
Camera* cam = renderer->getCamera();
|
||||
cam->moveTo(glm::vec3(0, 0, 3));
|
||||
cam->lookAt(glm::vec2(0, 0));
|
||||
|
||||
// create skybox module
|
||||
std::string filenames[6] = {
|
||||
"../data/skybox_ft", "../data/skybox_bk",
|
||||
"../data/skybox_up", "../data/skybox_dn",
|
||||
"../data/skybox_lf", "../data/skybox_rt"
|
||||
};
|
||||
Texture* skyboxCubemap = new Texture(filenames);
|
||||
SkyboxModule* skybox = new SkyboxModule(skyboxCubemap);
|
||||
ResourceBase::setTexture("skybox", new Texture(filenames));
|
||||
ResourceBase::setTexture("textureNoise", new Texture("../data/noise.png"));
|
||||
ResourceBase::setTexture("perlinNoise", new Texture());
|
||||
|
||||
// create a mesh
|
||||
//Mesh* myMesh = new GridMesh(10, 10, true);
|
||||
//Mesh* myMesh = Utils::loadOBJ("../data/sword.obj");
|
||||
Mesh* myMesh = new Sphere(4);
|
||||
myMesh->initGL();
|
||||
ResourceBase::setMesh("grid", new GridMesh(10, 10, true));
|
||||
ResourceBase::setMesh("sword", Utils::loadOBJ("../data/sword.obj"));
|
||||
ResourceBase::setMesh("sphere", new Sphere(4));
|
||||
|
||||
std::string vertSource = Utils::fileToString("../phong.vert");
|
||||
std::string fragSource = Utils::fileToString("../phong.frag");
|
||||
Shader* phongShader = new Shader(vertSource, fragSource);
|
||||
PhongMaterial* mat = new PhongMaterial(phongShader, glm::vec3(1), glm::vec3(1), 20.0f);
|
||||
Texture* tex = new Texture(); //new Texture("../data/noise.png");
|
||||
mat->setTexture(tex);
|
||||
ResourceBase::setShader("phong", new Shader(vertSource, fragSource));
|
||||
}
|
||||
|
||||
Entity* myEntity = new Entity(myMesh, mat);
|
||||
glm::mat4* transform = myEntity->getTransform();
|
||||
//*transform = glm::scale(*transform, glm::vec3(0.01f));
|
||||
void MyGLWidget::buildScene()
|
||||
{
|
||||
// camera positionning
|
||||
Camera* cam = renderer->getCamera();
|
||||
cam->moveTo(glm::vec3(0, 0, 3));
|
||||
cam->lookAt(glm::vec2(0, 0));
|
||||
|
||||
// create skybox module
|
||||
SkyboxModule* skybox = new SkyboxModule(ResourceBase::getTexture("skybox"));
|
||||
renderer->addModule(skybox);
|
||||
|
||||
// create phong module
|
||||
Lights* directionnalLights = new Lights();
|
||||
Lights* pointLights = new Lights();
|
||||
directionnalLights->addLight(glm::vec3(6, 4, -4), glm::vec3(0.7f, 0.6f, 0.4f)); // sun
|
||||
|
||||
PhongModule* myPhongModule = new PhongModule(directionnalLights, new Lights(), phongShader);
|
||||
myPhongModule->addEntity(myEntity);
|
||||
|
||||
renderer->addModule(skybox);
|
||||
PhongModule* myPhongModule = new PhongModule(directionnalLights, pointLights, ResourceBase::getShader("phong"));
|
||||
renderer->addModule(myPhongModule);
|
||||
|
||||
// create a material
|
||||
PhongMaterial* mat = new PhongMaterial(glm::vec3(1), glm::vec3(1), 20.0f);
|
||||
mat->setTexture(ResourceBase::getTexture("textureNoise"));
|
||||
ResourceBase::setMaterial("basicMat", mat);
|
||||
|
||||
// create an entity
|
||||
Mesh* myMesh = ResourceBase::getMesh("sphere");
|
||||
myMesh->initGL();
|
||||
Entity* myEntity = new Entity(myMesh, ResourceBase::getMaterial("basicMat"));
|
||||
glm::mat4* transform = myEntity->getTransform();
|
||||
*transform = glm::rotate(*transform, 100.0f, glm::vec3(0, 1, 0));
|
||||
myPhongModule->addEntity(myEntity);
|
||||
}
|
||||
|
||||
void MyGLWidget::initializeGL()
|
||||
@ -81,6 +92,7 @@ void MyGLWidget::initializeGL()
|
||||
if(renderer != NULL)
|
||||
delete(renderer);
|
||||
renderer = new SparrowRenderer(width(), height());
|
||||
loadResources();
|
||||
buildScene();
|
||||
controller = new FocusController(new glm::vec3(0));
|
||||
controller->setCamera(renderer->getCamera());
|
||||
|
@ -24,6 +24,7 @@ public:
|
||||
|
||||
protected:
|
||||
void initializeGL();
|
||||
void loadResources();
|
||||
void buildScene();
|
||||
void resizeGL(int width, int height);
|
||||
void paintGL();
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include "material.h"
|
||||
#include "glm/vec3.hpp"
|
||||
#include "resourcebase.h"
|
||||
|
||||
class Texture;
|
||||
|
||||
@ -13,8 +14,14 @@ class PhongMaterial : public Material
|
||||
float ns;
|
||||
Texture* tex;
|
||||
public:
|
||||
PhongMaterial(Shader* myShader) : Material(myShader), kd(0.5f), ks(0.5f), ns(10), tex(NULL) {}
|
||||
PhongMaterial(Shader* myShader, glm::vec3 myKd, glm::vec3 myKs, float myNs) : Material(myShader), kd(myKd), ks(myKs), ns(myNs), tex(NULL) {}
|
||||
PhongMaterial() : kd(0.5f), ks(0.5f), ns(10), tex(NULL)
|
||||
{
|
||||
shader = ResourceBase::getShader("phong");
|
||||
}
|
||||
PhongMaterial(glm::vec3 myKd, glm::vec3 myKs, float myNs) : kd(myKd), ks(myKs), ns(myNs), tex(NULL)
|
||||
{
|
||||
shader = ResourceBase::getShader("phong");
|
||||
}
|
||||
virtual void bindAttributes();
|
||||
|
||||
void setTexture(Texture* myTexture);
|
||||
|
35
resource.h
Normal file
35
resource.h
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef RESOURCE
|
||||
#define RESOURCE
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
template<typename T>
|
||||
class Resource
|
||||
{
|
||||
public:
|
||||
T* data;
|
||||
std::string sourcePath;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class DataBank
|
||||
{
|
||||
std::unordered_map<std::string, Resource<T>*> dataMap;
|
||||
std::vector<Resource<T>*> dataList;
|
||||
public:
|
||||
T* get(std::string name)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void set(std::string name, T*)
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
#endif // RESOURCE
|
||||
|
@ -1,73 +1,60 @@
|
||||
#include "resourcebase.h"
|
||||
|
||||
ResourceBase::~ResourceBase()
|
||||
ResourceBase::DataBase<Texture> ResourceBase::textures;
|
||||
ResourceBase::DataBase<Mesh> ResourceBase::meshes;
|
||||
ResourceBase::DataBase<Material> ResourceBase::materials;
|
||||
ResourceBase::DataBase<Shader> ResourceBase::shaders;
|
||||
ResourceBase::DataBase<Entity> ResourceBase::entities;
|
||||
|
||||
void ResourceBase::setTexture(const std::string &textureName, Texture* myTexture)
|
||||
{
|
||||
for(std::string str : textureNames)
|
||||
delete(textures[str]);
|
||||
for(std::string str : meshNames)
|
||||
delete(meshes[str]);
|
||||
for(std::string str : materialNames)
|
||||
delete(materials[str]);
|
||||
for(std::string str : shaderNames)
|
||||
delete(shaders[str]);
|
||||
for(std::string str : entityNames)
|
||||
delete(entities[str]);
|
||||
textures.add(textureName, myTexture);
|
||||
}
|
||||
|
||||
void ResourceBase::addTexture(std::string textureName, Texture* myTexture)
|
||||
void ResourceBase::setMesh(const std::string &meshName, Mesh* myMesh )
|
||||
{
|
||||
textureNames.push_back(textureName);
|
||||
textures[textureName] = myTexture;
|
||||
meshes.add(meshName, myMesh);
|
||||
}
|
||||
|
||||
void ResourceBase::addMesh(std::string meshName, Mesh* myMesh)
|
||||
void ResourceBase::setMaterial(const std::string &materialName, Material* myMaterial)
|
||||
{
|
||||
meshNames.push_back(meshName);
|
||||
meshes[meshName] = myMesh;
|
||||
materials.add(materialName, myMaterial);
|
||||
}
|
||||
|
||||
void ResourceBase::addMaterial(std::string materialName, Material* myMaterial)
|
||||
void ResourceBase::setShader(const std::string &shaderName, Shader* myShader)
|
||||
{
|
||||
materialNames.push_back(materialName);
|
||||
materials[materialName] = myMaterial;
|
||||
shaders.add(shaderName, myShader);
|
||||
}
|
||||
|
||||
void ResourceBase::addShader(std::string shaderName, Shader* myShader)
|
||||
void ResourceBase::setEntity(const std::string &entityName, Entity* myEntity)
|
||||
{
|
||||
shaderNames.push_back(shaderName);
|
||||
shaders[shaderName] = myShader;
|
||||
}
|
||||
|
||||
void ResourceBase::addEntity(std::string entityName, Entity* myEntity)
|
||||
{
|
||||
entityNames.push_back(entityName);
|
||||
entities[entityName] = myEntity;
|
||||
entities.add(entityName, myEntity);
|
||||
}
|
||||
|
||||
|
||||
Texture* ResourceBase::getTexture(std::string textureName)
|
||||
Texture* ResourceBase::getTexture(const std::string &textureName)
|
||||
{
|
||||
return textures[textureName];
|
||||
return textures.get(textureName);
|
||||
}
|
||||
|
||||
Mesh* ResourceBase::getMesh(std::string meshName)
|
||||
Mesh* ResourceBase::getMesh(const std::string &meshName)
|
||||
{
|
||||
return meshes[meshName];
|
||||
return meshes.get(meshName);
|
||||
}
|
||||
|
||||
Material* ResourceBase::getMaterial(std::string materialName)
|
||||
Material* ResourceBase::getMaterial(const std::string &materialName)
|
||||
{
|
||||
return materials[materialName];
|
||||
return materials.get(materialName);
|
||||
}
|
||||
|
||||
Shader* ResourceBase::getShader(std::string shaderName)
|
||||
Shader* ResourceBase::getShader(const std::string &shaderName)
|
||||
{
|
||||
return shaders[shaderName];
|
||||
return shaders.get(shaderName);
|
||||
}
|
||||
|
||||
Entity* ResourceBase::getEntity(std::string entityName)
|
||||
Entity* ResourceBase::getEntity(const std::string &entityName)
|
||||
{
|
||||
return entities[entityName];
|
||||
return entities.get(entityName);
|
||||
}
|
||||
|
||||
|
||||
|
@ -13,33 +13,44 @@ class Entity;
|
||||
|
||||
class ResourceBase
|
||||
{
|
||||
private:
|
||||
std::vector<std::string> textureNames;
|
||||
std::vector<std::string> meshNames;
|
||||
std::vector<std::string> materialNames;
|
||||
std::vector<std::string> shaderNames;
|
||||
std::vector<std::string> entityNames;
|
||||
|
||||
std::unordered_map<std::string, Texture*> textures;
|
||||
std::unordered_map<std::string, Mesh*> meshes;
|
||||
std::unordered_map<std::string, Material*> materials;
|
||||
std::unordered_map<std::string, Shader*> shaders;
|
||||
std::unordered_map<std::string, Entity*> entities;
|
||||
public:
|
||||
ResourceBase();
|
||||
~ResourceBase();
|
||||
static void setTexture(const std::string &textureName, Texture* myTexture);
|
||||
static void setMesh(const std::string &meshName, Mesh* myMesh);
|
||||
static void setMaterial(const std::string &materialName, Material* myMaterial);
|
||||
static void setShader(const std::string &shaderName, Shader* myShader);
|
||||
static void setEntity(const std::string &entityName, Entity* myEntity);
|
||||
|
||||
void addTexture(std::string textureName, Texture* myTexture);
|
||||
void addMesh(std::string meshName, Mesh* myMesh);
|
||||
void addMaterial(std::string materialName, Material* myMaterial);
|
||||
void addShader(std::string shaderName, Shader* myShader);
|
||||
void addEntity(std::string entityName, Entity* myEntity);
|
||||
static Texture* getTexture(const std::string &textureName);
|
||||
static Mesh* getMesh(const std::string &meshName);
|
||||
static Material* getMaterial(const std::string &materialName);
|
||||
static Shader* getShader(const std::string &shaderName);
|
||||
static Entity* getEntity(const std::string &entityName);
|
||||
|
||||
Texture* getTexture(std::string textureName);
|
||||
Mesh* getMesh(std::string meshName);
|
||||
Material* getMaterial(std::string materialName);
|
||||
Shader* getShader(std::string shaderName);
|
||||
Entity* getEntity(std::string entityName);
|
||||
private:
|
||||
template <typename T>
|
||||
class DataBase
|
||||
{
|
||||
public:
|
||||
std::vector<std::string> names;
|
||||
std::unordered_map<std::string, T*> data;
|
||||
|
||||
void add(const std::string &name, T* t)
|
||||
{
|
||||
data[name] = t;
|
||||
names.push_back(name);
|
||||
}
|
||||
|
||||
T* get(const std::string &name)
|
||||
{
|
||||
return data[name];
|
||||
}
|
||||
};
|
||||
|
||||
static DataBase<Texture> textures;
|
||||
static DataBase<Mesh> meshes;
|
||||
static DataBase<Material> materials;
|
||||
static DataBase<Shader> shaders;
|
||||
static DataBase<Entity> entities;
|
||||
};
|
||||
|
||||
#endif // RESOURCEBASE_H
|
||||
|
@ -63,7 +63,8 @@ HEADERS += mainwindow.h \
|
||||
phongmodule.h \
|
||||
skyboxmodule.h \
|
||||
basicmodule.h \
|
||||
module.h
|
||||
module.h \
|
||||
resource.h
|
||||
|
||||
FORMS += mainwindow.ui
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user