YoonaAI commited on
Commit
0ae44e3
·
1 Parent(s): acc89ee

Upload 10 files

Browse files
lib/renderer/gl/data/color.fs ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #version 330 core
2
+
3
+ layout (location = 0) out vec4 FragColor;
4
+ layout (location = 1) out vec4 FragNormal;
5
+ layout (location = 2) out vec4 FragDepth;
6
+
7
+ in vec3 Color;
8
+ in vec3 CamNormal;
9
+ in vec3 depth;
10
+
11
+
12
+ void main()
13
+ {
14
+ FragColor = vec4(Color,1.0);
15
+
16
+ vec3 cam_norm_normalized = normalize(CamNormal);
17
+ vec3 rgb = (cam_norm_normalized + 1.0) / 2.0;
18
+ FragNormal = vec4(rgb, 1.0);
19
+ FragDepth = vec4(depth.xyz, 1.0);
20
+ }
lib/renderer/gl/data/color.vs ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #version 330 core
2
+
3
+ layout (location = 0) in vec3 a_Position;
4
+ layout (location = 1) in vec3 a_Color;
5
+ layout (location = 2) in vec3 a_Normal;
6
+
7
+ out vec3 CamNormal;
8
+ out vec3 CamPos;
9
+ out vec3 Color;
10
+ out vec3 depth;
11
+
12
+
13
+ uniform mat3 RotMat;
14
+ uniform mat4 NormMat;
15
+ uniform mat4 ModelMat;
16
+ uniform mat4 PerspMat;
17
+
18
+ void main()
19
+ {
20
+ vec3 a_Position = (NormMat * vec4(a_Position,1.0)).xyz;
21
+ gl_Position = PerspMat * ModelMat * vec4(RotMat * a_Position, 1.0);
22
+ Color = a_Color;
23
+
24
+ mat3 R = mat3(ModelMat) * RotMat;
25
+ CamNormal = (R * a_Normal);
26
+
27
+ depth = vec3(gl_Position.z / gl_Position.w);
28
+
29
+ }
lib/renderer/gl/data/normal.fs ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #version 330
2
+
3
+ out vec4 FragColor;
4
+
5
+ in vec3 CamNormal;
6
+
7
+ void main()
8
+ {
9
+ vec3 cam_norm_normalized = normalize(CamNormal);
10
+ vec3 rgb = (cam_norm_normalized + 1.0) / 2.0;
11
+ FragColor = vec4(rgb, 1.0);
12
+ }
lib/renderer/gl/data/normal.vs ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #version 330
2
+
3
+ layout (location = 0) in vec3 Position;
4
+ layout (location = 1) in vec3 Normal;
5
+
6
+ out vec3 CamNormal;
7
+
8
+ uniform mat4 ModelMat;
9
+ uniform mat4 PerspMat;
10
+
11
+ void main()
12
+ {
13
+ gl_Position = PerspMat * ModelMat * vec4(Position, 1.0);
14
+ CamNormal = (ModelMat * vec4(Normal, 0.0)).xyz;
15
+ }
lib/renderer/gl/data/prt.fs ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #version 330
2
+
3
+ uniform vec3 SHCoeffs[9];
4
+ uniform uint analytic;
5
+
6
+ uniform uint hasNormalMap;
7
+ uniform uint hasAlbedoMap;
8
+
9
+ uniform sampler2D AlbedoMap;
10
+ uniform sampler2D NormalMap;
11
+
12
+ in VertexData {
13
+ vec3 Position;
14
+ vec3 Depth;
15
+ vec3 ModelNormal;
16
+ vec2 Texcoord;
17
+ vec3 Tangent;
18
+ vec3 Bitangent;
19
+ vec3 PRT1;
20
+ vec3 PRT2;
21
+ vec3 PRT3;
22
+ vec3 Label;
23
+ } VertexIn;
24
+
25
+ layout (location = 0) out vec4 FragColor;
26
+ layout (location = 1) out vec4 FragNormal;
27
+ layout (location = 2) out vec4 FragPosition;
28
+ layout (location = 3) out vec4 FragAlbedo;
29
+ layout (location = 4) out vec4 FragShading;
30
+ layout (location = 5) out vec4 FragPRT1;
31
+ layout (location = 6) out vec4 FragPRT2;
32
+ // layout (location = 7) out vec4 FragPRT3;
33
+ layout (location = 7) out vec4 FragLabel;
34
+
35
+
36
+ vec4 gammaCorrection(vec4 vec, float g)
37
+ {
38
+ return vec4(pow(vec.x, 1.0/g), pow(vec.y, 1.0/g), pow(vec.z, 1.0/g), vec.w);
39
+ }
40
+
41
+ vec3 gammaCorrection(vec3 vec, float g)
42
+ {
43
+ return vec3(pow(vec.x, 1.0/g), pow(vec.y, 1.0/g), pow(vec.z, 1.0/g));
44
+ }
45
+
46
+ void evaluateH(vec3 n, out float H[9])
47
+ {
48
+ float c1 = 0.429043, c2 = 0.511664,
49
+ c3 = 0.743125, c4 = 0.886227, c5 = 0.247708;
50
+
51
+ H[0] = c4;
52
+ H[1] = 2.0 * c2 * n[1];
53
+ H[2] = 2.0 * c2 * n[2];
54
+ H[3] = 2.0 * c2 * n[0];
55
+ H[4] = 2.0 * c1 * n[0] * n[1];
56
+ H[5] = 2.0 * c1 * n[1] * n[2];
57
+ H[6] = c3 * n[2] * n[2] - c5;
58
+ H[7] = 2.0 * c1 * n[2] * n[0];
59
+ H[8] = c1 * (n[0] * n[0] - n[1] * n[1]);
60
+ }
61
+
62
+ vec3 evaluateLightingModel(vec3 normal)
63
+ {
64
+ float H[9];
65
+ evaluateH(normal, H);
66
+ vec3 res = vec3(0.0);
67
+ for (int i = 0; i < 9; i++) {
68
+ res += H[i] * SHCoeffs[i];
69
+ }
70
+ return res;
71
+ }
72
+
73
+ // nC: coarse geometry normal, nH: fine normal from normal map
74
+ vec3 evaluateLightingModelHybrid(vec3 nC, vec3 nH, mat3 prt)
75
+ {
76
+ float HC[9], HH[9];
77
+ evaluateH(nC, HC);
78
+ evaluateH(nH, HH);
79
+
80
+ vec3 res = vec3(0.0);
81
+ vec3 shadow = vec3(0.0);
82
+ vec3 unshadow = vec3(0.0);
83
+ for(int i = 0; i < 3; ++i){
84
+ for(int j = 0; j < 3; ++j){
85
+ int id = i*3+j;
86
+ res += HH[id]* SHCoeffs[id];
87
+ shadow += prt[i][j] * SHCoeffs[id];
88
+ unshadow += HC[id] * SHCoeffs[id];
89
+ }
90
+ }
91
+ vec3 ratio = clamp(shadow/unshadow,0.0,1.0);
92
+ res = ratio * res;
93
+
94
+ return res;
95
+ }
96
+
97
+ vec3 evaluateLightingModelPRT(mat3 prt)
98
+ {
99
+ vec3 res = vec3(0.0);
100
+ for(int i = 0; i < 3; ++i){
101
+ for(int j = 0; j < 3; ++j){
102
+ res += prt[i][j] * SHCoeffs[i*3+j];
103
+ }
104
+ }
105
+
106
+ return res;
107
+ }
108
+
109
+ void main()
110
+ {
111
+ vec2 uv = VertexIn.Texcoord;
112
+ vec3 nC = normalize(VertexIn.ModelNormal);
113
+ vec3 nml = nC;
114
+ mat3 prt = mat3(VertexIn.PRT1, VertexIn.PRT2, VertexIn.PRT3);
115
+
116
+ if(hasAlbedoMap == uint(0))
117
+ FragAlbedo = vec4(1.0);
118
+ else
119
+ FragAlbedo = texture(AlbedoMap, uv);//gammaCorrection(texture(AlbedoMap, uv), 1.0/2.2);
120
+
121
+ if(hasNormalMap == uint(0))
122
+ {
123
+ if(analytic == uint(0))
124
+ FragShading = vec4(evaluateLightingModelPRT(prt), 1.0f);
125
+ else
126
+ FragShading = vec4(evaluateLightingModel(nC), 1.0f);
127
+ }
128
+ else
129
+ {
130
+ vec3 n_tan = normalize(texture(NormalMap, uv).rgb*2.0-vec3(1.0));
131
+
132
+ mat3 TBN = mat3(normalize(VertexIn.Tangent),normalize(VertexIn.Bitangent),nC);
133
+ vec3 nH = normalize(TBN * n_tan);
134
+
135
+ if(analytic == uint(0))
136
+ FragShading = vec4(evaluateLightingModelHybrid(nC,nH,prt),1.0f);
137
+ else
138
+ FragShading = vec4(evaluateLightingModel(nH), 1.0f);
139
+
140
+ nml = nH;
141
+ }
142
+
143
+ FragShading = gammaCorrection(FragShading, 2.2);
144
+ FragColor = clamp(FragAlbedo * FragShading, 0.0, 1.0);
145
+ FragNormal = vec4(0.5*(nml+vec3(1.0)), 1.0);
146
+ FragPosition = vec4(VertexIn.Depth.xyz, 1.0);
147
+ FragShading = vec4(clamp(0.5*FragShading.xyz, 0.0, 1.0),1.0);
148
+ // FragColor = gammaCorrection(clamp(FragAlbedo * FragShading, 0.0, 1.0),2.2);
149
+ // FragNormal = vec4(0.5*(nml+vec3(1.0)), 1.0);
150
+ // FragPosition = vec4(VertexIn.Position,VertexIn.Depth.x);
151
+ // FragShading = vec4(gammaCorrection(clamp(0.5*FragShading.xyz, 0.0, 1.0),2.2),1.0);
152
+ // FragAlbedo = gammaCorrection(FragAlbedo,2.2);
153
+ FragPRT1 = vec4(VertexIn.PRT1,1.0);
154
+ FragPRT2 = vec4(VertexIn.PRT2,1.0);
155
+ // FragPRT3 = vec4(VertexIn.PRT3,1.0);
156
+ FragLabel = vec4(VertexIn.Label,1.0);
157
+ }
lib/renderer/gl/data/prt.vs ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #version 330
2
+
3
+ layout (location = 0) in vec3 a_Position;
4
+ layout (location = 1) in vec3 a_Normal;
5
+ layout (location = 2) in vec2 a_TextureCoord;
6
+ layout (location = 3) in vec3 a_Tangent;
7
+ layout (location = 4) in vec3 a_Bitangent;
8
+ layout (location = 5) in vec3 a_PRT1;
9
+ layout (location = 6) in vec3 a_PRT2;
10
+ layout (location = 7) in vec3 a_PRT3;
11
+ layout (location = 8) in vec3 a_Label;
12
+
13
+ out VertexData {
14
+ vec3 Position;
15
+ vec3 Depth;
16
+ vec3 ModelNormal;
17
+ vec2 Texcoord;
18
+ vec3 Tangent;
19
+ vec3 Bitangent;
20
+ vec3 PRT1;
21
+ vec3 PRT2;
22
+ vec3 PRT3;
23
+ vec3 Label;
24
+ } VertexOut;
25
+
26
+ uniform mat3 RotMat;
27
+ uniform mat4 NormMat;
28
+ uniform mat4 ModelMat;
29
+ uniform mat4 PerspMat;
30
+
31
+ float s_c3 = 0.94617469575; // (3*sqrt(5))/(4*sqrt(pi))
32
+ float s_c4 = -0.31539156525;// (-sqrt(5))/(4*sqrt(pi))
33
+ float s_c5 = 0.54627421529; // (sqrt(15))/(4*sqrt(pi))
34
+
35
+ float s_c_scale = 1.0/0.91529123286551084;
36
+ float s_c_scale_inv = 0.91529123286551084;
37
+
38
+ float s_rc2 = 1.5853309190550713*s_c_scale;
39
+ float s_c4_div_c3 = s_c4/s_c3;
40
+ float s_c4_div_c3_x2 = (s_c4/s_c3)*2.0;
41
+
42
+ float s_scale_dst2 = s_c3 * s_c_scale_inv;
43
+ float s_scale_dst4 = s_c5 * s_c_scale_inv;
44
+
45
+ void OptRotateBand0(float x[1], mat3 R, out float dst[1])
46
+ {
47
+ dst[0] = x[0];
48
+ }
49
+
50
+ // 9 multiplies
51
+ void OptRotateBand1(float x[3], mat3 R, out float dst[3])
52
+ {
53
+ // derived from SlowRotateBand1
54
+ dst[0] = ( R[1][1])*x[0] + (-R[1][2])*x[1] + ( R[1][0])*x[2];
55
+ dst[1] = (-R[2][1])*x[0] + ( R[2][2])*x[1] + (-R[2][0])*x[2];
56
+ dst[2] = ( R[0][1])*x[0] + (-R[0][2])*x[1] + ( R[0][0])*x[2];
57
+ }
58
+
59
+ // 48 multiplies
60
+ void OptRotateBand2(float x[5], mat3 R, out float dst[5])
61
+ {
62
+ // Sparse matrix multiply
63
+ float sh0 = x[3] + x[4] + x[4] - x[1];
64
+ float sh1 = x[0] + s_rc2*x[2] + x[3] + x[4];
65
+ float sh2 = x[0];
66
+ float sh3 = -x[3];
67
+ float sh4 = -x[1];
68
+
69
+ // Rotations. R0 and R1 just use the raw matrix columns
70
+ float r2x = R[0][0] + R[0][1];
71
+ float r2y = R[1][0] + R[1][1];
72
+ float r2z = R[2][0] + R[2][1];
73
+
74
+ float r3x = R[0][0] + R[0][2];
75
+ float r3y = R[1][0] + R[1][2];
76
+ float r3z = R[2][0] + R[2][2];
77
+
78
+ float r4x = R[0][1] + R[0][2];
79
+ float r4y = R[1][1] + R[1][2];
80
+ float r4z = R[2][1] + R[2][2];
81
+
82
+ // dense matrix multiplication one column at a time
83
+
84
+ // column 0
85
+ float sh0_x = sh0 * R[0][0];
86
+ float sh0_y = sh0 * R[1][0];
87
+ float d0 = sh0_x * R[1][0];
88
+ float d1 = sh0_y * R[2][0];
89
+ float d2 = sh0 * (R[2][0] * R[2][0] + s_c4_div_c3);
90
+ float d3 = sh0_x * R[2][0];
91
+ float d4 = sh0_x * R[0][0] - sh0_y * R[1][0];
92
+
93
+ // column 1
94
+ float sh1_x = sh1 * R[0][2];
95
+ float sh1_y = sh1 * R[1][2];
96
+ d0 += sh1_x * R[1][2];
97
+ d1 += sh1_y * R[2][2];
98
+ d2 += sh1 * (R[2][2] * R[2][2] + s_c4_div_c3);
99
+ d3 += sh1_x * R[2][2];
100
+ d4 += sh1_x * R[0][2] - sh1_y * R[1][2];
101
+
102
+ // column 2
103
+ float sh2_x = sh2 * r2x;
104
+ float sh2_y = sh2 * r2y;
105
+ d0 += sh2_x * r2y;
106
+ d1 += sh2_y * r2z;
107
+ d2 += sh2 * (r2z * r2z + s_c4_div_c3_x2);
108
+ d3 += sh2_x * r2z;
109
+ d4 += sh2_x * r2x - sh2_y * r2y;
110
+
111
+ // column 3
112
+ float sh3_x = sh3 * r3x;
113
+ float sh3_y = sh3 * r3y;
114
+ d0 += sh3_x * r3y;
115
+ d1 += sh3_y * r3z;
116
+ d2 += sh3 * (r3z * r3z + s_c4_div_c3_x2);
117
+ d3 += sh3_x * r3z;
118
+ d4 += sh3_x * r3x - sh3_y * r3y;
119
+
120
+ // column 4
121
+ float sh4_x = sh4 * r4x;
122
+ float sh4_y = sh4 * r4y;
123
+ d0 += sh4_x * r4y;
124
+ d1 += sh4_y * r4z;
125
+ d2 += sh4 * (r4z * r4z + s_c4_div_c3_x2);
126
+ d3 += sh4_x * r4z;
127
+ d4 += sh4_x * r4x - sh4_y * r4y;
128
+
129
+ // extra multipliers
130
+ dst[0] = d0;
131
+ dst[1] = -d1;
132
+ dst[2] = d2 * s_scale_dst2;
133
+ dst[3] = -d3;
134
+ dst[4] = d4 * s_scale_dst4;
135
+ }
136
+
137
+ void main()
138
+ {
139
+ // normalization
140
+ vec3 pos = (NormMat * vec4(a_Position,1.0)).xyz;
141
+
142
+ mat3 R = mat3(ModelMat) * RotMat;
143
+ VertexOut.ModelNormal = (R * a_Normal);
144
+ VertexOut.Position = R * pos;
145
+ VertexOut.Texcoord = a_TextureCoord;
146
+ VertexOut.Tangent = (R * a_Tangent);
147
+ VertexOut.Bitangent = (R * a_Bitangent);
148
+ VertexOut.Label = a_Label;
149
+
150
+ float PRT0, PRT1[3], PRT2[5];
151
+ PRT0 = a_PRT1[0];
152
+ PRT1[0] = a_PRT1[1];
153
+ PRT1[1] = a_PRT1[2];
154
+ PRT1[2] = a_PRT2[0];
155
+ PRT2[0] = a_PRT2[1];
156
+ PRT2[1] = a_PRT2[2];
157
+ PRT2[2] = a_PRT3[0];
158
+ PRT2[3] = a_PRT3[1];
159
+ PRT2[4] = a_PRT3[2];
160
+
161
+ OptRotateBand1(PRT1, R, PRT1);
162
+ OptRotateBand2(PRT2, R, PRT2);
163
+
164
+ VertexOut.PRT1 = vec3(PRT0,PRT1[0],PRT1[1]);
165
+ VertexOut.PRT2 = vec3(PRT1[2],PRT2[0],PRT2[1]);
166
+ VertexOut.PRT3 = vec3(PRT2[2],PRT2[3],PRT2[4]);
167
+
168
+ gl_Position = PerspMat * ModelMat * vec4(RotMat * pos, 1.0);
169
+
170
+ VertexOut.Depth = vec3(gl_Position.z / gl_Position.w);
171
+ }
lib/renderer/gl/data/prt_uv.fs ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #version 330
2
+
3
+ uniform vec3 SHCoeffs[9];
4
+ uniform uint analytic;
5
+
6
+ uniform uint hasNormalMap;
7
+ uniform uint hasAlbedoMap;
8
+
9
+ uniform sampler2D AlbedoMap;
10
+ uniform sampler2D NormalMap;
11
+
12
+ in VertexData {
13
+ vec3 Position;
14
+ vec3 ModelNormal;
15
+ vec3 CameraNormal;
16
+ vec2 Texcoord;
17
+ vec3 Tangent;
18
+ vec3 Bitangent;
19
+ vec3 PRT1;
20
+ vec3 PRT2;
21
+ vec3 PRT3;
22
+ } VertexIn;
23
+
24
+ layout (location = 0) out vec4 FragColor;
25
+ layout (location = 1) out vec4 FragPosition;
26
+ layout (location = 2) out vec4 FragNormal;
27
+
28
+ vec4 gammaCorrection(vec4 vec, float g)
29
+ {
30
+ return vec4(pow(vec.x, 1.0/g), pow(vec.y, 1.0/g), pow(vec.z, 1.0/g), vec.w);
31
+ }
32
+
33
+ vec3 gammaCorrection(vec3 vec, float g)
34
+ {
35
+ return vec3(pow(vec.x, 1.0/g), pow(vec.y, 1.0/g), pow(vec.z, 1.0/g));
36
+ }
37
+
38
+ void evaluateH(vec3 n, out float H[9])
39
+ {
40
+ float c1 = 0.429043, c2 = 0.511664,
41
+ c3 = 0.743125, c4 = 0.886227, c5 = 0.247708;
42
+
43
+ H[0] = c4;
44
+ H[1] = 2.0 * c2 * n[1];
45
+ H[2] = 2.0 * c2 * n[2];
46
+ H[3] = 2.0 * c2 * n[0];
47
+ H[4] = 2.0 * c1 * n[0] * n[1];
48
+ H[5] = 2.0 * c1 * n[1] * n[2];
49
+ H[6] = c3 * n[2] * n[2] - c5;
50
+ H[7] = 2.0 * c1 * n[2] * n[0];
51
+ H[8] = c1 * (n[0] * n[0] - n[1] * n[1]);
52
+ }
53
+
54
+ vec3 evaluateLightingModel(vec3 normal)
55
+ {
56
+ float H[9];
57
+ evaluateH(normal, H);
58
+ vec3 res = vec3(0.0);
59
+ for (int i = 0; i < 9; i++) {
60
+ res += H[i] * SHCoeffs[i];
61
+ }
62
+ return res;
63
+ }
64
+
65
+ // nC: coarse geometry normal, nH: fine normal from normal map
66
+ vec3 evaluateLightingModelHybrid(vec3 nC, vec3 nH, mat3 prt)
67
+ {
68
+ float HC[9], HH[9];
69
+ evaluateH(nC, HC);
70
+ evaluateH(nH, HH);
71
+
72
+ vec3 res = vec3(0.0);
73
+ vec3 shadow = vec3(0.0);
74
+ vec3 unshadow = vec3(0.0);
75
+ for(int i = 0; i < 3; ++i){
76
+ for(int j = 0; j < 3; ++j){
77
+ int id = i*3+j;
78
+ res += HH[id]* SHCoeffs[id];
79
+ shadow += prt[i][j] * SHCoeffs[id];
80
+ unshadow += HC[id] * SHCoeffs[id];
81
+ }
82
+ }
83
+ vec3 ratio = clamp(shadow/unshadow,0.0,1.0);
84
+ res = ratio * res;
85
+
86
+ return res;
87
+ }
88
+
89
+ vec3 evaluateLightingModelPRT(mat3 prt)
90
+ {
91
+ vec3 res = vec3(0.0);
92
+ for(int i = 0; i < 3; ++i){
93
+ for(int j = 0; j < 3; ++j){
94
+ res += prt[i][j] * SHCoeffs[i*3+j];
95
+ }
96
+ }
97
+
98
+ return res;
99
+ }
100
+
101
+ void main()
102
+ {
103
+ vec2 uv = VertexIn.Texcoord;
104
+ vec3 nM = normalize(VertexIn.ModelNormal);
105
+ vec3 nC = normalize(VertexIn.CameraNormal);
106
+ vec3 nml = nC;
107
+ mat3 prt = mat3(VertexIn.PRT1, VertexIn.PRT2, VertexIn.PRT3);
108
+
109
+ vec4 albedo, shading;
110
+ if(hasAlbedoMap == uint(0))
111
+ albedo = vec4(1.0);
112
+ else
113
+ albedo = texture(AlbedoMap, uv);//gammaCorrection(texture(AlbedoMap, uv), 1.0/2.2);
114
+
115
+ if(hasNormalMap == uint(0))
116
+ {
117
+ if(analytic == uint(0))
118
+ shading = vec4(evaluateLightingModelPRT(prt), 1.0f);
119
+ else
120
+ shading = vec4(evaluateLightingModel(nC), 1.0f);
121
+ }
122
+ else
123
+ {
124
+ vec3 n_tan = normalize(texture(NormalMap, uv).rgb*2.0-vec3(1.0));
125
+
126
+ mat3 TBN = mat3(normalize(VertexIn.Tangent),normalize(VertexIn.Bitangent),nC);
127
+ vec3 nH = normalize(TBN * n_tan);
128
+
129
+ if(analytic == uint(0))
130
+ shading = vec4(evaluateLightingModelHybrid(nC,nH,prt),1.0f);
131
+ else
132
+ shading = vec4(evaluateLightingModel(nH), 1.0f);
133
+
134
+ nml = nH;
135
+ }
136
+
137
+ shading = gammaCorrection(shading, 2.2);
138
+ FragColor = clamp(albedo * shading, 0.0, 1.0);
139
+ FragPosition = vec4(VertexIn.Position,1.0);
140
+ FragNormal = vec4(0.5*(nM+vec3(1.0)),1.0);
141
+ }
lib/renderer/gl/data/prt_uv.vs ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #version 330
2
+
3
+ layout (location = 0) in vec3 a_Position;
4
+ layout (location = 1) in vec3 a_Normal;
5
+ layout (location = 2) in vec2 a_TextureCoord;
6
+ layout (location = 3) in vec3 a_Tangent;
7
+ layout (location = 4) in vec3 a_Bitangent;
8
+ layout (location = 5) in vec3 a_PRT1;
9
+ layout (location = 6) in vec3 a_PRT2;
10
+ layout (location = 7) in vec3 a_PRT3;
11
+
12
+ out VertexData {
13
+ vec3 Position;
14
+ vec3 ModelNormal;
15
+ vec3 CameraNormal;
16
+ vec2 Texcoord;
17
+ vec3 Tangent;
18
+ vec3 Bitangent;
19
+ vec3 PRT1;
20
+ vec3 PRT2;
21
+ vec3 PRT3;
22
+ } VertexOut;
23
+
24
+ uniform mat3 RotMat;
25
+ uniform mat4 NormMat;
26
+ uniform mat4 ModelMat;
27
+ uniform mat4 PerspMat;
28
+
29
+ #define pi 3.1415926535897932384626433832795
30
+
31
+ float s_c3 = 0.94617469575; // (3*sqrt(5))/(4*sqrt(pi))
32
+ float s_c4 = -0.31539156525;// (-sqrt(5))/(4*sqrt(pi))
33
+ float s_c5 = 0.54627421529; // (sqrt(15))/(4*sqrt(pi))
34
+
35
+ float s_c_scale = 1.0/0.91529123286551084;
36
+ float s_c_scale_inv = 0.91529123286551084;
37
+
38
+ float s_rc2 = 1.5853309190550713*s_c_scale;
39
+ float s_c4_div_c3 = s_c4/s_c3;
40
+ float s_c4_div_c3_x2 = (s_c4/s_c3)*2.0;
41
+
42
+ float s_scale_dst2 = s_c3 * s_c_scale_inv;
43
+ float s_scale_dst4 = s_c5 * s_c_scale_inv;
44
+
45
+ void OptRotateBand0(float x[1], mat3 R, out float dst[1])
46
+ {
47
+ dst[0] = x[0];
48
+ }
49
+
50
+ // 9 multiplies
51
+ void OptRotateBand1(float x[3], mat3 R, out float dst[3])
52
+ {
53
+ // derived from SlowRotateBand1
54
+ dst[0] = ( R[1][1])*x[0] + (-R[1][2])*x[1] + ( R[1][0])*x[2];
55
+ dst[1] = (-R[2][1])*x[0] + ( R[2][2])*x[1] + (-R[2][0])*x[2];
56
+ dst[2] = ( R[0][1])*x[0] + (-R[0][2])*x[1] + ( R[0][0])*x[2];
57
+ }
58
+
59
+ // 48 multiplies
60
+ void OptRotateBand2(float x[5], mat3 R, out float dst[5])
61
+ {
62
+ // Sparse matrix multiply
63
+ float sh0 = x[3] + x[4] + x[4] - x[1];
64
+ float sh1 = x[0] + s_rc2*x[2] + x[3] + x[4];
65
+ float sh2 = x[0];
66
+ float sh3 = -x[3];
67
+ float sh4 = -x[1];
68
+
69
+ // Rotations. R0 and R1 just use the raw matrix columns
70
+ float r2x = R[0][0] + R[0][1];
71
+ float r2y = R[1][0] + R[1][1];
72
+ float r2z = R[2][0] + R[2][1];
73
+
74
+ float r3x = R[0][0] + R[0][2];
75
+ float r3y = R[1][0] + R[1][2];
76
+ float r3z = R[2][0] + R[2][2];
77
+
78
+ float r4x = R[0][1] + R[0][2];
79
+ float r4y = R[1][1] + R[1][2];
80
+ float r4z = R[2][1] + R[2][2];
81
+
82
+ // dense matrix multiplication one column at a time
83
+
84
+ // column 0
85
+ float sh0_x = sh0 * R[0][0];
86
+ float sh0_y = sh0 * R[1][0];
87
+ float d0 = sh0_x * R[1][0];
88
+ float d1 = sh0_y * R[2][0];
89
+ float d2 = sh0 * (R[2][0] * R[2][0] + s_c4_div_c3);
90
+ float d3 = sh0_x * R[2][0];
91
+ float d4 = sh0_x * R[0][0] - sh0_y * R[1][0];
92
+
93
+ // column 1
94
+ float sh1_x = sh1 * R[0][2];
95
+ float sh1_y = sh1 * R[1][2];
96
+ d0 += sh1_x * R[1][2];
97
+ d1 += sh1_y * R[2][2];
98
+ d2 += sh1 * (R[2][2] * R[2][2] + s_c4_div_c3);
99
+ d3 += sh1_x * R[2][2];
100
+ d4 += sh1_x * R[0][2] - sh1_y * R[1][2];
101
+
102
+ // column 2
103
+ float sh2_x = sh2 * r2x;
104
+ float sh2_y = sh2 * r2y;
105
+ d0 += sh2_x * r2y;
106
+ d1 += sh2_y * r2z;
107
+ d2 += sh2 * (r2z * r2z + s_c4_div_c3_x2);
108
+ d3 += sh2_x * r2z;
109
+ d4 += sh2_x * r2x - sh2_y * r2y;
110
+
111
+ // column 3
112
+ float sh3_x = sh3 * r3x;
113
+ float sh3_y = sh3 * r3y;
114
+ d0 += sh3_x * r3y;
115
+ d1 += sh3_y * r3z;
116
+ d2 += sh3 * (r3z * r3z + s_c4_div_c3_x2);
117
+ d3 += sh3_x * r3z;
118
+ d4 += sh3_x * r3x - sh3_y * r3y;
119
+
120
+ // column 4
121
+ float sh4_x = sh4 * r4x;
122
+ float sh4_y = sh4 * r4y;
123
+ d0 += sh4_x * r4y;
124
+ d1 += sh4_y * r4z;
125
+ d2 += sh4 * (r4z * r4z + s_c4_div_c3_x2);
126
+ d3 += sh4_x * r4z;
127
+ d4 += sh4_x * r4x - sh4_y * r4y;
128
+
129
+ // extra multipliers
130
+ dst[0] = d0;
131
+ dst[1] = -d1;
132
+ dst[2] = d2 * s_scale_dst2;
133
+ dst[3] = -d3;
134
+ dst[4] = d4 * s_scale_dst4;
135
+ }
136
+
137
+ void main()
138
+ {
139
+ // normalization
140
+ mat3 R = mat3(ModelMat) * RotMat;
141
+ VertexOut.ModelNormal = a_Normal;
142
+ VertexOut.CameraNormal = (R * a_Normal);
143
+ VertexOut.Position = a_Position;
144
+ VertexOut.Texcoord = a_TextureCoord;
145
+ VertexOut.Tangent = (R * a_Tangent);
146
+ VertexOut.Bitangent = (R * a_Bitangent);
147
+ float PRT0, PRT1[3], PRT2[5];
148
+ PRT0 = a_PRT1[0];
149
+ PRT1[0] = a_PRT1[1];
150
+ PRT1[1] = a_PRT1[2];
151
+ PRT1[2] = a_PRT2[0];
152
+ PRT2[0] = a_PRT2[1];
153
+ PRT2[1] = a_PRT2[2];
154
+ PRT2[2] = a_PRT3[0];
155
+ PRT2[3] = a_PRT3[1];
156
+ PRT2[4] = a_PRT3[2];
157
+
158
+ OptRotateBand1(PRT1, R, PRT1);
159
+ OptRotateBand2(PRT2, R, PRT2);
160
+
161
+ VertexOut.PRT1 = vec3(PRT0,PRT1[0],PRT1[1]);
162
+ VertexOut.PRT2 = vec3(PRT1[2],PRT2[0],PRT2[1]);
163
+ VertexOut.PRT3 = vec3(PRT2[2],PRT2[3],PRT2[4]);
164
+
165
+ gl_Position = vec4(a_TextureCoord, 0.0, 1.0) - vec4(0.5, 0.5, 0, 0);
166
+ gl_Position[0] *= 2.0;
167
+ gl_Position[1] *= 2.0;
168
+ }
lib/renderer/gl/data/quad.fs ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #version 330 core
2
+ out vec4 FragColor;
3
+
4
+ in vec2 TexCoord;
5
+
6
+ uniform sampler2D screenTexture;
7
+
8
+ void main()
9
+ {
10
+ FragColor = texture(screenTexture, TexCoord);
11
+ }
lib/renderer/gl/data/quad.vs ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #version 330 core
2
+ layout (location = 0) in vec2 aPos;
3
+ layout (location = 1) in vec2 aTexCoord;
4
+
5
+ out vec2 TexCoord;
6
+
7
+ void main()
8
+ {
9
+ gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
10
+ TexCoord = aTexCoord;
11
+ }