[vtkusers] Light source similar to a flashlight?

David Doria daviddoria at gmail.com
Sun Nov 25 07:13:04 EST 2012


On Wed, Nov 21, 2012 at 11:40 AM, web-Dmitri web-Danewitz
<d.danewitz at web.de> wrote:

Thank you David,
>
> It still does not work for me. I made a very small example, explaining my
> problem.
>
> What I expect to see when I shine on the surface is for it to look
> something like this:
> http://i.imgur.com/DpH6m.jpg
>
> Instead it looks like this:
> http://i.imgur.com/Wz4ga.jpg
>
> But when I just make the surface to be shined on smaller (-5, 5, -5, 5,
> -5, 5) instead of (-500, 500, -500, 500, -5, 5) it look like this:
> http://i.imgur.com/Je5BD.jpg
>
> Is there a away to make it look like picture 1?
>
> Here's the short code used for this example:
>

I can't get this to work either. I have simplified the example by creating
a plane on which to shine the light. I have placed a sphere at the point
that the light is supposed to be focused on. I have used a vtkLightActor to
verify that the light is pointing where we expect. The scene looks correct
without this line:

renderer->AddLight(light);

but as soon as I add that light, the plane disappears entirely (is black?)
when viewed from any direction, and the sphere is only visible when viewed
from the direction of the light.

Can anyone explain what we are doing wrong here?

#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, 1};


  // 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->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); // commenting this line shows the plane
in normal light


  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, 0.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();

  renderWindowInteractor->Start();


}



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


More information about the vtkusers mailing list