CG-Project2
Loading...
Searching...
No Matches
Texture.hpp
1#pragma once
2
3#include "../../include/Core/Structs.hpp"
4#include "../../include/Core/Utils.hpp"
5
6namespace ogl {
7
8 class Texture {
9 public:
10 /*
11 * Initialize the texture.
12 * This operation must be done before using this object.
13 */
14 void onAttach();
15
16 /*
17 * Destroy the texture on the GPU.
18 */
19 void onDetach();
20
21 /*
22 * Bind the current texture id.
23 */
24 void bind() const;
25
26 /*
27 * Unbind the current texture id.
28 */
29 void unbind() const;
30
31 /*
32 * Set an `int` parameter for the current texture in bound.
33 *
34 * @param pname Parameter name.
35 * @param pval Parameter `int` value.
36 */
37 void setTexParameteri(const unsigned int &pname, const int &pval);
38
39 /*
40 * Set a `float` parameter of the current texture in bound.
41 *
42 * @param pname Parameter name.
43 * @param pval Parameter `float` value.
44 */
45 void setTexParameterf(const unsigned int &pname, const float &pval);
46
47 /*
48 * Bind the `Texture` id and calls `fastCreatetexture2D`.
49 *
50 * @param *data Pointer to texture's data.
51 */
52 inline void createTexture2D(void *data) {
53 this->bind();
54 this->fastCreateTexture2D(data);
55 }
56
57 /*
58 * Creates the texture already binded.
59 *
60 * @param *data Pointer to texture's data.
61 */
62 void fastCreateTexture2D(void *data);
63
64 void fastCreateCustomTexture2D(const unsigned int &target, const unsigned int &level, const unsigned int &internalFormat, const unsigned int &width, const unsigned int &height, const unsigned int &border, const unsigned int &format, const unsigned int &type, void *data);
65
66 /*
67 * Generates the binded texture mipmap, only if the texture is created.
68 */
69 void generateMipmap();
70
71 /*
72 * Retrieves the texture id.
73 *
74 * @returns texture id.
75 */
76 inline unsigned int getId() const { return this->m_id; }
77
78 /*
79 * Retrieves the texture width.
80 *
81 * @returns texture width.
82 */
83 inline unsigned int getWidth() const { return this->m_size.x; }
84
85 /*
86 * Changes texture width.
87 *
88 * @param width Texture width.
89 */
90 inline void setWidth(const unsigned int &width) {
91 this->m_size.x = width;
92 this->rescaleTexture();
93 }
94
95 /*
96 * Retrieves the texture height.
97 *
98 * @returns texture height.
99 */
100 inline unsigned int getHeight() const { return this->m_size.y; }
101
102 /*
103 * Changes texture height.
104 *
105 * @param height Texture height.
106 */
107 inline void setHeight(const unsigned int &height) {
108 this->m_size.y = height;
109 this->rescaleTexture();
110 }
111
112 /*
113 * Retrieves the texture size.
114 *
115 * @returns texture size.
116 */
117 inline Pair<unsigned int> getSize() const { return this->m_size; }
118
119 inline void setSize(const Pair<unsigned int> &size) {
120 this->m_size = size;
121 this->rescaleTexture();
122 }
123
124 Texture() = default;
125
126 Texture(const TextureParams &params, const Pair<unsigned int> &size) :
127 m_params(params), m_size(size) {
128 }
129
130 Texture(const TextureParams &params, const unsigned int &width, const unsigned int &height) :
131 m_params(params), m_size({width, height}) {
132 }
133
134 ~Texture() = default;
135
136 private:
138 void rescaleTexture();
139
140 unsigned int m_id = 0;
141
142 TextureParams m_params{};
143
144 Pair<unsigned int> m_size{};
145
146 void *m_data = NULL;
147
148 bool m_attached = false;
149 bool m_created = false;
150 };
151
152} // namespace ogl
Definition Utils.hpp:4
Data structure to create an ogl::Texture using these parameters.
Definition Structs.hpp:20