/
surface.hpp
191 lines (134 loc) · 4.74 KB
/
surface.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
#ifndef SOILLIB_UTIL_SURFACE
#define SOILLIB_UTIL_SURFACE
#include <soillib/soillib.hpp>
/*
additional methods to implement:
- gradient function
- higher detail normal vector (finite differences)
-
*/
// some lerp
// now what would make the most sense
// if there was some interface to perform entire
// operations on lerped sets!
namespace soil {
namespace surface {
// Surface Map Constraints
template<typename T>
concept surface_t = requires(T t){
{ t.height(glm::ivec2()) } -> std::same_as<float>;
{ t.oob(glm::ivec2()) } -> std::same_as<bool>;
};
// Surface Map Function Implementations
template<surface_t T>
const static inline float height(T& map, glm::vec2 p){
if(map.oob(p))
return 0.0f;
glm::ivec2 m = p;
if(map.oob(m + glm::ivec2(1)))
return map.height(m);
glm::vec2 f = glm::fract(p);
float h00 = map.height(m + glm::ivec2(0, 0));
float h01 = map.height(m + glm::ivec2(0, 1));
float h10 = map.height(m + glm::ivec2(1, 0));
float h11 = map.height(m + glm::ivec2(1, 1));
return (1.0f-f.y)*(h00*(1.0f-f.x) + h01*f.x) + f.y*(h10*(1.0f-f.x) + h11*f.x);
}
template<surface_t T>
const static inline glm::vec2 gradient(T& map, glm::ivec2 p){
glm::vec2 pxa = p;
if(!map.oob(p - glm::ivec2(1, 0)))
pxa -= glm::ivec2(1, 0);
glm::vec2 pxb = p;
if(!map.oob(p + glm::ivec2(1, 0)))
pxb += glm::ivec2(1, 0);
glm::vec2 pya = p;
if(!map.oob(p - glm::ivec2(0, 1)))
pya -= glm::ivec2(0, 1);
glm::vec2 pyb = p;
if(!map.oob(p + glm::ivec2(0, 1)))
pyb += glm::ivec2(0, 1);
// Compute Gradient
glm::vec2 g = glm::vec2(0, 0);
g.x = (map.height(pxb) - map.height(pxa))/length(pxb-pxa);
g.y = (map.height(pyb) - map.height(pya))/length(pyb-pya);
return g;
}
template<surface_t T>
const static inline glm::vec2 gradient_detailed(T& map, glm::ivec2 p){
// Generate the Finite Difference Samples
struct Point {
glm::ivec2 pos;
bool oob = true;
float height;
} px[5], py[5];
px[0].pos = p + glm::ivec2(-2, 0);
px[1].pos = p + glm::ivec2(-1, 0);
px[2].pos = p + glm::ivec2( 0, 0);
px[3].pos = p + glm::ivec2( 1, 0);
px[4].pos = p + glm::ivec2( 2, 0);
py[0].pos = p + glm::ivec2( 0,-2);
py[1].pos = p + glm::ivec2( 0,-1);
py[2].pos = p + glm::ivec2( 0, 0);
py[3].pos = p + glm::ivec2( 0, 1);
py[4].pos = p + glm::ivec2( 0, 2);
for(size_t i = 0; i < 5; i++){
if(!map.oob(px[i].pos)){
px[i].oob = false;
px[i].height = map.height(px[i].pos);
}
if(!map.oob(py[i].pos)){
py[i].oob = false;
py[i].height = map.height(py[i].pos);
}
}
// Compute Gradient
glm::vec2 g = glm::vec2(0, 0);
// X-Element
if(!px[0].oob && !px[4].oob)
g.x = (1.0f*px[0].height - 8.0f*px[1].height + 8.0f*px[3].height - 1.0f*px[4].height)/12.0f;
else if(!px[0].oob && !px[3].oob)
g.x = (1.0f*px[0].height - 6.0f*px[1].height + 3.0f*px[2].height + 2.0f*px[3].height)/6.0f;
else if(!px[0].oob && !px[2].oob)
g.x = (1.0f*px[0].height - 4.0f*px[1].height + 3.0f*px[2].height)/2.0f;
else if(!px[1].oob && !px[4].oob)
g.x = (-2.0f*px[1].height - 3.0f*px[2].height + 6.0f*px[3].height - 1.0f*px[4].height)/6.0f;
else if(!px[2].oob && !px[4].oob)
g.x = (-3.0f*px[2].height + 4.0f*px[3].height - 1.0f*px[4].height)/2.0f;
else if(!px[1].oob && !px[3].oob)
g.x = (-1.0f*px[1].height + 1.0f*px[3].height)/2.0f;
else if(!px[2].oob && !px[3].oob)
g.x = (-1.0f*px[2].height + 1.0f*px[3].height)/1.0f;
else if(!px[1].oob && !px[2].oob)
g.x = (-1.0f*px[1].height + 1.0f*px[2].height)/1.0f;
// Y-Element
if(!py[0].oob && !py[4].oob)
g.y = (1.0f*py[0].height - 8.0f*py[1].height + 8.0f*py[3].height - 1.0f*py[4].height)/12.0f;
else if(!py[0].oob && !py[3].oob)
g.y = (1.0f*py[0].height - 6.0f*py[1].height + 3.0f*py[2].height + 2.0f*py[3].height)/6.0f;
else if(!py[0].oob && !py[2].oob)
g.y = (1.0f*py[0].height - 4.0f*py[1].height + 3.0f*py[2].height)/2.0f;
else if(!py[1].oob && !py[4].oob)
g.y = (-2.0f*py[1].height - 3.0f*py[2].height + 6.0f*py[3].height - 1.0f*py[4].height)/6.0f;
else if(!py[2].oob && !py[4].oob)
g.y = (-3.0f*py[2].height + 4.0f*py[3].height - 1.0f*py[4].height)/2.0f;
else if(!py[1].oob && !py[3].oob)
g.y = (-1.0f*py[1].height + 1.0f*py[3].height)/2.0f;
else if(!py[2].oob && !py[3].oob)
g.y = (-1.0f*py[2].height + 1.0f*py[3].height)/1.0f;
else if(!py[1].oob && !py[2].oob)
g.y = (-1.0f*py[1].height + 1.0f*py[2].height)/1.0f;
return g;
}
// Surface Normal from Surface Gradient
template<surface_t T>
const static inline glm::vec3 normal(T& map, glm::ivec2 p){
const glm::vec2 g = gradient_detailed(map, p);
glm::vec3 n = glm::vec3(-g.x, 1.0f, -g.y);
if(length(n) > 0)
n = normalize(n);
return n;
}
}; // end of namespace surface
}; // end of namespace soil
#endif