Skip to content

Commit

Permalink
fix grid-absolute for off-center models (f3d-app#1049)
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyer authored Oct 11, 2023
1 parent 8d8358b commit 7b1f5ea
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion application/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions library/VTKExtensions/Rendering/vtkF3DOpenGLGridMapper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions library/VTKExtensions/Rendering/vtkF3DOpenGLGridMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion library/VTKExtensions/Rendering/vtkF3DRenderer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions testing/baselines/TestGridAbsolute.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7b1f5ea

Please sign in to comment.