|
|
@ -24,7 +24,7 @@ VkVertexInputBindingDescription vertex_binding_description() { |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
std::array<VkVertexInputAttributeDescription, 2> vertex_attributes_description() { |
|
|
std::array<VkVertexInputAttributeDescription, 4> vertex_attributes_description() { |
|
|
return { |
|
|
return { |
|
|
VkVertexInputAttributeDescription { |
|
|
VkVertexInputAttributeDescription { |
|
|
.location = 0, |
|
|
.location = 0, |
|
|
@ -32,10 +32,22 @@ std::array<VkVertexInputAttributeDescription, 2> vertex_attributes_description() |
|
|
.format = VK_FORMAT_R32G32B32_SFLOAT, |
|
|
.format = VK_FORMAT_R32G32B32_SFLOAT, |
|
|
.offset = offsetof(Vertex, position), |
|
|
.offset = offsetof(Vertex, position), |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
VkVertexInputAttributeDescription { |
|
|
.location = 1, |
|
|
.location = 1, |
|
|
.binding = 0, |
|
|
.binding = 0, |
|
|
.format = VK_FORMAT_R32G32B32_SFLOAT, |
|
|
.format = VK_FORMAT_R32G32B32_SFLOAT, |
|
|
|
|
|
.offset = offsetof(Vertex, position2), |
|
|
|
|
|
}, |
|
|
|
|
|
VkVertexInputAttributeDescription { |
|
|
|
|
|
.location = 2, |
|
|
|
|
|
.binding = 0, |
|
|
|
|
|
.format = VK_FORMAT_R32G32B32_SFLOAT, |
|
|
|
|
|
.offset = offsetof(Vertex, normal), |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
.location = 3, |
|
|
|
|
|
.binding = 0, |
|
|
|
|
|
.format = VK_FORMAT_R32G32B32_SFLOAT, |
|
|
.offset = offsetof(Vertex, color), |
|
|
.offset = offsetof(Vertex, color), |
|
|
}, |
|
|
}, |
|
|
}; |
|
|
}; |
|
|
@ -43,40 +55,42 @@ std::array<VkVertexInputAttributeDescription, 2> vertex_attributes_description() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Vertex> vertices = { |
|
|
std::vector<Vertex> vertices = { |
|
|
{{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}}, |
|
|
// {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}},
|
|
|
{{0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}}, |
|
|
// {{0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}},
|
|
|
{{-0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}}, |
|
|
// {{-0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}},
|
|
|
{{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}}, |
|
|
// {{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}},
|
|
|
|
|
|
|
|
|
{{-0.5f, -0.5f, 0.5f}, {0.0f, 1.0f, 1.0f}}, |
|
|
// {{-0.5f, -0.5f, 0.5f}, {0.0f, 1.0f, 1.0f}},
|
|
|
{{0.5f, -0.5f, 0.5f}, {1.0f, 0.0f, 1.0f}}, |
|
|
// {{0.5f, -0.5f, 0.5f}, {1.0f, 0.0f, 1.0f}},
|
|
|
{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 0.0f}}, |
|
|
// {{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 0.0f}},
|
|
|
{{0.5f, 0.5f, 0.5f}, {0.2f, 0.2f, 0.2f}}, |
|
|
// {{0.5f, 0.5f, 0.5f}, {0.2f, 0.2f, 0.2f}},
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
std::vector<uint32_t> indices = { |
|
|
std::vector<uint32_t> indices = { |
|
|
0, 1, 2, |
|
|
// 0, 1, 2,
|
|
|
2, 1, 3, |
|
|
// 2, 1, 3,
|
|
|
|
|
|
|
|
|
1, 5, 3, |
|
|
// 1, 5, 3,
|
|
|
3, 5, 7, |
|
|
// 3, 5, 7,
|
|
|
|
|
|
|
|
|
5, 4, 7, |
|
|
// 5, 4, 7,
|
|
|
7, 4, 6, |
|
|
// 7, 4, 6,
|
|
|
|
|
|
|
|
|
4, 0, 6, |
|
|
// 4, 0, 6,
|
|
|
6, 0, 2, |
|
|
// 6, 0, 2,
|
|
|
|
|
|
|
|
|
0, 4, 1, |
|
|
// 0, 4, 1,
|
|
|
1, 4, 5, |
|
|
// 1, 4, 5,
|
|
|
|
|
|
|
|
|
2, 3, 6, |
|
|
// 2, 3, 6,
|
|
|
6, 3, 7, |
|
|
// 6, 3, 7,
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
struct Uniforms { |
|
|
struct Uniforms { |
|
|
alignas(16) Eigen::Matrix4f scene_from_model; |
|
|
alignas(16) Eigen::Matrix4f scene_from_model; |
|
|
alignas(16) Eigen::Matrix4f projection_from_scene; |
|
|
alignas(16) Eigen::Matrix4f projection_from_scene; |
|
|
|
|
|
alignas(8) Eigen::Vector2f half_screen_size; |
|
|
|
|
|
alignas(4) float lod; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -91,24 +105,21 @@ VulkanTutorial::VulkanTutorial() { |
|
|
vertices.resize(6 * Cell::vertex_count(subdiv_count)); |
|
|
vertices.resize(6 * Cell::vertex_count(subdiv_count)); |
|
|
indices.resize(6 * 3 * Cell::triangle_count(subdiv_count)); |
|
|
indices.resize(6 * 3 * Cell::triangle_count(subdiv_count)); |
|
|
|
|
|
|
|
|
// MeshView mesh(
|
|
|
|
|
|
// vertices.size(),
|
|
|
|
|
|
// reinterpret_cast<Byte*>(vertices.data()) + offsetof(Vertex, position),
|
|
|
|
|
|
// sizeof(Vertex),
|
|
|
|
|
|
// nullptr,
|
|
|
|
|
|
// sizeof(Vertex),
|
|
|
|
|
|
// reinterpret_cast<Byte*>(vertices.data()) + offsetof(Vertex, color),
|
|
|
|
|
|
// sizeof(Vertex),
|
|
|
|
|
|
// indices.size(),
|
|
|
|
|
|
// reinterpret_cast<Byte*>(indices.data()),
|
|
|
|
|
|
// sizeof(Index)
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
Vector3AV positions( |
|
|
Vector3AV positions( |
|
|
vertices.size(), vertices.data(), |
|
|
vertices.size(), vertices.data(), |
|
|
sizeof(Vertex), offsetof(Vertex, position) |
|
|
sizeof(Vertex), offsetof(Vertex, position) |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
Vector3AV positions2( |
|
|
|
|
|
vertices.size(), vertices.data(), |
|
|
|
|
|
sizeof(Vertex), offsetof(Vertex, position2) |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
Vector3AV normals( |
|
|
|
|
|
vertices.size(), vertices.data(), |
|
|
|
|
|
sizeof(Vertex), offsetof(Vertex, normal) |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
Vector3AV colors( |
|
|
Vector3AV colors( |
|
|
vertices.size(), vertices.data(), |
|
|
vertices.size(), vertices.data(), |
|
|
sizeof(Vertex), offsetof(Vertex, color) |
|
|
sizeof(Vertex), offsetof(Vertex, color) |
|
|
@ -120,7 +131,7 @@ VulkanTutorial::VulkanTutorial() { |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
Planet planet; |
|
|
Planet planet; |
|
|
planet.build_mesh(positions, triangles, subdiv_count); |
|
|
planet.build_mesh(positions, positions2, normals, triangles, subdiv_count); |
|
|
|
|
|
|
|
|
for (size_t vertex_index = 0; vertex_index < vertices.size(); vertex_index += 1) { |
|
|
for (size_t vertex_index = 0; vertex_index < vertices.size(); vertex_index += 1) { |
|
|
colors[vertex_index] = |
|
|
colors[vertex_index] = |
|
|
@ -195,7 +206,7 @@ void VulkanTutorial::draw_frame() { |
|
|
} |
|
|
} |
|
|
m_last_frame_time = now; |
|
|
m_last_frame_time = now; |
|
|
|
|
|
|
|
|
const float alpha = SecondsD(m_time).count() * (2.0 * M_PI) / 3.0; |
|
|
const float alpha = SecondsD(m_time).count() * (2.0 * M_PI) / 10.0; |
|
|
const float dist = 2.0f; |
|
|
const float dist = 2.0f; |
|
|
const Eigen::Matrix4f view = look_at_matrix( |
|
|
const Eigen::Matrix4f view = look_at_matrix( |
|
|
// Eigen::Vector3f(0.0f, 0.0f, -dist),
|
|
|
// Eigen::Vector3f(0.0f, 0.0f, -dist),
|
|
|
@ -226,6 +237,11 @@ void VulkanTutorial::draw_frame() { |
|
|
const Uniforms uniforms = { |
|
|
const Uniforms uniforms = { |
|
|
.scene_from_model = model.matrix(), |
|
|
.scene_from_model = model.matrix(), |
|
|
.projection_from_scene = proj * view, |
|
|
.projection_from_scene = proj * view, |
|
|
|
|
|
.half_screen_size = { |
|
|
|
|
|
0.5 * m_swapchain.extent().width, |
|
|
|
|
|
0.5 * m_swapchain.extent().height, |
|
|
|
|
|
}, |
|
|
|
|
|
.lod = std::cos(alpha) * 0.5f + 0.5f, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
void* uniform_buffer; |
|
|
void* uniform_buffer; |
|
|
@ -381,7 +397,7 @@ void VulkanTutorial::create_descriptor_set_layout() { |
|
|
.binding = 0, |
|
|
.binding = 0, |
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
|
|
.descriptorCount = 1, |
|
|
.descriptorCount = 1, |
|
|
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT, |
|
|
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
VkDescriptorSetLayoutCreateInfo layout_info { |
|
|
VkDescriptorSetLayoutCreateInfo layout_info { |
|
|
@ -405,6 +421,12 @@ void VulkanTutorial::create_graphic_pipeline() { |
|
|
vkDestroyShaderModule(m_context.device(), vertex_shader_module, nullptr); |
|
|
vkDestroyShaderModule(m_context.device(), vertex_shader_module, nullptr); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
auto const geometry_shader_module = |
|
|
|
|
|
m_context.create_shader_module_from_file("shaders/shader.geom.spv"); |
|
|
|
|
|
auto const geometry_shader_guard = make_guard([&]{ |
|
|
|
|
|
vkDestroyShaderModule(m_context.device(), geometry_shader_module, nullptr); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
auto const fragment_shader_module = |
|
|
auto const fragment_shader_module = |
|
|
m_context.create_shader_module_from_file("shaders/shader.frag.spv"); |
|
|
m_context.create_shader_module_from_file("shaders/shader.frag.spv"); |
|
|
auto const fragment_shader_guard = make_guard([&]{ |
|
|
auto const fragment_shader_guard = make_guard([&]{ |
|
|
@ -418,6 +440,12 @@ void VulkanTutorial::create_graphic_pipeline() { |
|
|
.module = vertex_shader_module, |
|
|
.module = vertex_shader_module, |
|
|
.pName = "main", |
|
|
.pName = "main", |
|
|
}, |
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, |
|
|
|
|
|
.stage = VK_SHADER_STAGE_GEOMETRY_BIT, |
|
|
|
|
|
.module = geometry_shader_module, |
|
|
|
|
|
.pName = "main", |
|
|
|
|
|
}, |
|
|
{ |
|
|
{ |
|
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, |
|
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, |
|
|
.stage = VK_SHADER_STAGE_FRAGMENT_BIT, |
|
|
.stage = VK_SHADER_STAGE_FRAGMENT_BIT, |
|
|
@ -534,7 +562,7 @@ void VulkanTutorial::create_graphic_pipeline() { |
|
|
|
|
|
|
|
|
VkGraphicsPipelineCreateInfo pipeline_info { |
|
|
VkGraphicsPipelineCreateInfo pipeline_info { |
|
|
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, |
|
|
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, |
|
|
.stageCount = 2, |
|
|
.stageCount = 3, |
|
|
.pStages = shader_stage_infos, |
|
|
.pStages = shader_stage_infos, |
|
|
.pVertexInputState = &vertex_info, |
|
|
.pVertexInputState = &vertex_info, |
|
|
.pInputAssemblyState = &assembly_info, |
|
|
.pInputAssemblyState = &assembly_info, |
|
|
@ -819,6 +847,13 @@ void VulkanTutorial::create_command_buffers() { |
|
|
offsets |
|
|
offsets |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
vkCmdBindIndexBuffer( |
|
|
|
|
|
command_buffer, |
|
|
|
|
|
m_index_buffer, |
|
|
|
|
|
0, |
|
|
|
|
|
VK_INDEX_TYPE_UINT32 |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
vkCmdBindDescriptorSets( |
|
|
vkCmdBindDescriptorSets( |
|
|
command_buffer, |
|
|
command_buffer, |
|
|
VK_PIPELINE_BIND_POINT_GRAPHICS, |
|
|
VK_PIPELINE_BIND_POINT_GRAPHICS, |
|
|
@ -827,13 +862,6 @@ void VulkanTutorial::create_command_buffers() { |
|
|
0, nullptr |
|
|
0, nullptr |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
vkCmdBindIndexBuffer( |
|
|
|
|
|
command_buffer, |
|
|
|
|
|
m_index_buffer, |
|
|
|
|
|
0, |
|
|
|
|
|
VK_INDEX_TYPE_UINT32 |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
vkCmdDrawIndexed( |
|
|
vkCmdDrawIndexed( |
|
|
command_buffer, |
|
|
command_buffer, |
|
|
uint32_t(indices.size()), |
|
|
uint32_t(indices.size()), |
|
|
|