From f007bffd2c651647e916f6fd1731e621ab9a2645 Mon Sep 17 00:00:00 2001 From: snoyer Date: Wed, 11 Oct 2023 16:06:00 +0100 Subject: [PATCH] fix `grid-absolute` for off-center models (#1049) --- application/testing/CMakeLists.txt | 2 +- .../Rendering/vtkF3DOpenGLGridMapper.cxx | 15 +++++++++++---- .../Rendering/vtkF3DOpenGLGridMapper.h | 6 ++++++ .../VTKExtensions/Rendering/vtkF3DRenderer.cxx | 11 ++++++++++- testing/baselines/TestGridAbsolute.png | 4 ++-- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/application/testing/CMakeLists.txt b/application/testing/CMakeLists.txt index 9c72fd6590..0d98765ead 100644 --- a/application/testing/CMakeLists.txt +++ b/application/testing/CMakeLists.txt @@ -123,7 +123,7 @@ f3d_test(NAME TestGridX DATA suzanne.ply ARGS -g --up=+X DEFAULT_LIGHTS) f3d_test(NAME TestGridY DATA suzanne.ply ARGS -g --up=+Y DEFAULT_LIGHTS) f3d_test(NAME TestGridZ DATA suzanne.ply ARGS -g --up=+Z DEFAULT_LIGHTS) f3d_test(NAME TestGridOptions DATA suzanne.ply ARGS -g --camera-elevation-angle=45 --grid-unit=2 --grid-subdivisions=3 -t DEFAULT_LIGHTS) -f3d_test(NAME TestGridAbsolute DATA suzanne.ply ARGS -g --camera-elevation-angle=45 --grid-absolute -t DEFAULT_LIGHTS) +f3d_test(NAME TestGridAbsolute DATA f3d.vtp ARGS -g --up=-Y --camera-direction=-.5,+1,+1 --grid-absolute -t DEFAULT_LIGHTS) f3d_test(NAME TestAxis DATA suzanne.ply ARGS -x DEFAULT_LIGHTS) f3d_test(NAME TestPointCloud DATA pointsCloud.vtp ARGS -o --point-size=20 DEFAULT_LIGHTS) # Using default lights because of ResetCamera f3d_test(NAME TestPointCloudBar DATA pointsCloud.vtp ARGS -sob --point-size=20 DEFAULT_LIGHTS) # Using default lights because of ResetCamera diff --git a/library/VTKExtensions/Rendering/vtkF3DOpenGLGridMapper.cxx b/library/VTKExtensions/Rendering/vtkF3DOpenGLGridMapper.cxx index b7f3dfd17c..6aa9744162 100644 --- a/library/VTKExtensions/Rendering/vtkF3DOpenGLGridMapper.cxx +++ b/library/VTKExtensions/Rendering/vtkF3DOpenGLGridMapper.cxx @@ -40,16 +40,20 @@ void vtkF3DOpenGLGridMapper::ReplaceShaderValues( std::string VSSource = shaders[vtkShader::Vertex]->GetSource(); std::string FSSource = shaders[vtkShader::Fragment]->GetSource(); - const std::string axes = this->UpIndex == 0 ? "zyx" : this->UpIndex == 1 ? "xzy" : "xyz"; + const std::string axes3d = this->UpIndex == 0 ? "zyx" : this->UpIndex == 1 ? "xzy" : "xyz"; + const std::string axes2d = this->UpIndex == 0 ? "zy" : this->UpIndex == 1 ? "xz" : "xy"; // clang-format off vtkShaderProgram::Substitute(VSSource, "//VTK::PositionVC::Dec", + "uniform vec3 originOffset;\n" "uniform float fadeDist;\n" "out vec2 gridCoord;\n" + "out vec2 gridOffset;\n" ); vtkShaderProgram::Substitute(VSSource, "//VTK::PositionVC::Impl", "gridCoord = vertexMC.xy * fadeDist;\n" - "gl_Position = MCDCMatrix * vec4(vertexMC." + axes + " * fadeDist, 1.0);\n" + "gridOffset = originOffset." + axes2d + ";\n" + "gl_Position = MCDCMatrix * vec4(vertexMC." + axes3d + " * fadeDist, 1.0);\n" ); vtkShaderProgram::Substitute(FSSource, "//VTK::CustomUniforms::Dec", @@ -63,6 +67,7 @@ void vtkF3DOpenGLGridMapper::ReplaceShaderValues( "uniform vec4 axis1Color;\n" "uniform vec4 axis2Color;\n" "in vec2 gridCoord;\n" + "in vec2 gridOffset;\n" "float antialias(float dist, float linewidth){\n" " float aa = lineAntialias;\n" @@ -75,7 +80,8 @@ void vtkF3DOpenGLGridMapper::ReplaceShaderValues( "}\n" ); vtkShaderProgram::Substitute(FSSource, "//VTK::UniformFlow::Impl", - " vec2 majorCoord = gridCoord / unitSquare;\n" + " vec2 fromCenter = gridCoord / unitSquare;\n" + " vec2 majorCoord = (gridCoord - gridOffset) / unitSquare;\n" " vec2 minorCoord = majorCoord * float(subdivisions);\n" " vec2 majorGrid = abs(fract(majorCoord - 0.5) - 0.5) / fwidth(majorCoord);\n" " vec2 minorGrid = abs(fract(minorCoord - 0.5) - 0.5) / fwidth(minorCoord);\n" @@ -92,7 +98,7 @@ void vtkF3DOpenGLGridMapper::ReplaceShaderValues( " color = mix(color, axis2Color, axis2Weight);\n" " color = mix(color, axis1Color, axis1Weight);\n" - " float sqDist = unitSquare * unitSquare * dot(majorCoord, majorCoord);\n" + " float sqDist = unitSquare * unitSquare * dot(fromCenter, fromCenter);\n" " float radialFadeFactor = 1.0 - sqDist / (fadeDist * fadeDist);\n" " color.w *= radialFadeFactor;\n" @@ -143,6 +149,7 @@ void vtkF3DOpenGLGridMapper::SetMapperShaderParameters( } } + cellBO.Program->SetUniform3f("originOffset", this->OriginOffset); cellBO.Program->SetUniformf("fadeDist", this->FadeDistance); cellBO.Program->SetUniformf("unitSquare", this->UnitSquare); cellBO.Program->SetUniformi("subdivisions", this->Subdivisions); diff --git a/library/VTKExtensions/Rendering/vtkF3DOpenGLGridMapper.h b/library/VTKExtensions/Rendering/vtkF3DOpenGLGridMapper.h index 347207e676..5eeaadf2a6 100644 --- a/library/VTKExtensions/Rendering/vtkF3DOpenGLGridMapper.h +++ b/library/VTKExtensions/Rendering/vtkF3DOpenGLGridMapper.h @@ -17,6 +17,11 @@ class vtkF3DOpenGLGridMapper : public vtkOpenGLPolyDataMapper vtkTypeMacro(vtkF3DOpenGLGridMapper, vtkOpenGLPolyDataMapper); void PrintSelf(ostream& os, vtkIndent indent) override; + /** + * Set the origin of the axes relative to the actor position + */ + vtkSetVector3Macro(OriginOffset, double); + /** * Set the distance where the grid disappear. */ @@ -59,6 +64,7 @@ class vtkF3DOpenGLGridMapper : public vtkOpenGLPolyDataMapper bool GetNeedToRebuildShaders(vtkOpenGLHelper& cellBO, vtkRenderer* ren, vtkActor* act) override; + double OriginOffset[3] = { 0.0, 0.0, 0.0 }; double FadeDistance = 10.0; double UnitSquare = 1.0; int Subdivisions = 10; diff --git a/library/VTKExtensions/Rendering/vtkF3DRenderer.cxx b/library/VTKExtensions/Rendering/vtkF3DRenderer.cxx index b17e0a5f91..258eaa60f2 100644 --- a/library/VTKExtensions/Rendering/vtkF3DRenderer.cxx +++ b/library/VTKExtensions/Rendering/vtkF3DRenderer.cxx @@ -487,7 +487,14 @@ void vtkF3DRenderer::ConfigureGridUsingCurrentActors() } double gridPos[3] = { 0, 0, 0 }; - if (!this->GridAbsolute) + if (this->GridAbsolute) + { + for (int i = 0; i < 3; i++) + { + gridPos[i] = this->UpVector[i] ? 0 : 0.5 * (bounds[2 * i] + bounds[2 * i + 1]); + } + } + else { for (int i = 0; i < 3; i++) { @@ -507,6 +514,8 @@ void vtkF3DRenderer::ConfigureGridUsingCurrentActors() gridMapper->SetUnitSquare(tmpUnitSquare); gridMapper->SetSubdivisions(this->GridSubdivisions); gridMapper->SetUpIndex(this->UpIndex); + if (this->GridAbsolute) + gridMapper->SetOriginOffset(-gridPos[0], -gridPos[1], -gridPos[2]); this->GridActor->GetProperty()->SetColor(0.0, 0.0, 0.0); this->GridActor->ForceTranslucentOn(); diff --git a/testing/baselines/TestGridAbsolute.png b/testing/baselines/TestGridAbsolute.png index de9a5ec771..77f4e34fdd 100644 --- a/testing/baselines/TestGridAbsolute.png +++ b/testing/baselines/TestGridAbsolute.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ec895bc50caac2deba0af9cc561a0db91d375014fa69b522bef610a3a3fc9ae5 -size 43811 +oid sha256:6b00dd4d9456d232b4b6d7238f4c08bbbe27251a29fb97d979a29f09b89cb653 +size 36347