Texture.hpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#ifndef SMK_TEXTURE_HPP
6#define SMK_TEXTURE_HPP
7
8#include <smk/OpenGL.hpp>
9#include <string>
10
11namespace smk {
12
13/// A texture loaded from a file into the GPU. This class support the move and
14/// copy operators. Its underlying GPU texture is refcounted and released when
15/// then last smk::Texture is deleted.
16///
17/// Example:
18/// --------
19///
20/// ~~~cpp
21/// auto texture = smk::Texture("./ball.png");
22/// ~~~
23///
24/// Supported image encoding:
25/// -------------------------
26///
27/// The SMK library uses the stb_image. So it supports the same image types:
28/// - JPEG baseline & progressive (12 bpc/arithmetic not supported, same as
29/// stock IJG lib)
30/// - PNG 1/2/4/8/16-bit-per-channel
31/// - TGA
32/// - BMP non-1bpp, non-RLE
33/// - PSD (composited view only, no extra channels, 8/16 bit-per-channel)
34/// - GIF
35/// - HDR (radiance rgbE format)
36/// - PIC (Softimage PIC)
37/// - PNM (PPM and PGM binary only)
38struct Texture {
39 public:
40 struct Option {
41 GLint min_filter = GL_LINEAR_MIPMAP_LINEAR;
42 GLint mag_filter = GL_LINEAR;
43 GLint wrap_s = GL_CLAMP_TO_EDGE;
44 GLint wrap_t = GL_CLAMP_TO_EDGE;
45 GLint internal_format = GL_RGBA;
46 GLint format = GL_RGBA;
47 GLint type = GL_UNSIGNED_BYTE;
48 bool generate_mipmap = true;
49 };
50
51 Texture(); // empty texture.
52 Texture(const std::string& filename);
53 Texture(const std::string& filename, const Option& option);
54 Texture(const uint8_t* data, int width, int height);
55 Texture(const uint8_t* data, int width, int height, const Option& option);
56 Texture(GLuint id, int width, int height);
57 ~Texture();
58
59 void Bind(GLuint active_texture = GL_TEXTURE0) const;
60
61 int width() const;
62 int height() const;
63 GLuint id() const;
64
65 operator bool() const { return id_ != 0; }
66
67 // --- Copyable Movable resource ---------------------------------------------
68 Texture(Texture&&) noexcept;
69 Texture(const Texture&);
70 Texture& operator=(Texture&&) noexcept;
71 Texture& operator=(const Texture&);
72 //----------------------------------------------------------------------------
73 bool operator==(const Texture& other) const;
74 bool operator!=(const Texture& other) const;
75
76 private:
77 void Release();
78 void Load(const uint8_t* data, int width, int height, const Option& option);
79 GLuint id_ = 0;
80 int width_ = 0;
81 int height_ = 0;
82
83 // Used to support copy. Nullptr as long as this class is not copied.
84 // Otherwise an integer counting how many instances shares this resource.
85 mutable int* ref_count_ = nullptr;
86};
87
88} // namespace smk
89
90#endif // SMK_TEXTURE_HPP
GLuint id() const
Access the ID of the texture.
Definition: Texture.cpp:207
int height() const
Access the height of the texture.
Definition: Texture.cpp:201
int width() const
Access the width of the texture.
Definition: Texture.cpp:195
Texture()
The null texture.