cesium-native  0.41.0
AttributeCompression.h
1 #pragma once
2 
3 #include "Library.h"
4 #include "Math.h"
5 
6 #include <glm/glm.hpp>
7 
8 namespace CesiumUtility {
12 class CESIUMUTILITY_API AttributeCompression final {
13 
14 public:
25  template <
26  typename T,
27  class = typename std::enable_if<std::is_unsigned<T>::value>::type>
28  static glm::dvec3 octDecodeInRange(T x, T y, T rangeMax) {
29  glm::dvec3 result;
30 
31  result.x = CesiumUtility::Math::fromSNorm(x, rangeMax);
32  result.y = CesiumUtility::Math::fromSNorm(y, rangeMax);
33  result.z = 1.0 - (glm::abs(result.x) + glm::abs(result.y));
34 
35  if (result.z < 0.0) {
36  const double oldVX = result.x;
37  result.x =
38  (1.0 - glm::abs(result.y)) * CesiumUtility::Math::signNotZero(oldVX);
39  result.y =
40  (1.0 - glm::abs(oldVX)) * CesiumUtility::Math::signNotZero(result.y);
41  }
42 
43  return glm::normalize(result);
44  }
45 
56  static glm::dvec3 octDecode(uint8_t x, uint8_t y) {
57  constexpr uint8_t rangeMax = 255;
58  return AttributeCompression::octDecodeInRange(x, y, rangeMax);
59  }
60 
68  static glm::dvec3 decodeRGB565(const uint16_t value) {
69  constexpr uint16_t mask5 = (1 << 5) - 1;
70  constexpr uint16_t mask6 = (1 << 6) - 1;
71  constexpr float normalize5 = 1.0f / 31.0f; // normalize [0, 31] to [0, 1]
72  constexpr float normalize6 = 1.0f / 63.0f; // normalize [0, 63] to [0, 1]
73 
74  const uint16_t red = static_cast<uint16_t>(value >> 11);
75  const uint16_t green = static_cast<uint16_t>((value >> 5) & mask6);
76  const uint16_t blue = value & mask5;
77 
78  return glm::dvec3(red * normalize5, green * normalize6, blue * normalize5);
79  };
80 };
81 
82 } // namespace CesiumUtility
Functions to handle compressed attributes in different formats.
static glm::dvec3 octDecode(uint8_t x, uint8_t y)
Decodes a unit-length vector in 2 byte 'oct' encoding to a normalized 3-component vector.
static glm::dvec3 decodeRGB565(const uint16_t value)
Decodes a RGB565-encoded color to a 3-component vector containing the normalized RGB values.
static glm::dvec3 octDecodeInRange(T x, T y, T rangeMax)
Decodes a unit-length vector in 'oct' encoding to a normalized 3-component vector.
static constexpr double signNotZero(double value) noexcept
Returns 1.0 if the given value is positive or zero, and -1.0 if it is negative.
Definition: Math.h:258
static constexpr double fromSNorm(double value, double rangeMaximum=255.0) noexcept
Converts a SNORM value in the range [0, rangeMaximum] to a scalar in the range [-1....
Definition: Math.h:385
Utility classes for Cesium.