[vtkusers] Transparency/Opacity - DepthSort example

John Platt jcplatt at lineone.net
Sat Mar 20 15:46:24 EST 2004


Hi All,

 

I have run the depthsort example to see what effects I can expect using
transparent colours. The spheres are shown speckled (see attachment). Is
this a limitation? I am running vtk4.5.0 on XP sp1. The C++ translation
of the example is included below.

 

Thanks in advance.

 

John.

 

/*

# This example demonstrates the use of vtkDepthSortPolyData. This is a 

# poor man's algorithm to sort polygons for proper transparent blending.

# It sorts polygons based on a single point (i.e., centroid) so the
sorting

# may not work for overlapping or intersection polygons.

#

*/

 

#include "stdafx.h"

 

#include "vtkRenderWindowInteractor.h"

#include "vtkRenderWindow.h"

#include "vtkCamera.h"

#include "vtkRenderer.h"

#include "vtkActor.h"

#include "vtkProperty.h"

 

#include "vtkSphereSource.h"

#include "vtkAppendPolyData.h"

#include "vtkDepthSortPolyData.h"

#include "vtkPolyDataMapper.h"

#include "vtkPolyData.h"

 

int APIENTRY WinMain(HINSTANCE hInstance,

                     HINSTANCE hPrevInstance,

                     LPSTR     lpCmdLine,

                     int       nCmdShow)

{

   // Create a bunch of spheres that overlap and cannot be easily
arranged

   // so that the blending works without sorting. They are appended into
a

   // single vtkPolyData because the filter only sorts within a single 

   // vtkPolyData input.

 

   vtkSphereSource* sphere = vtkSphereSource::New();

   sphere->SetThetaResolution( 80 );

   sphere->SetPhiResolution( 40 );

   sphere->SetRadius( 1 );

   sphere->SetCenter( 0, 0, 0 );

 

   vtkSphereSource* sphere2 = vtkSphereSource::New();

   sphere2->SetThetaResolution( 80 );

   sphere2->SetPhiResolution( 40 );

   sphere2->SetRadius( 0.5 );

   sphere2->SetCenter( 1, 0, 0 );

 

   vtkSphereSource* sphere3 = vtkSphereSource::New();

   sphere3->SetThetaResolution( 80 );

   sphere3->SetPhiResolution( 40 );

   sphere3->SetRadius( 0.5 );

   sphere3->SetCenter( -1, 0, 0 );

 

   vtkSphereSource* sphere4 = vtkSphereSource::New();

   sphere4->SetThetaResolution( 80 );

   sphere4->SetPhiResolution( 40 );

   sphere4->SetRadius( 0.5 );

   sphere4->SetCenter( 0, 1, 0 );

 

   vtkSphereSource* sphere5 = vtkSphereSource::New();

   sphere5->SetThetaResolution( 80 );

   sphere5->SetPhiResolution( 40 );

   sphere5->SetRadius( 0.5 );

   sphere5->SetCenter( 0, -1, 0 );

 

   vtkAppendPolyData* appendData = vtkAppendPolyData::New();

   appendData->AddInput( sphere->GetOutput() );

   appendData->AddInput( sphere2->GetOutput() );

   appendData->AddInput( sphere3->GetOutput() );

   appendData->AddInput( sphere4->GetOutput() );

   appendData->AddInput( sphere5->GetOutput() );

 

   // The dephSort object is set up to generate scalars representing

   // the sort depth.  A camera is assigned for the sorting. The camera

   // define the sort vector (position and focal point).

 

   vtkCamera* camera = vtkCamera::New();

 

   vtkDepthSortPolyData* depthSort = vtkDepthSortPolyData::New();

   depthSort->SetInput( appendData->GetOutput() );

//   depthSort SetDirectionToBackToFront

//   depthSort SetVector 1 1 1

   depthSort->SetCamera( camera );

   depthSort-> SortScalarsOn();

   depthSort->Update();

 

   vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();

   mapper->SetInput( depthSort->GetOutput() );

   mapper->SetScalarRange( 0, depthSort->GetOutput()->GetNumberOfCells()
);

 

   vtkActor* actor = vtkActor::New();

   actor->SetMapper( mapper );

   actor->GetProperty()->SetOpacity( 0.5 );

   actor->GetProperty()->SetColor( 1.0, 0.0, 0.0 );

   actor->RotateX( -72 );

 

   // If an Prop3D is supplied, then its transformation matrix is taken

   // into account during sorting.

 

   depthSort->SetProp3D( actor );

 

   // Create the RenderWindow & Renderer.

   vtkRenderer* ren1 = vtkRenderer::New();

   ren1->SetActiveCamera( camera );

 

   vtkRenderWindow* renWin = vtkRenderWindow::New();

   renWin->AddRenderer( ren1 );

 

   vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();

   iren->SetRenderWindow( renWin );

 

   // Add the actors to the renderer, set the background and size.

   ren1->AddActor( actor );

   ren1->SetBackground( 1, 1, 1 );

   renWin->SetSize( 300, 200 );

 

   iren->Initialize();

   ren1->ResetCamera();

   ren1->GetActiveCamera()->Zoom( 2.2 );

   renWin->Render();

 

// Main message loop:

            MSG msg;

            while (GetMessage(&msg, NULL, 0, 0)) 

            {

            TranslateMessage(&msg);

                        DispatchMessage(&msg);

            }

 

            return 0;

}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20040320/4c51ef37/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Image2.jpg
Type: image/jpeg
Size: 17923 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20040320/4c51ef37/attachment.jpg>


More information about the vtkusers mailing list