summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/41-tess/isubd.sh
blob: 32100b53a46332cd687b21bfa7ffb6cae45fec3d (plain) (blame)
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
uint findMSB_(uint x)
{
	uint i;
	uint mask;
	uint res = -1;

	for (i = 0; i < 32; i++)
	{
		mask = 0x80000000 >> i;

		if ((x & mask) != 0)
		{
			res = 31 - i;
			break;
		}
	}

	return res;
}

uint parentKey(in uint key)
{
	return (key >> 1u);
}

void childrenKeys(in uint key, out uint children[2])
{
	children[0] = (key << 1u) | 0u;
	children[1] = (key << 1u) | 1u;
}

bool isRootKey(in uint key)
{
	return (key == 1u);
}

bool isLeafKey(in uint key)
{
	return findMSB_(key) == 31;
}

bool isChildZeroKey(in uint key)
{
	return ((key & 1u) == 0u);
}

// barycentric interpolation
vec3 berp(in vec3 v[3], in vec2 u)
{
	return v[0] + u.x * (v[1] - v[0]) + u.y * (v[2] - v[0]);
}

vec4 berp(in vec4 v[3], in vec2 u)
{
	return v[0] + u.x * (v[1] - v[0]) + u.y * (v[2] - v[0]);
}

// get xform from bit value
mat3 bitToXform(in uint bit)
{
	float b = float(bit);
	float c = 1.0f - b;

	vec3 c1 = vec3(0.0f, c   , b   );
	vec3 c2 = vec3(0.5f, b   , 0.0f);
	vec3 c3 = vec3(0.5f, 0.0f, c   );

	return mtxFromCols(c1, c2, c3);
}

// get xform from key
mat3 keyToXform(in uint key)
{
	vec3 c1 = vec3(1.0f, 0.0f, 0.0f);
	vec3 c2 = vec3(0.0f, 1.0f, 0.0f);
	vec3 c3 = vec3(0.0f, 0.0f, 1.0f);

	mat3 xf = mtxFromCols(c1, c2, c3);

	while (key > 1u) {
		xf = mul(xf, bitToXform(key & 1u));
		key = key >> 1u;
	}

	return xf;
}

// get xform from key as well as xform from parent key
mat3 keyToXform(in uint key, out mat3 xfp)
{
	xfp = keyToXform(parentKey(key));
	return keyToXform(key);
}

// subdivision routine (vertex position only)
void subd(in uint key, in vec4 v_in[3], out vec4 v_out[3])
{
	mat3 xf = keyToXform(key);

	mat4x3 m = mtxFromRows(v_in[0], v_in[1], v_in[2]);

	mat4x3 v = mul(xf, m);

	v_out[0] = mtxGetRow(v, 0);
	v_out[1] = mtxGetRow(v, 1);
	v_out[2] = mtxGetRow(v, 2);
}

// subdivision routine (vertex position only)
// also computes parent position
void subd(in uint key, in vec4 v_in[3], out vec4 v_out[3], out vec4 v_out_p[3])
{
	mat3 xfp; mat3 xf = keyToXform(key, xfp);

	mat4x3 m = mtxFromRows(v_in[0], v_in[1], v_in[2]);

	mat4x3 v = mul(xf, m);
	mat4x3 vp = mul(xfp, m);

	v_out[0] = mtxGetRow(v, 0);
	v_out[1] = mtxGetRow(v, 1);
	v_out[2] = mtxGetRow(v, 2);

	v_out_p[0] = mtxGetRow(vp, 0);
	v_out_p[1] = mtxGetRow(vp, 1);
	v_out_p[2] = mtxGetRow(vp, 2);
}