| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- //
- // material.cpp
- // graphics
- //
- // Created by Sam Jaffe on 7/5/16.
- //
- #include "game/graphics/material.hpp"
- #include "game/graphics/texture.hpp"
- #include "helper.hpp"
- using namespace graphics;
- namespace {
- using key_t = std::tuple<flyweight<shader_program>, std::string, std::string>;
- std::unordered_map<key_t, flyweight<material>> g_materials;
- }
- static math::vec2i ZERO{{0, 0}};
- flyweight<texture> get_texture(std::string const & texture,
- std::string const & uniform) {
- if (!texture.empty()) {
- try {
- return texture::create(texture);
- } catch (std::exception const & e) {
- // TODO: Logging
- }
- }
- if (uniform == "u_normalMap") {
- return texture::LIGHT_BLUE;
- } else if (uniform == "u_specularMap") {
- return texture::DARK_YELLOW;
- } else if (uniform == "u_diffuseMap") {
- return texture::WHITE;
- }
- throw;
- }
- flyweight<material> material::create(shader_program const & sp,
- std::string const & texture,
- std::string const & uniform) {
- key_t key = std::make_tuple(sp, texture, uniform);
- auto found = g_materials.find(key);
- if (found != g_materials.end()) { return found->second; }
- static unsigned int id{0};
- material mat{++id, sp};
- mat.uniforms.push_back({get_texture(texture, uniform),
- shaders::uniform_location(sp.id, uniform)});
- flyweight<material> fly{id, mat};
- return g_materials.emplace(key, fly).first->second;
- }
- material::material(unsigned int id, shader_program const & sp)
- : identity<material>(id), shaders(sp) {}
- math::vec2i material::size() const {
- return uniforms.empty() ? ZERO : uniforms.front().texture.actual().size;
- }
|