-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometryShader.glsl
90 lines (71 loc) · 2.26 KB
/
geometryShader.glsl
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
#version 430 core
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
uniform vec3 viewPos;
uniform vec3 lightPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform sampler2D heightsTexture;
uniform sampler2D normalTexture;
out vec3 tangentLightPos;
out vec3 tangentViewPos;
out vec3 tangentFragPos;
in TE_OUT {
vec3 FragPos;
vec2 TexCoords;
vec3 TangentLightPos;
vec3 TangentViewPos;
vec3 TangentFragPos;
float incUV;
} gs_in[];
out GS_OUT {
vec3 FragPos;
vec2 TexCoords;
vec3 TangentLightPos;
vec3 TangentViewPos;
vec3 TangentFragPos;
vec3 normal;
float hBase;
vec3 T;
vec3 B;
vec3 N;
float incUV;
} gs_out;
void main(void) {
vec3 edge1 = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
vec3 edge2 = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
vec2 deltaUV1 = gs_in[1].TexCoords - gs_in[0].TexCoords;
vec2 deltaUV2 = gs_in[2].TexCoords - gs_in[0].TexCoords;
float f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
vec3 tang;
vec3 bitang;
tang.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
tang.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
tang.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
bitang.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x);
bitang.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y);
bitang.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
vec3 normal = cross(bitang, tang);
vec3 T = normalize(mat3(model) * tang);
vec3 B = normalize(mat3(model) * bitang);
vec3 N = normalize(mat3(model) * normal);
mat3 TBN = transpose(mat3(T, B, N));
for( int i=0; i < gl_in.length( ); i++ ) {
gs_out.TexCoords = gs_in[i].TexCoords;
gs_out.hBase = gl_in[i].gl_Position.y / 10;
gs_out.normal = normalize(normal);
gs_out.T = tang;
gs_out.B = bitang;
gs_out.N = normalize(normal);
gl_Position = projection * view * model * vec4(gl_in[i].gl_Position.xyz, 1.f);
vec3 fragPos = vec3(model * vec4(gl_in[i].gl_Position.xyz, 1.0));
gs_out.TangentFragPos = TBN * fragPos;
gs_out.TangentLightPos = TBN * lightPos;
gs_out.TangentViewPos = TBN * viewPos;
gs_out.incUV = gs_in[i].incUV;
gs_out.FragPos = fragPos;
EmitVertex();
}
EndPrimitive( );
}