Brenta Engine 1.2
Loading...
Searching...
No Matches
material.hpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <brenta/renderer/opengl/shader.hpp>
9
10#include <glm/glm.hpp>
11
12#include <memory>
13#include <unordered_map>
14
15namespace brenta
16{
17
18class Texture;
19
20//
21// Material
22// --------
23//
24// A material stores a shader and the values of its uniforms. The
25// renderer uses the material to set which shader program should be
26// used, and which uniform values.
27//
28// Use `apply()` to set the shader and its uniforms.
29//
31{
32public:
33
34 class Builder
35 {
36 public:
37
38 Builder& shader(tenno::shared_ptr<Shader> shader);
39 Builder& integer(const std::string &name, int val);
40 Builder& floating(const std::string &name, float val);
41 Builder& vector(const std::string &name, glm::vec3 val);
42 Builder& texture(const std::string &name,
43 tenno::shared_ptr<Texture> val, int index);
44
45 // Add path to be watched for hot-reloading
46 Builder &watch(const std::filesystem::path &path);
47
48 Material build();
49 tenno::vector<std::filesystem::path> get_watch_paths() const;
50
51 private:
52
53 tenno::shared_ptr<Shader> _shader;
54 tenno::vector<std::pair<std::string, int>> ints;
55 tenno::vector<std::pair<std::string, float>> floats;
56 tenno::vector<std::pair<std::string, glm::vec3>> vectors;
57 tenno::vector<std::pair<std::string,
58 std::pair<int, tenno::shared_ptr<Texture>>>> textures;
59
60 };
61
62 tenno::shared_ptr<Shader> shader;
63
64 Material() = default;
65 Material(tenno::shared_ptr<Shader> s) : shader(s) {}
66 Material(Shader&& s);
67 Material(Material&& other) = default;
69 {
70 *this = builder.build();
71 }
72 Material &operator=(Material&& other) = default;
73
74 void apply();
75
76 Material &set_int(const std::string &name, int val);
77 Material &set_float(const std::string &name, float val);
78 Material &set_vector(const std::string &name, glm::vec3 val);
79 Material &set_texture(const std::string &name,
80 tenno::shared_ptr<Texture> val, int index);
81
82private:
83
84 std::unordered_map<std::string, int> ints;
85 std::unordered_map<std::string, float> floats;
86 std::unordered_map<std::string, glm::vec3> vectors;
87 std::unordered_map<std::string,
88 std::pair<int, tenno::shared_ptr<Texture>>> textures;
89
90};
91
92} // namespace brenta