Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
pixel_texture_basics.h
Go to the documentation of this file.
1#pragma once
2
3#include "Math/TVector2.h"
4#include "Utility/utility.h"
5#include "Math/math.h"
6
7#include <Common/primitive_type.h>
8
9#include <array>
10#include <cstddef>
11#include <stdexcept>
12#include <format>
13
14namespace ph
15{
16
17namespace pixel_texture
18{
19
20enum class EWrapMode
21{
22 Repeat = 0,
25};
26
27enum class ESampleMode
28{
29 Nearest = 0,
32};
33
39enum class EPixelLayout
40{
41 RGBA = 0,
42 R,
43 G,
44 B,
46 A,
47 RG,
48 RGB,
49 BGR,
50 ARGB,
51 ABGR,
52 BGRA
53};
54
59inline float64 uv_to_st_scalar(const float64 inputUV, const EWrapMode wrapMode)
60{
61 float64 outputST = 0.0;
62 switch(wrapMode)
63 {
65 {
66 const float64 st = math::fractional_part(inputUV);
67 outputST = st >= 0.0 ? st : st + 1.0;
68 break;
69 }
70
72 {
73 outputST = math::clamp(inputUV, 0.0, 1.0);
74 break;
75 }
76
78 {
79 const float64 flippedST = inputUV * -1.0 + 1.0;
80 outputST = math::clamp(flippedST, 0.0, 1.0);
81 break;
82 }
83
84 default:
85 PH_ASSERT_UNREACHABLE_SECTION();
86 }
87
88 PH_ASSERT_IN_RANGE_INCLUSIVE(outputST, 0.0, 1.0);
89 return outputST;
90}
91
95inline math::Vector2D uv_to_st(const math::Vector2D& inputUV, const EWrapMode wrapModeS, const EWrapMode wrapModeT)
96{
97 return
98 {
99 uv_to_st_scalar(inputUV.u(), wrapModeS),
100 uv_to_st_scalar(inputUV.v(), wrapModeT)
101 };
102}
103
104inline std::size_t num_pixel_elements(const EPixelLayout layout)
105{
106 switch(layout)
107 {
108 case EPixelLayout::R:
109 case EPixelLayout::G:
110 case EPixelLayout::B:
112 case EPixelLayout::A:
113 return 1;
114
115 case EPixelLayout::RG:
116 return 2;
117
120 return 3;
121
126 return 4;
127
128 default:
129 PH_ASSERT_UNREACHABLE_SECTION();
130 return 0;
131 }
132}
133
134inline std::size_t alpha_channel_index(const EPixelLayout layout)
135{
136 switch(layout)
137 {
138 case EPixelLayout::A:
141 return 0;
142
145 return 3;
146
147 default:
148 throw std::invalid_argument(std::format(
149 "Pixel layout does not contain alpha channel: {}", enum_to_string(layout)));
150 }
151}
152
153}// end namespace pixel_texture
154
155}// end namespace ph
T & u()
Definition TVector2.ipp:62
T & v()
Definition TVector2.ipp:68
Miscellaneous math utilities.
T fractional_part(const T value)
Retrieve the fractional part of value (with the sign unchanged). The result is not guaranteed to be t...
Definition math.h:258
T clamp(const T value, const T lowerBound, const T upperBound)
Clamps a value to [lowerBound, upperBound]. None of value, lowerBound and upperBound can be NaN,...
Definition math.h:77
EPixelLayout
Pixel layout of pixel texture. Represent the ordering of pixel components with respect to a color spa...
Definition pixel_texture_basics.h:40
math::Vector2D uv_to_st(const math::Vector2D &inputUV, const EWrapMode wrapModeS, const EWrapMode wrapModeT)
Transform (u, v) coordinates to (s, t) in [0, 1] according to wrap mode. The transformation will pres...
Definition pixel_texture_basics.h:95
EWrapMode
Definition pixel_texture_basics.h:21
std::size_t num_pixel_elements(const EPixelLayout layout)
Definition pixel_texture_basics.h:104
std::size_t alpha_channel_index(const EPixelLayout layout)
Definition pixel_texture_basics.h:134
float64 uv_to_st_scalar(const float64 inputUV, const EWrapMode wrapMode)
Transform (u, v) coordinates to (s, t) in [0, 1] according to wrap mode. This overload operates on si...
Definition pixel_texture_basics.h:59
ESampleMode
Definition pixel_texture_basics.h:28
The root for all renderer implementations.
Definition EEngineProject.h:6
std::string enum_to_string(const EnumType enumValue)
Definition utility.h:173