Skip to content

Commit 18c3a0e

Browse files
Add vertex normals
1 parent 9f3c22f commit 18c3a0e

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

attachments/38_ray_tracing.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,32 @@ struct Vertex {
5454
glm::vec3 pos;
5555
glm::vec3 color;
5656
glm::vec2 texCoord;
57+
glm::vec3 normal;
5758

5859
static vk::VertexInputBindingDescription getBindingDescription() {
5960
return { 0, sizeof(Vertex), vk::VertexInputRate::eVertex };
6061
}
6162

62-
static std::array<vk::VertexInputAttributeDescription, 3> getAttributeDescriptions() {
63+
static std::array<vk::VertexInputAttributeDescription, 4> getAttributeDescriptions() {
6364
return {
6465
vk::VertexInputAttributeDescription( 0, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, pos) ),
6566
vk::VertexInputAttributeDescription( 1, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, color) ),
66-
vk::VertexInputAttributeDescription( 2, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, texCoord) )
67+
vk::VertexInputAttributeDescription( 2, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, texCoord) ),
68+
vk::VertexInputAttributeDescription( 3, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, normal) )
6769
};
6870
}
6971

7072
bool operator==(const Vertex& other) const {
71-
return pos == other.pos && color == other.color && texCoord == other.texCoord;
73+
return pos == other.pos && color == other.color && texCoord == other.texCoord && normal == other.normal;
7274
}
7375
};
7476

7577
template<> struct std::hash<Vertex> {
7678
size_t operator()(Vertex const& vertex) const noexcept {
77-
return ((hash<glm::vec3>()(vertex.pos) ^ (hash<glm::vec3>()(vertex.color) << 1)) >> 1) ^ (hash<glm::vec2>()(vertex.texCoord) << 1);
79+
auto h = std::hash<glm::vec3>()(vertex.pos) ^ (std::hash<glm::vec3>()(vertex.color) << 1);
80+
h = (h >> 1) ^ (std::hash<glm::vec2>()(vertex.texCoord) << 1);
81+
h = (h >> 1) ^ (std::hash<glm::vec3>()(vertex.normal) << 1);
82+
return h;
7883
}
7984
};
8085

@@ -824,6 +829,16 @@ class HelloTriangleApplication {
824829

825830
vertex.color = {1.0f, 1.0f, 1.0f};
826831

832+
if (index.normal_index >= 0) {
833+
vertex.normal = {
834+
attrib.normals[3 * index.normal_index + 0],
835+
attrib.normals[3 * index.normal_index + 1],
836+
attrib.normals[3 * index.normal_index + 2]
837+
};
838+
} else {
839+
vertex.normal = {0.0f, 0.0f, 0.0f};
840+
}
841+
827842
if (!uniqueVertices.contains(vertex)) {
828843
uniqueVertices[vertex] = static_cast<uint32_t>(vertices.size());
829844
vertices.push_back(vertex);

attachments/38_ray_tracing.slang

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ struct VSInput {
22
float3 inPosition;
33
float3 inColor;
44
float2 inTexCoord;
5+
float3 inNormal;
56
};
67

78
struct UniformBuffer {
@@ -17,6 +18,7 @@ struct VSOutput
1718
float4 pos : SV_Position;
1819
float3 fragColor;
1920
float2 fragTexCoord;
21+
float3 fragNormal;
2022
};
2123

2224
[shader("vertex")]
@@ -25,6 +27,7 @@ VSOutput vertMain(VSInput input) {
2527
output.pos = mul(ubo.proj, mul(ubo.view, mul(ubo.model, float4(input.inPosition, 1.0))));
2628
output.fragColor = input.inColor;
2729
output.fragTexCoord = input.inTexCoord;
30+
output.fragNormal = input.inNormal;
2831
return output;
2932
}
3033

0 commit comments

Comments
 (0)