1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkHiddenLineRemovalPass.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 "vtk_glew.h"
#include "vtkHiddenLineRemovalPass.h"
#include "vtkActor.h"
#include "vtkMapper.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLError.h"
#include "vtkProp.h"
#include "vtkProperty.h"
#include "vtkRenderer.h"
#include "vtkRenderState.h"
#include <string>
// Define to print debug statements to the OpenGL CS stream (useful for e.g.
// apitrace debugging):
//#define ANNOTATE_STREAM
namespace
{
void annotate(const std::string &str)
{
#ifdef ANNOTATE_STREAM
vtkOpenGLStaticCheckErrorMacro("Error before glDebug.")
glDebugMessageInsert(GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER,
GL_DEBUG_SEVERITY_NOTIFICATION,
0, str.size(), str.c_str());
vtkOpenGLClearErrorMacro();
#else // ANNOTATE_STREAM
(void)str;
#endif // ANNOTATE_STREAM
}
}
vtkStandardNewMacro(vtkHiddenLineRemovalPass)
//------------------------------------------------------------------------------
void vtkHiddenLineRemovalPass::PrintSelf(std::ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//------------------------------------------------------------------------------
void vtkHiddenLineRemovalPass::Render(const vtkRenderState *s)
{
this->NumberOfRenderedProps = 0;
// Separate the wireframe props from the others:
std::vector<vtkProp*> wireframeProps;
std::vector<vtkProp*> otherProps;
for (int i = 0; i < s->GetPropArrayCount(); ++i)
{
bool isWireframe = false;
vtkProp *prop = s->GetPropArray()[i];
vtkActor *actor = vtkActor::SafeDownCast(prop);
if (actor)
{
vtkProperty *property = actor->GetProperty();
if (property->GetRepresentation() == VTK_WIREFRAME)
{
isWireframe = true;
}
}
if (isWireframe)
{
wireframeProps.push_back(prop);
}
else
{
otherProps.push_back(prop);
}
}
vtkViewport *vp = s->GetRenderer();
// Render the non-wireframe geometry as normal:
annotate("Rendering non-wireframe props.");
this->NumberOfRenderedProps = this->RenderProps(otherProps, vp);
vtkOpenGLStaticCheckErrorMacro("Error after non-wireframe geometry.");
// Store the coincident topology parameters -- we want to force polygon
// offset to keep the drawn lines sharp:
int ctMode = vtkMapper::GetResolveCoincidentTopology();
double ctFactor, ctUnits;
vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(ctFactor,
ctUnits);
vtkMapper::SetResolveCoincidentTopology(VTK_RESOLVE_POLYGON_OFFSET);
vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(2.0, 2.0);
// Draw the wireframe props as surfaces into the depth buffer only:
annotate("Rendering wireframe prop surfaces.");
this->SetRepresentation(wireframeProps, VTK_SURFACE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
this->RenderProps(wireframeProps, vp);
vtkOpenGLStaticCheckErrorMacro("Error after wireframe surface rendering.");
// Now draw the wireframes as normal:
annotate("Rendering wireframes.");
this->SetRepresentation(wireframeProps, VTK_WIREFRAME);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
this->NumberOfRenderedProps = this->RenderProps(wireframeProps, vp);
vtkOpenGLStaticCheckErrorMacro("Error after wireframe rendering.");
// Restore the previous coincident topology parameters:
vtkMapper::SetResolveCoincidentTopology(ctMode);
vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(ctFactor,
ctUnits);
}
//------------------------------------------------------------------------------
bool vtkHiddenLineRemovalPass::WireframePropsExist(vtkProp **propArray,
int nProps)
{
for (int i = 0; i < nProps; ++i)
{
vtkActor *actor = vtkActor::SafeDownCast(propArray[i]);
if (actor)
{
vtkProperty *property = actor->GetProperty();
if (property->GetRepresentation() == VTK_WIREFRAME)
{
return true;
}
}
}
return false;
}
//------------------------------------------------------------------------------
vtkHiddenLineRemovalPass::vtkHiddenLineRemovalPass()
{
}
//------------------------------------------------------------------------------
vtkHiddenLineRemovalPass::~vtkHiddenLineRemovalPass()
{
}
//------------------------------------------------------------------------------
void vtkHiddenLineRemovalPass::SetRepresentation(std::vector<vtkProp *> &props,
int repr)
{
for (std::vector<vtkProp*>::iterator it = props.begin(), itEnd = props.end();
it != itEnd; ++it)
{
vtkActor *actor = vtkActor::SafeDownCast(*it);
if (actor)
{
actor->GetProperty()->SetRepresentation(repr);
}
}
}
//------------------------------------------------------------------------------
int vtkHiddenLineRemovalPass::RenderProps(std::vector<vtkProp *> &props,
vtkViewport *vp)
{
int propsRendered = 0;
for (std::vector<vtkProp*>::iterator it = props.begin(), itEnd = props.end();
it != itEnd; ++it)
{
propsRendered += (*it)->RenderOpaqueGeometry(vp);
}
return propsRendered;
}
|