Commit d117992f authored by Ken Martin's avatar Ken Martin
Browse files

Fix issue with compositepolydatamapper2 with scalar colors

parent 74fad9c4
if(VTK_RENDERING_BACKEND STREQUAL "OpenGL2")
set(extra_opengl2_tests
TestCompositePolyDataMapper2.cxx,NO_DATA
TestCompositePolyDataMapper2Scalars.cxx,NO_DATA
# the following produce incorrect results in OpenGL
TestTranslucentLUTAlphaBlending.cxx
TestTranslucentLUTTextureAlphaBlending.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCompositePolyDataMapper2.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 "vtkActor.h"
#include "vtkCamera.h"
#include "vtkCompositeDataSet.h"
#include "vtkCompositeDataDisplayAttributes.h"
#include "vtkCompositePolyDataMapper2.h"
#include "vtkCullerCollection.h"
#include "vtkInformation.h"
#include "vtkMath.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkNew.h"
#include "vtkProperty.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkTimerLog.h"
#include "vtkTrivialProducer.h"
#include <vtkTestUtilities.h>
#include <vtkRegressionTestImage.h>
#include "vtkCylinderSource.h"
#include "vtkElevationFilter.h"
int TestCompositePolyDataMapper2Scalars(int argc, char* argv[])
{
vtkSmartPointer<vtkRenderWindow> win =
vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkRenderer> ren =
vtkSmartPointer<vtkRenderer>::New();
win->AddRenderer(ren);
win->SetInteractor(iren);
vtkSmartPointer<vtkCompositePolyDataMapper2> mapper =
vtkSmartPointer<vtkCompositePolyDataMapper2>::New();
vtkNew<vtkCompositeDataDisplayAttributes> cdsa;
mapper->SetCompositeDataDisplayAttributes(cdsa.GetPointer());
int resolution = 18;
vtkNew<vtkCylinderSource> cyl;
cyl->CappingOn();
cyl->SetRadius(0.2);
cyl->SetResolution(resolution);
vtkNew<vtkElevationFilter> elev;
elev->SetInputConnection(cyl->GetOutputPort());
// geometry range is -0.5 to 0.5 but these colors are
// pretty
elev->SetLowPoint(0,-1.0,0);
elev->SetHighPoint(0.0,1.0,0.0);
// build a composite dataset
vtkNew<vtkMultiBlockDataSet> data;
// int blocksPerLevel[3] = {1,64,256};
int blocksPerLevel[3] = {1,32,64};
std::vector<vtkSmartPointer<vtkMultiBlockDataSet> > blocks;
blocks.push_back(data.GetPointer());
unsigned levelStart = 0;
unsigned levelEnd = 1;
int numLevels = sizeof(blocksPerLevel) / sizeof(blocksPerLevel[0]);
int numLeaves = 0;
int numNodes = 0;
vtkStdString blockName("Rolf");
for (int level = 1; level < numLevels; ++level)
{
int nblocks=blocksPerLevel[level];
for (unsigned parent = levelStart; parent < levelEnd; ++parent)
{
blocks[parent]->SetNumberOfBlocks(nblocks);
for (int block=0; block < nblocks; ++block, ++numNodes)
{
if (level == numLevels - 1)
{
vtkNew<vtkPolyData> child;
cyl->SetCenter(block*0.25, 0.0, parent*0.5);
elev->Update();
child->DeepCopy(elev->GetOutput(0));
blocks[parent]->SetBlock(
block, (block % 2) ? NULL : child.GetPointer());
blocks[parent]->GetMetaData(block)->Set(
vtkCompositeDataSet::NAME(), blockName.c_str());
// test not setting it on some
if (block % 11)
{
mapper->SetBlockOpacity(parent+numLeaves, (block + 3) % 7 == 0 ? 0.3 : 1.0);
mapper->SetBlockVisibility(parent+numLeaves, (block % 7) != 0);
}
++numLeaves;
}
else
{
vtkNew<vtkMultiBlockDataSet> child;
blocks[parent]->SetBlock(block, child.GetPointer());
blocks.push_back(child.GetPointer());
}
}
}
levelStart = levelEnd;
levelEnd = static_cast<unsigned>(blocks.size());
}
mapper->SetInputData((vtkPolyData *)(data.GetPointer()));
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
//actor->GetProperty()->SetEdgeColor(1,0,0);
//actor->GetProperty()->EdgeVisibilityOn();
ren->AddActor(actor);
win->SetSize(400,400);
ren->RemoveCuller(ren->GetCullers()->GetLastItem());
ren->ResetCamera();
vtkSmartPointer<vtkTimerLog> timer = vtkSmartPointer<vtkTimerLog>::New();
win->Render(); // get the window up
// modify the data to force a rebuild of OpenGL structs
// after rendering set one cylinder to white
mapper->SetBlockColor(911,1.0,1.0,1.0);
mapper->SetBlockOpacity(911,1.0);
mapper->SetBlockVisibility(911,1.0);
timer->StartTimer();
win->Render();
timer->StopTimer();
cout << "First frame time: " << timer->GetElapsedTime() << "\n";
timer->StartTimer();
int numFrames = 2;
for (int i = 0; i <= numFrames; i++)
{
ren->GetActiveCamera()->Elevation(40.0/numFrames);
ren->GetActiveCamera()->Zoom(pow(2.0,1.0/numFrames));
ren->GetActiveCamera()->Roll(20.0/numFrames);
win->Render();
}
timer->StopTimer();
double t = timer->GetElapsedTime();
cout << "Avg Frame time: " << t/numFrames << " Frame Rate: " << numFrames / t << "\n";
int retVal = vtkRegressionTestImageThreshold( win.GetPointer(),15);
if ( retVal == vtkRegressionTester::DO_INTERACTOR)
{
iren->Start();
}
return !retVal;
}
......@@ -70,6 +70,28 @@ void vtkCompositePolyDataMapper2::FreeStructures()
this->RenderValues.resize(0);
}
void vtkCompositePolyDataMapper2::ReplaceShaderColor(
std::map<vtkShader::Type, vtkShader *> shaders,
vtkRenderer *ren, vtkActor *actor)
{
std::string FSSource = shaders[vtkShader::Fragment]->GetSource();
vtkShaderProgram::Substitute(FSSource,"//VTK::Color::Dec",
"uniform bool OverridesColor;\n"
"//VTK::Color::Dec",false);
vtkShaderProgram::Substitute(FSSource,"//VTK::Color::Impl",
"//VTK::Color::Impl\n"
" if (OverridesColor) {\n"
" ambientColor = ambientColorUniform;\n"
" diffuseColor = diffuseColorUniform; }\n",
false);
shaders[vtkShader::Fragment]->SetSource(FSSource);
this->Superclass::ReplaceShaderColor(shaders,ren,actor);
}
// ---------------------------------------------------------------------------
// Description:
// Method initiates the mapping process. Generally sent by the actor
......@@ -290,6 +312,7 @@ void vtkCompositePolyDataMapper2::BuildRenderValues(
rv.Visibility = vis;
rv.Color = color;
rv.PickId = my_flat_index;
rv.OverridesColor = (this->BlockState.AmbientColor.size() > 1);
this->RenderValues.push_back(rv);
}
......@@ -312,6 +335,7 @@ void vtkCompositePolyDataMapper2::BuildRenderValues(
rv.Visibility = vis;
rv.Color = color;
rv.PickId = my_flat_index;
rv.OverridesColor = (this->BlockState.AmbientColor.size() > 1);
this->RenderValues.push_back(rv);
}
lastVertex = this->VertexOffsets[my_flat_index];
......@@ -417,6 +441,7 @@ void vtkCompositePolyDataMapper2::RenderPieceDraw(
prog->SetUniform3f("diffuseColorUniform", diffuseColor);
prog->SetUniformi("PrimitiveIDOffset",
this->PrimitiveIDOffset);
prog->SetUniformi("OverridesColor", it->OverridesColor);
glDrawRangeElements(mode,
static_cast<GLuint>(it->StartVertex),
static_cast<GLuint>(it->EndVertex),
......
......@@ -45,6 +45,13 @@ protected:
vtkCompositePolyDataMapper2();
~vtkCompositePolyDataMapper2();
// Description:
// Perform string replacments on the shader templates, called from
// ReplaceShaderValues
virtual void ReplaceShaderColor(
std::map<vtkShader::Type, vtkShader *> shaders,
vtkRenderer *ren, vtkActor *act);
// Description:
// Build the VBO/IBO, called by UpdateBufferObjects
virtual void BuildBufferObjects(vtkRenderer *ren, vtkActor *act);
......@@ -70,6 +77,7 @@ protected:
unsigned int EndIndex;
unsigned int EndEdgeIndex;
double Opacity;
bool OverridesColor;
bool Visibility;
vtkColor3d Color;
unsigned int PickId;
......
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