FCT
载入中...
搜索中...
未找到
VertexFactory.h
浏览该文件的文档.
1#pragma once
2#include <string>
3#include <vector>
4#include <cstring>
5#include <cstdlib>
6#include <stdexcept>
7#include <memory>
9#include "./DataTypes.h"
10
11
12namespace FCT {
13
14/*
15 struct VertexAttribute {
16 PipelineAttributeType type;
17 std::string name;
18 DataType dataType;
19 size_t offset;
20 bool flat;
21
22 VertexAttribute(PipelineAttributeType t, const std::string& n, DataType dt, size_t o)
23 : type(t), name(n), dataType(dt), offset(o) {
24 flat = false;
25 }
26 VertexAttribute(PipelineAttributeType t, const std::string& n, DataType dt, size_t o,bool f)
27 : type(t), name(n), dataType(dt), offset(o),flat(f) {
28
29 }
30 };
31
32 class VertexFactory;
33 class VertexData;
34 class VertexArray;
35
36 class VertexData {
37 public:
38 VertexData(const VertexFactory& factory);
39
40 ~VertexData() {
41 delete[] m_data;
42 }
43
44 VertexData(const VertexData&) = delete;
45 VertexData& operator=(const VertexData&) = delete;
46 VertexData(VertexData&&) = delete;
47 VertexData& operator=(VertexData&&) = delete;
48
49 template<typename T>
50 void setAttribute(const std::string& name, const T& value);
51 template<typename T>
52 T getAttribute(const std::string& name) const;
53
54 void* data() { return m_data; }
55 const void* data() const { return m_data; }
56 size_t getSize() const { return m_dataSize; }
57
58 private:
59 const VertexFactory* m_factory;
60 char* m_data;
61 size_t m_dataSize;
62
63 friend class VertexFactory;
64 };
65
66 class VertexFactory : public RefCount {
67 public:
68 VertexFactory() = default;
69
70 void addAttribute(PipelineAttributeType type, const std::string& name = "", DataType dataType = DataType::Float,bool flat = false);
71
72 const std::vector<VertexAttribute>& getAttributes() const {
73 return attributes;
74 }
75
76 size_t getStride() const;
77
78 VertexData* createVertex() const;
79
80 template<typename T>
81 void setAttributeData(VertexData& vertexData, const std::string& name, const T& value) const;
82
83 void setVertexData(VertexData& vertexData, const void* data, size_t dataSize) const;
84
85 const VertexAttribute& getAttribute(const std::string& name) const;
86 private:
87 std::vector<VertexAttribute> attributes;
88 };
89 template<typename T>
90 void VertexData::setAttribute(const std::string& name, const T& value) {
91 const auto& attr = m_factory->getAttribute(name);
92 if (GetDataTypeSize(attr.dataType) != sizeof(T)) {
93 throw std::runtime_error("Attribute size mismatch");
94 }
95 std::memcpy(static_cast<char*>(m_data) + attr.offset, &value, GetDataTypeSize(attr.dataType));
96 }
97
98 template<typename T>
99 T VertexData::getAttribute(const std::string& name) const {
100 const auto& attr = m_factory->getAttribute(name);
101 if (GetDataTypeSize(attr.dataType) != sizeof(T)) {
102 throw std::runtime_error("Attribute size mismatch");
103 }
104 T result;
105 std::memcpy(&result, static_cast<const char*>(m_data) + attr.offset, GetDataTypeSize(attr.dataType));
106 return result;
107 }
108
109 template<typename T>
110 void VertexFactory::setAttributeData(VertexData& vertexData, const std::string& name, const T& value) const {
111 for (const auto& attr : attributes) {
112 if (attr.name == name) {
113 if (GetDataTypeSize(attr.dataType) != sizeof(T)) {
114 throw std::runtime_error("属性大小不匹配");
115 }
116 std::memcpy((char*)vertexData.data() + attr.offset, &value, GetDataTypeSize(attr.dataType));
117 return;
118 }
119 }
120 throw std::runtime_error("未找到属性");
121 }
122*/
123} // namespace FCT