[vtkusers] vtk transparency problem

Tracy Hu tracy.hu at yahoo.com
Fri Jun 20 13:46:41 EDT 2008


Hi, Clint,

I wonder if you could briefly look at my code below? I implemented everything as you suggested. But in the resulted two triangles, blue one always appear in front of red one no matter how I rotate them. Did I do something incorrect? I'm using vtk5.3

Thanks a lot.
Tracy
-------------------------------------------------------------------------------------------------------------------------------
#ifndef MYPAINTER_H
#define MYPAINTER_H

#include "vtkPainter.h"

class MyPainter :public vtkPainter
{
public:
    vtkTypeRevisionMacro(vtkPainter, vtkObject);
    static MyPainter* New();
protected:
    virtual void RenderInternal(vtkRenderer* renderer, vtkActor* actor, 
    unsigned long typeflags);
};
#endif

-------------------------------------------------------------------------------------------------------------------
#include "MyPainter.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGL.h"

vtkStandardNewMacro(MyPainter);
vtkCxxRevisionMacro(MyPainter, "$Revision: 1.6 $");

void MyPainter::RenderInternal(vtkRenderer *renderer, vtkActor *actor, unsigned long typeflags)
{
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
vtkPainter::RenderInternal(renderer, actor, typeflags);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
vtkPainter::RenderInternal(renderer, actor, typeflags);
glDisable(GL_CULL_FACE);
}

--------------------------------------------------------------------------------------------------------------
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkPoints.h"
#include "vtkFloatArray.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkLookupTable.h"
#include "MyPainter.h"
#include "vtkPainterPolyDataMapper.h"
#include "vtkPolyData.h"
#include "vtkProperty.h"

int main()
{

    vtkPoints *points = vtkPoints::New();
        points->InsertNextPoint(1.0, 6.0, 5.0);
        points->InsertNextPoint(2.0, 3.0, 0.0);
        points->InsertNextPoint(3.0, 4.5, 0.0);
        points->InsertNextPoint(5.0, 5.5, 5.0);

    vtkFloatArray *colorArray = vtkFloatArray::New();
        colorArray->InsertNextValue(0);
        colorArray->InsertNextValue(1);
        colorArray->SetName("colorArray");

    vtkCellArray *cells = vtkCellArray::New();
    int ptsTri[3];
        ptsTri[0]=0;
        ptsTri[1]=1;
        ptsTri[2]=2;
    cells->InsertNextCell(3, ptsTri);

    ptsTri[0]=1;
    ptsTri[1]=2;
    ptsTri[2]=3;
    cells->InsertNextCell(3, ptsTri);

vtkPolyData *polydata = vtkPolyData::New();
        polydata->SetPoints(points);
        polydata->SetPolys(cells);
        polydata->GetCellData()->AddArray(colorArray);
        polydata->GetCellData()->SetActiveAttribute("colorArray",0);
 
vtkLookupTable *lut = vtkLookupTable::New();
        lut->SetNumberOfColors(2);
        lut->Build();
        lut->SetTableValue(0, 1.0,0.0,0.0,1.0);
        lut->SetTableValue(1, 0.0,0.0,1.0,1.0);
        lut->SetTableRange(0,1);

vtkPainterPolyDataMapper *mapper = vtkPainterPolyDataMapper::New();
    mapper->SetInputConnection(polydata->GetProducerPort());
    mapper->SetLookupTable(lut);
    mapper->UseLookupTableScalarRangeOn();

MyPainter *transparentPainter = MyPainter::New();
    transparentPainter->SetDelegatePainter(mapper->GetPainter());
    mapper->SetPainter(transparentPainter);

vtkActor *actor = vtkActor::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetOpacity(0.9);
    actor->RotateX(-72);

vtkRenderer *ren = vtkRenderer::New();
    ren->AddActor(actor);
    ren->SetBackground(1, 1, 1);
vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren);
    renWin->SetSize(600, 600);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);
vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();
    iren->SetInteractorStyle(style);

renWin->Render();
iren->Initialize();
iren->Start();
return 0;

}



--- On Thu, 6/19/08, clinton at elemtech.com <clinton at elemtech.com> wrote:
From: clinton at elemtech.com <clinton at elemtech.com>
Subject: Re: vtk transparency problem
To: tracy.hu at yahoo.com
Date: Thursday, June 19, 2008, 4:32 PM

I use a subclass of vtkPainter & VTK 5.2/CVS to do it now.

In the RenderInternal() of my vtkPainter subclass, I call 
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
this->Superclass::RenderInternal(renderer, actor, typeflags);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
this->Superclass::RenderInternal(renderer, actor, typeflags);
glDisable(GL_CULL_FACE);

That might be all you need without modifying vtkRenderer or vtkProperty.
To use the painter, 

vtkPainterPolyDataMapper* mapper = ...

MyPainter* transparentPainter = MyPainter::New();
transparentPainter->SetDelegatePainter(mapper->GetPainter());
mapper->SetPainter(transparentPainter);

I don't have a sample app, or the code isolated enough to send you cpp
files.

Clint

On Thursday 19 June 2008 10:57:25 am Tracy Hu wrote:
> Clint,
>  
> I saw your message posted two years ago and tried to follow what  you
> suggested to solve vtk transparency problem. I have not get success yet,
> and I think I must not be doing it right. I wonder if  you could do me a
> favor and send me those code that you used to solve the problem?  or if
you
> could make it more explict, e.g., how did you overload
> vtkRender::UpdataGeometry, where did you insert glCullFace(GL_BACK), etc.
?
> 
> Thank you so much
>  
> Tracy
>
>
---------------------------------------------------------------------------
>--------------------------- One thing I do that really helps with scenes in
> VTK is to make two passes at rendering the transparent data.
>
> I've overloaded vtk*Renderer::UpdateGeometry.
> I've also overloaded vtkOpenGLProperty::Render.
>
> The UpdateGeometry function does a cheap z-depth sort at the actor level,
> instead of each polygon.
>
> I turn enable culling..
> glEnable(GL_CULL_FACE)
>
> Then draw each polygons with
> glCullFace(GL_FRONT)
>
> then on the second pass, draw each polygon with
> glCullFace(GL_BACK)
>
> For the types of things I'm drawing, this is a cheap way to get pretty
good
> results, but it isn't perfect.  I've heard no complaints from any
of our
> users.  I might see some minor artifacts when I have terribly convoluted
> geometry or pieces of geometry tangled up with each other.
>
> That might work for you.
>
> Clint


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20080620/1e067d9a/attachment.htm>


More information about the vtkusers mailing list