|
|
@@ -6,3 +6,45 @@
|
|
|
//
|
|
|
|
|
|
#include "game/graphics/material.hpp"
|
|
|
+
|
|
|
+#include "game/graphics/texture.hpp"
|
|
|
+#include "helper.hpp"
|
|
|
+
|
|
|
+using namespace graphics;
|
|
|
+
|
|
|
+namespace {
|
|
|
+ using key_t = std::pair<shaders::type, std::string>;
|
|
|
+ std::unordered_map<std::string, material> g_materials;
|
|
|
+}
|
|
|
+
|
|
|
+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) {
|
|
|
+ static unsigned int id{0};
|
|
|
+ material mat{++id, sp};
|
|
|
+ mat.uniforms.push_back({get_texture(texture, uniform),
|
|
|
+ shaders::uniform_location(sp.id, uniform)});
|
|
|
+ return g_materials.emplace("", std::move(mat)).first->second;
|
|
|
+}
|
|
|
+
|
|
|
+material::material(unsigned int id, shader_program const & sp)
|
|
|
+ : identity<material>(id), shaders(sp) {}
|