Raito 0.1
Loading...
Searching...
No Matches
OpenGLMaterial.h
Go to the documentation of this file.
1/*
2MIT License
3
4Copyright (c) 2023 Víctor Falcón Zaro
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#pragma once
26
28#include <utility>
29
30
32{
34 {
35 UniformValue() = default;
36 UniformValue(ShaderValue data, ubyte* value, size_t size)
37 : Data(data), Value(value), Size(size){}
38
40 {
41 delete[] Value;
42 }
43
45 {
46 Size = rhs.Size;
47 Data = rhs.Data;
48
49 Value = new ubyte[Size];
50 memcpy(Value, rhs.Value, Size);
51 }
52
54 {
55 if(this == &rhs)
56 {
57 return *this;
58 }
59
60 ubyte* tmp = new ubyte[rhs.Size];
61 memcpy(tmp, rhs.Value, rhs.Size);
62
63 delete[] Value;
64
65 Value = tmp;
66 Size = rhs.Size;
67 Data = rhs.Data;
68
69 return *this;
70 }
71
72 UniformValue(UniformValue&& rhs) noexcept
73 : Data(rhs.Data), Value(rhs.Value), Size(rhs.Size)
74 {
75 rhs.Value = nullptr;
76 }
77
79 {
80 if(this == &rhs)
81 {
82 return *this;
83 }
84
85 delete[] Value;
86 Value = rhs.Value;
87 rhs.Value = nullptr;
88
89 Size = rhs.Size;
90 Data = rhs.Data;
91
92 return *this;
93 }
94
96 ubyte* Value = nullptr;
97 size_t Size = 0;
98
99 };
100
101 class OpenGLMaterial final
102 {
103 public:
104 explicit OpenGLMaterial(u32 shaderId);
105 ~OpenGLMaterial() = default;
106
108 {
109 m_ShaderId = rhs.m_ShaderId;
110 m_Uniforms.clear();
111 for(const auto& uniform : rhs.m_Uniforms)
112 {
113 m_Uniforms[uniform.first] = uniform.second;
114 }
115 }
116
118 {
119 if (this != &rhs)
120 {
121 m_ShaderId = rhs.m_ShaderId;
122 m_Uniforms.clear();
123
124 for (const auto& pair : rhs.m_Uniforms)
125 {
126 m_Uniforms[pair.first] = pair.second;
127 }
128 }
129 return *this;
130 }
131
133 {
134 m_ShaderId = rhs.m_ShaderId;
135 m_Uniforms = std::move(rhs.m_Uniforms);
136 rhs.m_ShaderId = 0; // Or any other invalid value
137 }
138
140 {
141 if (this != &rhs)
142 {
143 m_ShaderId = rhs.m_ShaderId;
144 m_Uniforms = std::move(rhs.m_Uniforms);
145 rhs.m_ShaderId = 0; // Or any other invalid value
146 }
147 return *this;
148 }
149
150 void Bind();
151 void UnBind() const;
152
153 void SetValuePtr(const char* name, ubyte* value, size_t size)
154 {
155 if (!m_Uniforms.contains(name))
156 {
157 return;
158 }
159 memcpy(m_Uniforms[name].Value, value, size);
160 }
161
162 template<typename T>
163 void SetValue(const char* name, const T value)
164 {
165 if(!m_Uniforms.contains(name))
166 {
167 return;
168 }
169 memcpy(m_Uniforms[name].Value, &value, sizeof(T));
170 }
171
172
173 NODISCARD const ShaderValue& GetUniformValue(const std::string& name) { return m_Uniforms[name].Data; }
174 private:
175 u32 m_ShaderId;
176 std::unordered_map<std::string, UniformValue> m_Uniforms{};
177 };
178}
unsigned char ubyte
Unsigned byte type.
Definition BasicTypes.h:31
unsigned __int32 u32
Unsigned integer 32 bit type.
Definition BasicTypes.h:55
#define NODISCARD
Definition Common.h:80
Definition OpenGLMaterial.h:102
void Bind()
Definition OpenGLMaterial.cpp:44
NODISCARD const ShaderValue & GetUniformValue(const std::string &name)
Definition OpenGLMaterial.h:173
OpenGLMaterial(OpenGLMaterial const &rhs)
Definition OpenGLMaterial.h:107
void SetValuePtr(const char *name, ubyte *value, size_t size)
Definition OpenGLMaterial.h:153
OpenGLMaterial(OpenGLMaterial &&rhs) noexcept
Definition OpenGLMaterial.h:132
OpenGLMaterial(u32 shaderId)
Definition OpenGLMaterial.cpp:32
void UnBind() const
Definition OpenGLMaterial.cpp:96
void SetValue(const char *name, const T value)
Definition OpenGLMaterial.h:163
OpenGLMaterial & operator=(OpenGLMaterial &&rhs) noexcept
Definition OpenGLMaterial.h:139
OpenGLMaterial & operator=(const OpenGLMaterial &rhs)
Definition OpenGLMaterial.h:117
Definition OpenGLCommon.h:47
Definition OpenGLShader.h:63
Definition OpenGLMaterial.h:34
ubyte * Value
Definition OpenGLMaterial.h:96
UniformValue(UniformValue &&rhs) noexcept
Definition OpenGLMaterial.h:72
ShaderValue Data
Definition OpenGLMaterial.h:95
UniformValue & operator=(UniformValue &&rhs) noexcept
Definition OpenGLMaterial.h:78
UniformValue & operator=(UniformValue const &rhs)
Definition OpenGLMaterial.h:53
UniformValue(ShaderValue data, ubyte *value, size_t size)
Definition OpenGLMaterial.h:36
UniformValue(UniformValue const &rhs)
Definition OpenGLMaterial.h:44
~UniformValue()
Definition OpenGLMaterial.h:39
size_t Size
Definition OpenGLMaterial.h:97