Debian Codegraph @

libs/ibl/src/Cubemap.cpp

raw
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <ibl/Cubemap.h>
18
19 using namespace filament::math;
20
21 namespace filament {
22 namespace ibl {
23
24
25 Cubemap::Cubemap(size_t dim) {
26 resetDimensions(dim);
27 }
28
29 Cubemap::~Cubemap() = default;
30
31 size_t Cubemap::getDimensions() const {
32 return mDimensions;
33 }
34
35 void Cubemap::resetDimensions(size_t dim) {
36 mDimensions = dim;
37 mScale = 2.0f / dim;
38 mUpperBound = std::nextafter((float) mDimensions, 0.0f);
39 for (auto& mFace : mFaces) {
40 mFace.reset();
41 }
42 }
43
44 void Cubemap::setImageForFace(Face face, const Image& image) {
45 mFaces[size_t(face)].set(image);
46 }
47
48 Cubemap::Address Cubemap::getAddressFor(const float3& r) {
49 Cubemap::Address addr;
50 float sc, tc, ma;
51 const float rx = std::abs(r.x);
52 const float ry = std::abs(r.y);
53 const float rz = std::abs(r.z);
54 if (rx >= ry && rx >= rz) {
55 ma = 1.0f / rx;
56 if (r.x >= 0) {
57 addr.face = Face::PX;
58 sc = -r.z;
59 tc = -r.y;
60 } else {
61 addr.face = Face::NX;
62 sc = r.z;
63 tc = -r.y;
64 }
65 } else if (ry >= rx && ry >= rz) {
66 ma = 1.0f / ry;
67 if (r.y >= 0) {
68 addr.face = Face::PY;
69 sc = r.x;
70 tc = r.z;
71 } else {
72 addr.face = Face::NY;
73 sc = r.x;
74 tc = -r.z;
75 }
76 } else {
77 ma = 1.0f / rz;
78 if (r.z >= 0) {
79 addr.face = Face::PZ;
80 sc = r.x;
81 tc = -r.y;
82 } else {
83 addr.face = Face::NZ;
84 sc = -r.x;
85 tc = -r.y;
86 }
87 }
88 // ma is guaranteed to be >= sc and tc
89 addr.s = (sc * ma + 1.0f) * 0.5f;
90 addr.t = (tc * ma + 1.0f) * 0.5f;
91 return addr;
92 }
93
94 void Cubemap::makeSeamless() {
95 size_t dim = getDimensions();
96 size_t D = dim;
97
98 // here we assume that all faces share the same underlying image
99 const size_t bpr = getImageForFace(Face::NX).getBytesPerRow();
100 const size_t bpp = getImageForFace(Face::NX).getBytesPerPixel();
101
102 auto getTexel = [](Image& image, ssize_t x, ssize_t y) -> Texel* {
103 return (Texel*)((uint8_t*)image.getData() + x * image.getBytesPerPixel() + y * image.getBytesPerRow());
104 };
105
106 auto stitch = [ & ](
107 Face faceDst, ssize_t xdst, ssize_t ydst, size_t incDst,
108 Face faceSrc, size_t xsrc, size_t ysrc, ssize_t incSrc) {
109 Image& imageDst = getImageForFace(faceDst);
110 Image& imageSrc = getImageForFace(faceSrc);
111 Texel* dst = getTexel(imageDst, xdst, ydst);
112 Texel* src = getTexel(imageSrc, xsrc, ysrc);
113 for (size_t i = 0; i < dim; ++i) {
114 *dst = *src;
115 dst = (Texel*)((uint8_t*)dst + incDst);
116 src = (Texel*)((uint8_t*)src + incSrc);
117 }
118 };
119
120 auto corners = [ & ](Face face) {
121 size_t L = D - 1;
122 Image& image = getImageForFace(face);
123 *getTexel(image, -1, -1) = (*getTexel(image, 0, 0) + *getTexel(image, -1, 0) + *getTexel(image, 0, -1)) / 3;
124 *getTexel(image, L+1, -1) = (*getTexel(image, L, 0) + *getTexel(image, L, -1) + *getTexel(image, L+1, 0)) / 3;
125 *getTexel(image, -1, L+1) = (*getTexel(image, 0, L) + *getTexel(image, -1, L) + *getTexel(image, 0, L+1)) / 3;
126 *getTexel(image, L+1, L+1) = (*getTexel(image, L, L) + *getTexel(image, L+1, L) + *getTexel(image, L+1, L)) / 3;
127 };
128
129 // +Y / Top
130 stitch( Face::PY, -1, 0, bpr, Face::NX, 0, 0, bpp); // left
131 stitch( Face::PY, 0, -1, bpp, Face::NZ, D-1, 0, -bpp); // top
132 stitch( Face::PY, D, 0, bpr, Face::PX, D-1, 0, -bpp); // right
133 stitch( Face::PY, 0, D, bpp, Face::PZ, 0, 0, bpp); // bottom
134 corners(Face::PY);
135
136 // -X / Left
137 stitch( Face::NX, -1, 0, bpr, Face::NZ, D-1, 0, bpr); // left
138 stitch( Face::NX, 0, -1, bpp, Face::PY, 0, 0, bpr); // top
139 stitch( Face::NX, D, 0, bpr, Face::PZ, 0, 0, bpr); // right
140 stitch( Face::NX, 0, D, bpp, Face::NY, 0, D-1, -bpr); // bottom
141 corners(Face::NX);
142
143 // +Z / Front
144 stitch( Face::PZ, -1, 0, bpr, Face::NX, D-1, 0, bpr); // left
145 stitch( Face::PZ, 0, -1, bpp, Face::PY, 0, D-1, bpp); // top
146 stitch( Face::PZ, D, 0, bpr, Face::PX, 0, 0, bpr); // right
147 stitch( Face::PZ, 0, D, bpp, Face::NY, 0, 0, bpp); // bottom
148 corners(Face::PZ);
149
150 // +X / Right
151 stitch( Face::PX, -1, 0, bpr, Face::PZ, D-1, 0, bpr); // left
152 stitch( Face::PX, 0, -1, bpp, Face::PY, D-1, D-1, -bpr); // top
153 stitch( Face::PX, D, 0, bpr, Face::NZ, 0, 0, bpr); // right
154 stitch( Face::PX, 0, D, bpp, Face::NY, D-1, 0, bpr); // bottom
155 corners(Face::PX);
156
157 // -Z / Back
158 stitch( Face::NZ, -1, 0, bpr, Face::PX, D-1, 0, bpr); // left
159 stitch( Face::NZ, 0, -1, bpp, Face::PY, D-1, 0, -bpp); // top
160 stitch( Face::NZ, D, 0, bpr, Face::NX, 0, 0, bpr); // right
161 stitch( Face::NZ, 0, D, bpp, Face::NY, D-1, D-1, -bpp); // bottom
162 corners(Face::NZ);
163
164 // -Y / Bottom
165 stitch( Face::NY, -1, 0, bpr, Face::NX, D-1, D-1, -bpp); // left
166 stitch( Face::NY, 0, -1, bpp, Face::PZ, 0, D-1, bpp); // top
167 stitch( Face::NY, D, 0, bpr, Face::PX, 0, D-1, bpp); // right
168 stitch( Face::NY, 0, D, bpp, Face::NZ, D-1, D-1, -bpp); // bottom
169 corners(Face::NY);
170 }
171
172 Cubemap::Texel Cubemap::filterAt(const Image& image, float x, float y) {
173 const size_t x0 = size_t(x);
174 const size_t y0 = size_t(y);
175 // we allow ourselves to read past the width/height of the Image because the data is valid
176 // and contain the "seamless" data.
177 size_t x1 = x0 + 1;
178 size_t y1 = y0 + 1;
179
180 const float u = float(x - x0);
181 const float v = float(y - y0);
182 const float one_minus_u = 1 - u;
183 const float one_minus_v = 1 - v;
184 const Texel& c0 = sampleAt(image.getPixelRef(x0, y0));
185 const Texel& c1 = sampleAt(image.getPixelRef(x1, y0));
186 const Texel& c2 = sampleAt(image.getPixelRef(x0, y1));
187 const Texel& c3 = sampleAt(image.getPixelRef(x1, y1));
188 return (one_minus_u*one_minus_v)*c0 + (u*one_minus_v)*c1 + (one_minus_u*v)*c2 + (u*v)*c3;
189 }
190
191 Cubemap::Texel Cubemap::filterAtCenter(const Image& image, size_t x0, size_t y0) {
192 // we allow ourselves to read past the width/height of the Image because the data is valid
193 // and contain the "seamless" data.
194 size_t x1 = x0 + 1;
195 size_t y1 = y0 + 1;
196 const Texel& c0 = sampleAt(image.getPixelRef(x0, y0));
197 const Texel& c1 = sampleAt(image.getPixelRef(x1, y0));
198 const Texel& c2 = sampleAt(image.getPixelRef(x0, y1));
199 const Texel& c3 = sampleAt(image.getPixelRef(x1, y1));
200 return (c0 + c1 + c2 + c3) * 0.25f;
201 }
202
203 Cubemap::Texel Cubemap::trilinearFilterAt(const Cubemap& l0, const Cubemap& l1, float lerp,
204 const float3& L)
205 {
206 Cubemap::Address addr(getAddressFor(L));
207 const Image& i0 = l0.getImageForFace(addr.face);
208 const Image& i1 = l1.getImageForFace(addr.face);
209 float x0 = std::min(addr.s * l0.mDimensions, l0.mUpperBound);
210 float y0 = std::min(addr.t * l0.mDimensions, l0.mUpperBound);
211 float x1 = std::min(addr.s * l1.mDimensions, l1.mUpperBound);
212 float y1 = std::min(addr.t * l1.mDimensions, l1.mUpperBound);
213 float3 c0 = filterAt(i0, x0, y0);
214 c0 += lerp * (filterAt(i1, x1, y1) - c0);
215 return c0;
216 }
217
218 } // namespace ibl
219 } // namespace filament