-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c8a144
commit d941bb9
Showing
5 changed files
with
285 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <unordered_map> | ||
#include <string> | ||
#include <type_traits> | ||
|
||
#include "external/glm/glm.hpp" | ||
|
||
namespace hippo::graphics | ||
{ | ||
class Shader; | ||
class Texture; | ||
class Material | ||
{ | ||
public: | ||
Material(std::shared_ptr<Shader> shader, std::shared_ptr<Texture> texture = nullptr); | ||
Material(const Material& other); | ||
~Material(); | ||
|
||
inline Shader* GetShader() const { return mShader.get(); } | ||
inline Texture* GetTexture() const { return mTexture.get(); } | ||
|
||
void SetShader(std::shared_ptr<Shader> shader); | ||
void SetTexture(std::shared_ptr<Texture> texture); | ||
void UpdateShaderUniforms(); | ||
|
||
#define GETUNIFORMVALUE(mapName, defaultReturn) \ | ||
const auto& it = mapName.find(name);\ | ||
if (it != mapName.end())\ | ||
{\ | ||
return it->second;\ | ||
}\ | ||
return defaultReturn; | ||
|
||
template<typename T> | ||
inline T GetUniformValue(const std::string& name) const | ||
{ | ||
if constexpr (std::is_same<T, int>()) { GETUNIFORMVALUE(mUniformInts, 0) } | ||
else if constexpr (std::is_same<T, float>()) { GETUNIFORMVALUE(mUniformFloats, 0.f) } | ||
else if constexpr (std::is_same<T, glm::vec2>()) { GETUNIFORMVALUE(mUniformFloat2s, glm::vec2(0.f)) } | ||
else if constexpr (std::is_same<T, glm::vec3>()) { GETUNIFORMVALUE(mUniformFloat3s, glm::vec3(0.f)) } | ||
else if constexpr (std::is_same<T, glm::vec4>()) { GETUNIFORMVALUE(mUniformFloat4s, glm::vec4(0.f)) } | ||
else if constexpr (std::is_same<T, glm::mat3>()) { GETUNIFORMVALUE(mUniformMat3s, glm::mat3(1.f)) } | ||
else if constexpr (std::is_same<T, glm::mat4>()) { GETUNIFORMVALUE(mUniformMat4s, glm::mat4(1.f)) } | ||
else | ||
{ | ||
static_assert(false, "Unsupported data type in Material::GetUniformValue()"); | ||
} | ||
} | ||
#undef GETUNIFORMVALUE | ||
|
||
template<typename T> | ||
inline void SetUniformValue(const std::string& name, const T& val) | ||
{ | ||
if constexpr (std::is_same<T, int>()) { mUniformInts[name] = val; } | ||
else if constexpr (std::is_same<T, float>()) { mUniformFloats[name] = val; } | ||
else if constexpr (std::is_same<T, glm::vec2>()) { mUniformFloat2s[name] = val; } | ||
else if constexpr (std::is_same<T, glm::vec3>()) { mUniformFloat3s[name] = val; } | ||
else if constexpr (std::is_same<T, glm::vec4>()) { mUniformFloat4s[name] = val; } | ||
else if constexpr (std::is_same<T, glm::mat3>()) { mUniformMat3s[name] = val; } | ||
else if constexpr (std::is_same<T, glm::mat4>()) { mUniformMat4s[name] = val; } | ||
else | ||
{ | ||
static_assert(false, "Unsupported data type in Material::SetUniformValue()"); | ||
} | ||
} | ||
|
||
private: | ||
std::shared_ptr<Shader> mShader; | ||
std::shared_ptr<Texture> mTexture; | ||
|
||
// Data | ||
std::unordered_map<std::string, int> mUniformInts; | ||
std::unordered_map<std::string, float> mUniformFloats; | ||
std::unordered_map<std::string, glm::vec2> mUniformFloat2s; | ||
std::unordered_map<std::string, glm::vec3> mUniformFloat3s; | ||
std::unordered_map<std::string, glm::vec4> mUniformFloat4s; | ||
std::unordered_map<std::string, glm::mat3> mUniformMat3s; | ||
std::unordered_map<std::string, glm::mat4> mUniformMat4s; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include "hippo/graphics/material.h" | ||
#include "hippo/graphics/shader.h" | ||
|
||
#include "hippo/log.h" | ||
|
||
namespace hippo::graphics | ||
{ | ||
|
||
Material::Material(std::shared_ptr<Shader> shader, std::shared_ptr<Texture> texture /*= nullptr*/) | ||
: mShader(shader) | ||
, mTexture(texture) | ||
{ | ||
HIPPO_ASSERT(mShader, "Attempting to instantiate a material with a nullptr shader"); | ||
} | ||
|
||
Material::Material(const Material& other) | ||
{ | ||
mShader = other.mShader; | ||
mTexture = other.mTexture; | ||
|
||
// Data | ||
mUniformInts = other.mUniformInts; | ||
mUniformFloats = other.mUniformFloats; | ||
mUniformFloat2s = other.mUniformFloat2s; | ||
mUniformFloat3s = other.mUniformFloat3s; | ||
mUniformFloat4s = other.mUniformFloat4s; | ||
mUniformMat3s = other.mUniformMat3s; | ||
mUniformMat4s = other.mUniformMat4s; | ||
} | ||
|
||
Material::~Material() {} | ||
|
||
void Material::SetShader(std::shared_ptr<Shader> shader) | ||
{ | ||
HIPPO_ASSERT(shader, "Attempting to set a nullptr shader"); | ||
if (shader) | ||
{ | ||
mShader = shader; | ||
} | ||
} | ||
|
||
void Material::SetTexture(std::shared_ptr<Texture> texture) | ||
{ | ||
mTexture = texture; | ||
} | ||
|
||
void Material::UpdateShaderUniforms() | ||
{ | ||
if (mShader) | ||
{ | ||
for (const auto& it : mUniformInts) | ||
{ | ||
mShader->SetUniformInt(it.first, it.second); | ||
} | ||
for (const auto& it : mUniformFloats) | ||
{ | ||
mShader->SetUniformFloat(it.first, it.second); | ||
} | ||
for (const auto& it : mUniformFloat2s) | ||
{ | ||
mShader->SetUniformFloat2(it.first, it.second); | ||
} | ||
for (const auto& it : mUniformFloat3s) | ||
{ | ||
mShader->SetUniformFloat3(it.first, it.second); | ||
} | ||
for (const auto& it : mUniformFloat4s) | ||
{ | ||
mShader->SetUniformFloat4(it.first, it.second); | ||
} | ||
for (const auto& it : mUniformMat3s) | ||
{ | ||
mShader->SetUniformMat3(it.first, it.second); | ||
} | ||
for (const auto& it : mUniformMat4s) | ||
{ | ||
mShader->SetUniformMat4(it.first, it.second); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.