Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TBilinearPixelTex2D.h
Go to the documentation of this file.
1#pragma once
2
4#include "Math/TVector2.h"
5
6#include <utility>
7#include <type_traits>
8#include <cmath>
9
10namespace ph
11{
12
13template<typename T, std::size_t N>
14class TBilinearPixelTex2D : public TPixelTex2D<T, N>
15{
16public:
17 using TPixelTex2D<T, N>::TPixelTex2D;
18
19 inline void sample(
20 const SampleLocation& sampleLocation,
21 TTexPixel<T, N>* const out_value) const override
22 {
23 const float64 u = sampleLocation.uvw().x();
24 const float64 v = sampleLocation.uvw().y();
25 const uint32 w = this->getWidthPx();
26 const uint32 h = this->getHeightPx();
27
28 PH_ASSERT(w != 0 && h != 0);
29
30 float64 normU, normV;
31 this->normalizeUV(u, v, &normU, &normV);
32
33 const float64 x = normU * w;
34 const float64 y = normV * h;
35 const float64 x0 = std::floor(x - 0.5) + 0.5;
36 const float64 y0 = std::floor(y - 0.5) + 0.5;
37 const float64 x1 = x0 + 1.0;
38 const float64 y1 = y0 + 1.0;
39
40 const float64 weights[4]
41 {
42 (x1 - x) * (y1 - y),// weight 00
43 (x1 - x) * (y - y0),// weight 01
44 (x - x0) * (y1 - y),// weight 10
45 (x - x0) * (y - y0) // weight 11
46 };
47 const math::Vector2D xys[4]
48 {
49 {x0, y0}, {x0, y1}, {x1, y0}, {x1, y1}
50 };
51
52 TTexPixel<float64, N> accuPixel(0);
53 TTexPixel<T, N> pixel;
54 for(std::size_t i = 0; i < 4; ++i)
55 {
56 float64 normUi, normVi;
57 this->normalizeUV(xys[i].x() * this->getTexelSizeU(),
58 xys[i].y() * this->getTexelSizeV(),
59 &normUi, &normVi);
60
61 uint32 xi = static_cast<uint32>(normUi * w);
62 uint32 yi = static_cast<uint32>(normVi * h);
63 xi = xi < w ? xi : w - 1;
64 yi = yi < h ? yi : h - 1;
65
66 this->getPixel(xi, yi, &pixel);
67 accuPixel.addLocal(TTexPixel<float64, N>(pixel).mulLocal(weights[i]));
68 }
69
70 // taking care of pixel value rounding
71 if constexpr(std::is_integral_v<T>)
72 {
73 accuPixel.addLocal(0.5);
74 }
75
76 *out_value = TTexPixel<T, N>(accuPixel);
77 }
78};
79
80}// end namespace ph
Definition SampleLocation.h:22
math::Vector3R uvw() const
Gets and sets the uvw coordinates of this sample location.
Definition SampleLocation.h:90
float64 getTexelSizeV() const
Definition TAbstractPixelTex2D.h:42
uint32 getWidthPx() const
Definition TAbstractPixelTex2D.h:39
uint32 getHeightPx() const
Definition TAbstractPixelTex2D.h:40
float64 getTexelSizeU() const
Definition TAbstractPixelTex2D.h:41
void normalizeUV(const float64 u, const float64 v, float64 *const out_u, float64 *const out_v) const
Definition TAbstractPixelTex2D.h:52
Definition TBilinearPixelTex2D.h:15
void sample(const SampleLocation &sampleLocation, TTexPixel< T, N > *const out_value) const override
Definition TBilinearPixelTex2D.h:19
Definition TPixelTex2D.h:16
void getPixel(const uint32 x, const uint32 y, TTexPixel< T, N > *const out_pixel) const
Definition TPixelTex2D.h:40
TPixelTex2D()
Definition TPixelTex2D.h:18
Derived & addLocal(const Derived &rhs)
Definition TArithmeticArrayBase.ipp:40
Definition TArithmeticArray.h:13
T & y()
Definition TVector3.ipp:189
T & x()
Definition TVector3.ipp:183
The root for all renderer implementations.
Definition EEngineProject.h:6
math::TArithmeticArray< T, N > TTexPixel
Definition TTexPixel.h:11