Overte C++ Documentation
CubeMap.h
1 //
2 // CubeMap.h
3 // image/src/image
4 //
5 // Created by Olivier Prat on 03/27/2019.
6 // Copyright 2019 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_image_CubeMap_h
13 #define hifi_image_CubeMap_h
14 
15 #include <gpu/Texture.h>
16 #include <glm/vec4.hpp>
17 #include <vector>
18 #include <array>
19 #include <atomic>
20 
21 #include "Image.h"
22 
23 namespace image {
24 
25  class CubeMap {
26 
27  enum {
28  EDGE_WIDTH = 1
29  };
30 
31  public:
32 
33  CubeMap(int width, int height, int mipCount);
34  CubeMap(const std::vector<Image>& faces, int mipCount, const std::atomic<bool>& abortProcessing = false);
35 
36  void reset(int width, int height, int mipCount);
37  void copyTo(CubeMap& other) const;
38 
39  void applyGamma(float value);
40 
41  gpu::uint16 getMipCount() const { return (gpu::uint16)_mips.size(); }
42  int getMipWidth(gpu::uint16 mipLevel) const {
43  return std::max(1, _width >> mipLevel);
44  }
45  int getMipHeight(gpu::uint16 mipLevel) const {
46  return std::max(1, _height >> mipLevel);
47  }
48  gpu::Vec2i getMipDimensions(gpu::uint16 mipLevel) const {
49  return gpu::Vec2i(getMipWidth(mipLevel), getMipHeight(mipLevel));
50  }
51 
52  size_t getMipLineStride(gpu::uint16 mipLevel) const {
53  return getMipWidth(mipLevel) + 2 * EDGE_WIDTH;
54  }
55 
56  glm::vec4* editFace(gpu::uint16 mipLevel, int face) {
57  return _mips[mipLevel][face].data() + (getMipLineStride(mipLevel) + 1)*EDGE_WIDTH;
58  }
59 
60  const glm::vec4* getFace(gpu::uint16 mipLevel, int face) const {
61  return _mips[mipLevel][face].data() + (getMipLineStride(mipLevel) + 1)*EDGE_WIDTH;
62  }
63 
64  Image getFaceImage(gpu::uint16 mipLevel, int face) const;
65 
66  void convolveForGGX(CubeMap& output, const std::atomic<bool>& abortProcessing) const;
67  glm::vec4 fetchLod(const glm::vec3& dir, float lod) const;
68 
69  private:
70 
71  struct GGXSamples;
72  class Mip;
73  class ConstMip;
74 
75  using Face = std::vector<glm::vec4>;
76  using Faces = std::array<Face, 6>;
77 
78  int _width;
79  int _height;
80  std::vector<Faces> _mips;
81 
82  static void getFaceUV(const glm::vec3& dir, int* index, glm::vec2* uv);
83  static void generateGGXSamples(GGXSamples& data, float roughness, const int resolution);
84  static void copyFace(int width, int height, const glm::vec4* source, size_t srcLineStride, glm::vec4* dest, size_t dstLineStride);
85  void convolveMipFaceForGGX(const GGXSamples& samples, CubeMap& output, gpu::uint16 mipLevel, int face, const std::atomic<bool>& abortProcessing) const;
86  glm::vec4 computeConvolution(const glm::vec3& normal, const GGXSamples& samples) const;
87 
88  };
89 
90 }
91 
92 #endif // hifi_image_CubeMap_h