Skip to content

Commit fc57f4b

Browse files
author
Zota0
committed
Alpha blending and Specular lights YaY
Also all lights are now sorted! YaY!
1 parent a234604 commit fc57f4b

16 files changed

Lines changed: 105 additions & 10 deletions

File tree

assets/shaders/point_light.frag

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct PointLight {
1212
layout(set = 0, binding = 0) uniform GlobalUbo {
1313
mat4 projection;
1414
mat4 view;
15+
mat4 invView;
1516
vec4 ambientLight;
1617
PointLight pointLights[32];
1718
int numLights;
@@ -23,8 +24,12 @@ layout(push_constant) uniform Push {
2324
float radius;
2425
} push;
2526

27+
const float PI = 3.1415926538;
2628
void main() {
2729
float dist = sqrt(dot(fragOffset, fragOffset));
2830
if(dist >= 1.0) discard;
29-
outColor = vec4(push.color.xyz, 1.0);
31+
32+
float cosDist = 0.5 * (cos(dist * PI) + 1.0); // 1 -> 0
33+
34+
outColor = vec4(push.color.xyz + (cosDist * 0.75) , cosDist);
3035
}
368 Bytes
Binary file not shown.

assets/shaders/point_light.vert

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct PointLight {
1919
layout(set = 0, binding = 0) uniform GlobalUbo {
2020
mat4 projection;
2121
mat4 view;
22+
mat4 invView;
2223
vec4 ambientLight;
2324
PointLight pointLights[32];
2425
int numLights;
80 Bytes
Binary file not shown.

assets/shaders/simple.frag

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct PointLight {
1515
layout(set = 0, binding = 0) uniform GlobalUbo {
1616
mat4 projection;
1717
mat4 view;
18+
mat4 invView;
1819
vec4 ambientLight;
1920
PointLight pointLights[32];
2021
int numLights;
@@ -28,17 +29,32 @@ layout(push_constant) uniform Push {
2829
void main() {
2930

3031
vec3 diffuseLight = ubo.ambientLight.xyz * ubo.ambientLight.w;
32+
vec3 specularLight = vec3(0.0);
3133
vec3 surfaceNormal = normalize(fragNormalWorld);
3234

35+
vec3 cameraPosInWorld = ubo.invView[3].xyz;
36+
vec3 viewDir = normalize(cameraPosInWorld - fragPosWorld);
37+
3338
for (int i = 0; i < ubo.numLights; i++) {
3439
PointLight light = ubo.pointLights[i];
40+
3541
vec3 dirToLight = light.position.xyz - fragPosWorld;
3642
float attenuation = 1.0 / dot(dirToLight, dirToLight) /* distance squared */;
37-
float cosAngleOfIncidence = max(dot(surfaceNormal, normalize(dirToLight)), 0.0);
43+
44+
dirToLight = normalize(dirToLight);
45+
float cosAngleOfIncidence = max(dot(surfaceNormal, dirToLight), 0.0);
3846
vec3 power = light.color.xyz * light.color.w * attenuation;
3947

4048
diffuseLight += power * cosAngleOfIncidence;
49+
50+
// Specular Lighting
51+
vec3 halfAngle = normalize(dirToLight + viewDir);
52+
float blinnTerm = dot(surfaceNormal, halfAngle);
53+
blinnTerm = clamp(blinnTerm, 0, 1);
54+
blinnTerm = pow(blinnTerm, 128.0); // higher values -> sharper highlight
55+
56+
specularLight += power * blinnTerm;
4157
}
4258

43-
outColor = vec4( (diffuseLight * fragColor), 1.0);
59+
outColor = vec4( (diffuseLight * fragColor + specularLight * fragColor), 1.0);
4460
}

assets/shaders/simple.frag.spv

976 Bytes
Binary file not shown.

assets/shaders/simple.vert

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct PointLight {
1717
layout(set = 0, binding = 0) uniform GlobalUbo {
1818
mat4 projection;
1919
mat4 view;
20+
mat4 invView;
2021
vec4 ambientLight;
2122
PointLight pointLights[32];
2223
int numLights;

assets/shaders/simple.vert.spv

80 Bytes
Binary file not shown.

src/camera/camera.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ namespace BlockyVulkan {
5353
viewMat[3][0] = -glm::dot(u, pos);
5454
viewMat[3][1] = -glm::dot(v, pos);
5555
viewMat[3][2] = -glm::dot(w, pos);
56+
57+
inverseViewMat = glm::mat4{ 1.f };
58+
inverseViewMat[0][0] = u.x;
59+
inverseViewMat[0][1] = u.y;
60+
inverseViewMat[0][2] = u.z;
61+
inverseViewMat[1][0] = v.x;
62+
inverseViewMat[1][1] = v.y;
63+
inverseViewMat[1][2] = v.z;
64+
inverseViewMat[2][0] = w.x;
65+
inverseViewMat[2][1] = w.y;
66+
inverseViewMat[2][2] = w.z;
67+
inverseViewMat[3][0] = pos.x;
68+
inverseViewMat[3][1] = pos.y;
69+
inverseViewMat[3][2] = pos.z;
5670
}
5771

5872
void Camera::SetViewTarget(vec3 pos, vec3 target, vec3 up) {
@@ -83,5 +97,19 @@ namespace BlockyVulkan {
8397
viewMat[3][0] = -glm::dot(u, pos);
8498
viewMat[3][1] = -glm::dot(v, pos);
8599
viewMat[3][2] = -glm::dot(w, pos);
100+
101+
inverseViewMat = glm::mat4{ 1.f };
102+
inverseViewMat[0][0] = u.x;
103+
inverseViewMat[0][1] = u.y;
104+
inverseViewMat[0][2] = u.z;
105+
inverseViewMat[1][0] = v.x;
106+
inverseViewMat[1][1] = v.y;
107+
inverseViewMat[1][2] = v.z;
108+
inverseViewMat[2][0] = w.x;
109+
inverseViewMat[2][1] = w.y;
110+
inverseViewMat[2][2] = w.z;
111+
inverseViewMat[3][0] = pos.x;
112+
inverseViewMat[3][1] = pos.y;
113+
inverseViewMat[3][2] = pos.z;
86114
}
87115
}

src/camera/camera.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ namespace BlockyVulkan {
3333

3434
const mat4& GetProj() const { return projMat; }
3535
const mat4& GetView() const { return viewMat; }
36+
const mat4& GetInvView() const { return inverseViewMat; };
37+
const vec3 getPos() const { return vec3(inverseViewMat[3]); }
3638

3739
private:
3840
mat4 projMat{ 1.f };
3941
mat4 viewMat{ 1.f };
42+
mat4 inverseViewMat{1.f};
4043

4144
};
4245
}

0 commit comments

Comments
 (0)