galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Texture.cpp
Go to the documentation of this file.
1
7
9#include "galaxy/utils/Globals.hpp"
10
11#include "Texture.hpp"
12
13#ifdef GALAXY_WIN_PLATFORM
14#pragma warning(push)
15#pragma warning(disable : 26493)
16#endif
17
18namespace galaxy
19{
20 namespace graphics
21 {
23 : m_id {0}
24 , m_width {0}
25 , m_height {0}
26 , m_mode {TextureMode::CLAMP_TO_EDGE}
27 , m_filter {TextureFilter::NEAREST}
28 , m_anisotropy {0}
29 {
30 }
31
33 {
34 if (this->m_id != 0)
35 {
36 glDeleteTextures(1, &this->m_id);
37 }
38
39 this->m_id = t.m_id;
40 this->m_width = t.m_width;
41 this->m_height = t.m_height;
42 this->m_filter = t.m_filter;
43 this->m_mode = t.m_mode;
44 this->m_anisotropy = t.m_anisotropy;
45
46 t.m_id = 0;
47 }
48
50 {
51 if (this != &t)
52 {
53 if (this->m_id != 0)
54 {
55 glDeleteTextures(1, &this->m_id);
56 }
57
58 this->m_id = t.m_id;
59 this->m_width = t.m_width;
60 this->m_height = t.m_height;
61 this->m_filter = t.m_filter;
62 this->m_mode = t.m_mode;
63 this->m_anisotropy = t.m_anisotropy;
64
65 t.m_id = 0;
66 }
67
68 return *this;
69 }
70
72 {
73 if (m_id != 0)
74 {
75 glDeleteTextures(1, &m_id);
76 }
77 }
78
79 TextureView Texture::make_view(const unsigned int minlevel, const unsigned int numlevels, const unsigned int minlayer, const unsigned int numlayers)
80 {
81 TextureView tv {m_id, minlevel, numlevels, minlayer, numlayers};
82 tv.m_mode = m_mode;
83 tv.m_filter = m_filter;
84 tv.m_anisotropy = m_anisotropy;
85 tv.m_width = m_width;
86 tv.m_height = m_height;
87
88 return tv;
89 }
90
91 void Texture::mode(const TextureMode mode)
92 {
93 glTextureParameteri(m_id, GL_TEXTURE_WRAP_S, static_cast<GLint>(mode));
94 glTextureParameteri(m_id, GL_TEXTURE_WRAP_T, static_cast<GLint>(mode));
95 }
96
98 {
99 return m_mode;
100 }
101
103 {
105
106 switch (m_filter)
107 {
109 glTextureParameteri(m_id, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
110 glTextureParameteri(m_id, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
111 break;
112
114 glTextureParameteri(m_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
115 glTextureParameteri(m_id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
116 break;
117 }
118 }
119
121 {
122 return m_filter;
123 }
124
125 void Texture::anisotropy(const int level)
126 {
127 m_anisotropy = std::clamp(level, 0, 16);
128 if (m_anisotropy <= 0)
129 {
130 m_anisotropy = 0;
131 }
132 else if (m_anisotropy <= 2)
133 {
134 m_anisotropy = 2;
135 }
136 else if (m_anisotropy <= 4)
137 {
138 m_anisotropy = 4;
139 }
140 else if (m_anisotropy <= 8)
141 {
142 m_anisotropy = 8;
143 }
144 else
145 {
146 m_anisotropy = 16;
147 }
148
149 glTextureParameterf(m_id, GL_TEXTURE_MAX_ANISOTROPY, static_cast<float>(m_anisotropy));
150 }
151
153 {
154 return m_anisotropy;
155 }
156
157 float Texture::width() const
158 {
159 return static_cast<float>(m_width);
160 }
161
162 float Texture::height() const
163 {
164 return static_cast<float>(m_height);
165 }
166
167 unsigned int Texture::id() const
168 {
169 return m_id;
170 }
171 } // namespace graphics
172} // namespace galaxy
173
174#ifdef GALAXY_WIN_PLATFORM
175#pragma warning(pop)
176#endif
OpenGL 2D TextureView.
OpenGL 2D Texture.
Definition Texture.hpp:26
int m_height
Cached height.
Definition Texture.hpp:171
int anisotropy() const
Get current anisotropy level.
Definition Texture.cpp:152
virtual ~Texture()
Destructor.
Definition Texture.cpp:71
float width() const
Get texture width.
Definition Texture.cpp:157
unsigned int id() const
Gets opengl handle.
Definition Texture.cpp:167
unsigned int m_id
OpenGL id.
Definition Texture.hpp:161
Texture & operator=(Texture &&)
Move assignment operator.
Definition Texture.cpp:49
int m_width
Cached width.
Definition Texture.hpp:166
float height() const
Get texture height.
Definition Texture.cpp:162
TextureMode m_mode
Mode.
Definition Texture.hpp:176
TextureMode mode() const
Get texture mode.
Definition Texture.cpp:97
TextureView make_view(const unsigned int minlevel=0, const unsigned int numlevels=1, const unsigned int minlayer=0, const unsigned int numlayers=1)
Create a texture view.
Definition Texture.cpp:79
TextureFilter m_filter
Filter.
Definition Texture.hpp:181
Texture()
Constructor.
Definition Texture.cpp:22
TextureFilter filter() const
Get texture filter.
Definition Texture.cpp:120
int m_anisotropy
Ansiotrophy level.
Definition Texture.hpp:186
TextureFilter
Mipmap filtering.
Definition Enums.hpp:94
@ NEAREST
Nearest-neighbour.
TextureMode
Texture wrapping modes.
Definition Enums.hpp:68
@ CLAMP_TO_EDGE
GL_CLAMP_TO_EDGE.
Animated.cpp galaxy.
Definition Animated.cpp:16