Skip to content

Commit 317a65c

Browse files
committed
Add tangent vertex-attribute
Simplify tangent calculation
1 parent d91f763 commit 317a65c

File tree

6 files changed

+34
-26
lines changed

6 files changed

+34
-26
lines changed

shaders/shader_2d.vert

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ layout (location = 0) in vec3 position;
44
layout (location = 1) in vec3 color;
55
layout (location = 2) in vec3 normal;
66
layout (location = 3) in vec2 uv;
7+
layout (location = 4) in vec3 tangent;
78

89
layout (location = 0) out vec3 frag_color;
910

shaders/shader_3d.frag

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ layout (set = 0, binding = 0) uniform global_ubo
2323
int num_lights;
2424
} ubo;
2525

26-
layout (set = 0, binding = 1) uniform sampler2D image;
27-
2826
layout (push_constant) uniform Push
2927
{
3028
mat4 model_matrix;
@@ -61,6 +59,5 @@ void main()
6159

6260
}
6361

64-
vec4 image_color = texture(image, frag_uv);
65-
out_color = vec4((diffuse_light * frag_color + specular_light * frag_color) * image_color, 1.0f);
62+
out_color = vec4(diffuse_light * frag_color + specular_light * frag_color, 1.0f);
6663
}

shaders/shader_3d.vert

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ layout (location = 0) in vec3 position;
44
layout (location = 1) in vec3 color;
55
layout (location = 2) in vec3 normal;
66
layout (location = 3) in vec2 uv;
7+
layout (location = 4) in vec3 tangent;
78

89
layout(location = 0) out vec3 frag_color;
910
layout(location = 1) out vec3 frag_pos_world;

src/model.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ namespace dae
8888
.offset = offsetof(vertex, uv)
8989
};
9090
attribute_descriptions.push_back(uv);
91+
92+
VkVertexInputAttributeDescription tangent{
93+
.location = 4,
94+
.binding = 0,
95+
.format = VK_FORMAT_R32G32B32_SFLOAT,
96+
.offset = offsetof(vertex, tangent)
97+
};
98+
attribute_descriptions.push_back(tangent);
9199

92100
return attribute_descriptions;
93101
}
@@ -167,28 +175,31 @@ namespace dae
167175
vertex &v0 = vertices[indices[i + 0]];
168176
vertex &v1 = vertices[indices[i + 1]];
169177
vertex &v2 = vertices[indices[i + 2]];
170-
171-
glm::vec3 edge0 = v1.position - v0.position;
172-
glm::vec3 edge1 = v2.position - v0.position;
173-
178+
179+
glm::vec3 edge1 = v1.position - v0.position;
180+
glm::vec3 edge2 = v2.position - v0.position;
181+
174182
glm::vec2 uv0 = v0.uv;
175183
glm::vec2 uv1 = v1.uv;
176184
glm::vec2 uv2 = v2.uv;
177-
178-
glm::vec2 diff_x = {uv1.x - uv0.x, uv2.x - uv0.x};
179-
glm::vec2 diff_y = {uv1.y - uv0.y, uv2.y - uv0.y};
180-
float r = 1.0f / (diff_x.x * diff_y.y - diff_y.x * diff_x.y);
181-
182-
glm::vec3 tangent = (edge0 * diff_y.y - edge1 * diff_y.x) * r;
183-
v0.tangent += tangent;
184-
v1.tangent += tangent;
185-
v2.tangent += tangent;
186-
}
187-
188-
// Create the tangents (reject)
189-
for (auto &vertex : vertices)
190-
{
191-
vertex.tangent = glm::normalize(reject(vertex.tangent, vertex.normal));
185+
186+
glm::vec2 delta_uv1 = uv1 - uv0;
187+
glm::vec2 delta_uv2 = uv2 - uv0;
188+
189+
float f = 1.0f / (delta_uv1.x * delta_uv2.y - delta_uv2.x * delta_uv1.y);
190+
191+
glm::vec3 tangent = f * (delta_uv2.y * edge1 - delta_uv1.y * edge2);
192+
tangent = glm::normalize(tangent);
193+
194+
float sx = delta_uv1.x;
195+
float sy = delta_uv2.x;
196+
float tx = delta_uv1.y;
197+
float ty = delta_uv2.y;
198+
float handedness = (sx * ty - sy * tx) > 0.0f ? 1.0f : -1.0f;
199+
tangent *= handedness;
200+
v0.tangent = tangent;
201+
v1.tangent = tangent;
202+
v2.tangent = tangent;
192203
}
193204
}
194205
}

src/systems/point_light_system.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ void point_light_system::render(frame_info &frame_info)
9999
);
100100
vkCmdDraw(frame_info.command_buffer, 6, 1, 0, 0);
101101
}
102-
103102
}
104103

105104
void point_light_system::create_pipeline_layout(VkDescriptorSetLayout global_set_layout)

src/systems/render_system_3d.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#include "render_system_3d.h"
22

33
// Standard includes
4+
#include <ranges>
45
#include <stdexcept>
56

67
// GLM includes
78
#define GLM_FORCE_RADIANS
89
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
9-
#include <ranges>
1010
#include <glm/glm.hpp>
11-
#include <glm/gtc/constants.hpp>
1211

1312
namespace dae
1413
{

0 commit comments

Comments
 (0)