Commit 6f0c543f authored by Ken Martin's avatar Ken Martin Committed by Kitware Robot

Merge topic 'better_sprites'

96b994f1 Fix shadowed var
57daf0bc Some more features and some cleanup reorg.
44e33c4a Cleanup comments
091552c1 Add ability to adjust the table size
edc24bde Improve the capabilities of the vtkPointGaussianMapper
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Tested-by: Scott Wittenburg's avatarScott Wittenburg <scott.wittenburg@kitware.com>
Reviewed-by: Utkarsh Ayachit's avatarUtkarsh Ayachit <utkarsh.ayachit@kitware.com>
Merge-request: !489
parents ae56f168 96b994f1
...@@ -14,15 +14,28 @@ ...@@ -14,15 +14,28 @@
#include "vtkPointGaussianMapper.h" #include "vtkPointGaussianMapper.h"
#include "vtkObjectFactory.h" #include "vtkObjectFactory.h"
#include "vtkPiecewiseFunction.h"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
vtkAbstractObjectFactoryNewMacro(vtkPointGaussianMapper) vtkAbstractObjectFactoryNewMacro(vtkPointGaussianMapper)
vtkCxxSetObjectMacro(vtkPointGaussianMapper, ScaleFunction, vtkPiecewiseFunction);
vtkCxxSetObjectMacro(vtkPointGaussianMapper, ScalarOpacityFunction, vtkPiecewiseFunction);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
vtkPointGaussianMapper::vtkPointGaussianMapper() vtkPointGaussianMapper::vtkPointGaussianMapper()
{ {
this->ScaleArray = 0; this->ScaleArray = 0;
this->DefaultRadius = 1.0; this->OpacityArray = 0;
this->SplatShaderCode = 0;
this->ScaleFunction = 0;
this->ScaleTableSize = 1024;
this->ScalarOpacityFunction = 0;
this->OpacityTableSize = 1024;
this->ScaleFactor = 1.0;
this->Emissive = 1; this->Emissive = 1;
} }
...@@ -30,6 +43,10 @@ vtkPointGaussianMapper::vtkPointGaussianMapper() ...@@ -30,6 +43,10 @@ vtkPointGaussianMapper::vtkPointGaussianMapper()
vtkPointGaussianMapper::~vtkPointGaussianMapper() vtkPointGaussianMapper::~vtkPointGaussianMapper()
{ {
this->SetScaleArray(0); this->SetScaleArray(0);
this->SetOpacityArray(0);
this->SetSplatShaderCode(0);
this->SetScalarOpacityFunction(0);
this->SetScaleFunction(0);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
...@@ -38,6 +55,10 @@ void vtkPointGaussianMapper::PrintSelf(ostream& os, vtkIndent indent) ...@@ -38,6 +55,10 @@ void vtkPointGaussianMapper::PrintSelf(ostream& os, vtkIndent indent)
this->Superclass::PrintSelf(os, indent); this->Superclass::PrintSelf(os, indent);
os << indent << "Scale Array: " << (this->ScaleArray ? this->ScaleArray : "(none)") << "\n"; os << indent << "Scale Array: " << (this->ScaleArray ? this->ScaleArray : "(none)") << "\n";
os << indent << "Default Radius: " << this->DefaultRadius << "\n"; os << indent << "Opacity Array: " << (this->OpacityArray ? this->OpacityArray : "(none)") << "\n";
os << indent << "SplatShaderCode: " << (this->SplatShaderCode ? this->SplatShaderCode : "(none)") << "\n";
os << indent << "ScaleFactor: " << this->ScaleFactor << "\n";
os << indent << "Emissive: " << this->Emissive << "\n"; os << indent << "Emissive: " << this->Emissive << "\n";
os << indent << "OpacityTableSize: " << this->OpacityTableSize << "\n";
os << indent << "ScaleTableSize: " << this->ScaleTableSize << "\n";
} }
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#include "vtkRenderingCoreModule.h" // For export macro #include "vtkRenderingCoreModule.h" // For export macro
#include "vtkPolyDataMapper.h" #include "vtkPolyDataMapper.h"
class vtkPiecewiseFunction;
class VTKRENDERINGCORE_EXPORT vtkPointGaussianMapper : public vtkPolyDataMapper class VTKRENDERINGCORE_EXPORT vtkPointGaussianMapper : public vtkPolyDataMapper
{ {
public: public:
...@@ -30,18 +32,32 @@ public: ...@@ -30,18 +32,32 @@ public:
vtkTypeMacro(vtkPointGaussianMapper, vtkPolyDataMapper) vtkTypeMacro(vtkPointGaussianMapper, vtkPolyDataMapper)
void PrintSelf(ostream& os, vtkIndent indent); void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Set/Get the optional scale transfer function. This is only
// used when a ScaleArray is also specified.
void SetScaleFunction(vtkPiecewiseFunction *);
vtkGetObjectMacro(ScaleFunction,vtkPiecewiseFunction);
// Description:
// The size of the table used in computing scale, used when
// converting a vtkPiecewiseFunction to a table
vtkSetMacro(ScaleTableSize, int);
vtkGetMacro(ScaleTableSize, int);
// Description: // Description:
// Convenience method to set the array to scale with. // Convenience method to set the array to scale with.
vtkSetStringMacro(ScaleArray); vtkSetStringMacro(ScaleArray);
vtkGetStringMacro(ScaleArray); vtkGetStringMacro(ScaleArray);
// Description: // Description:
// Set the default radius of the point gaussians. This is used if the // Set the default scale factor of the point gaussians. This
// array to scale with has not been set or is set to NULL. If there // defaults to 1.0. All radius computations will be scaled by the factor
// is no scale array and the default radius is set to zero then // including the ScaleArray. If a vtkPiecewideFunction is used the
// the splats wil be rendered as simple points requiring less memory. // scaling happens prior to the function lookup.
vtkSetMacro(DefaultRadius,double); // A scale factor of 0.0 indicates that the splats should be rendered
vtkGetMacro(DefaultRadius,double); // as simple points.
vtkSetMacro(ScaleFactor,double);
vtkGetMacro(ScaleFactor,double);
// Description: // Description:
// Treat the points/splats as emissive light sources. The default is true. // Treat the points/splats as emissive light sources. The default is true.
...@@ -49,12 +65,50 @@ public: ...@@ -49,12 +65,50 @@ public:
vtkGetMacro(Emissive, int); vtkGetMacro(Emissive, int);
vtkBooleanMacro(Emissive, int); vtkBooleanMacro(Emissive, int);
// Description:
// Set/Get the optional opacity transfer function. This is only
// used when an OpacityArray is also specified.
void SetScalarOpacityFunction(vtkPiecewiseFunction *);
vtkGetObjectMacro(ScalarOpacityFunction,vtkPiecewiseFunction);
// Description:
// The size of the table used in computing opacities, used when
// converting a vtkPiecewiseFunction to a table
vtkSetMacro(OpacityTableSize, int);
vtkGetMacro(OpacityTableSize, int);
// Description:
// Method to set the optional opacity array. If specified this
// array will be used to generate the opacity values.
vtkSetStringMacro(OpacityArray);
vtkGetStringMacro(OpacityArray);
// Description:
// Method to override the fragment shader code for the splat. You can
// set this to draw other shapes. For the OPenGL2 backend some of
// the variables you can use and/or modify include,
// opacity - 0.0 to 1.0
// diffuseColor - vec3
// ambientColor - vec3
// offsetVCVSOutput - vec2 offset in view coordinates from the splat center
vtkSetStringMacro(SplatShaderCode);
vtkGetStringMacro(SplatShaderCode);
protected: protected:
vtkPointGaussianMapper(); vtkPointGaussianMapper();
~vtkPointGaussianMapper(); ~vtkPointGaussianMapper();
char *ScaleArray; char *ScaleArray;
double DefaultRadius; char *OpacityArray;
char *SplatShaderCode;
vtkPiecewiseFunction *ScaleFunction;
int ScaleTableSize;
vtkPiecewiseFunction *ScalarOpacityFunction;
int OpacityTableSize;
double ScaleFactor;
int Emissive; int Emissive;
private: private:
......
vtk_add_test_cxx(${vtk-module}CxxTests tests vtk_add_test_cxx(${vtk-module}CxxTests tests
#TestRenderWidget.cxx # Very experimental, fails, does nothing useful yet. #TestRenderWidget.cxx # Very experimental, fails, does nothing useful yet.
TestPointGaussianMapper.cxx TestPointGaussianMapper.cxx
TestPointGaussianMapperOpacity.cxx
TestVBOPLYMapper.cxx TestVBOPLYMapper.cxx
TestVBOPointsLines.cxx TestVBOPointsLines.cxx
TestGaussianBlurPass.cxx TestGaussianBlurPass.cxx
......
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
// .SECTION Thanks // .SECTION Thanks
// <verbatim> // <verbatim>
// //
// This file is part of the PointSprites plugin developed and contributed by // This file is based loosely on the PointSprites plugin developed
// and contributed by
// //
// Copyright (c) CSCS - Swiss National Supercomputing Centre // Copyright (c) CSCS - Swiss National Supercomputing Centre
// EDF - Electricite de France // EDF - Electricite de France
...@@ -25,8 +26,6 @@ ...@@ -25,8 +26,6 @@
// Stephane Ploix (EDF) // Stephane Ploix (EDF)
// //
// </verbatim> // </verbatim>
// .SECTION Description
// this program tests the point sprite support by vtkPointSpriteProperty.
#include "vtkActor.h" #include "vtkActor.h"
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestSprites.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.
=========================================================================*/
// .SECTION Thanks
// <verbatim>
//
// This file is based loosely on the PointSprites plugin developed
// and contributed by
//
// Copyright (c) CSCS - Swiss National Supercomputing Centre
// EDF - Electricite de France
//
// John Biddiscombe, Ugo Varetto (CSCS)
// Stephane Ploix (EDF)
//
// </verbatim>
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkColorTransferFunction.h"
#include "vtkDataObject.h"
#include "vtkDataSetAttributes.h"
#include "vtkNew.h"
#include "vtkPiecewiseFunction.h"
#include "vtkPointGaussianMapper.h"
#include "vtkPointSource.h"
#include "vtkProperty.h"
#include "vtkRandomAttributeGenerator.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSphereSource.h"
#include "vtkTimerLog.h"
#include "vtkTestUtilities.h"
#include "vtkRegressionTestImage.h"
int TestPointGaussianMapperOpacity(int argc, char *argv[])
{
int desiredPoints = 1.0e4;
vtkNew<vtkPointSource> points;
points->SetNumberOfPoints(desiredPoints);
points->SetRadius(pow(desiredPoints,0.33)*10.0);
points->Update();
vtkNew<vtkRandomAttributeGenerator> randomAttr;
randomAttr->SetInputConnection(points->GetOutputPort());
vtkNew<vtkPointGaussianMapper> mapper;
vtkNew<vtkRenderer> renderer;
renderer->SetBackground(0.0, 0.0, 0.0);
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(300, 300);
renderWindow->SetMultiSamples(0);
renderWindow->AddRenderer(renderer.Get());
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renderWindow.Get());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper.Get());
renderer->AddActor(actor.Get());
randomAttr->SetDataTypeToFloat();
randomAttr->GeneratePointScalarsOn();
randomAttr->GeneratePointVectorsOn();
randomAttr->GeneratePointArrayOn();
randomAttr->Update();
mapper->SetInputConnection(randomAttr->GetOutputPort());
mapper->SetColorModeToMapScalars();
mapper->SetScalarModeToUsePointFieldData();
mapper->SelectColorArray("RandomPointVectors");
mapper->SetInterpolateScalarsBeforeMapping(0);
mapper->SetScaleArray("RandomPointScalars");
mapper->SetOpacityArray("RandomPointArray");
mapper->EmissiveOff();
// show other shader examples
// the fragment that is rendered is that of a triangle
// large enough to encompass a circle of radius 3
mapper->SetSplatShaderCode(
// this first line keeps the default color opacity calcs
// which you can then modify with additional code below
"//VTK::Color::Impl\n"
// example of a circle with black edges
// " float dist = sqrt(dot(offsetVCVSOutput.xy,offsetVCVSOutput.xy));\n"
// " if (dist > 1.1) { discard; }\n"
// " if (dist < 0.5) { discard; }\n"
// // apply a black edge around the circle
// " if (dist > 1.0 || dist < 0.6) { diffuseColor = vec3(0,0,0); ambientColor = vec3(0,0,0); }\n"
// example for a square
" if (abs(offsetVCVSOutput.x) > 1.0 || abs(offsetVCVSOutput.y) > 1.0) { discard; }\n"
" if (abs(offsetVCVSOutput.x) < 0.6 && abs(offsetVCVSOutput.y) < 0.6) { discard; }\n"
);
vtkNew<vtkColorTransferFunction> ctf;
ctf->AddHSVPoint(0.0,0.1,0.7,1.0);
ctf->AddHSVPoint(1.0,0.9,0.7,1.0);
ctf->SetColorSpaceToHSV();
ctf->HSVWrapOff();
mapper->SetLookupTable(ctf.Get());
vtkNew<vtkPiecewiseFunction> otf;
otf->AddPoint(0.0,0.3);
otf->AddPoint(1.0,1.0);
mapper->SetScalarOpacityFunction(otf.Get());
vtkNew<vtkTimerLog> timer;
timer->StartTimer();
renderWindow->Render();
timer->StopTimer();
double firstRender = timer->GetElapsedTime();
cerr << "first render time: " << firstRender << endl;
timer->StartTimer();
int numRenders = 85;
for (int i = 0; i < numRenders; ++i)
{
renderer->GetActiveCamera()->Azimuth(1);
renderer->GetActiveCamera()->Elevation(1);
renderWindow->Render();
}
timer->StopTimer();
double elapsed = timer->GetElapsedTime();
int numPts = mapper->GetInput()->GetPoints()->GetNumberOfPoints();
cerr << "interactive render time: " << elapsed / numRenders << endl;
cerr << "number of points: " << numPts << endl;
cerr << "points per second: " << numPts*(numRenders/elapsed) << endl;
renderer->GetActiveCamera()->SetPosition(0,0,1);
renderer->GetActiveCamera()->SetFocalPoint(0,0,0);
renderer->GetActiveCamera()->SetViewUp(0,1,0);
renderer->ResetCamera();
// renderer->GetActiveCamera()->Print(cerr);
renderer->GetActiveCamera()->Zoom(10.0);
renderWindow->Render();
int retVal = vtkRegressionTestImage( renderWindow.Get() );
if ( retVal == vtkRegressionTester::DO_INTERACTOR)
{
iren->Start();
}
return !retVal;
}
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "vtkOpenGLHelper.h" #include "vtkOpenGLHelper.h"
#include "vtkCellArray.h"
#include "vtkHardwareSelector.h" #include "vtkHardwareSelector.h"
#include "vtkMath.h" #include "vtkMath.h"
#include "vtkObjectFactory.h" #include "vtkObjectFactory.h"
...@@ -25,6 +26,7 @@ ...@@ -25,6 +26,7 @@
#include "vtkOpenGLPolyDataMapper.h" #include "vtkOpenGLPolyDataMapper.h"
#include "vtkOpenGLVertexArrayObject.h" #include "vtkOpenGLVertexArrayObject.h"
#include "vtkOpenGLVertexBufferObject.h" #include "vtkOpenGLVertexBufferObject.h"
#include "vtkPiecewiseFunction.h"
#include "vtkPointData.h" #include "vtkPointData.h"
#include "vtkPolyData.h" #include "vtkPolyData.h"
#include "vtkProperty.h" #include "vtkProperty.h"
...@@ -46,6 +48,15 @@ public: ...@@ -46,6 +48,15 @@ public:
vtkPointGaussianMapper *Owner; vtkPointGaussianMapper *Owner;
bool UsingPoints;
float *OpacityTable; // the table
float OpacityScale; // used for quick lookups
float OpacityOffset; // used for quick lookups
float *ScaleTable; // the table
float ScaleScale; // used for quick lookups
float ScaleOffset; // used for quick lookups
protected: protected:
vtkOpenGLPointGaussianMapperHelper(); vtkOpenGLPointGaussianMapperHelper();
~vtkOpenGLPointGaussianMapperHelper(); ~vtkOpenGLPointGaussianMapperHelper();
...@@ -83,13 +94,17 @@ protected: ...@@ -83,13 +94,17 @@ protected:
virtual void RenderPieceDraw(vtkRenderer *ren, vtkActor *act); virtual void RenderPieceDraw(vtkRenderer *ren, vtkActor *act);
// create the table for opacity values
void BuildOpacityTable(vtkPolyData *);
// create the table for scale values
void BuildScaleTable(vtkPolyData *);
// Description: // Description:
// Does the shader source need to be recomputed // Does the shader source need to be recomputed
virtual bool GetNeedToRebuildShaders(vtkOpenGLHelper &cellBO, virtual bool GetNeedToRebuildShaders(vtkOpenGLHelper &cellBO,
vtkRenderer *ren, vtkActor *act); vtkRenderer *ren, vtkActor *act);
bool UsingPoints;
private: private:
vtkOpenGLPointGaussianMapperHelper(const vtkOpenGLPointGaussianMapperHelper&); // Not implemented. vtkOpenGLPointGaussianMapperHelper(const vtkOpenGLPointGaussianMapperHelper&); // Not implemented.
void operator=(const vtkOpenGLPointGaussianMapperHelper&); // Not implemented. void operator=(const vtkOpenGLPointGaussianMapperHelper&); // Not implemented.
...@@ -102,6 +117,8 @@ vtkStandardNewMacro(vtkOpenGLPointGaussianMapperHelper) ...@@ -102,6 +117,8 @@ vtkStandardNewMacro(vtkOpenGLPointGaussianMapperHelper)
vtkOpenGLPointGaussianMapperHelper::vtkOpenGLPointGaussianMapperHelper() vtkOpenGLPointGaussianMapperHelper::vtkOpenGLPointGaussianMapperHelper()
{ {
this->Owner = NULL; this->Owner = NULL;
this->OpacityTable = 0;
this->ScaleTable = 0;
} }
...@@ -112,10 +129,7 @@ void vtkOpenGLPointGaussianMapperHelper::GetShaderTemplate( ...@@ -112,10 +129,7 @@ void vtkOpenGLPointGaussianMapperHelper::GetShaderTemplate(
{ {
this->Superclass::GetShaderTemplate(shaders,ren,actor); this->Superclass::GetShaderTemplate(shaders,ren,actor);
vtkPolyData *poly = this->CurrentInput; if (this->Owner->GetScaleFactor() == 0.0)
bool hasScaleArray = this->Owner->GetScaleArray() != NULL &&
poly->GetPointData()->HasArray(this->Owner->GetScaleArray());
if (!hasScaleArray && this->Owner->GetDefaultRadius() == 0.0)
{ {
this->UsingPoints = true; this->UsingPoints = true;
} }
...@@ -159,20 +173,25 @@ void vtkOpenGLPointGaussianMapperHelper::ReplaceShaderColor( ...@@ -159,20 +173,25 @@ void vtkOpenGLPointGaussianMapperHelper::ReplaceShaderColor(
{ {
if (!this->UsingPoints) if (!this->UsingPoints)
{ {
std::string VSSource = shaders[vtkShader::Vertex]->GetSource();
std::string FSSource = shaders[vtkShader::Fragment]->GetSource(); std::string FSSource = shaders[vtkShader::Fragment]->GetSource();
vtkShaderProgram::Substitute(FSSource,"//VTK::Color::Impl", if (this->Owner->GetSplatShaderCode())
// compute the eye position and unit direction {
"//VTK::Color::Impl\n" vtkShaderProgram::Substitute(FSSource,"//VTK::Color::Impl",
" float dist2 = dot(offsetVCVSOutput.xy,offsetVCVSOutput.xy);\n" this->Owner->GetSplatShaderCode(), false);
" if (dist2 > 9.0) { discard; }\n" }
" float gaussian = exp(-0.5*dist2);\n" else
" opacity = opacity*gaussian;" {
// " opacity = opacity*0.5;" vtkShaderProgram::Substitute(FSSource,"//VTK::Color::Impl",
, false); // compute the eye position and unit direction
"//VTK::Color::Impl\n"
shaders[vtkShader::Vertex]->SetSource(VSSource); " float dist2 = dot(offsetVCVSOutput.xy,offsetVCVSOutput.xy);\n"
" if (dist2 > 9.0) { discard; }\n"
" float gaussian = exp(-0.5*dist2);\n"
" opacity = opacity*gaussian;"
// " opacity = opacity*0.5;"
, false);
}
shaders[vtkShader::Fragment]->SetSource(FSSource); shaders[vtkShader::Fragment]->SetSource(FSSource);
} }
...@@ -216,6 +235,16 @@ bool vtkOpenGLPointGaussianMapperHelper::GetNeedToRebuildShaders( ...@@ -216,6 +235,16 @@ bool vtkOpenGLPointGaussianMapperHelper::GetNeedToRebuildShaders(
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
vtkOpenGLPointGaussianMapperHelper::~vtkOpenGLPointGaussianMapperHelper() vtkOpenGLPointGaussianMapperHelper::~vtkOpenGLPointGaussianMapperHelper()
{ {
if (this->OpacityTable)
{
delete [] this->OpacityTable;
this->OpacityTable = 0;
}
if (this->ScaleTable)
{
delete [] this->ScaleTable;
this->ScaleTable = 0;
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
...@@ -282,69 +311,135 @@ void vtkOpenGLPointGaussianMapperHelper::SetMapperShaderParameters(vtkOpenGLHelp ...@@ -282,69 +311,135 @@ void vtkOpenGLPointGaussianMapperHelper::SetMapperShaderParameters(vtkOpenGLHelp
namespace namespace
{ {
// internal function called by CreateVBO // internal function called by CreateVBO
// if verts are provided then we only draw those points
// otherwise we draw all the points
template< typename PointDataType, typename SizeDataType > template< typename PointDataType, typename SizeDataType >
void vtkOpenGLPointGaussianMapperHelperPackVBOTemplate2( void vtkOpenGLPointGaussianMapperHelperPackVBOTemplate3(
std::vector< float >::iterator& it, std::vector< float >::iterator& it,
PointDataType* points, vtkIdType numPts, PointDataType* points,
SizeDataType* sizes,
vtkIdType index,
vtkOpenGLPointGaussianMapperHelper *self,
unsigned char *colors, int colorComponents, unsigned char *colors, int colorComponents,
SizeDataType* sizes, float defaultSize) vtkDataArray *opacities, float defaultScale)
{ {
PointDataType *pointPtr; PointDataType *pointPtr;
unsigned char *colorPtr; unsigned char *colorPtr;
unsigned char white[4] = {255, 255, 255, 255};
vtkucfloat rcolor;
// if there are no per point sizes and the default size is zero // if there are no per point sizes and the default size is zero
// then just render points, saving memory and speed // then just render points, saving memory and speed
if (!sizes && defaultSize == 0.0)