[vtkusers] matrix rotation "jumps"

Christopher.Moore at noaa.gov Christopher.Moore at noaa.gov
Mon Oct 25 20:43:47 EDT 2004


I'm using the GetTransformedPosition() method in vtkLight to simply rotate 
a light's position around the origin (as the sun would rotate around the 
earth).

I set up a vtkMatrix4x4 with the proper elements to rotate around the z 
axis, and use a callback to repeatedly call:

light->SetPosition(light->GetTransformedPosition());

It works great until it reaches a certain point in the orbit (angle=3pi/2) 
where it "jumps" to angle=0.

Any ideas?

Just when I thought I was getting good at this,
Chris

sample code (sorry so long):
don't forget to press "u" to see the action...

#include "vtkRenderer.h"
#include "vtkCamera.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkLight.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkMatrix4x4.h"
#include "vtkCommand.h"

vtkLight *light;
vtkMatrix4x4 *matrix;

class rotateCallback : public vtkCommand {
public:
  static rotateCallback *New() {
    return new rotateCallback;
  }
  virtual void Execute(vtkObject *caller, unsigned long, void*) {
    vtkRenderWindowInteractor *rwi = 
reinterpret_cast<vtkRenderWindowInteractor*>(caller);
    light->SetPosition(light->GetTransformedPosition());
    rwi->GetRenderWindow()->Render();
  }
};

int main( int argc, char *argv[] ) {

  float angle = 0.1; // radians

  vtkRenderWindow *renWin = vtkRenderWindow::New();
  vtkRenderer *ren = vtkRenderer::New();
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  vtkSphereSource *ss = vtkSphereSource::New();
  vtkPolyDataMapper *sMapper = vtkPolyDataMapper::New();
  vtkActor *sphereActor = vtkActor::New();

  renWin->AddRenderer(ren);
  iren->SetRenderWindow(renWin);

  matrix = vtkMatrix4x4::New();
  matrix->Identity();
  matrix->SetElement(0, 0, cos(angle));
  matrix->SetElement(0, 1, -1.0 * sin(angle));
  matrix->SetElement(1, 0, sin(angle));
  matrix->SetElement(1, 1, cos(angle));

  light = vtkLight::New();
  light->SetLightTypeToSceneLight();
  light->SetIntensity(1.0);
  light->SetPosition(-1.0,0.0,0.0);
  light->SetFocalPoint(1.0,0.0,0.0);
  light->SetTransformMatrix(matrix);
  light->SwitchOn();
  ren->AddLight(light);

  ss->SetRadius(1.0);
  sMapper->SetInput(ss->GetOutput());
  sphereActor->SetMapper(sMapper);
  ren->AddActor(sphereActor);

  rotateCallback *rotate = rotateCallback::New();
  iren->AddObserver(vtkCommand::UserEvent, rotate);

  iren->Start();

}





More information about the vtkusers mailing list