Commit 52fd8ad8 authored by Kyle Lutz's avatar Kyle Lutz Committed by Utkarsh Ayachit

Refactoring code to set color/opacity/visibility for blocks.

This commit refactors some of the code added in recent weeks to add ability for
users to set display paramaters for blocks within a composite dataset. This
commit additionally adds support for changing block opacity.

Blocks colors/opacities can also be overridden when using scalar coloring.
The only issue is when depth-peeling gets used. In that case, point-data coloring
with texture (i.e. using interpolate scalars before mapping) results in color
bleeds. We need to dig into the depth peeling code to address that issue.

Change-Id: I2c86912b45bc0516dc4dc726df9448e0569c8312
parent cdde4af4
......@@ -47,6 +47,11 @@ bool vtkCompositeDataDisplayAttributes::GetBlockVisibility(unsigned int flat_ind
}
}
bool vtkCompositeDataDisplayAttributes::HasBlockVisibilities() const
{
return !this->BlockVisibilities.empty();
}
bool vtkCompositeDataDisplayAttributes::HasBlockVisibility(unsigned int flat_index) const
{
return this->BlockVisibilities.count(flat_index) == size_t(1);
......@@ -91,6 +96,11 @@ vtkColor3d vtkCompositeDataDisplayAttributes::GetBlockColor(
return vtkColor3d();
}
bool vtkCompositeDataDisplayAttributes::HasBlockColors() const
{
return !this->BlockColors.empty();
}
bool vtkCompositeDataDisplayAttributes::HasBlockColor(
unsigned int flat_index) const
{
......@@ -112,3 +122,40 @@ void vtkCompositeDataDisplayAttributes::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
void vtkCompositeDataDisplayAttributes::SetBlockOpacity(unsigned int flat_index, double opacity)
{
this->BlockOpacities[flat_index] = opacity;
}
double vtkCompositeDataDisplayAttributes::GetBlockOpacity(unsigned int flat_index) const
{
std::map<unsigned int, double>::const_iterator iter = this->BlockOpacities.find(flat_index);
if(iter != this->BlockOpacities.end())
{
return iter->second;
}
return 0;
}
bool vtkCompositeDataDisplayAttributes::HasBlockOpacities() const
{
return !this->BlockOpacities.empty();
}
bool vtkCompositeDataDisplayAttributes::HasBlockOpacity(unsigned int flat_index) const
{
return this->BlockOpacities.find(flat_index) != this->BlockOpacities.end();
}
void vtkCompositeDataDisplayAttributes::RemoveBlockOpacity(unsigned int flat_index)
{
this->BlockOpacities.erase(flat_index);
}
void vtkCompositeDataDisplayAttributes::RemoveBlockOpacities()
{
this->BlockOpacities.clear();
}
......@@ -34,6 +34,10 @@ public:
vtkTypeMacro(vtkCompositeDataDisplayAttributes, vtkObject)
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Returns true if any block has any block visibility is set.
bool HasBlockVisibilities() const;
// Description:
// Set/get the visibility for the block with \p flat_index.
void SetBlockVisibility(unsigned int flat_index, bool visible);
......@@ -59,6 +63,10 @@ public:
void GetBlockColor(unsigned int flat_index, double color[3]) const;
vtkColor3d GetBlockColor(unsigned int flat_index) const;
// Description:
// Returns true if any block has any block color is set.
bool HasBlockColors() const;
// Description:
// Returns true if the block with the given \p flat_index has a color.
bool HasBlockColor(unsigned int flat_index) const;
......@@ -71,6 +79,27 @@ public:
// Removes all block colors.
void RemoveBlockColors();
// Description:
// Set/get the opacity for the block with flat_index.
void SetBlockOpacity(unsigned int flat_index, double opacity);
double GetBlockOpacity(unsigned int flat_index) const;
// Description:
// Returns true if any block has an opacity set.
bool HasBlockOpacities() const;
// Description:
// Returns true if the block with flat_index has an opacity set.
bool HasBlockOpacity(unsigned int flat_index) const;
// Description:
// Removes the set opacity for the block with flat_index.
void RemoveBlockOpacity(unsigned int flat_index);
// Description:
// Removes all block opacities.
void RemoveBlockOpacities();
protected:
vtkCompositeDataDisplayAttributes();
~vtkCompositeDataDisplayAttributes();
......@@ -82,6 +111,7 @@ private:
private:
std::map<unsigned int, bool> BlockVisibilities;
std::map<unsigned int, vtkColor3d> BlockColors;
std::map<unsigned int, double> BlockOpacities;
};
#endif // __vtkCompositeDataDisplayAttributes_h
This diff is collapsed.
......@@ -24,8 +24,13 @@
#include "vtkRenderingCoreModule.h" // For export macro
#include "vtkPainter.h"
#include "vtkColor.h" // needed for vtkColor3d
#include <stack> // needed for RenderBlockState.
class vtkColor3d;
class vtkCompositeDataDisplayAttributes;
class vtkInformationObjectBaseKey;
class vtkProperty;
class vtkRenderWindow;
class VTKRENDERINGCORE_EXPORT vtkCompositePainter : public vtkPainter
{
......@@ -39,6 +44,18 @@ public:
// simply forwards the input data object as the output.
virtual vtkDataObject* GetOutput();
// Description:
// Key used to pass a vtkCompositeDataDisplayAttributes instance doing the
// painter pipeline.
static vtkInformationObjectBaseKey* DISPLAY_ATTRIBUTES();
// Description:
// Set/get the composite data set display attributes. If set, these attributes
// can be used by the painter to control specific rendering attributes on a
// per-block basis for a multi-block dataset.
void SetCompositeDataDisplayAttributes(vtkCompositeDataDisplayAttributes *attributes);
vtkGetObjectMacro(CompositeDataDisplayAttributes, vtkCompositeDataDisplayAttributes)
//BTX
protected:
vtkCompositePainter();
......@@ -48,6 +65,11 @@ protected:
// Take part in garbage collection.
virtual void ReportReferences(vtkGarbageCollector *collector);
// Description:
// Called before RenderInternal() if the Information has been changed
// since the last time this method was called.
virtual void ProcessInformation(vtkInformation* information);
// Description:
// Performs the actual rendering. Subclasses may override this method.
// default implementation merely call a Render on the DelegatePainter,
......@@ -57,16 +79,36 @@ protected:
virtual void RenderInternal(vtkRenderer* renderer, vtkActor* actor,
unsigned long typeflags, bool forceCompileOnly);
class RenderBlockState
{
public:
std::stack<bool> Visibility;
std::stack<double> Opacity;
std::stack<vtkColor3d> AmbientColor;
std::stack<vtkColor3d> DiffuseColor;
std::stack<vtkColor3d> SpecularColor;
double RenderedOpacity;
vtkColor3d RenderedAmbientColor;
vtkColor3d RenderedDiffuseColor;
vtkColor3d RenderedSpecularColor;
};
void RenderBlock(vtkRenderer *renderer,
vtkActor *actor,
unsigned long typeflags,
bool forceCompileOnly,
vtkDataObject *dobj,
unsigned int &flat_index,
bool &visible,
vtkColor3d &color);
RenderBlockState &state);
// Description:
// Overridden in vtkOpenGLCompositePainter to pass attributes to OpenGL.
virtual void UpdateRenderingState(
vtkRenderWindow*, vtkProperty*, RenderBlockState&) {}
vtkDataObject* OutputData;
vtkCompositeDataDisplayAttributes *CompositeDataDisplayAttributes;
private:
vtkCompositePainter(const vtkCompositePainter&); // Not implemented.
void operator=(const vtkCompositePainter&); // Not implemented.
......
......@@ -137,6 +137,12 @@ bool vtkCompositePolyDataMapper2::GetIsOpaque()
}
}
}
else if(this->CompositeAttributes &&
this->CompositeAttributes->HasBlockOpacities())
{
return false;
}
return this->Superclass::GetIsOpaque();
}
......@@ -221,6 +227,46 @@ void vtkCompositePolyDataMapper2::RemoveBlockColors()
}
}
//----------------------------------------------------------------------------
void vtkCompositePolyDataMapper2::SetBlockOpacity(unsigned int index, double opacity)
{
if(this->CompositeAttributes)
{
this->CompositeAttributes->SetBlockOpacity(index, opacity);
this->Modified();
}
}
//----------------------------------------------------------------------------
double vtkCompositePolyDataMapper2::GetBlockOpacity(unsigned int index)
{
if(this->CompositeAttributes)
{
return this->CompositeAttributes->GetBlockOpacity(index);
}
return 1.;
}
//----------------------------------------------------------------------------
void vtkCompositePolyDataMapper2::RemoveBlockOpacity(unsigned int index)
{
if(this->CompositeAttributes)
{
this->CompositeAttributes->RemoveBlockOpacity(index);
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkCompositePolyDataMapper2::RemoveBlockOpacities()
{
if(this->CompositeAttributes)
{
this->CompositeAttributes->RemoveBlockOpacities();
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkCompositePolyDataMapper2::SetCompositeDataDisplayAttributes(
vtkCompositeDataDisplayAttributes *attributes)
......@@ -228,25 +274,7 @@ void vtkCompositePolyDataMapper2::SetCompositeDataDisplayAttributes(
if(this->CompositeAttributes != attributes)
{
this->CompositeAttributes = attributes;
// set composite display attributes on the composite painter
vtkDefaultPainter *defaultPainter =
vtkDefaultPainter::SafeDownCast(this->Painter);
if(defaultPainter)
{
vtkCompositePainter *compositePainter =
defaultPainter->GetCompositePainter();
compositePainter->
SetCompositeDataDisplayAttributes(this->CompositeAttributes);
}
// set composite display attributes on the selection painter
if(this->SelectionCompositePainter)
{
this->SelectionCompositePainter->
SetCompositeDataDisplayAttributes(this->CompositeAttributes);
}
this->Modified();
}
}
......@@ -257,6 +285,15 @@ vtkCompositePolyDataMapper2::GetCompositeDataDisplayAttributes()
return this->CompositeAttributes;
}
//----------------------------------------------------------------------------
void vtkCompositePolyDataMapper2::UpdatePainterInformation()
{
this->Superclass::UpdatePainterInformation();
this->PainterInformation->Set(
vtkCompositePainter::DISPLAY_ATTRIBUTES(),
this->CompositeAttributes);
}
//----------------------------------------------------------------------------
void vtkCompositePolyDataMapper2::PrintSelf(ostream& os, vtkIndent indent)
{
......
......@@ -61,10 +61,22 @@ public:
// Description:
// Set/get the color for a block given its flat index.
void SetBlockColor(unsigned int index, double color[3]);
void SetBlockColor(unsigned int index, double r, double g, double b)
{
double color[3] = {r, g, b};
this->SetBlockColor(index, color);
}
double* GetBlockColor(unsigned int index);
void RemoveBlockColor(unsigned int index);
void RemoveBlockColors();
// Description:
// Set/get the opacity for a block given its flat index.
void SetBlockOpacity(unsigned int index, double opacity);
double GetBlockOpacity(unsigned int index);
void RemoveBlockOpacity(unsigned int index);
void RemoveBlockOpacities();
//BTX
protected:
vtkCompositePolyDataMapper2();
......@@ -84,6 +96,11 @@ protected:
// Need to loop over the hierarchy to compute bounds
virtual void ComputeBounds();
// Description:
// Called when the PainterInformation becomes obsolete. Overridden to pass
// CompositeDataDisplayAttributes to the painters.
virtual void UpdatePainterInformation();
// Description:
// Time stamp for computation of bounds.
vtkTimeStamp BoundsMTime;
......
......@@ -26,7 +26,6 @@
#include "vtkCellData.h"
#include "vtkCommand.h"
#include "vtkCompositeDataDisplayAttributes.h"
#include "vtkDataSet.h"
#include "vtkDebugLeaks.h"
#include "vtkGarbageCollector.h"
......@@ -40,7 +39,6 @@
vtkCxxSetObjectMacro(vtkPainter, Input, vtkDataObject);
vtkCxxSetObjectMacro(vtkPainter, Information, vtkInformation);
vtkCxxSetObjectMacro(vtkPainter, CompositeDataDisplayAttributes, vtkCompositeDataDisplayAttributes)
vtkInformationKeyMacro(vtkPainter, STATIC_DATA, Integer);
vtkInformationKeyMacro(vtkPainter, CONSERVE_MEMORY, Integer);
vtkInformationKeyMacro(vtkPainter, HIGH_QUALITY, Integer);
......@@ -89,8 +87,6 @@ vtkPainter::vtkPainter()
vtkPainter::STATIC_DATA()->Set(this->Information, 0);
vtkPainter::CONSERVE_MEMORY()->Set(this->Information, 0);
vtkPainter::HIGH_QUALITY()->Set(this->Information, 1);
this->CompositeDataDisplayAttributes = 0;
}
//-----------------------------------------------------------------------------
......@@ -110,8 +106,6 @@ vtkPainter::~vtkPainter()
}
this->Timer->Delete();
this->SetCompositeDataDisplayAttributes(0);
}
//-----------------------------------------------------------------------------
......@@ -372,15 +366,4 @@ void vtkPainter::PrintSelf(ostream &os, vtkIndent indent)
{
os << "(none)" << endl;
}
os << indent << "CompositeDataDisplayAttributes: " ;
if (this->CompositeDataDisplayAttributes)
{
os << endl;
this->CompositeDataDisplayAttributes->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
}
......@@ -46,7 +46,6 @@
class vtkAbstractArray;
class vtkActor;
class vtkCompositeDataDisplayAttributes;
class vtkDataObject;
class vtkDataSet;
class vtkInformation;
......@@ -151,13 +150,6 @@ public:
virtual vtkDataObject* GetOutput()
{ return this->Input; }
// Description:
// Set/get the composite data set display attributes. If set, these attributes
// can be used by the painter to control specific rendering attributes on a
// per-block basis for a multi-block dataset.
void SetCompositeDataDisplayAttributes(vtkCompositeDataDisplayAttributes *attributes);
vtkGetObjectMacro(CompositeDataDisplayAttributes, vtkCompositeDataDisplayAttributes)
//BTX
protected:
vtkPainter();
......@@ -246,7 +238,6 @@ protected:
double TimeToDraw;
vtkTimerLog* Timer;
vtkCompositeDataDisplayAttributes *CompositeDataDisplayAttributes;
vtkWeakPointer<vtkWindow> LastWindow; // Window used for previous render.
// This is not reference counted.
......
......@@ -38,17 +38,6 @@ vtkPainterDeviceAdapter::~vtkPainterDeviceAdapter()
{
}
//-----------------------------------------------------------------------------
void vtkPainterDeviceAdapter::SendMaterialProperties(int,
int,
const void*,
const void*,
const void*,
const void*)
{
// should be implemented by subclasses
}
//-----------------------------------------------------------------------------
void vtkPainterDeviceAdapter::PrintSelf(ostream &os, vtkIndent indent)
{
......
......@@ -96,15 +96,6 @@ public:
virtual void SendAttribute(int index, int components, int type,
const void *attribute, vtkIdType offset=0) = 0;
// Description:
// Sends material properties to the graphics card.
virtual void SendMaterialProperties(int components,
int type,
const void *ambient,
const void *diffuse,
const void *specular,
const void *specular_power);
// Description:
// Sets an array of attributes. This allows you to send all the data for
// a particular attribute with one call, thus greatly reducing function
......
......@@ -279,24 +279,34 @@ void vtkProperty::SetColor(double r, double g, double b)
}
//----------------------------------------------------------------------------
// Return composite color of object (ambient + diffuse + specular). Return value
// is a pointer to rgb values.
double *vtkProperty::GetColor()
void vtkProperty::ComputeCompositeColor(double result[3],
double ambient, const double ambient_color[3],
double diffuse, const double diffuse_color[3],
double specular, const double specular_color[3])
{
double norm = 0.0;
if ((this->Ambient + this->Diffuse + this->Specular)>0)
if ((ambient + diffuse + specular)>0)
{
norm = 1.0 / (this->Ambient + this->Diffuse + this->Specular);
norm = 1.0 / (ambient + diffuse + specular);
}
for (int i = 0; i < 3; i ++)
{
this->Color[i] = this->AmbientColor[i] * this->Ambient * norm;
this->Color[i] = this->Color[i] + this->DiffuseColor[i] * this->Diffuse * norm;
this->Color[i] = this->Color[i] + this->SpecularColor[i] * this->Specular * norm;
result[i] = ( ambient * ambient_color[i] +
diffuse * diffuse_color[i] +
specular * specular_color[i] ) * norm;
}
}
//----------------------------------------------------------------------------
// Return composite color of object (ambient + diffuse + specular). Return value
// is a pointer to rgb values.
double *vtkProperty::GetColor()
{
vtkProperty::ComputeCompositeColor(this->Color,
this->Ambient, this->AmbientColor,
this->Diffuse, this->DiffuseColor,
this->Specular, this->SpecularColor);
return this->Color;
}
......@@ -904,17 +914,6 @@ void vtkProperty::PostRender(vtkActor* actor, vtkRenderer* renderer)
}
}
//----------------------------------------------------------------------------
void vtkProperty::RenderMaterial(vtkActor *,
vtkRenderer *,
double *,
double *,
double *,
double)
{
// should be implemented by subclasses
}
//----------------------------------------------------------------------------
void vtkProperty::AddShaderVariable(const char* name, int numVars, int* x)
{
......
......@@ -92,18 +92,6 @@ public:
virtual void PostRender(vtkActor*, vtkRenderer*);
//ETX
// Description:
// Render the material for the face. This method should be implemented
// by subclasses. The default implementation does nothing.
//
// Warning: Experimental. This API may change in future releases.
virtual void RenderMaterial(vtkActor *actor,
vtkRenderer *renderer,
double *ambient,
double *diffuse,
double *specular,
double specular_power);
// Description:
// Set/Get lighting flag for an object. Initial value is true.
vtkGetMacro(Lighting, bool);
......@@ -390,6 +378,13 @@ protected:
vtkProperty();
~vtkProperty();
// Description:
// Computes composite color. Used by GetColor().
static void ComputeCompositeColor(double result[3],
double ambient, const double ambient_color[3],
double diffuse, const double diffuse_color[3],
double specular, const double specular_color[3]);
// Description:
// Load property iVar values from the Material XML.
void LoadProperty();
......
......@@ -58,6 +58,7 @@ set(Module_SRCS
vtkOpenGLCamera.cxx
vtkOpenGLClipPlanesPainter.cxx
vtkOpenGLCoincidentTopologyResolutionPainter.cxx
vtkOpenGLCompositePainter.cxx
vtkOpenGLDisplayListPainter.cxx
vtkOpenGLExtensionManager.cxx
vtkOpenGLGL2PSHelper.cxx
......@@ -274,6 +275,7 @@ set(opengl_overrides
Camera
ClipPlanesPainter
CoincidentTopologyResolutionPainter
CompositePainter
DisplayListPainter
Glyph3DMapper
ImageSliceMapper
......
......@@ -93,9 +93,10 @@ if(VTK_DATA_ROOT)
set(RenderingTestsWithArguments
${RenderingTestsWithArguments}
TestAreaSelections.cxx
TestGlyph3DMapperArrow.cxx
TestMultiblockDisplayProperties.cxx
TestMultiTexturing.cxx
TestMultiTexturingTransform.cxx
TestGlyph3DMapperArrow.cxx
TestImageSliceMapperBorder.cxx
TestImageResliceMapperBorder.cxx
TestScalarBar.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestMultiblockDisplayProperties.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 tests setting up of display properties of individual blocks in
// composite datasets.
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkCompositeDataDisplayAttributes.h"
#include "vtkCompositePolyDataMapper2.h"
#include "vtkDataObject.h"
#include "vtkNew.h"
#include "vtkProperty.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTestUtilities.h"
#include "vtkXMLMultiBlockDataReader.h"
int TestMultiblockDisplayProperties(int argc, char* argv[])
{
vtkNew<vtkXMLMultiBlockDataReader> reader;
char * fname = vtkTestUtilities::ExpandDataFileName(
argc, argv, "Data/many_blocks/many_blocks.vtm");
reader->SetFileName(fname);
delete[] fname;
vtkNew<vtkRenderWindow> renWin;
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin.GetPointer());
vtkNew<vtkRenderer> renderer;
renWin->AddRenderer(renderer.GetPointer());
vtkNew<vtkActor> actor;
renderer->AddActor(actor.GetPointer());
vtkNew<vtkCompositePolyDataMapper2> mapper;
mapper->SetInputConnection(reader->GetOutputPort());
actor->SetMapper(mapper.GetPointer());
renWin->SetSize(400,400);
renderer->GetActiveCamera()->SetViewUp(0, 0, 1);
renderer->GetActiveCamera()->SetPosition(-1.3, 0, 1.7);
renderer->GetActiveCamera()->SetFocalPoint(0, 0, 1.6);
renderer->SetBackground(0.1,0.2,0.4);
renderer->ResetCamera();
renWin->Render();
vtkNew<vtkCompositeDataDisplayAttributes> attributes;
mapper->SetCompositeDataDisplayAttributes(attributes.GetPointer());
mapper->SetBlockVisibility(1, 0);
mapper->SetBlockVisibility(23, 1);
mapper->SetBlockVisibility(27, 0);
mapper->SetBlockVisibility(29, 0);
renWin->Render();
mapper->RemoveBlockVisibility(29);
renWin->Render();
// Color "Group B" green.
mapper->SetBlockColor(67, 0, 0.33, 0.0);
renWin->Render();
// Show "Group ACAA" and color it yellow.
mapper->SetBlockVisibility(46, true);
mapper->SetBlockColor(46, 1, 1, 0.5);
renWin->Render();
// Set opacity on "Group AC" to 0.5
mapper->SetBlockOpacity(34, 0.5);
renWin->Render();
// Change solid color.
actor->GetProperty()->SetColor(0.5, 0.1, 0.1);
renWin->Render();
// Remove all opacity overrides.
mapper->RemoveBlockOpacities();
renWin->Render();
int retVal = vtkRegressionTestImage(renWin.GetPointer());
if (retVal == vtkRegressionTester::DO_INTERACTOR)
{
iren->Start();
}
return !retVal;
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenGLCompositePainter.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.
=========================================================================*/
#include "vtkOpenGLCompositePainter.h"
#include "vtkInformation.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGL.h"
#include "vtkOpenGLProperty.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkPolyDataPainter.h"
vtkStandardNewMacro(vtkOpenGLCompositePainter)
//-----------------------------------------------------------------------------
vtkOpenGLCompositePainter::vtkOpenGLCompositePainter()
{
this->PushedOpenGLAttribs = false;
}
//-----------------------------------------------------------------------------
vtkOpenGLCompositePainter::~vtkOpenGLCompositePainter()
{