11 #include <glm/glm.hpp>
12 #include <glm/gtc/packing.hpp>
14 #include "GpuHelpers.h"
15 #include "GLMHelpers.h"
19 using MeshPointer = std::shared_ptr<Mesh>;
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;
31 QVariant glmVecToVariant(
const T& v,
bool asArray =
false) {
32 static const auto len = T().length();
35 for (
int i = 0; i < len; i++) {
41 for (
int i = 0; i < len; i++) {
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;
61 for (
int i = 0; i < len; i++) {
64 value = map.value(components[i]).toFloat();
66 value = list.value(i).toFloat();
68 #ifdef DEBUG_BUFFERVIEW_HELPERS
70 qWarning().nospace() <<
"vec" << len <<
"." << components[i] <<
" NAN received from script.... " << v.toString();
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);
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);
85 template <
typename T> QVector<T> variantToVector(
const QVariant& value);
86 template <
typename T> QVector<T> bufferToVector(
const gpu::BufferView& view,
const char *hint =
"");
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 =
"");
92 gpu::BufferView clone(
const gpu::BufferView& input);
93 gpu::BufferView resized(
const gpu::BufferView& input, glm::uint32 numElements);
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)));