[vtkusers] a probem with "controlling shading with a vtkVolumeProperty"

付文宇 fuwenyu at emails.bjut.edu.cn
Tue Dec 12 03:26:47 EST 2006


hi everyone
 
   I do a exercise followed the book "The VTK User's Guide" (Updated for version
4.0) from page146 to 147. According to the book "Since the vtkProperty used for
the vtkActor,and the vtkVolumeProperty used for the vtkVolume were set up with the
same ambient,diffuse,specular,and specular power values, and the color of both
spheres is white,they have similar appearances".But the result I saw is
different(The image I saw is in the attach file).I do not know why.My code is in
the next:
/*=========================================================================
#include "vtkSphereSource.h"
#include "vtkSTLReader.h"
//#include "vtkShrinkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkSLCReader.h"
#include "vtkPiecewiseFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkVolumeRayCastMapper.h"
#include "vtkVolume.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkVolume.h"
#include "vtkLight.h"
#include "vtkProperty.h"
#include "vtkCamera.h"

int main()
{
   //create a geometric sphere 
	vtkSphereSource *sphere=vtkSphereSource::New();
	sphere->SetRadius(20);
	sphere->SetCenter(70,25,25);
	sphere->SetThetaResolution(50);
	sphere->SetPhiResolution(50);
  vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
	mapper->SetInput(sphere->GetOutput());
	
   vtkActor *actor = vtkActor::New();
	  actor->SetMapper(mapper);
	  actor->GetProperty()->SetColor(1,1,1);
	  actor->GetProperty()->SetAmbient(0.01);
	  actor->GetProperty()->SetDiffuse(0.7);
	  actor->GetProperty()->SetSpecular(0.5);
	  actor->GetProperty()->SetSpecularPower(70.0);
//read in a volumetric sphere	
  vtkSLCReader *reader = vtkSLCReader::New();
	reader->SetFileName("D:/fwy/VTKÔ´ÂëºÍ°²×°³ÌÐò/vtkdata-5.0.2/VTKData/Data/sphere.s
lc");
//use this tfun for both opacity and color
vtkPiecewiseFunction *opacityTransferFunction=vtkPiecewiseFunction::New();
		opacityTransferFunction->AddSegment(0,1.0,255,1.0);

//make the volume property match the geometric one
vtkVolumeProperty *volumeProperty=vtkVolumeProperty::New();
		volumeProperty->SetColor(opacityTransferFunction);
		volumeProperty->SetScalarOpacity(opacityTransferFunction);
		volumeProperty->ShadeOn();
		volumeProperty->SetInterpolationTypeToLinear();
		volumeProperty->SetDiffuse(0.7);
		volumeProperty->SetAmbient(0.01);
		volumeProperty->SetSpecular(0.5);
		volumeProperty->SetSpecularPower(70.0);

vtkVolumeRayCastCompositeFunction
*compositionFunction=vtkVolumeRayCastCompositeFunction::New();
vtkVolumeRayCastMapper *volumeMapper=vtkVolumeRayCastMapper::New();
	volumeMapper->SetInput(reader->GetOutput());
	volumeMapper->SetVolumeRayCastFunction(compositionFunction);

vtkVolume *volume=vtkVolume::New();
	volume->SetMapper(volumeMapper);
	volume->SetProperty(volumeProperty);

  vtkRenderer *ren1 = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer(ren1);
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);

//add both the geometric and volumetric spheres to the renderer
  ren1->AddViewProp(volume);
  ren1->AddViewProp(actor);
  //create a red,green,and blue light
  vtkLight *redlight=vtkLight::New();
	redlight->SetColor(1,0,0);
	redlight->SetPosition(1000,25,25);
	redlight->SetFocalPoint(25,25,25);
	redlight->SetIntensity(0.5);
  vtkLight *greenlight=vtkLight::New();
	greenlight->SetColor(0,1,0);
	greenlight->SetPosition(25,1000,25);
	greenlight->SetFocalPoint(25,25,25);
	greenlight->SetIntensity(0.5);

  vtkLight *bluelight=vtkLight::New();
	bluelight->SetColor(0,0,1);
	bluelight->SetPosition(25,25,1000);
	bluelight->SetFocalPoint(25,25,25);
	bluelight->SetIntensity(0.5);

//add the lights to the rendere
	ren1->AddLight(redlight);
	ren1->AddLight(greenlight);
	ren1->AddLight(bluelight);

  renWin->Render();
   iren->Start();

   sphere->Delete();
  mapper->Delete();
  actor->Delete();
reader->Delete();
opacityTransferFunction->Delete();
volumeProperty->Delete();
compositionFunction->Delete();
volumeMapper->Delete();
volume->Delete();
redlight->Delete();
greenlight->Delete();
bluelight->Delete();

  ren1->Delete();
  renWin->Delete();
  iren->Delete();

  return 0;
}






-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.jpg
Type: image/pjpeg
Size: 23027 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20061212/3e4d8fe6/attachment.bin>


More information about the vtkusers mailing list