@@ -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
7577template <> 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);
0 commit comments