فهرست منبع

Vertex shader light and view vec transformation

The light and view direction are transformed from world to tangent space in the vertex shader with the normal being sampled from the normal map in the fragment shader.

This sampled normal value is then used with the view and light direction received from the vertex shader.
Jef Belmans 2 سال پیش
والد
کامیت
178da70fd4

BIN
build/release/.ninja_deps


+ 13 - 0
build/release/.ninja_log

@@ -124,3 +124,16 @@
 1	2718	0	coral_renderer/CMakeFiles/copy_assets	6856ffc941de2b85
 1	2718	0	C:/Game Development/Visual Studio Solutions/Coral3D/build/release/coral_renderer/CMakeFiles/copy_assets	6856ffc941de2b85
 6	144	7171753206448649	C:/Game Development/Visual Studio Solutions/Coral3D/shaders/compiled/simple_shader.vert.spv	dd4b741ae13c35f5
+13	128	7171756678771457	C:/Game Development/Visual Studio Solutions/Coral3D/shaders/compiled/simple_shader.frag.spv	c8e05f14162426a5
+6	117	7171756790597313	C:/Game Development/Visual Studio Solutions/Coral3D/shaders/compiled/simple_shader.vert.spv	dd4b741ae13c35f5
+117	2415	7171756813618829	coral_renderer/CMakeFiles/coral_renderer.dir/first_app.cpp.obj	d432260f2316bfd6
+2415	2733	7171756816757416	coral_renderer/coral_renderer.exe	aaf4dcad0f31df8b
+6	109	7171756943313098	C:/Game Development/Visual Studio Solutions/Coral3D/shaders/compiled/simple_shader.vert.spv	dd4b741ae13c35f5
+0	3481	0	coral_renderer/CMakeFiles/copy_assets	6856ffc941de2b85
+0	3481	0	C:/Game Development/Visual Studio Solutions/Coral3D/build/release/coral_renderer/CMakeFiles/copy_assets	6856ffc941de2b85
+5	116	7171757972545079	C:/Game Development/Visual Studio Solutions/Coral3D/shaders/compiled/simple_shader.frag.spv	c8e05f14162426a5
+0	2618	0	coral_renderer/CMakeFiles/copy_assets	6856ffc941de2b85
+0	2618	0	C:/Game Development/Visual Studio Solutions/Coral3D/build/release/coral_renderer/CMakeFiles/copy_assets	6856ffc941de2b85
+6	109	7171758692874296	C:/Game Development/Visual Studio Solutions/Coral3D/shaders/compiled/simple_shader.frag.spv	c8e05f14162426a5
+1	2643	0	coral_renderer/CMakeFiles/copy_assets	6856ffc941de2b85
+1	2643	0	C:/Game Development/Visual Studio Solutions/Coral3D/build/release/coral_renderer/CMakeFiles/copy_assets	6856ffc941de2b85

+ 2 - 2
build/release/Testing/Temporary/LastTest.log

@@ -1,3 +1,3 @@
-Start testing: Sep 23 14:23 Romance Daylight Time
+Start testing: Sep 23 14:37 Romance Daylight Time
 ----------------------------------------------------------
-End testing: Sep 23 14:23 Romance Daylight Time
+End testing: Sep 23 14:37 Romance Daylight Time

BIN
shaders/compiled/simple_shader.frag.spv


BIN
shaders/compiled/simple_shader.vert.spv


+ 4 - 5
shaders/simple_shader.frag

@@ -24,7 +24,7 @@ layout (location = 0) out vec4 outFragColor;
 
 vec3 calculate_diffuse(vec3 color, vec3 normal)
 {
-	float diffuse_strength = dot(normal, -fs_in.lightDir);
+	float diffuse_strength = dot(normal, -normalize(fs_in.lightDir));
 
 	// HALF-LAMBERT
 	diffuse_strength = diffuse_strength * 0.5f + 0.5f;
@@ -40,10 +40,10 @@ vec3 calculate_specular(vec3 V, vec3 L, vec3 normal)
 	float specularStrength = clamp(dot(halfVector, V), 0.f, 1.0f);
 
 	// SHININESS
-	float exp = 10.f;
+	float exp = 15.f;
 	float specularity = pow(specularStrength, exp);
 
-	vec3 specularColor = vec3(0.3f, 0.3f, 0.3f);
+	vec3 specularColor = vec3(1.f, 1.f, 1.f);
 
 	return specularColor * specularity;
 }
@@ -62,7 +62,6 @@ void main()
 	vec3 normal = normalize(localNormal);
 
 	vec3 diffuse = calculate_diffuse(color.rgb, normal);
-	vec3 specular = calculate_specular(fs_in.viewDir, fs_in.lightDir, normal);
 
-	outFragColor = vec4(diffuse + specular, color.a);
+	outFragColor = vec4(diffuse, color.a);
 }

+ 3 - 3
shaders/simple_shader.vert

@@ -44,6 +44,7 @@ void main()
 	vec3 N = normalize(modelM3 * inNormal);
 	vec3 B = normalize(cross(N, T)) * inTangent.w;
 
+	// Transposed matrix to transform light and view vectors from world to tangent space.
 	mat3 TBN = mat3(
 	T.x, B.x, N.x,
 	T.y, B.y, N.y,
@@ -55,7 +56,6 @@ void main()
 	vs_out.bitangent = normalize(modelM3 * inBitangent);
 	vs_out.texcoord = inTexcoord;
 
-	vs_out.lightDir = normalize(ubo.globalLightDirection.xyz) * TBN;
-	// vs_out.viewDir = -normalize(ubo.view * primitive.model * vec4(inPosition, 1.f)).xyz;
-	vs_out.viewDir = normalize(worldPos.xyz - ubo.cameraPos.xyz) * TBN;
+	vs_out.lightDir = ubo.globalLightDirection.xyz * TBN;
+	vs_out.viewDir = (worldPos.xyz - ubo.cameraPos.xyz) * TBN;
 }