Overte C++ Documentation
BufferViewHelpers.h
1 //
2 // Copyright 2018 High Fidelity, Inc.
3 //
4 // Distributed under the Apache License, Version 2.0.
5 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
6 //
7 #pragma once
8 
9 #include <QtCore>
10 #include <memory>
11 #include <glm/glm.hpp>
12 #include <glm/gtc/packing.hpp>
13 
14 #include "GpuHelpers.h"
15 #include "GLMHelpers.h"
16 
17 namespace graphics {
18  class Mesh;
19  using MeshPointer = std::shared_ptr<Mesh>;
20 }
21 
22 class Extents;
23 class AABox;
24 
25 namespace buffer_helpers {
26  extern QMap<QString,int> ATTRIBUTES;
27  extern const std::array<const char*, 4> XYZW;
28  extern const std::array<const char*, 4> ZERO123;
29 
30  template <typename T>
31  QVariant glmVecToVariant(const T& v, bool asArray = false) {
32  static const auto len = T().length();
33  if (asArray) {
34  QVariantList list;
35  for (int i = 0; i < len; i++) {
36  list << v[i];
37  }
38  return list;
39  } else {
40  QVariantMap obj;
41  for (int i = 0; i < len; i++) {
42  obj[XYZW[i]] = v[i];
43  }
44  return obj;
45  }
46  }
47 
48  template <typename T>
49  const T glmVecFromVariant(const QVariant& v) {
50  auto isMap = v.type() == (QVariant::Type)QMetaType::QVariantMap;
51  static const auto len = T().length();
52  const auto& components = isMap ? XYZW : ZERO123;
53  T result;
54  QVariantMap map;
55  QVariantList list;
56  if (isMap) {
57  map = v.toMap();
58  } else {
59  list = v.toList();
60  }
61  for (int i = 0; i < len; i++) {
62  float value;
63  if (isMap) {
64  value = map.value(components[i]).toFloat();
65  } else {
66  value = list.value(i).toFloat();
67  }
68 #ifdef DEBUG_BUFFERVIEW_HELPERS
69  if (value != value) { // NAN
70  qWarning().nospace() << "vec" << len << "." << components[i] << " NAN received from script.... " << v.toString();
71  }
72 #endif
73  result[i] = value;
74  }
75  return result;
76  }
77 
78 
79  glm::uint32 forEachVariant(const gpu::BufferView& view, std::function<bool(glm::uint32 index, const QVariant& value)> func, const char* hint = "");
80  template <typename T> glm::uint32 forEach(const gpu::BufferView& view, std::function<bool(glm::uint32 index, const T& value)> func);
81 
82  template <typename T> gpu::BufferView newFromVector(const QVector<T>& elements, const gpu::Element& elementType);
83  template <typename T> gpu::BufferView newFromVariantList(const QVariantList& list, const gpu::Element& elementType);
84 
85  template <typename T> QVector<T> variantToVector(const QVariant& value);
86  template <typename T> QVector<T> bufferToVector(const gpu::BufferView& view, const char *hint = "");
87 
88  // note: these do value conversions from the underlying buffer type into the template type
89  template <typename T> T getValue(const gpu::BufferView& view, glm::uint32 index, const char* hint = "");
90  template <typename T> bool setValue(const gpu::BufferView& view, glm::uint32 index, const T& value, const char* hint = "");
91 
92  gpu::BufferView clone(const gpu::BufferView& input);
93  gpu::BufferView resized(const gpu::BufferView& input, glm::uint32 numElements);
94 
95  namespace mesh {
96  glm::uint32 forEachVertex(const graphics::MeshPointer& mesh, std::function<bool(glm::uint32 index, const QVariantMap& attributes)> func);
97  bool setVertexAttributes(const graphics::MeshPointer& mesh, glm::uint32 index, const QVariantMap& attributes);
98  QVariant getVertexAttributes(const graphics::MeshPointer& mesh, glm::uint32 index);
99  graphics::MeshPointer clone(const graphics::MeshPointer& mesh);
100  gpu::BufferView getBufferView(const graphics::MeshPointer& mesh, quint8 slot);
101  std::map<QString, gpu::BufferView> getAllBufferViews(const graphics::MeshPointer& mesh);
102  template <typename T> QVector<T> attributeToVector(const graphics::MeshPointer& mesh, gpu::Stream::InputSlot slot) {
103  return bufferToVector<T>(getBufferView(mesh, slot), qUtf8Printable(gpu::toString(slot)));
104  }
105  }
106 }