1
0
Fork 0

Add resource store

This commit is contained in:
Meoweg 2015-11-11 17:28:11 +00:00
parent f92ec0c468
commit c29875ef17
11 changed files with 121 additions and 32 deletions

View File

@ -28,10 +28,10 @@ static Scene scene;
static Camera camera(scene);
static float camera_angles_x = 0;
static Model *protagonist;
static Model *suzanne;
static Model *teapot;
static Model *bunny;
static const Model *protagonist;
static const Model *suzanne;
static const Model *teapot;
static const Model *bunny;
static Object *protagonist1;
static Object *suzanne1;
@ -59,22 +59,22 @@ int main()
glfwSetKeyCallback(on_key);
emscripten_set_mousemove_callback(nullptr, nullptr, false, on_em_mousemove);
Program program("textured");
program.use();
const Program *program = Store().load<Program>("textured");
program->use();
mvp_uniform = program.get_uniform_location("mvp");
local_modelview_uniform = program.get_uniform_location("local_modelview");
mvp_uniform = program->get_uniform_location("mvp");
local_modelview_uniform = program->get_uniform_location("local_modelview");
texture_uniform = program.get_uniform_location("texture");
texture_uniform = program->get_uniform_location("texture");
glUniform1i(texture_uniform, 0);
camera.projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 10.0f);
camera.position.z = 2;
protagonist = new Model("protagonist.obj");
suzanne = new Model("suzanne.obj");
teapot = new Model("teapot.obj");
bunny = new Model("bunny.obj");
protagonist = Store().load<Model>("protagonist.obj");
suzanne = Store().load<Model>("suzanne.obj");
teapot = Store().load<Model>("teapot.obj");
bunny = Store().load<Model>("bunny.obj");
protagonist1 = new Object(*protagonist);
protagonist1->position.z = 4;

View File

@ -8,7 +8,7 @@ class Material
public:
inline void use() const;
Texture *texture;
const Texture *texture;
};
void Material::use() const

View File

@ -5,9 +5,14 @@
#include <fstream>
#include <sstream>
const std::string Model::filename(const std::string &name)
{
return "/data/models/" + name;
}
Model::Model(const std::string &name)
{
std::ifstream file("/data/models/" + name, std::ios::in);
std::ifstream file(filename(name), std::ios::in);
Mtllib *mtllib = nullptr;
@ -76,7 +81,7 @@ Model::Model(const std::string &name)
else
if (line.substr(0, 7) == "mtllib ")
{
mtllib = new Mtllib(line.substr(7));
mtllib = (Mtllib*)Store().load<Mtllib>(line.substr(7));
}
else
if (line.substr(0, 7) == "usemtl ")

View File

@ -1,6 +1,7 @@
#ifndef _MODEL_HPP_
#define _MODEL_HPP_
#include "store.hpp"
#include "gl.hpp"
#include "material.hpp"
@ -9,10 +10,16 @@
#include <glm/glm.hpp>
class Model
class Model:
public Store::Resource
{
public:
friend class Store;
Model(const std::string &name);
static const std::string filename(const std::string &name);
public:
void draw() const;
private:
@ -28,7 +35,7 @@ private:
std::vector<GLushort> elements;
GLuint id;
Material *_material;
const Material *_material;
static GLuint create_array_buffer(GLenum type, GLsizeiptr size, const GLvoid *data);
};

View File

@ -3,9 +3,14 @@
#include <string>
#include <fstream>
const std::string Mtllib::filename(const std::string &name)
{
return "/data/materials/" + name;
}
Mtllib::Mtllib(const std::string &name)
{
std::ifstream file("/data/materials/" + name, std::ios::in);
std::ifstream file(filename(name), std::ios::in);
Material *material = nullptr;
@ -23,7 +28,7 @@ Mtllib::Mtllib(const std::string &name)
else
if (line.substr(0, 7) == "map_Kd ")
{
material->texture = new Texture(line.substr(7).c_str());
material->texture = Store().load<Texture>(line.substr(7));
}
}
}

View File

@ -1,17 +1,23 @@
#ifndef _MTLLIB_HPP_
#define _MTLLIB_HPP_
#include "store.hpp"
#include "material.hpp"
#include <string>
#include <map>
class Mtllib
class Mtllib:
public Store::Resource
{
public:
friend class Store;
Mtllib(const std::string &name);
std::map<std::string, Material*> materials;
static const std::string filename(const std::string &name);
public:
std::map<std::string, const Material*> materials;
};
#endif // _MTLLIB_HPP_

View File

@ -2,12 +2,16 @@
#include "shader.hpp"
#include <string>
#include <fstream>
Program::Program(const char *const name)
const std::string Program::filename(const std::string &name)
{
const std::string path = std::string("/data/shaders/") + name + '/';
return "/data/shaders/" + name;
}
Program::Program(const std::string &name)
{
const std::string path = filename(name) + '/';
const Shader vertex_shader = Shader(GL_VERTEX_SHADER, (path + "vertex.glsl").c_str());
const Shader fragment_shader = Shader(GL_FRAGMENT_SHADER, (path + "fragment.glsl").c_str());

View File

@ -1,12 +1,21 @@
#ifndef _PROGRAM_HPP_
#define _PROGRAM_HPP_
#include "store.hpp"
#include "gl.hpp"
class Program
#include <string>
class Program:
public Store::Resource
{
friend class Store;
Program(const std::string &name);
static const std::string filename(const std::string &name);
public:
Program(const char *name);
void use() const;
GLuint get_uniform_location(const GLchar *name) const;

40
src/store.hpp Normal file
View File

@ -0,0 +1,40 @@
#ifndef _STORE_HPP_
#define _STORE_HPP_
#include <type_traits>
#include <string>
#include <map>
class Store
{
public:
class Resource
{
friend class Store;
__attribute__((unused)) // Used by friend class Store
unsigned long _ref_count = 0;
};
template <class T>
const T *load(const std::string &name);
private:
std::map<std::string, Resource*> _resources;
};
template <class T>
const T *Store::load(const std::string &name)
{
static_assert(std::is_convertible<T, Resource>::value
&& !std::is_same<T, Resource>::value,
"Store can load only Resource's children");
if (_resources.find(name) == _resources.end())
_resources[T::filename(name)] = new T(name);
_resources[T::filename(name)]->_ref_count++;
return (T*)_resources[T::filename(name)];
}
#endif // _STORE_HPP_

View File

@ -4,9 +4,14 @@
#include <SDL_image.h>
const std::string Texture::filename(const std::string &name)
{
return "/data/textures/" + name;
}
Texture::Texture(const std::string &name)
{
SDL_Surface *surface = IMG_Load(("/data/textures/" + name).c_str());
SDL_Surface *surface = IMG_Load(filename(name).c_str());
glGenTextures(1, &_id);
glBindTexture(GL_TEXTURE_2D, _id);

View File

@ -1,14 +1,22 @@
#ifndef _TEXTURE_HPP_
#define _TEXTURE_HPP_
#include "store.hpp"
#include "gl.hpp"
#include <string>
class Texture
class Texture:
public Store::Resource
{
public:
friend class Store;
Texture(const std::string &name);
static const std::string filename(const std::string &name);
public:
void use() const;
private: