Commit 2140f97b authored by Aashish Chaudhary's avatar Aashish Chaudhary

Fixed volume rendering not working for cell data

The issue was related to the fact that we were computing wrong
extents.

(cherry picked from commit 7aed3d51cc4337eca87b9661d1506507792f6458)
parent b0444dc4
......@@ -54,6 +54,7 @@ endif()
set (VolumeOpenGL2CxxTests
TestGPURayCastCameraInside.cxx
TestGPURayCastCameraInsideSmallSpacing.cxx
TestGPURayCastCellData.cxx
TestGPURayCastClipping.cxx
TestGPURayCastGradientOpacity.cxx
TestGPURayCastPositionalLights.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestGPURayCastVolumeUpdate.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// This test volume tests whether updating the volume MTime updates the ,
// geometry in the volume mapper.
#include <vtkColorTransferFunction.h>
#include <vtkDataArray.h>
#include <vtkGPUVolumeRayCastMapper.h>
#include <vtkImageData.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNew.h>
#include <vtkOutlineFilter.h>
#include <vtkPiecewiseFunction.h>
#include <vtkPointDataToCellData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRegressionTestImage.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRTAnalyticSource.h>
#include <vtkSmartPointer.h>
#include <vtkTesting.h>
#include <vtkTestUtilities.h>
#include <vtkVolumeProperty.h>
#include <vtkXMLImageDataReader.h>
int TestGPURayCastCellData(int argc, char *argv[])
{
cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;
double scalarRange[2];
vtkNew<vtkActor> outlineActor;
vtkNew<vtkPolyDataMapper> outlineMapper;
vtkNew<vtkGPUVolumeRayCastMapper> volumeMapper;
vtkNew<vtkXMLImageDataReader> reader;
char* volumeFile = vtkTestUtilities::ExpandDataFileName(
argc, argv, "Data/vase_1comp.vti");
reader->SetFileName(volumeFile);
delete[] volumeFile;
vtkNew<vtkPointDataToCellData> pointToCell;
pointToCell->SetInputConnection(reader->GetOutputPort());
volumeMapper->SetInputConnection(pointToCell->GetOutputPort());
// Add outline filter
vtkNew<vtkOutlineFilter> outlineFilter;
outlineFilter->SetInputConnection(pointToCell->GetOutputPort());
outlineMapper->SetInputConnection(outlineFilter->GetOutputPort());
outlineActor->SetMapper(outlineMapper.GetPointer());
volumeMapper->GetInput()->GetScalarRange(scalarRange);
volumeMapper->SetSampleDistance(0.1);
volumeMapper->SetAutoAdjustSampleDistances(0);
volumeMapper->SetBlendModeToComposite();
vtkNew<vtkRenderWindow> renWin;
renWin->SetMultiSamples(0);
renWin->SetSize(400, 400);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin.GetPointer());
vtkNew<vtkInteractorStyleTrackballCamera> style;
iren->SetInteractorStyle(style.GetPointer());
renWin->Render(); // make sure we have an OpenGL context.
vtkNew<vtkRenderer> ren;
ren->SetBackground(0.2, 0.2, 0.5);
renWin->AddRenderer(ren.GetPointer());
vtkNew<vtkPiecewiseFunction> scalarOpacity;
scalarOpacity->AddPoint(50, 0.0);
scalarOpacity->AddPoint(75, 1.0);
vtkNew<vtkVolumeProperty> volumeProperty;
volumeProperty->ShadeOn();
volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);
volumeProperty->SetScalarOpacity(scalarOpacity.GetPointer());
vtkNew<vtkColorTransferFunction> colorTransferFunction;
colorTransferFunction->RemoveAllPoints();
colorTransferFunction->AddRGBPoint(scalarRange[0], 0.6, 0.4, 0.1);
volumeProperty->SetColor(colorTransferFunction.GetPointer());
vtkNew<vtkVolume> volume;
volume->SetMapper(volumeMapper.GetPointer());
volume->SetProperty(volumeProperty.GetPointer());
ren->AddVolume(volume.GetPointer());
ren->AddActor(outlineActor.GetPointer());
ren->ResetCamera();
renWin->Render();
ren->ResetCamera();
iren->Initialize();
int retVal = vtkRegressionTestImage( renWin.GetPointer() );
if( retVal == vtkRegressionTester::DO_INTERACTOR)
{
iren->Start();
}
return !retVal;
}
......@@ -731,6 +731,16 @@ bool vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::LoadVolume(
// Update texture size
imageData->GetExtent(this->Extents);
if (this->Parent->CellFlag)
{
int i = 1;
while (i < 6)
{
this->Extents[i]--;
i += 2;
}
}
int i = 0;
while(i < 3)
{
......@@ -929,6 +939,17 @@ void vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::ComputeBounds(
input->GetOrigin(origin);
input->GetExtent(this->Extents);
if (this->Parent->CellFlag)
{
int i = 1;
while (i < 6)
{
this->Extents[i]--;
i += 2;
}
}
int swapBounds[3];
swapBounds[0] = (this->CellSpacing[0] < 0);
swapBounds[1] = (this->CellSpacing[1] < 0);
......
......@@ -1573,67 +1573,74 @@ namespace vtkvolume
//--------------------------------------------------------------------------
std::string ClippingInit(vtkRenderer* vtkNotUsed(ren),
vtkVolumeMapper* vtkNotUsed(mapper),
vtkVolumeMapper* mapper,
vtkVolume* vtkNotUsed(vol))
{
return std::string("\
\n int clippingPlanesSize = int(in_clippingPlanes[0]);\
\n vec4 objDataPos = vec4(0.0);\
\n mat4 textureToObjMat = in_volumeMatrix *\
\n in_textureDatasetMatrix;\
\n for (int i = 0; i < clippingPlanesSize; i = i + 6)\
\n {\
\n if (in_useJittering)\
\n {\
\n objDataPos = textureToObjMat * vec4(g_dataPos - (g_dirStep\
\n * jitterValue), 1.0);\
\n }\
\n else\
\n {\
\n objDataPos = textureToObjMat * vec4(g_dataPos - g_dirStep, 1.0);\
\n }\
\n if (objDataPos.w != 0.0)\
\n {\
\n objDataPos = objDataPos/objDataPos.w; objDataPos.w = 1.0;\
\n }\
\n vec3 planeOrigin = vec3(in_clippingPlanes[i + 1],\
\n in_clippingPlanes[i + 2],\
\n in_clippingPlanes[i + 3]);\
\n vec3 planeNormal = vec3(in_clippingPlanes[i + 4],\
\n in_clippingPlanes[i + 5],\
\n in_clippingPlanes[i + 6]);\
\n vec3 normalizedPlaneNormal = normalize(planeNormal);\
\n\
\n float planeD = -planeOrigin[0] * normalizedPlaneNormal[0] - planeOrigin[1]\
\n * normalizedPlaneNormal[1] - planeOrigin[2] * normalizedPlaneNormal[2];\
\n bool frontFace = dot(rayDir, normalizedPlaneNormal) > 0;\
\n float dist = dot(rayDir, normalizedPlaneNormal);\
\n if (dist != 0.0) { dist = (-planeD - dot(normalizedPlaneNormal, objDataPos.xyz)) / dist; }\
\n if (frontFace && dist > 0.0 && dot(vec3(objDataPos.xyz - planeOrigin), planeNormal) < 0)\
\n {\
\n vec4 newObjDataPos = vec4(objDataPos.xyz + dist * rayDir, 1.0);\
\n newObjDataPos = in_inverseTextureDatasetMatrix\
\n * in_inverseVolumeMatrix * vec4(newObjDataPos.xyz, 1.0);\
\n if (newObjDataPos.w != 0.0)\
\n {\
\n newObjDataPos /= newObjDataPos.w;\
\n }\
\n if (in_useJittering)\
\n {\
\n g_dataPos = newObjDataPos.xyz + g_dirStep * jitterValue;\
\n }\
\n else\
\n {\
\n g_dataPos = newObjDataPos.xyz + g_dirStep;\
\n }\
\n bool stop = dot(sign(g_dataPos - l_texMin), sign(l_texMax - g_dataPos))\
\n < 3.0;\
\n if (stop)\
\n {\
\n discard;\
\n }\
\n }\
\n }");
if (!mapper->GetClippingPlanes())
{
return std::string();
}
else
{
return std::string("\
\n int clippingPlanesSize = int(in_clippingPlanes[0]);\
\n vec4 objDataPos = vec4(0.0);\
\n mat4 textureToObjMat = in_volumeMatrix *\
\n in_textureDatasetMatrix;\
\n for (int i = 0; i < clippingPlanesSize; i = i + 6)\
\n {\
\n if (in_useJittering)\
\n {\
\n objDataPos = textureToObjMat * vec4(g_dataPos - (g_dirStep\
\n * jitterValue), 1.0);\
\n }\
\n else\
\n {\
\n objDataPos = textureToObjMat * vec4(g_dataPos - g_dirStep, 1.0);\
\n }\
\n if (objDataPos.w != 0.0)\
\n {\
\n objDataPos = objDataPos/objDataPos.w; objDataPos.w = 1.0;\
\n }\
\n vec3 planeOrigin = vec3(in_clippingPlanes[i + 1],\
\n in_clippingPlanes[i + 2],\
\n in_clippingPlanes[i + 3]);\
\n vec3 planeNormal = vec3(in_clippingPlanes[i + 4],\
\n in_clippingPlanes[i + 5],\
\n in_clippingPlanes[i + 6]);\
\n vec3 normalizedPlaneNormal = normalize(planeNormal);\
\n\
\n float planeD = -planeOrigin[0] * normalizedPlaneNormal[0] - planeOrigin[1]\
\n * normalizedPlaneNormal[1] - planeOrigin[2] * normalizedPlaneNormal[2];\
\n bool frontFace = dot(rayDir, normalizedPlaneNormal) > 0;\
\n float dist = dot(rayDir, normalizedPlaneNormal);\
\n if (dist != 0.0) { dist = (-planeD - dot(normalizedPlaneNormal, objDataPos.xyz)) / dist; }\
\n if (frontFace && dist > 0.0 && dot(vec3(objDataPos.xyz - planeOrigin), planeNormal) < 0)\
\n {\
\n vec4 newObjDataPos = vec4(objDataPos.xyz + dist * rayDir, 1.0);\
\n newObjDataPos = in_inverseTextureDatasetMatrix\
\n * in_inverseVolumeMatrix * vec4(newObjDataPos.xyz, 1.0);\
\n if (newObjDataPos.w != 0.0)\
\n {\
\n newObjDataPos /= newObjDataPos.w;\
\n }\
\n if (in_useJittering)\
\n {\
\n g_dataPos = newObjDataPos.xyz + g_dirStep * jitterValue;\
\n }\
\n else\
\n {\
\n g_dataPos = newObjDataPos.xyz + g_dirStep;\
\n }\
\n bool stop = dot(sign(g_dataPos - l_texMin), sign(l_texMax - g_dataPos))\
\n < 3.0;\
\n if (stop)\
\n {\
\n discard;\
\n }\
\n }\
\n }");
}
}
//--------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment