Adding a limit to the number of primitves exiting my geometry shader

stops the shape from breaking...
This commit is contained in:
Warwick 2022-07-29 13:01:01 +01:00
parent ecf7929152
commit b1c966b47d
2 changed files with 55 additions and 20 deletions

View file

@ -1,6 +1,6 @@
#version 330 core
layout (triangles) in;
layout (triangle_strip) out;
layout (triangle_strip, max_vertices = 3) out;
uniform mat4 Model;
@ -12,7 +12,16 @@ out vec2 texCoord;
out vec3 normCoord;
out vec3 WorldPos;
out mat3 TBN;
vec3 lightPosition = vec3(1, 1, 2);
uniform vec3 CameraPos;
out GS_OUT {
mat3 TBN;
vec3 tangentLightPos;
vec3 tangentViewPos;
vec3 tangentFragPos;
} gs_out;
void main(void)
{
@ -38,20 +47,24 @@ void main(void)
// [ddTxBxTyByTzBz]=1ΔU1ΔV2ΔU2ΔV1[ΔV2ΔU2ΔV1ΔU1][E1xE2xE1yE2yE1zE2z]
// https://learnopengl.com/Advanced-Lighting/Normal-Mapping
float f = 1.0 / (duv1.x * duv2.y - duv2.x * duv1.y);
float invDet = 1.0 / (duv1.x * duv2.y - duv2.x * duv1.y);
float tx = f * (duv2.y * edge1.x - duv1.y * edge2.x);
float ty = f * (duv2.y * edge1.y - duv1.y * edge2.y);
float tz = f * (duv2.y * edge1.z - duv1.y * edge2.z);
vec3 tangent = vec3(tx, ty, tz);
vec3 tangent = vec3 (invDet * (duv2.y * edge1 - duv1.y * edge2));
vec3 bitangent = vec3 (invDet * (-duv2.x * edge1 - duv1.x * edge2));
//Calculate TBN
vec3 T = normalize(vec3(Model * vec4(tangent, 0.0)));
vec3 N = normalize(vec3(Model * vec4(gnormCoord[0], 0.0)));
vec3 B = cross(N, T);
//mat3 normalMatrix = transpose(inverse(mat3(Model)));
TBN = mat3(T, B, N);
vec3 T = normalize(vec3(Model * vec4(tangent, 0.0f)));
vec3 B = normalize(vec3(Model * vec4(bitangent, 0.0f)));
vec3 N = normalize(vec3(Model * vec4(cross(edge2, edge1), 0.0f)));
mat3 TBN = transpose(mat3(T, B, N));
gs_out.TBN = TBN;
gs_out.tangentLightPos = TBN * lightPosition;
gs_out.tangentViewPos = TBN * CameraPos;
gs_out.tangentFragPos = TBN * gWorldPos[0];
// send data to Fragment Shader
gl_Position = gl_in[0].gl_Position;
@ -64,12 +77,16 @@ void main(void)
texCoord = gtexCoord[1];
normCoord = gnormCoord[1];
WorldPos = gWorldPos[1];
gs_out.tangentFragPos = TBN * gWorldPos[1];
EmitVertex();
gl_Position = gl_in[2].gl_Position;
texCoord = gtexCoord[2];
normCoord = gnormCoord[2];
WorldPos = gWorldPos[2];
gs_out.tangentFragPos = TBN * gWorldPos[2];
EmitVertex();
EndPrimitive();
};

View file

@ -4,6 +4,14 @@ out vec4 FragColor;
in vec2 texCoord;
in vec3 normCoord;
in vec3 WorldPos;
in GS_OUT {
mat3 TBN;
vec3 tangentLightPos;
vec3 tangentViewPos;
vec3 tangentFragPos;
} from_gs;
in mat3 TBN;
// TODO: make temporary hard coded world/camera pos dynamic
@ -71,27 +79,34 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
return ggx1 * ggx2;
}
vec3 normal(){
vec3 normalMapNormal(){
// load and invert normal
vec3 normal = texture(texture_normal1, texCoord).rgb * 2.0 - 1.0;
normal = normalize(TBN * normal);
vec3 normal = normalize(texture(texture_normal1, texCoord).rgb * 2.0 - 1.0);
return normal;
vec3 lightDir = normalize(from_gs.tangentLightPos - from_gs.tangentFragPos);
vec3 viewDir = normalize(from_gs.tangentViewPos - from_gs.tangentFragPos);
vec3 reflectDir = reflect(-lightDir, normal);
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(normal, halfwayDir), 0.0),32.0);
vec3 normMapSpecular = vec3(0.2) * spec;
return normMapSpecular;
}
vec3 PBR(vec3 albedo, float roughness, float metallic, float ao)
{
// Establish a temporary hard coded light position
vec3 lightPosition = vec3( 1, 1, 2);
vec3 lightPosition = vec3(1, 1, 2);
//vec3 lightPosition = vec3( (sin(tick / 1000.0)*2), 1 + sin(tick / 600.0)*2, 2.0);
//vec3 lightColor = vec3(1.0, 1.0, 1.0) - sin(tick / 90);
vec3 lightColor = vec3(13.47, 11.31, 10.79);
vec3 N = normalize(normCoord);
vec3 V = normalize(CameraPos - WorldPos);
//N = (N + normal()) / 2;
//N = normal(); //For seeing if normal map tracks with light.
//N = (N + normalMapNormal()) / 2;
//N = normalMapNormal(); //For seeing if normal map tracks with light.
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);
@ -140,5 +155,8 @@ void main()
float ao = texture(texture_rma1, texCoord).b;
//FragColor = vec4(PBR(albedo, roughness, metallic, ao), 1.0);
FragColor = vec4(normal(), 1.0);
FragColor = vec4(PBR(albedo, roughness, metallic, ao) + normalMapNormal(), 1.0);
//FragColor = vec4(normalMapNormal(), 1.0);
//FragColor = vec4(vec3(0.5) + normalMapNormal(), 1.0);
//FragColor = vec4(vec3(0.5), 1.0);
}