Lugdunum  0.1.0
Texture.cpp
Go to the documentation of this file.
2 
3 #if defined(LUG_SYSTEM_WINDOWS)
4  #pragma warning(push)
5  #pragma warning(disable : 4244)
6  #pragma warning(disable : 4456)
7 
8 #endif
9 #define STB_IMAGE_IMPLEMENTATION
10 #include <stb_image.h>
11 #if defined(LUG_SYSTEM_WINDOWS)
12  #pragma warning(pop)
13 #endif
14 
15 #if defined(LUG_SYSTEM_ANDROID)
16  #include <android/asset_manager.h>
17 
19  #include <lug/Window/Window.hpp>
20 #endif
21 
23 
24 namespace lug {
25 namespace Graphics {
26 namespace Builder {
27 
28 Texture::Texture(Renderer& renderer) : _renderer(renderer) {}
29 
31  for (auto& layer : _layers) {
32  delete[] layer.data;
33  layer.data = nullptr;
34  }
35 }
36 
38  Resource::SharedPtr<Render::Texture> texture{nullptr};
39 
40  switch (_renderer.getType()) {
43  }
44 
45  if (texture) {
46  texture->_width = _width;
47  texture->_height = _height;
48 
49  texture->_format = _format;
50 
51  texture->_magFilter = _magFilter;
52  texture->_minFilter = _minFilter;
53  texture->_mipMapFilter = _mipMapFilter;
54  texture->_wrapS = _wrapS;
55  texture->_wrapT = _wrapT;
56  }
57 
58  return texture;
59 }
60 
61 bool Texture::addLayer(uint32_t width, uint32_t height, Render::Texture::Format format, const unsigned char* data) {
62  if (!_width && !_height) {
63  _width = width;
64  _height = height;
65  } else if (width != _width || height != _height) {
66  return false;
67  }
68 
69  if (format == Render::Texture::Format::Undefined) {
70  return false;
72  _format = format;
73  } else if (_format != format) {
74  return false;
75  }
76 
77  if (!data) {
78  _layers.push_back({data});
79  return true;
80  }
81 
82  const size_t sizePixel = Render::Texture::formatToSize(format);
83 
84  unsigned char* newData = new unsigned char[width * height * sizePixel];
85 
86  if (!newData) {
87  return false;
88 
89  }
90  std::memcpy(newData, data, width * height * sizePixel);
91 
92  _layers.push_back({newData});
93 
94  return true;
95 }
96 
97 
98 bool Texture::addLayer(const std::string& filename, bool hdr) {
99  int texWidth{0};
100  int texHeight{0};
101  int texChannels{0};
102 
103  stbi_uc* pixels = nullptr;
104 
105 #if defined(LUG_SYSTEM_ANDROID)
106  // Load shader from compressed asset
107  AAsset* asset = AAssetManager_open((lug::Window::priv::WindowImpl::activity)->assetManager, filename.c_str(), AASSET_MODE_STREAMING);
108 
109  if (!asset) {
110  LUG_LOG.error("Vulkan::Texture::build: Can't open Android asset \"{}\"", filename);
111  return false;
112  }
113 
114  uint32_t size = AAsset_getLength(asset);
115 
116  if (size <= 0) {
117  LUG_LOG.error("Vulkan::Texture::build: Android asset \"{}\" is empty", filename);
118  return false;
119  }
120 
121  unsigned char* data = new unsigned char[size];
122 
123  AAsset_read(asset, reinterpret_cast<char*>(data), size);
124  AAsset_close(asset);
125 
126  if (hdr) {
127  pixels = reinterpret_cast<unsigned char*>(stbi_loadf_from_memory(data, size, &texWidth, &texHeight, &texChannels, STBI_rgb_alpha));
128  } else {
129  pixels = stbi_load_from_memory(data, size, &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
130  }
131 
132  delete[] data;
133 #else
134  if (hdr) {
135  pixels = reinterpret_cast<unsigned char*>(stbi_loadf(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha));
136  } else {
137  pixels = stbi_load(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
138  }
139 #endif
140 
141  if (!pixels) {
142  return false;
143  }
144 
146  if (!addLayer(texWidth, texHeight, format, pixels)) {
147  stbi_image_free(pixels);
148  return false;
149  }
150 
151  stbi_image_free(pixels);
152  return true;
153 }
154 
155 } // Builder
156 } // Graphics
157 } // lug
Render::Texture::WrappingMode _wrapT
Definition: Texture.hpp:81
Render::Texture::Format _format
Definition: Texture.hpp:72
Render::Texture::Filter _minFilter
Definition: Texture.hpp:77
Render::Texture::Filter _magFilter
Definition: Texture.hpp:76
Resource::SharedPtr< lug::Graphics::Render::Texture > build(const ::lug::Graphics::Builder::Texture &builder)
Definition: Texture.cpp:17
static size_t formatToSize(Render::Texture::Format format)
Definition: Texture.inl:33
Dummy class for a shared pointer.
Definition: Resource.hpp:66
Texture(Renderer &renderer)
Definition: Texture.cpp:28
bool addLayer(const std::string &filename, bool hdr=false)
Definition: Texture.cpp:98
static ANativeActivity * activity
Render::Texture::WrappingMode _wrapS
Definition: Texture.hpp:80
#define LUG_LOG
Definition: Logger.hpp:73
Type getType() const
Definition: Renderer.inl:5
Resource::SharedPtr< Render::Texture > build()
Definition: Texture.cpp:37
std::vector< Layer > _layers
Definition: Texture.hpp:84
Render::Texture::Filter _mipMapFilter
Definition: Texture.hpp:78