-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvertex.cpp
221 lines (165 loc) · 4.14 KB
/
vertex.cpp
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
219
220
221
#include "vertex.hpp"
#include <math.h>
#include "vec.hpp"
#include <vec/vec.hpp>
#include <half/half.hpp>
///http://aras-p.info/texts/CompactNormalStorage.html
/*half4 encode (half3 n, float3 view)
{
half p = sqrt(n.z*8+8);
return half4(n.xy/p + 0.5,0,0);
}*/
/*half3 decode (half2 enc, float3 view)
{
half2 fenc = enc*4-2;
half f = dot(fenc,fenc);
half g = sqrt(1-f/4);
half3 n;
n.xy = fenc*g;
n.z = 1-f/2;
return n;
}*/
//normalize(N.xy)*sqrt(N.z*0.5 + 0.5)
/*N.z=length2(G.xy)*2 - 1
N.xy=normalize(G.xy)*sqrt(1-N.z*N.z)*/
cl_float2 encode_normal(cl_float4 val)
{
val = normalise(val);
if(approx_equal(val.x, 0) && approx_equal(val.y, 0))
{
val.x = 0.01f;
}
/*float p = sqrt(val.z * 8 + 8);
return {val.x/p + 0.5f, val.y/p + 0.5f};*/
cl_float2 r = normalise((cl_float2){val.x, val.y});
float sqr = sqrt(std::max(val.z * 0.5f + 0.5f, 0.f));
cl_float2 ret;
ret.x = r.x * sqr;
ret.y = r.y * sqr;
return ret;
}
cl_float4 decode_normal(cl_float2 val)
{
/*cl_float2 fenc = {val.x * 4 - 2, val.y * 4 - 2};
float f = fenc.x * fenc.x + fenc.y * fenc.y;
float g = sqrt(1 - f/4);
cl_float4 n;
n.x = fenc.x * g;
n.y = fenc.y * g;
n.z = 1 - f/2;
n.w = 0;
return normalise(n);*/
cl_float2 r = normalise((cl_float2){val.x, val.y});
cl_float4 n;
n.w = 0;
n.z = (val.x * val.x + val.y * val.y) * 2 - 1;
n.x = r.x * sqrt(1 - n.z * n.z);
n.y = r.y * sqrt(1 - n.y * n.y);
return n;
}
cl_ushort2 float_to_short(cl_float2 val)
{
cl_ushort2 us;
for(int i=0; i<2; i++)
us.s[i] = ((val.s[i] + 1) / 2) * pow(2, 16)-1;
return us;
}
cl_float2 short_to_float(cl_ushort2 val)
{
cl_float2 ret;
for(int i=0; i<2; i++)
{
ret.s[i] = val.s[i];
ret.s[i] /= pow(2, 16)-1;
ret.s[i] *= 2;
ret.s[i] -= 1;
}
return ret;
}
cl_ushort float_to_half(cl_float val)
{
return half_float::detail::float2half<(std::float_round_style)(HALF_ROUND_STYLE)>(val);
}
cl_float half_to_float(cl_ushort val)
{
return half_float::detail::half2float(val);
}
cl_uint float2_to_packed_half(cl_float2 val)
{
cl_ushort x = float_to_half(val.x);
cl_ushort y = float_to_half(val.y);
return ((cl_uint)x << 16) | (cl_uint)y;
}
cl_float2 packed_half_to_float2(cl_uint h)
{
cl_ushort x = h >> 16;
cl_ushort y = h;
return {half_to_float(x), half_to_float(y)};
}
cl_float4 vertex::get_pos() const
{
//return {x, y, z, 0};
return pos;
}
cl_float4 vertex::get_normal() const
{
//return decode_normal(short_to_float(normal));
return normal;
}
cl_float2 vertex::get_vt() const
{
//return packed_half_to_float2(vt);
return vt;
}
cl_uint vertex::get_pad() const
{
return pad;
}
cl_uint vertex::get_vertex_col() const
{
return vertex_col;
}
/*cl_uint vertex::get_pad2() const
{
return pad2;
}*/
void vertex::set_pos(cl_float4 val)
{
//x = val.x;
//y = val.y;
//z = val.z;
pos = val;
}
void vertex::set_normal(cl_float4 val)
{
//normal = float_to_short(encode_normal(val));
normal = val;
}
///ok new plan
///we can clamp to the range -1 -> 2
///gives us both directions of wraparound
///and then we can 2 -> 1
void vertex::set_vt(cl_float2 vtm)
{
/*vtm.x = vtm.x >= 1 ? 1.0f - (vtm.x - floor(vtm.x)) : vtm.x;
vtm.x = vtm.x < 0 ? 1.0f + fabs(vtm.x) - fabs(floor(vtm.x)) : vtm.x;
vtm.y = vtm.y >= 1 ? 1.0f - (vtm.y - floor(vtm.y)) : vtm.y;
vtm.y = vtm.y < 0 ? 1.0f + fabs(vtm.y) - fabs(floor(vtm.y)) : vtm.y;*/
//vt = float2_to_packed_half(vtm);
vt = vtm;
}
void vertex::set_pad(cl_uint val)
{
pad = val;
}
void vertex::set_vertex_col(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
vertex_col = (cl_uint)r << 32 - 8;
vertex_col |= (cl_uint)g << 32 - 16;
vertex_col |= (cl_uint)b << 32 - 24;
vertex_col |= (cl_uint)a;
}
/*void vertex::set_pad2(cl_uint val)
{
pad2 = val;
}*/