Elevate Engine 1
Loading...
Searching...
No Matches
Texture.cpp
Go to the documentation of this file.
1#include "eepch.h"
2
4
5#include <filesystem>
6#include <glm/common.hpp>
7
12
13namespace Elevate
14{
15 bool Texture::MatchesPath(std::string pathToMatch)
16 {
17 std::filesystem::path currentPath = std::filesystem::absolute(m_meta.Path).lexically_normal();
18 std::filesystem::path otherPath = std::filesystem::absolute(pathToMatch).lexically_normal();
19 return currentPath == otherPath;
20 }
21
22 TexturePtr Texture::CreateFromFile(const std::string& path, TextureType usage)
23 {
24 return TextureManager::LoadTextureAsync(path, usage);
25 }
26
27 TexturePtr Texture::CreateFromColor(const glm::vec3& color, const std::string& name, uint32_t width, uint32_t height)
28 {
29 size_t size = 3 * width * height;
30 std::vector<unsigned char> pixels(size);
31
32 unsigned char r = static_cast<unsigned char>(color.r);
33 unsigned char g = static_cast<unsigned char>(color.g);
34 unsigned char b = static_cast<unsigned char>(color.b);
35
36 for (int i = 0; i < size; i += 3)
37 {
38 pixels[i + 0] = r;
39 pixels[i + 1] = g;
40 pixels[i + 2] = b;
41 }
42
44 .Name(name)
45 .Path("")
46 .size(width, height)
51 .Build();
52
53 return CreateFromData(pixels.data(), meta);
54 }
55
56 TexturePtr Texture::CreateFromColor(const glm::vec4& color, const std::string& name, uint32_t width, uint32_t height)
57 {
58 size_t size = 4 * width * height;
59 std::vector<unsigned char> pixels(size);
60
61 unsigned char r = static_cast<unsigned char>(glm::clamp(color.r, 0.0f, 1.0f) * 255.0f);
62 unsigned char g = static_cast<unsigned char>(glm::clamp(color.g, 0.0f, 1.0f) * 255.0f);
63 unsigned char b = static_cast<unsigned char>(glm::clamp(color.b, 0.0f, 1.0f) * 255.0f);
64 unsigned char a = static_cast<unsigned char>(glm::clamp(color.a, 0.0f, 1.0f) * 255.0f);
65
66 for (int i = 0; i < size; i += 4)
67 {
68 pixels[i + 0] = r;
69 pixels[i + 1] = g;
70 pixels[i + 2] = b;
71 pixels[i + 3] = a;
72 }
73
75 .Name(name)
76 .Path("")
77 .size(width, height)
82 .Build();
83
84 return CreateFromData(pixels.data(), meta);
85 }
86
88 {
89 TexturePtr texture;
90 switch (Renderer::GetAPI())
91 {
93 texture = nullptr;
94 break;
96 texture = std::make_shared<OpenGLTexture>(data, meta);
97 break;
98 default:
99 EE_CORE_ASSERT(false, "A supported RendererAPI needs to be supported!");
100 return nullptr;
101 break;
102 }
103
104 return TextureManager::RegisterTexture(texture);
105 }
106}
static RendererAPI::GraphicAPI GetAPI()
Definition Renderer.h:24
static TexturePtr RegisterTexture(TexturePtr texture)
static TexturePtr LoadTextureAsync(const std::string &path, TextureType usage=TextureType::Diffuse)
bool MatchesPath(std::string pathToMatch)
Definition Texture.cpp:15
TextureMetadata m_meta
Definition Texture.h:133
static TexturePtr CreateFromData(unsigned char *data, TextureMetadata &meta)
Definition Texture.cpp:87
static TexturePtr CreateFromColor(const glm::vec3 &color, const std::string &name, uint32_t width=1, uint32_t height=1)
Definition Texture.cpp:27
static TexturePtr CreateFromFile(const std::string &path, TextureType usage=TextureType::Diffuse)
Definition Texture.cpp:22
TextureType
Definition Texture.h:29
std::shared_ptr< Texture > TexturePtr
Definition Texture.h:13
TextureMetadataBuilder & Format(const TextureFormat fmt)
Definition Texture.h:82
TextureMetadata Build()
Definition Texture.h:90
TextureMetadataBuilder & Source(const TextureSource src)
Definition Texture.h:84
TextureMetadataBuilder & Usage(const TextureType type)
Definition Texture.h:83
TextureMetadataBuilder & Name(const std::string name)
Definition Texture.h:79
TextureMetadataBuilder & size(const uint32_t w, const uint32_t h)
Definition Texture.h:81
TextureMetadataBuilder & State(const TextureState state)
Definition Texture.h:85
TextureMetadataBuilder & Path(const std::string &path)
Definition Texture.h:80