Skip to content
/ VTK Public
forked from Kitware/VTK

Commit 92021a6

Browse files
lucas-givordjcfr
authored andcommitted
[Backport] vtkOpenGLPolyDataMapper: add line opacity control
Merge-request: vtk/vtk!10307 (cherry picked from commit Kitware/VTK@28e580e)
1 parent 9e103ca commit 92021a6

File tree

6 files changed

+114
-10
lines changed

6 files changed

+114
-10
lines changed

Rendering/Core/Testing/Cxx/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ vtk_add_test_cxx(vtkRenderingCoreCxxTests tests
5252
TestDiscretizableColorTransferFunctionStringArray.cxx,NO_VALID
5353
TestDragEvent.cxx
5454
TestEdgeFlags.cxx
55+
TestEdgeOpacity.cxx
5556
TestFollowerPicking.cxx
5657
TestGlyph3DMapper.cxx
5758
TestGlyph3DMapper2.cxx,NO_VALID,NO_RT,NO_DATA
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*=========================================================================
2+
3+
Program: Visualization Toolkit
4+
Module: TestEdgeOpacity.cxx
5+
6+
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7+
All rights reserved.
8+
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9+
10+
This software is distributed WITHOUT ANY WARRANTY; without even
11+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12+
PURPOSE. See the above copyright notice for more information.
13+
14+
=========================================================================*/
15+
16+
#include "vtkActor.h"
17+
#include "vtkPolyDataMapper.h"
18+
#include "vtkProperty.h"
19+
#include "vtkRegressionTestImage.h"
20+
#include "vtkRenderWindow.h"
21+
#include "vtkRenderWindowInteractor.h"
22+
#include "vtkRenderer.h"
23+
#include "vtkSphereSource.h"
24+
25+
#include "vtkSmartPointer.h"
26+
#define VTK_CREATE(type, var) vtkSmartPointer<type> var = vtkSmartPointer<type>::New()
27+
28+
namespace
29+
{
30+
void SetupActorWithEdgeOpacity(vtkActor* actor, vtkPolyDataMapper* mapper, int* pos, double opacity)
31+
{
32+
actor->SetMapper(mapper);
33+
actor->SetPosition(pos[0], pos[1], pos[2]);
34+
actor->GetProperty()->EdgeVisibilityOn();
35+
actor->GetProperty()->SetEdgeColor(0.0, 0.0, 0.5);
36+
actor->GetProperty()->SetEdgeOpacity(opacity);
37+
}
38+
}
39+
40+
int TestEdgeOpacity(int argc, char* argv[])
41+
{
42+
// to make sure that wireframe will be visible
43+
vtkMapper::SetResolveCoincidentTopologyToShiftZBuffer();
44+
vtkMapper::SetResolveCoincidentTopologyZShift(0.1);
45+
46+
VTK_CREATE(vtkSphereSource, sphere);
47+
VTK_CREATE(vtkPolyDataMapper, mapper);
48+
mapper->SetInputConnection(sphere->GetOutputPort());
49+
50+
VTK_CREATE(vtkActor, actor1);
51+
int pos[3] = { 0, 0, 0 };
52+
double opacity = 0.33;
53+
::SetupActorWithEdgeOpacity(actor1, mapper, pos, opacity);
54+
55+
VTK_CREATE(vtkActor, actor2);
56+
pos[0] = 1.5;
57+
opacity = 0.66;
58+
::SetupActorWithEdgeOpacity(actor2, mapper, pos, opacity);
59+
60+
VTK_CREATE(vtkActor, actor3);
61+
pos[0] = 2;
62+
opacity = 1.0;
63+
::SetupActorWithEdgeOpacity(actor3, mapper, pos, opacity);
64+
65+
VTK_CREATE(vtkRenderer, renderer);
66+
renderer->AddActor(actor1);
67+
renderer->AddActor(actor2);
68+
renderer->AddActor(actor3);
69+
renderer->ResetCamera();
70+
71+
VTK_CREATE(vtkRenderWindow, renwin);
72+
renwin->AddRenderer(renderer);
73+
renwin->SetSize(250, 250);
74+
renwin->SetMultiSamples(0);
75+
76+
int retVal = vtkRegressionTestImage(renwin);
77+
if (retVal == vtkRegressionTester::DO_INTERACTOR)
78+
{
79+
VTK_CREATE(vtkRenderWindowInteractor, iren);
80+
iren->SetRenderWindow(renwin);
81+
iren->Initialize();
82+
iren->Start();
83+
retVal = vtkRegressionTester::PASSED;
84+
}
85+
86+
return (retVal == vtkRegressionTester::PASSED) ? 0 : 1;
87+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0528f46438944175cb0a52f0d9b7312b038d53e0225627ba4e84fa6d34079fac0639302bd72d1bceb8c32f5843b6e684ff021ffb6285929256cfb01d34a3c9a7

Rendering/Core/vtkProperty.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,15 @@ class VTKRENDERINGCORE_EXPORT vtkProperty : public vtkObject
372372
vtkGetMacro(Opacity, double);
373373
///@}
374374

375+
///@{
376+
/**
377+
* Set/Get the line opacity. 1.0 is totally opaque and 0.0 is completely
378+
* transparent.
379+
*/
380+
vtkSetClampMacro(EdgeOpacity, double, 0.0, 1.0);
381+
vtkGetMacro(EdgeOpacity, double);
382+
///@}
383+
375384
///@{
376385
/**
377386
* Set/Get the ambient surface color. Not all renderers support separate
@@ -780,6 +789,7 @@ class VTKRENDERINGCORE_EXPORT vtkProperty : public vtkObject
780789
double Specular;
781790
double SpecularPower;
782791
double Opacity;
792+
double EdgeOpacity = 1.0;
783793
double EdgeTint[3];
784794
float PointSize;
785795
float LineWidth;

Rendering/OpenGL2/vtkOpenGLES30PolyDataMapper.cxx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ void vtkOpenGLES30PolyDataMapper::ReplaceShaderEdges(
490490
vtkShaderProgram::Substitute(FSSource, "//VTK::Edges::Dec",
491491
"noperspective in vec4 edgeEqn[3];\n"
492492
"uniform float lineWidth;\n"
493-
"uniform vec3 edgeColor;\n");
493+
"uniform vec3 edgeColor;\n"
494+
"uniform float edgeOpacity;\n");
494495

495496
std::string fsimpl =
496497
// distance gets larger as you go inside the polygon
@@ -518,15 +519,16 @@ void vtkOpenGLES30PolyDataMapper::ReplaceShaderEdges(
518519
act->GetProperty()->GetRenderLinesAsTubes() && ren->GetLights()->GetNumberOfItems() > 0;
519520
if (canRenderLinesAsTube)
520521
{
521-
fsimpl += " diffuseColor = mix(diffuseColor, diffuseIntensity*edgeColor, emix);\n"
522-
" ambientColor = mix(ambientColor, ambientIntensity*edgeColor, emix);\n"
522+
fsimpl +=
523+
" diffuseColor = mix(diffuseColor, diffuseIntensity*edgeColor, emix * edgeOpacity);\n"
524+
" ambientColor = mix(ambientColor, ambientIntensity*edgeColor, emix * edgeOpacity);\n"
523525
// " else { discard; }\n" // this yields wireframe only
524526
;
525527
}
526528
else
527529
{
528-
fsimpl += " diffuseColor = mix(diffuseColor, vec3(0.0), emix);\n"
529-
" ambientColor = mix( ambientColor, edgeColor, emix);\n"
530+
fsimpl += " diffuseColor = mix(diffuseColor, vec3(0.0), emix * edgeOpacity);\n"
531+
" ambientColor = mix( ambientColor, edgeColor, emix * edgeOpacity);\n"
530532
// " else { discard; }\n" // this yields wireframe only
531533
;
532534
}

Rendering/OpenGL2/vtkOpenGLPolyDataMapper.cxx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ void vtkOpenGLPolyDataMapper::ReplaceShaderEdges(
617617
vtkShaderProgram::Substitute(FSSource, "//VTK::Edges::Dec",
618618
"noperspective in vec4 edgeEqn[3];\n"
619619
"uniform float lineWidth;\n"
620-
"uniform vec3 edgeColor;\n");
620+
"uniform vec3 edgeColor;\n"
621+
"uniform float edgeOpacity;\n");
621622

622623
std::string fsimpl =
623624
// distance gets larger as you go inside the polygon
@@ -644,15 +645,16 @@ void vtkOpenGLPolyDataMapper::ReplaceShaderEdges(
644645
actor->GetProperty()->GetRenderLinesAsTubes() && ren->GetLights()->GetNumberOfItems() > 0;
645646
if (canRenderLinesAsTube)
646647
{
647-
fsimpl += " diffuseColor = mix(diffuseColor, diffuseIntensity*edgeColor, emix);\n"
648-
" ambientColor = mix(ambientColor, ambientIntensity*edgeColor, emix);\n"
648+
fsimpl +=
649+
" diffuseColor = mix(diffuseColor, diffuseIntensity*edgeColor, emix * edgeOpacity);\n"
650+
" ambientColor = mix(ambientColor, ambientIntensity*edgeColor, emix * edgeOpacity);\n"
649651
// " else { discard; }\n" // this yields wireframe only
650652
;
651653
}
652654
else
653655
{
654-
fsimpl += " diffuseColor = mix(diffuseColor, vec3(0.0), emix);\n"
655-
" ambientColor = mix( ambientColor, edgeColor, emix);\n"
656+
fsimpl += " diffuseColor = mix(diffuseColor, vec3(0.0), emix * edgeOpacity);\n"
657+
" ambientColor = mix( ambientColor, edgeColor, emix * edgeOpacity);\n"
656658
// " else { discard; }\n" // this yields wireframe only
657659
;
658660
}
@@ -2743,6 +2745,7 @@ void vtkOpenGLPolyDataMapper::SetMapperShaderParameters(
27432745
dims[3] = vp[3];
27442746
cellBO.Program->SetUniform4f("vpDims", dims);
27452747
cellBO.Program->SetUniform3f("edgeColor", actor->GetProperty()->GetEdgeColor());
2748+
cellBO.Program->SetUniformf("edgeOpacity", actor->GetProperty()->GetEdgeOpacity());
27462749
}
27472750
vtkOpenGLCheckErrorMacro("failed after UpdateShader");
27482751

0 commit comments

Comments
 (0)