Commit 6745bdbf authored by Nicholas Milef's avatar Nicholas Milef

CLEANUP: added ability to expand surface meshes, abstracted interactor, and...

CLEANUP: added ability to expand surface meshes, abstracted interactor, and fixed transforms in Vulkan renderer
parent 54a68c1f
......@@ -217,15 +217,15 @@ endif(Uncrustify_EXECUTABLE)
find_package( Assimp REQUIRED )
include_directories( ${Assimp_INCLUDE_DIRS} )
# glm
find_package(glm)
include_directories( ${glm_INCLUDE_DIR} )
if( ${PROJECT_NAME}_USE_Vulkan )
# glfw
find_package(glfw)
include_directories( ${glfw_INCLUDE_DIR} )
# glm
find_package(glm)
include_directories( ${glm_INCLUDE_DIR} )
# gli
find_package(gli)
include_directories( ${gli_INCLUDE_DIR} )
......
......@@ -60,6 +60,7 @@
#include "imstkSurfaceMesh.h"
#include "imstkMeshIO.h"
#include "imstkLineMesh.h"
#include "imstkDecalPool.h"
// Maps
#include "imstkTetraTriangleMap.h"
......@@ -2653,13 +2654,13 @@ void testScreenShotUtility()
cam1->setFocalPoint(Vec3d(1, 1, 0));
#ifndef iMSTK_USE_Vulkan
auto viewer = std::dynamic_pointer_cast<VTKViewer>(sdk->getViewer());
auto viewer = sdk->getViewer();
auto screenShotUtility
= std::dynamic_pointer_cast<VTKScreenCaptureUtility>(viewer->getScreenCaptureUtility());
// Set up for screen shot
sdk->getViewer()->getScreenCaptureUtility()->setScreenShotPrefix("screenShot_");
viewer->getScreenCaptureUtility()->setScreenShotPrefix("screenShot_");
// Create a call back on key press of 'b' to take the screen shot
viewer->setOnCharFunction('b', [&](VTKInteractorStyle* c) -> bool
viewer->setOnCharFunction('b', [&](InteractorStyle* c) -> bool
{
screenShotUtility->saveScreenShot();
return false;
......
......@@ -23,7 +23,6 @@
namespace imstk
{
Decal::Decal()
: AnalyticalGeometry(Geometry::Type::Decal)
{
......@@ -35,8 +34,8 @@ Decal::print() const
{
Geometry::print();
LOG(INFO) << "Dimensions: " << m_dimensions.x
<< ", " << m_dimensions.y
<< ", " << m_dimensions.z;
<< ", " << m_dimensions.y
<< ", " << m_dimensions.z;
}
double
......@@ -56,7 +55,7 @@ Decal::updateDecal(glm::mat4& viewMatrix)
{
glm::mat4 transform;
glm::vec3 scale((float)this->getScaling());
glm::vec3 scale((float) this->getScaling());
transform = glm::scale(transform, scale);
auto rotation = this->getRotation();
......@@ -74,5 +73,4 @@ Decal::updateDecal(glm::mat4& viewMatrix)
m_inverse = glm::inverse(m_transform);
}
}
\ No newline at end of file
......@@ -31,7 +31,6 @@
namespace imstk
{
class Decal : public AnalyticalGeometry
{
public:
......@@ -64,7 +63,6 @@ protected:
glm::mat4 m_transform;
glm::mat4 m_inverse;
};
}
#endif
\ No newline at end of file
......@@ -23,7 +23,6 @@
namespace imstk
{
DecalPool::DecalPool(unsigned int maxNumDecals /*= 128*/)
: Geometry(Geometry::Type::DecalPool)
{
......@@ -102,7 +101,7 @@ DecalPool::removeDecal()
std::deque<std::shared_ptr<Decal>>&
DecalPool::getDecals()
{
{
return m_orderedDecals;
}
......@@ -129,5 +128,4 @@ DecalPool::getMaxNumDecals()
{
return m_maxNumDecals;
}
}
\ No newline at end of file
......@@ -31,7 +31,6 @@
namespace imstk
{
class DecalPool : public Geometry
{
public:
......@@ -72,7 +71,6 @@ protected:
glm::vec3 m_vertexPositions[8];
glm::ivec3 m_triangles[12];
};
}
#endif
\ No newline at end of file
......@@ -221,7 +221,7 @@ PointSet::getPointDataArray(const std::string& arrayName) const
size_t
PointSet::getNumVertices() const
{
return m_initialVertexPositions.size();
return m_vertexPositions.size();
}
......
......@@ -91,6 +91,7 @@ SurfaceMesh::getVolume() const
void
SurfaceMesh::computeVertexNeighborTriangles()
{
m_vertexNeighborTriangles.clear();
m_vertexNeighborTriangles.resize(m_vertexPositions.size());
size_t triangleId = 0;
......@@ -107,9 +108,10 @@ SurfaceMesh::computeVertexNeighborTriangles()
void
SurfaceMesh::computeVertexNeighborVertices()
{
m_vertexNeighborVertices.clear();
m_vertexNeighborVertices.resize(m_vertexPositions.size());
if (m_vertexNeighborTriangles.empty())
if (m_vertexNeighborTriangles.size() != m_vertexPositions.size())
{
this->computeVertexNeighborTriangles();
}
......@@ -148,13 +150,15 @@ SurfaceMesh::computeTrianglesNormals()
void
SurfaceMesh::computeVertexNormals()
{
m_vertexNormals.resize(m_vertexPositions.size());
if (m_vertexNeighborTriangles.empty())
if (m_topologyChanged)
{
this->computeVertexNeighborTriangles();
this->computeUVSeamVertexGroups();
m_topologyChanged = false;
}
m_vertexNormals.resize(m_vertexPositions.size());
this->computeTrianglesNormals();
StdVectorOfVec3d temp_normals(m_vertexNormals.size());
......@@ -168,15 +172,17 @@ SurfaceMesh::computeVertexNormals()
}
// Correct for UV seams
Vec3d normal;
for (size_t vertexId = 0; vertexId < m_vertexNormals.size(); ++vertexId)
{
NormalGroup group = {m_vertexPositions[vertexId], m_vertexNormals[vertexId]};
m_vertexNormals[vertexId] = temp_normals[vertexId];
normal = temp_normals[vertexId];
if (m_UVSeamVertexGroups.find(group) == m_UVSeamVertexGroups.end())
{
m_vertexNormals[vertexId].normalize();
normal.normalize();
m_vertexNormals[vertexId] = normal;
continue;
}
......@@ -184,10 +190,11 @@ SurfaceMesh::computeVertexNormals()
for (auto index : seamGroup)
{
m_vertexNormals[vertexId] += temp_normals[index];
normal += temp_normals[index];
}
m_vertexNormals[vertexId].normalize();
normal.normalize();
m_vertexNormals[vertexId] = normal;
}
}
......@@ -313,10 +320,12 @@ SurfaceMesh::setTrianglesVertices(const std::vector<TriangleArray>& triangles)
m_trianglesVertices.reserve(m_maxNumTriangles);
m_vertexNormals.reserve(m_maxNumVertices);
m_vertexTangents.reserve(m_maxNumVertices);
m_topologyChanged = true;
}
if (triangles.size() <= m_maxNumTriangles)
{
m_topologyChanged = true;
m_trianglesVertices = triangles;
}
else
......@@ -518,6 +527,11 @@ SurfaceMesh::computeUVSeamVertexGroups()
// Reset vertex groups
m_UVSeamVertexGroups.clear();
if (m_vertexPositions.size() != m_vertexNormals.size())
{
return;
}
// Initial pass to bin vertices based on positions
for (size_t i = 0; i < m_vertexPositions.size(); i++)
{
......
......@@ -171,9 +171,11 @@ public:
std::shared_ptr<RenderMaterial> getRenderMaterial() const;
protected:
friend class VTKRenderer;
friend class VTKRenderDelegate;
friend class VulkanRenderDelegate;
friend class VulkanSurfaceMeshRenderDelegate;
friend class VulkanRenderer;
virtual void applyTranslation(const Vec3d t) = 0;
virtual void applyRotation(const Mat3d r) = 0;
......@@ -185,6 +187,7 @@ protected:
bool m_dataModified = false;
bool m_transformModified = false;
bool m_transformApplied = true;
bool m_renderDelegateCreated = false;
RigidTransform3d m_transform = RigidTransform3d::Identity(); ///> Transform
double m_scaling = 1.0;
......
......@@ -255,5 +255,4 @@ RenderMaterial::isDecal()
{
return m_isDecal;
}
}
......@@ -92,7 +92,6 @@ else()
set(RENDERING_DEPENDENCIES
${VulkanSDK_LIBRARIES}
glfw
glm
gli)
endif()
......@@ -112,6 +111,7 @@ imstk_add_library( Rendering
${VTK_LIBRARIES}
${RENDERING_DEPENDENCIES}
Scene
glm
#VERBOSE
)
......
......@@ -37,28 +37,8 @@ namespace imstk
{
VTKRenderer::VTKRenderer(std::shared_ptr<Scene> scene)
{
// Object actors
for ( const auto& obj : scene->getSceneObjects() )
{
auto geom = obj->getVisualGeometry();
if (geom == nullptr)
{
LOG(WARNING) << "Renderer::Renderer error: Could not retrieve visual geometry for '"
<< obj->getName() << "'.";
continue;
}
auto delegate = VTKRenderDelegate::make_delegate( geom );
if (delegate == nullptr)
{
LOG(WARNING) << "Renderer::Renderer error: Could not create render delegate for '"
<< obj->getName() << "'.";
continue;
}
m_renderDelegates.push_back( delegate );
m_objectVtkActors.push_back( delegate->getVtkActor() );
}
m_scene = scene;
this->updateRenderDelegates();
// Initialize textures for surface mesh render delegates
for ( const auto& renderDelegate : m_renderDelegates )
......@@ -192,6 +172,27 @@ VTKRenderer::updateSceneCamera(std::shared_ptr<Camera> imstkCam)
void
VTKRenderer::updateRenderDelegates()
{
// Object actors
for ( const auto& obj : m_scene->getSceneObjects() )
{
auto geom = obj->getVisualGeometry();
if (geom && !geom->m_renderDelegateCreated)
{
auto delegate = VTKRenderDelegate::make_delegate( geom );
if (delegate == nullptr)
{
LOG(WARNING) << "Renderer::Renderer error: Could not create render delegate for '"
<< obj->getName() << "'.";
continue;
}
m_renderDelegates.push_back( delegate );
m_objectVtkActors.push_back( delegate->getVtkActor() );
m_vtkRenderer->AddActor(delegate->getVtkActor());
geom->m_renderDelegateCreated = true;
}
}
for (auto delegate : m_renderDelegates)
{
delegate->update();
......
......@@ -106,6 +106,8 @@ protected:
std::vector<std::shared_ptr<VTKRenderDelegate>> m_renderDelegates;
std::shared_ptr<Scene> m_scene;
TextureManager<VTKTextureDelegate> m_textureManager;
};
}
......
......@@ -99,7 +99,7 @@ VulkanCapsuleRenderDelegate::VulkanCapsuleRenderDelegate(std::shared_ptr<Capsule
void
VulkanCapsuleRenderDelegate::update()
{
this->updateUniforms(m_geometry);
this->updateUniforms();
}
std::shared_ptr<Geometry>
......
......@@ -99,7 +99,7 @@ VulkanCubeRenderDelegate::VulkanCubeRenderDelegate(std::shared_ptr<Cube> cube, V
void
VulkanCubeRenderDelegate::update()
{
this->updateUniforms(m_geometry);
this->updateUniforms();
}
std::shared_ptr<Geometry>
......
......@@ -37,7 +37,7 @@ VulkanDecalRenderDelegate::VulkanDecalRenderDelegate(std::shared_ptr<DecalPool>
}
m_geometry->getRenderMaterial()->m_isDecal = true;
this->initializeData(memoryManager, m_geometry->getRenderMaterial());
this->initializeData(memoryManager, m_geometry->getRenderMaterial());
this->updateVertexBuffer();
}
......
......@@ -97,7 +97,7 @@ VulkanPlaneRenderDelegate::VulkanPlaneRenderDelegate(std::shared_ptr<Plane> plan
void
VulkanPlaneRenderDelegate::update()
{
this->updateTransform(m_geometry);
this->updateTransform();
m_vertexUniformBuffer->updateUniforms(sizeof(VulkanLocalVertexUniforms),
(void *)&m_localVertexUniforms);
}
......
......@@ -43,6 +43,7 @@ namespace imstk
std::shared_ptr<VulkanRenderDelegate>
VulkanRenderDelegate::make_delegate(std::shared_ptr<Geometry> geom, VulkanMemoryManager& memoryManager)
{
geom->m_renderDelegateCreated = true;
switch (geom->getType())
{
case Geometry::Type::Plane:
......@@ -84,7 +85,7 @@ VulkanRenderDelegate::make_delegate(std::shared_ptr<Geometry> geom, VulkanMemory
{
LOG(WARNING) << "RenderDelegate::make_delegate error: HexahedralMeshRenderDelegate not yet implemented";
return nullptr;
}*/
}*/
case Geometry::Type::DecalPool:
{
auto decalPool = std::dynamic_pointer_cast<DecalPool>(geom);
......@@ -93,6 +94,7 @@ VulkanRenderDelegate::make_delegate(std::shared_ptr<Geometry> geom, VulkanMemory
default:
{
LOG(WARNING) << "RenderDelegate::make_delegate error: Geometry type incorrect.";
geom->m_renderDelegateCreated = false;
return nullptr;
}
}
......@@ -124,35 +126,32 @@ VulkanRenderDelegate::initializeData(VulkanMemoryManager& memoryManager, std::sh
material,
memoryManager);
m_vertexBuffer = std::make_shared<VulkanVertexBuffer>(memoryManager, m_numVertices, m_vertexSize, m_numTriangles);
m_vertexBuffer = std::make_shared<VulkanVertexBuffer>(memoryManager,
m_numVertices,
m_vertexSize,
m_numTriangles,
m_loadFactor);
}
void
VulkanRenderDelegate::updateTransform(std::shared_ptr<Geometry> geometry)
VulkanRenderDelegate::updateTransform()
{
glm::mat4 transform;
glm::vec3 scale(geometry->getScaling());
transform = glm::scale(transform, scale);
auto rotation = geometry->getRotation();
glm::mat3 rotationMatrix(rotation(0, 0), rotation(0, 1), rotation(0, 2),
rotation(1, 0), rotation(1, 1), rotation(1, 2),
rotation(2, 0), rotation(2, 1), rotation(2, 2));
transform = glm::mat4(rotationMatrix) * transform;
transform[3][0] = geometry->getTranslation().x();
transform[3][1] = geometry->getTranslation().y();
transform[3][2] = geometry->getTranslation().z();
m_localVertexUniforms.transform = transform;
auto geometry = this->getGeometry();
if (!geometry->m_transformModified)
{
return;
}
AffineTransform3d T(geometry->m_transform.matrix());
T.scale(geometry->getScaling());
m_localVertexUniforms.transform = glm::make_mat4(T.data());
geometry->m_transformModified = false;
}
void
VulkanRenderDelegate::updateUniforms(std::shared_ptr<Geometry> geometry)
VulkanRenderDelegate::updateUniforms()
{
this->updateTransform(geometry);
auto geometry = this->getGeometry();
this->updateTransform();
m_vertexUniformBuffer->updateUniforms(sizeof(VulkanLocalVertexUniforms),
(void *)&m_localVertexUniforms);
......@@ -164,5 +163,4 @@ VulkanRenderDelegate::updateUniforms(std::shared_ptr<Geometry> geometry)
m_fragmentUniformBuffer->updateUniforms(sizeof(VulkanLocalVertexUniforms),
(void*)&m_localFragmentUniforms);
}
}
\ No newline at end of file
......@@ -25,6 +25,7 @@
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/quaternion.hpp"
#include "glm/gtc/type_ptr.hpp"
#include <array>
......@@ -89,9 +90,9 @@ public:
///
/// \brief Initialize data
///
void updateTransform(std::shared_ptr<Geometry> geometry);
void updateTransform();
void updateUniforms(std::shared_ptr<Geometry> geometry);
void updateUniforms();
protected:
friend class VulkanVertexBuffer;
......@@ -101,6 +102,7 @@ protected:
unsigned int m_numTriangles;
unsigned int m_numVertices;
unsigned int m_vertexSize;
double m_loadFactor = 1.0;
///
/// \brief Default constructor (protected)
......
......@@ -99,7 +99,7 @@ VulkanSphereRenderDelegate::VulkanSphereRenderDelegate(std::shared_ptr<Sphere> s
void
VulkanSphereRenderDelegate::update()
{
this->updateUniforms(m_geometry);
this->updateUniforms();
}
std::shared_ptr<Geometry>
......
......@@ -26,8 +26,9 @@ namespace imstk
VulkanSurfaceMeshRenderDelegate::VulkanSurfaceMeshRenderDelegate(std::shared_ptr<SurfaceMesh> surfaceMesh, VulkanMemoryManager& memoryManager)
: m_geometry(surfaceMesh)
{
m_numVertices = (uint32_t)m_geometry->getMaxNumVertices();
m_numTriangles = (uint32_t)m_geometry->getMaxNumTriangles();
m_numVertices = (uint32_t)m_geometry->getNumVertices();
m_numTriangles = (uint32_t)m_geometry->getNumTriangles();
m_loadFactor = m_geometry->getLoadFactor();
m_vertexSize = sizeof(VulkanBasicVertex);
if (!m_geometry->getRenderMaterial())
......@@ -60,12 +61,13 @@ VulkanSurfaceMeshRenderDelegate::updateVertexBuffer()
UVs = nullptr;
}
auto vertexPositions = m_geometry->getVertexPositions(Geometry::DataType::PreTransform);
for (unsigned i = 0; i < m_geometry->getNumVertices(); i++)
{
vertices[i].position = glm::vec3(
m_geometry->getVertexPosition(i)[0],
m_geometry->getVertexPosition(i)[1],
m_geometry->getVertexPosition(i)[2]);
vertexPositions[i][0],
vertexPositions[i][1],
vertexPositions[i][2]);
if (normals.size() == m_geometry->getNumVertices())
{
......@@ -95,6 +97,7 @@ VulkanSurfaceMeshRenderDelegate::updateVertexBuffer()
auto triangles = (std::array<uint32_t, 3> *)m_vertexBuffer->mapTriangles();
m_vertexBuffer->setNumIndices((uint32_t)m_geometry->getNumTriangles() * 3);
for (unsigned i = 0; i < m_geometry->getNumTriangles(); i++)
{
triangles[i][0] = (uint32_t)m_geometry->getTrianglesVertices()[i][0];
......@@ -107,7 +110,7 @@ VulkanSurfaceMeshRenderDelegate::updateVertexBuffer()
void
VulkanSurfaceMeshRenderDelegate::update()
{
this->updateUniforms(m_geometry);
this->updateUniforms();
if (m_geometry->m_dataModified)
{
......@@ -115,7 +118,6 @@ VulkanSurfaceMeshRenderDelegate::update()
this->updateVertexBuffer();
m_geometry->m_dataModified = false;
}
}
std::shared_ptr<Geometry>
......
......@@ -64,38 +64,38 @@ VulkanMaterialDelegate::createPipeline(VulkanRenderer * renderer)
if (m_material->isDecal())
{
VulkanShaderLoader vertexShaderLoader("./Shaders/VulkanShaders/Mesh/decal_vert.spv",
renderer->m_renderDevice,
m_pipelineComponents.vertexShader);
renderer->m_renderDevice,
m_pipelineComponents.vertexShader);
}
else
{
VulkanShaderLoader vertexShaderLoader("./Shaders/VulkanShaders/Mesh/mesh_vert.spv",
renderer->m_renderDevice,
m_pipelineComponents.vertexShader);
renderer->m_renderDevice,
m_pipelineComponents.vertexShader);
}
if (m_material->getTessellated())
{
VulkanShaderLoader tessellationControlShaderLoader("./Shaders/VulkanShaders/Mesh/mesh_tesc.spv",
renderer->m_renderDevice,
m_pipelineComponents.tessellationControlShader);
renderer->m_renderDevice,
m_pipelineComponents.tessellationControlShader);
VulkanShaderLoader tessellationEvaluationShaderLoader("./Shaders/VulkanShaders/Mesh/mesh_tese.spv",
renderer->m_renderDevice,
m_pipelineComponents.tessellationEvaluationShader);
renderer->m_renderDevice,
m_pipelineComponents.tessellationEvaluationShader);
}
if (m_material->isDecal())
{
VulkanShaderLoader fragmentShaderLoader("F:/iMSTK/Source/Rendering/VulkanRenderer/VulkanShaders/Mesh/decal_frag.spv",
renderer->m_renderDevice,
m_pipelineComponents.fragmentShader);
VulkanShaderLoader fragmentShaderLoader("./Shaders/VulkanShaders/Mesh/decal_frag.spv",
renderer->m_renderDevice,
m_pipelineComponents.fragmentShader);
}
else
{
VulkanShaderLoader fragmentShaderLoader("./Shaders/VulkanShaders/Mesh/mesh_frag.spv",
renderer->m_renderDevice,
m_pipelineComponents.fragmentShader);
renderer->m_renderDevice,
m_pipelineComponents.fragmentShader);
}
this->buildMaterial(renderer);
......
......@@ -23,7 +23,6 @@
namespace imstk
{
void
VulkanRenderPassGenerator::generateOpaqueRenderPass(
VkDevice& device, VkRenderPass& renderPass, VkSampleCountFlagBits& samples)
......@@ -257,5 +256,4 @@ VulkanRenderPassGenerator::generateDecalRenderPass(
vkCreateRenderPass(device, &renderPassInfo[0], nullptr, &renderPass);
}
}
\ No newline at end of file
......@@ -26,14 +26,12 @@
namespace imstk
{
class VulkanRenderPassGenerator
{
public:
static void generateOpaqueRenderPass(VkDevice& device, VkRenderPass& renderPass, VkSampleCountFlagBits& samples);
static void generateDecalRenderPass(VkDevice& device, VkRenderPass& renderPass, VkSampleCountFlagBits& samples);
};
}
#endif
\ No newline at end of file
......@@ -617,6 +617,8 @@ VulkanRenderer::renderFrame()
{
m_frameNumber++;
this->loadAllGeometry();
// Update global uniforms
this->updateGlobalUniforms();
......@@ -648,7 +650,7 @@ VulkanRenderer::renderFrame()
renderArea.extent = { m_width, m_height };
std::array<VkClearValue, 4> clearValues;
clearValues[0].color = { { 0.5, 0.5, 0.5, 1 } }; // Color
clearValues[0].color = { { (float)m_backgroundColor[0], (float)m_backgroundColor[1], (float)m_backgroundColor[2], 1 } }; // Color
clearValues[1].depthStencil = { { 1.0 }, { 0 } }; // Depth
clearValues[2].color = { { 0, 0, 0, 0 } }; // Normal
clearValues[3].color = { { 0, 0, 0, 0 } }; // Specular
......@@ -675,7 +677,10 @@ VulkanRenderer::renderFrame()
// Pass 1: Render opaque geometry
for (unsigned int renderDelegateIndex = 0; renderDelegateIndex < m_renderDelegates.size(); renderDelegateIndex++)
{
if (m_renderDelegates[renderDelegateIndex]->getGeometry()->getType() == Geometry::Type::DecalPool) { continue; }
if (m_renderDelegates[renderDelegateIndex]->getGeometry()->getType() == Geometry::Type::DecalPool)
{
continue;
}
auto material = m_renderDelegates[renderDelegateIndex]->m_material;
vkCmdBindPipeline(m_renderCommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, material->m_pipeline);
......@@ -706,7 +711,10 @@ VulkanRenderer::renderFrame()
for (unsigned int renderDelegateIndex = 0; renderDelegateIndex < m_renderDelegates.size(); renderDelegateIndex++)
{
if (m_renderDelegates[renderDelegateIndex]->getGeometry()->getType() != Geometry::Type::DecalPool) { continue; }
if (m_renderDelegates[renderDelegateIndex]->getGeometry()->getType() != Geometry::Type::DecalPool)
{
continue;
}
auto geometry = std::dynamic_pointer_cast<DecalPool>(m_renderDelegates[renderDelegateIndex]->getGeometry());
auto material = m_renderDelegates[renderDelegateIndex]->m_material;
......@@ -862,11 +870,14 @@ VulkanRenderer::setupSynchronization()
void
VulkanRenderer::loadAllGeometry()
{
// Add new objects
for (auto sceneObject : m_scene->getSceneObjects())
{
if (sceneObject->getVisualGeometry())
auto geometry = sceneObject->getVisualGeometry();
if (geometry && !geometry->m_renderDelegateCreated)