[vtkusers] Light source similar to a flashlight?

David Doria daviddoria at gmail.com
Sun Nov 25 08:37:52 EST 2012


On Sun, Nov 25, 2012 at 8:27 AM, Bill Lorensen <bill.lorensen at gmail.com>wrote:

> Apparently, if the renderer has a light at the first render, it does
> not create a default light.
>

Strange, that is not the behavior I'd expect.


> Move the AddLight after renderWindow->Render(); and you'll see the
> original light also.


With the following code, the back side of the plane/sphere are now indeed
again visible. However, the red light (the diffuse color) is applied to the
sphere (the yellow sphere has some hints of red/orange), but does not seem
to be applied to the plane? That is, I would expect a red circle to be
visible on the plane (and then the plane would be it's normal gray outside
of this circle).

#include <vtkLightCollection.h>

#include <vtkProperty.h>

#include <vtkRenderer.h>

#include <vtkRenderWindow.h>

#include <vtkPlaneSource.h>

#include <vtkSphereSource.h>

#include <vtkPolyDataMapper.h>

#include <vtkActor.h>

#include <vtkLight.h>

#include <vtkLightActor.h>

#include <vtkSmartPointer.h>

#include <vtkRenderWindowInteractor.h>


int main()

{

  vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();


  double lightPosition[3] = {0, 0, 2};


  // Create a light

  double lightFocalPoint[3] = {0,0,0};


  vtkSmartPointer<vtkLight> light = vtkSmartPointer<vtkLight>::New();

  light->SetLightTypeToSceneLight();

  light->SetPosition(lightPosition[0], lightPosition[1], lightPosition[2]);

  light->SetPositional(true); // required for vtkLightActor below

  light->SetConeAngle(10);

  light->SetFocalPoint(lightFocalPoint[0], lightFocalPoint[1],
lightFocalPoint[2]);

  light->SetDiffuseColor(1,0,0);

  light->SetAmbientColor(0,1,0);

  light->SetSpecularColor(0,0,1);


  vtkLightCollection* originalLights = renderer->GetLights();

  std::cout << "Originally there are " <<
originalLights->GetNumberOfItems() << " lights." << std::endl;


//  renderer->AddLight(light); // can't do this here - must do this
after the renderWindow->Render() below


  std::cout << "Now there are " << originalLights->GetNumberOfItems()
<< " lights." << std::endl;


  // Display where the light is

  vtkSmartPointer<vtkLightActor> lightActor =
vtkSmartPointer<vtkLightActor>::New();

  lightActor->SetLight(light);

  renderer->AddViewProp(lightActor);


  // Display where the light is focused

  vtkSmartPointer<vtkSphereSource> lightFocalPointSphere =
vtkSmartPointer<vtkSphereSource>::New();

  lightFocalPointSphere->SetCenter(lightFocalPoint);

  lightFocalPointSphere->SetRadius(.1);

  lightFocalPointSphere->Update();


  vtkSmartPointer<vtkPolyDataMapper> lightFocalPointMapper =

      vtkSmartPointer<vtkPolyDataMapper>::New();

  lightFocalPointMapper->SetInputConnection(lightFocalPointSphere->GetOutputPort());


  vtkSmartPointer<vtkActor> lightFocalPointActor =
vtkSmartPointer<vtkActor>::New();

  lightFocalPointActor->SetMapper(lightFocalPointMapper);

  lightFocalPointActor->GetProperty()->SetColor(1.0, 1.0, 0.0); //(R,G,B)

  renderer->AddViewProp(lightFocalPointActor);


  // Create a cube for the light to shine on

  vtkSmartPointer<vtkPlaneSource> plane =
vtkSmartPointer<vtkPlaneSource>::New();

  plane->Update();


  vtkSmartPointer<vtkPolyDataMapper> planeMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();

  planeMapper->SetInputData(plane->GetOutput());


  vtkSmartPointer<vtkActor> planeActor = vtkSmartPointer<vtkActor>::New();

  planeActor->SetMapper(planeMapper);

  renderer->AddActor(planeActor);


  vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();

  renderWindow->AddRenderer(renderer);


  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =

      vtkSmartPointer<vtkRenderWindowInteractor>::New();

  renderWindowInteractor->SetRenderWindow(renderWindow);


  renderWindow->Render();


  renderer->AddLight(light); // must do this after renderWindow->Render();


  renderWindowInteractor->Start();


}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20121125/c74c5740/attachment.htm>


More information about the vtkusers mailing list