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
13
namespace
nngn::term
{
14
15
using namespace
nngn::literals
;
16
18
class
Texture
{
19
public
:
20
using
texel3
=
vec3_base<u8>
;
21
using
texel4
=
vec4_base<u8>
;
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
;
29
private
:
30
uvec2
m_size
;
31
vec2
size_f
;
32
std::vector<texel4>
m_data
;
33
};
34
35
inline
Texture::Texture
(
uvec2
s
) :
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
41
inline
void
Texture::resize
(
uvec2
s
) { *
this
=
Texture
{
s
}; }
42
43
inline
void
Texture::copy
(
const
u8
*p) {
44
std::copy(p, p + this->
m_data
.size(), this->m_data.front().data());
45
}
46
47
inline
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
56
inline
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
nngn::Math
Definition
math.h:22
nngn::term::Texture
Buffer holding texture image data.
Definition
texture.h:18
nngn::term::Texture::size_f
vec2 size_f
Definition
texture.h:31
nngn::term::Texture::copy
void copy(const u8 *p)
Definition
texture.h:43
nngn::term::Texture::m_size
uvec2 m_size
Definition
texture.h:30
nngn::term::Texture::size
uvec2 size(void) const
Definition
texture.h:23
nngn::term::Texture::sample
texel4 sample(vec2 uv) const
Definition
texture.h:56
nngn::term::Texture::data
std::span< const texel4 > data(void) const
Definition
texture.h:24
nngn::term::Texture::resize
void resize(uvec2 size)
Definition
texture.h:41
nngn::term::Texture::Texture
Texture(uvec2 size={})
Definition
texture.h:35
nngn::term::Texture::m_data
std::vector< texel4 > m_data
Definition
texture.h:32
def.h
literals.h
math.h
nngn::literals
Definition
literals.h:9
nngn::term
Definition
const.h:6
nngn::u8
std::uint8_t u8
Definition
def.h:12
nngn::vec2_base< std::uint32_t >
nngn::vec2_base::x
T x
Definition
vec2.h:12
nngn::vec2_base::y
T y
Definition
vec2.h:12
nngn::vec3_base
Definition
vec3.h:12
nngn::vec4_base
Definition
vec4.h:12
nngn::vec4_base::x
T x
Definition
vec4.h:13
s
std::chrono::seconds s
Definition
timing.cpp:6
vec2.h
vec3.h
vec4.h
src
graphics
terminal
texture.h
Generated by
1.11.0