File: Texture.h

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (190 lines) | stat: -rw-r--r-- 6,208 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once

#include "../Base/Object.h"
#include "../Primitives/Rect.h"
#include "../Primitives/Color.h"
#include "../Primitives/Colorf.h"

#include <memory>

#include <Containers/StringView.h>

using namespace Death::Containers;

namespace nCine
{
	class ITextureLoader;
	class GLTexture;

	/// Texture filtering modes
	enum class SamplerFilter
	{
		Unknown,

		Nearest,
		Linear,
		NearestMipmapNearest,
		LinearMipmapNearest,
		NearestMipmapLinear,
		LinearMipmapLinear
	};

	/// Texture wrap modes
	enum class SamplerWrapping
	{
		Unknown,

		ClampToEdge,
		MirroredRepeat,
		Repeat
	};

	/// Texture
	class Texture : public Object
	{
		friend class Material;
		friend class Viewport;

	public:
		/// Texture formats
		enum class Format
		{
			Unknown,

			R8,
			RG8,
			RGB8,
			RGBA8
		};

		/// Creates an OpenGL texture name
		Texture();

		/// Creates an empty texture with the specified format, MIP levels, and size
		Texture(const char* name, Format format, std::int32_t mipMapCount, std::int32_t width, std::int32_t height);
		/// Creates an empty texture with the specified format, MIP levels, and size using a vector
		Texture(const char* name, Format format, std::int32_t mipMapCount, Vector2i size);
		/// Creates an empty texture with the specified format and size
		Texture(const char* name, Format format, std::int32_t width, std::int32_t height);
		/// Creates an empty texture with the specified format and size using a vector
		Texture(const char* name, Format format, Vector2i size);

		/// Creates a texture from an image file
		explicit Texture(StringView filename);

		~Texture() override;

		Texture(const Texture&) = delete;
		Texture& operator=(const Texture&) = delete;
		Texture(Texture&&);
		Texture& operator=(Texture&&);

		/// Initializes an empty texture with the specified format, MIP levels, and size
		void Init(const char* name, Format format, std::int32_t mipMapCount, std::int32_t width, std::int32_t height);
		/// Initializes an empty texture with the specified format, MIP levels, and size using a vector
		void Init(const char* name, Format format, std::int32_t mipMapCount, Vector2i size);
		/// Initializes an empty texture with the specified format and size
		void Init(const char* name, Format format, std::int32_t width, std::int32_t height);
		/// Initializes an empty texture with the specified format and size using a vector
		void Init(const char* name, Format format, Vector2i size);

		//bool loadFromMemory(const std::uint8_t* bufferPtr, unsigned long int bufferSize);
		bool LoadFromFile(StringView filename);

		/// Loads all texture texels in raw format from a memory buffer in the first mip level
		bool LoadFromTexels(const std::uint8_t* bufferPtr);
		/// Loads texels in raw format from a memory buffer to a texture sub-region in the first mip level
		bool LoadFromTexels(const std::uint8_t* bufferPtr, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height);
		/// Loads texels in raw format from a memory buffer to a texture sub-region with a rectangle in the first mip level
		bool LoadFromTexels(const std::uint8_t* bufferPtr, Recti region);
		/// Loads texels in raw format from a memory buffer to a specific texture mip level and sub-region
		bool LoadFromTexels(const std::uint8_t* bufferPtr, std::int32_t level, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height);
		/// Loads texels in raw format from a memory buffer to a specific texture mip level and sub-region with a rectangle
		bool LoadFromTexels(const std::uint8_t* bufferPtr, std::int32_t level, Recti region);

		/// Saves all texture texels in the first mip level in raw format to a memory buffer
		bool SaveToMemory(std::uint8_t* bufferPtr);
		/// Saves all texture texels in the specified texture mip level in raw format to a memory buffer
		bool SaveToMemory(std::uint8_t* bufferPtr, std::int32_t level);

		/// Returns texture width
		inline std::int32_t GetWidth() const {
			return width_;
		}
		/// Returns texture height
		inline std::int32_t GetHeight() const {
			return height_;
		}
		/// Returns texture MIP map levels
		inline std::int32_t GetMipMapLevels() const {
			return mipMapLevels_;
		}
		/// Returns texture size
		inline Vector2i GetSize() const {
			return Vector2i(width_, height_);
		}
		/// Returns texture rectangle
		inline Recti GetRect() const {
			return Recti(0, 0, width_, height_);
		}

		/// Returns `true` if the texture holds compressed data
		inline bool IsCompressed() const {
			return isCompressed_;
		}
		/// Returns the number of color channels
		std::uint32_t GetChannelCount() const;
		/// Returns the amount of video memory needed to load the texture
		inline std::uint32_t GetDataSize() const {
			return dataSize_;
		}

		/// Returns the texture filtering for minification
		inline SamplerFilter GetMinFiltering() const {
			return minFiltering_;
		}
		/// Returns the texture filtering for magnification
		inline SamplerFilter GetMagFiltering() const {
			return magFiltering_;
		}
		/// Returns texture wrap for both `s` and `t` coordinates
		inline SamplerWrapping GetWrap() const {
			return wrapMode_;
		}
		/// Sets the texture filtering for minification
		void SetMinFiltering(SamplerFilter filter);
		/// Sets the texture filtering for magnification
		void SetMagFiltering(SamplerFilter filter);
		/// Sets texture wrap for both `s` and `t` coordinates
		void SetWrap(SamplerWrapping wrapMode);

		/// Sets the OpenGL object label for the texture
		void SetGLTextureLabel(const char* label);

		/// Returns the user data opaque pointer for ImGui's `ImTextureID`
		void* GetGuiTexId() const;

		inline static ObjectType sType() {
			return ObjectType::Texture;
		}

	private:
		std::unique_ptr<GLTexture> glTexture_;
		std::int32_t width_;
		std::int32_t height_;
		std::int32_t mipMapLevels_;
		bool isCompressed_;
		Format format_;
		std::uint32_t dataSize_;

		SamplerFilter minFiltering_;
		SamplerFilter magFiltering_;
		SamplerWrapping wrapMode_;

		/// Initialize an empty texture by creating storage for it
		void Initialize(const ITextureLoader& texLoader);
		/// Loads the data in a previously initialized texture
		void Load(const ITextureLoader& texLoader);
	};

}