-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.hpp
218 lines (178 loc) · 6.51 KB
/
shader.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#pragma once
#include "common_header.hpp"
#include <concepts>
#include <algorithm>
#include <memory>
#include <any>
#include "matrix.hpp"
#include "image.hpp"
class RasterizerInfo {
public:
Mat4 M;
Mat4 V;
Mat4 P;
Vec3 camera_pos;
Vec3 camera_dir;
Vec3 camera_top;
RasterizerInfo operator+(const RasterizerInfo& rhs) const {
return *this;
}
RasterizerInfo operator*(float rhs) const {
return *this;
}
};
class AbstractInterpolatable {
public:
virtual ~AbstractInterpolatable() {}
};
template <typename T>
concept Interpolatable = requires(const T& a, float k) {
std::derived_from<T, AbstractInterpolatable>;
{ a * k } -> std::same_as<T>;
{ a + a } -> std::same_as<T>;
};
class AbstractFragment {
public:
Vec3 pos_world;
virtual AbstractInterpolatable& getProperties() = 0;
virtual const AbstractInterpolatable& getProperties() const = 0;
virtual ~AbstractFragment() {}
};
template <typename P>
requires Interpolatable<P>
class Fragment: public AbstractFragment {
public:
P properties;
Fragment() = default;
Fragment(const P& properties): properties(properties) {}
// 1. 对吗? 2. 能用模板简化吗?
Fragment(P&& properties): properties(properties) {}
virtual AbstractInterpolatable& getProperties() override { return properties; }
virtual const AbstractInterpolatable& getProperties() const override { return properties; }
};
class AbstractVertex {
public:
Vec3 pos_model;
AbstractVertex(): pos_model(Vec3()) {}
AbstractVertex(const Vec3& pos_model): pos_model(pos_model) {}
virtual ~AbstractVertex(){}
virtual AbstractInterpolatable& getProperties() = 0;
virtual const AbstractInterpolatable& getProperties() const = 0;
virtual size_t fragment_size() const = 0;
virtual AbstractFragment& linear_interpolation(
float k2, const AbstractVertex& v2,
float k3, const AbstractVertex& v3,
void* mem
) const = 0;
};
template <typename P>
requires Interpolatable<P>
class Vertex: public AbstractVertex {
public:
Vertex() = default;
Vertex(const Vec3& pos_model, const P& properties): AbstractVertex(pos_model), properties(properties) {}
P properties;
virtual AbstractInterpolatable& getProperties() override { return properties; }
virtual const AbstractInterpolatable& getProperties() const override { return properties; }
virtual size_t fragment_size() const { return sizeof(Fragment<P>); }
virtual AbstractFragment& linear_interpolation(
float k2, const AbstractVertex& v2,
float k3, const AbstractVertex& v3,
void* mem
) const {
float k1 = 1 - k2 - k3;
const auto& v1 = *this;
auto& p1 = dynamic_cast<const Vertex&>(v1).properties;
auto& p2 = dynamic_cast<const Vertex&>(v2).properties;
auto& p3 = dynamic_cast<const Vertex&>(v3).properties;
auto p = p1 * k1 + p2 * k2 + p3 * k3;
auto frag = new (mem) Fragment(std::move(p));
// frag->pos = this->pos * k1 + v2.pos * k2 + v3.pos * k3;
return *frag;
}
};
// TODO:Object<DerivedUniform> < Object<BaseUniform>
class AbstractFShader {
public:
virtual ~AbstractFShader() {}
virtual RGBAColor shade(const AbstractFragment& fragment, const std::any& uniform) const = 0;
};
template <typename P, typename Uniform>
requires Interpolatable<P>
class FShader: public AbstractFShader {
public:
// Shader 因此是一个抽象类,无法实例化。TODO:可以看一下如何用 Module 阻止用户直接继承 AbstractShader
virtual RGBAColor shade(const Fragment<P>& fragment, const Uniform& uniform) const = 0;
virtual RGBAColor shade(const AbstractFragment& fragment, const std::any& uniform) const override {
auto& n_frag = dynamic_cast<const Fragment<P>&>(fragment);
auto n_uniform = std::any_cast<Uniform>(uniform);
return shade(n_frag, n_uniform);
}
};
// how to use: subclassing a specific Shader<T, U> and than write the shade function
// TODO:Object<DerivedUniform> < Object<BaseUniform>
class AbstractVShader {
public:
virtual ~AbstractVShader() {}
virtual AbstractVertex& shade(const std::any& data, const std::any& uniform, const RasterizerInfo& info, void* mem) const = 0;
virtual size_t vertexSize() const = 0;
};
template <typename VertexDataT, typename P, typename Uniform>
requires Interpolatable<P>
class VShader: public AbstractVShader {
public:
virtual Vertex<P> shade(const VertexDataT& data, const Uniform& uniform, const RasterizerInfo& info) const = 0;
virtual size_t vertexSize() const override {
return sizeof(Vertex<P>);
}
// TODO 这个 M 应该怎么处理比较好
virtual AbstractVertex& shade(
const std::any& data,
const std::any& uniform,
const RasterizerInfo& info,
void* mem
) const {
auto n_data = std::any_cast<VertexDataT>(data);
auto n_uniform = std::any_cast<Uniform>(uniform);
Vertex<P> vertex = shade(n_data, n_uniform, info);
// memcpy(mem, &vertex, sizeof(vertex));
return *static_cast<AbstractVertex*>(new (mem) Vertex<P>(vertex));
}
};
class AbstractObject {
public:
std::vector<std::tuple<int, int, int>> triangles;
virtual const std::any getVertexData(int) const = 0;
virtual const AbstractVShader& getVShader() const = 0;
virtual const AbstractFShader& getFShader() const = 0;
virtual ~AbstractObject() {}
};
// TODO: 没必要和 ShaderT 绑定了
template <typename A, typename P, typename Uniform, typename VShaderT, typename FShaderT>
requires Interpolatable<P> && std::derived_from<FShaderT, FShader<P, Uniform>>
&& std::derived_from<VShaderT, VShader<A, P, Uniform>>
class Object: public AbstractObject {
public:
VShaderT vshader;
FShaderT fshader;
std::vector<A> vertices;
Object() {}
virtual const std::any getVertexData(int index) const override {
A vert = vertices[index];
std::any val = vert;
return val;
}
virtual const AbstractVShader& getVShader() const override {
return vshader;
}
virtual const AbstractFShader& getFShader() const override {
return fshader;
}
};
class NothingUniform {};
class NothingProperty: public AbstractInterpolatable {
public:
NothingProperty inversed() const { return *this; }
NothingProperty operator*(float) const { return *this; }
NothingProperty operator+(NothingProperty) const { return *this; }
};