MxEngine
Texture.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include "Utilities/STL/MxString.h"
32 #include "Utilities/STL/MxVector.h"
33 #include "Utilities/Math/Math.h"
34 #include "Utilities/Image/Image.h"
35 
36 namespace MxEngine
37 {
38  enum class TextureFormat : uint8_t
39  {
40  RGB,
41  RGBA,
42  RGB16,
43  RGB16F,
44  RGBA16,
45  RGBA16F,
46  RGB32F,
47  RGBA32F,
48  DEPTH,
49  };
50 
51  enum class TextureWrap : uint8_t
52  {
53  CLAMP_TO_EDGE,
54  CLAMP_TO_BORDER,
55  MIRRORED_REPEAT,
56  REPEAT,
57  };
58 
59  const char* EnumToString(TextureFormat format);
60  const char* EnumToString(TextureWrap wrap);
61 
62  class ImageData
63  {
64  uint8_t* data;
65  };
66 
67  class Texture
68  {
69  using BindableId = unsigned int;
70 
71  MxString filepath;
72  size_t width = 0, height = 0, channels = 0;
73  BindableId id = 0;
74  mutable BindableId activeId = 0;
75  unsigned int textureType = 0;
76  TextureFormat format = TextureFormat::RGB;
77  TextureWrap wrapType = TextureWrap::REPEAT;
78  uint8_t samples = 0;
79 
80  void FreeTexture();
81  public:
82  using RawData = uint8_t;
83  using RawDataPointer = RawData*;
84  using TextureBindId = BindableId;
85 
86  Texture();
87  Texture(const Texture&) = delete;
88  Texture(Texture&& texture) noexcept;
89  Texture& operator=(const Texture& texture) = delete;
90  Texture& operator=(Texture&& texture) noexcept;
91  Texture(const MxString& filepath, TextureWrap wrap = TextureWrap::REPEAT, bool genMipmaps = true, bool flipImage = true);
92  ~Texture();
93 
94  void Bind() const;
95  void Unbind() const;
96  BindableId GetNativeHandle() const;
97  void Load(const MxString& filepath, TextureWrap wrap = TextureWrap::REPEAT, bool genMipmaps = true, bool flipImage = true);
98  void Load(RawDataPointer data, int width, int height, TextureFormat format = TextureFormat::RGB, TextureWrap wrap = TextureWrap::REPEAT, bool genMipmaps = true);
99  void LoadMipmaps(RawDataPointer* data, size_t mipmaps, int biggestWidth, int biggestHeight, TextureWrap wrap = TextureWrap::REPEAT);
100  void LoadDepth(int width, int height, TextureWrap wrap = TextureWrap::CLAMP_TO_BORDER);
101  void LoadMultisample(int width, int height, TextureFormat format, int samples, TextureWrap wrap = TextureWrap::REPEAT);
102  Image GetRawTextureData() const;
103  void GenerateMipmaps();
104  void SetBorderColor(const Vector3& color);
105  bool IsMultisampled() const;
106  bool IsFloatingPoint() const;
107  bool IsDepthOnly() const;
108  int GetSampleCount() const;
109  size_t GetPixelSize() const;
110  TextureFormat GetFormat() const;
111  TextureWrap GetWrapType() const;
112  void Bind(TextureBindId id) const;
113  const MxString& GetPath() const;
114  unsigned int GetTextureType() const;
115  size_t GetWidth() const;
116  size_t GetHeight() const;
117  size_t GetChannelCount() const;
118  };
119 }
Definition: Texture.h:62
Definition: Image.h:38
Definition: Texture.h:67
Definition: Application.cpp:49