nngn
Loading...
Searching...
No Matches
texture.h
Go to the documentation of this file.
1#ifndef NNGN_GRAPHICS_TERMINAL_TEXTURE_H
2#define NNGN_GRAPHICS_TERMINAL_TEXTURE_H
3
4#include <cfloat>
5
6#include "math/math.h"
7#include "math/vec2.h"
8#include "math/vec3.h"
9#include "math/vec4.h"
10#include "utils/def.h"
11#include "utils/literals.h"
12
13namespace nngn::term {
14
15using namespace nngn::literals;
16
18class Texture {
19public:
22 explicit Texture(uvec2 size = {});
23 uvec2 size(void) const { return this->m_size; }
24 std::span<const texel4> data(void) const { return this->m_data; }
25 void resize(uvec2 size);
26 void copy(const u8 *p);
27 void copy(uvec2 size, const u8 *p);
28 texel4 sample(vec2 uv) const;
29private:
32 std::vector<texel4> m_data;
33};
34
36 m_size{s},
37 size_f{static_cast<vec2>(s) - FLT_EPSILON * static_cast<vec2>(s)},
38 m_data(4_z * Math::product(s))
39{}
40
41inline void Texture::resize(uvec2 s) { *this = Texture{s}; }
42
43inline void Texture::copy(const u8 *p) {
44 std::copy(p, p + this->m_data.size(), this->m_data.front().data());
45}
46
47inline void Texture::copy(uvec2 size, const u8 *p) {
48 auto *dst = this->m_data.front().data();
49 for(auto y = 0u; y != size.y; ++y) {
50 std::copy(p, p + 4_z * size.x, dst);
51 p += 4_z * size.x;
52 dst += 4_z * this->m_size.x;
53 }
54}
55
56inline auto Texture::sample(vec2 uv) const -> texel4 {
57 uv.x = std::clamp(uv.x, 0.0f, 1.0f);
58 uv.y = std::clamp(uv.y, 0.0f, 1.0f);
59 const auto p = static_cast<zvec2>(uv * this->size_f);
60 return this->m_data[this->m_size.x * p.y + p.x];
61}
62
63}
64
65#endif
Definition math.h:22
Buffer holding texture image data.
Definition texture.h:18
vec2 size_f
Definition texture.h:31
void copy(const u8 *p)
Definition texture.h:43
uvec2 m_size
Definition texture.h:30
uvec2 size(void) const
Definition texture.h:23
texel4 sample(vec2 uv) const
Definition texture.h:56
std::span< const texel4 > data(void) const
Definition texture.h:24
void resize(uvec2 size)
Definition texture.h:41
Texture(uvec2 size={})
Definition texture.h:35
std::vector< texel4 > m_data
Definition texture.h:32
Definition literals.h:9
Definition const.h:6
std::uint8_t u8
Definition def.h:12
T x
Definition vec2.h:12
T y
Definition vec2.h:12
Definition vec3.h:12
Definition vec4.h:12
T x
Definition vec4.h:13
std::chrono::seconds s
Definition timing.cpp:6