//====================================================================
//#
//#  pfCAVEExampleOOD.cxx                              #####     
//#                                                   ##   ##
//#  Author(s): Paul Rajlich                          ##
//#  Email: prajlich@ncsa.uiuc.ed                     ##   ##
//#  Date created:                                     #####
//#
//#  Description: Simple Performer example that uses
//#    vtkActorToPfTranslator class
//#
//====================================================================

#include <pfcave.h>
#include <stdio.h>
#include <stdlib.h>
#include <Performer/pf/pfLightSource.h>
#include <Performer/pf/pfSCS.h>

#include "vtkSphereSource.h"
#include "vtkConeSource.h"
#include "vtkGlyph3D.h"
#include "vtkPolyDataNormals.h"

#include "vtkActorToPFTranslator.h"

static void createScene(pfChannel *, int, char **);
void updateVTK();

// globals... 
vtkSphereSource *sphere;

// Normal pfCAVE application - nothing weird here!
int main(int argc, char **argv) {
  pfInit();
  pfCAVEConfig(&argc, argv, NULL);
  pfConfig();
  pfCAVEInitChannels();
  createScene(pfCAVEMasterChan(), argc, argv);
 // sorting... used for transparency so that transparent objects rendered last
  pfCAVEMasterChan()->setTravMode(PFTRAV_CULL, PFCULL_SORT);
  while (!CAVEgetbutton(CAVE_ESCKEY)) {
    updateVTK();
    pfSync();
    pfCAVEPreFrame();
    pfFrame();
    pfCAVEPostFrame();
  }
  CAVEHalt();
  pfExit();
  return 0;
}

void updateVTK() {
  // wand buttons change sphere radius
  if (CAVEBUTTON1) { sphere->SetRadius(sphere->GetRadius()+0.1); }
  if (CAVEBUTTON2) { sphere->SetRadius(sphere->GetRadius()-0.1); }
}

static void createScene(pfChannel *chan, int argc, char **argv) {
  pfScene *scene;
  pfGeoState *gstate;
  pfLightSource *light;
  scene = new pfScene;
  gstate = new pfGeoState;
  gstate->setMode(PFSTATE_ENLIGHTING, PF_ON);
  gstate->setMode(PFSTATE_CULLFACE, PFCF_OFF);
  scene->setGState(gstate);
  
  light = new pfLightSource;
  light->setColor(PFLT_DIFFUSE, 1.0, 1.0, 1.0);
  light->setAmbient(0.0, 0.0, 0.0);
  scene->addChild(light);

  // ****** setup vtk pipeline
  vtkConeSource *cone;
  vtkGlyph3D *glyph;
  vtkPolyDataMapper *sphereMapper, *spikeMapper;

  sphere = vtkSphereSource::New();
  sphereMapper = vtkPolyDataMapper::New();
    sphereMapper->SetInput(sphere->GetOutput());
  vtkActor *sphereActor = vtkActor::New();
    sphereActor->SetMapper(sphereMapper);
  sphereActor->GetProperty()->SetColor(0.2, 0.7, 0.2);
  if (argc < 2)
    sphereActor->GetProperty()->SetOpacity(1.0);
  else
    sphereActor->GetProperty()->SetOpacity(atof(argv[1]));

  cone = vtkConeSource::New();

  glyph = vtkGlyph3D::New();
  glyph->SetInput(sphere->GetOutput());
  glyph->SetSource(cone->GetOutput());
  glyph->SetVectorModeToUseNormal();
  glyph->SetScaleModeToScaleByVector();
  glyph->SetScaleFactor(0.25);

  vtkPolyDataNormals *spikeNormals = vtkPolyDataNormals::New();
  spikeNormals->SetInput(glyph->GetOutput());

  spikeMapper = vtkPolyDataMapper::New();
  spikeMapper->SetInput(spikeNormals->GetOutput());

  vtkActor *spikeActor = vtkActor::New();
  spikeActor->SetMapper(spikeMapper);
  spikeActor->GetProperty()->SetColor(0.2, 0.7, 0.2);
  // ****** end vtk stuff

  // create pfSCS that will center/scale the geometry
  pfMatrix mtxCenter;
  float *center;
  center = sphereActor->GetCenter();
  mtxCenter.makeTrans(-center[0], -center[1]+1.0, -center[2]);

  pfMatrix mtxScale;
  float *bounds = sphereActor->GetBounds();
  float scaleFactor = 5;
  mtxScale.makeScale(scaleFactor/(bounds[1]-bounds[0]),
		       scaleFactor/(bounds[3]-bounds[2]),
		       scaleFactor/(bounds[5]-bounds[4]));
  mtxCenter.postMult(mtxScale);

  pfSCS *scsCenter = new pfSCS(mtxCenter);
  scene->addChild(scsCenter);

  // ****** setup vtkActorToPFTranslators
  // two ways:
  // 1) have translator create new geode
  printf("translating...\n");
  vtkActorToPFTranslator *translator = vtkActorToPFTranslator::New();
  translator->SetActor(sphereActor);
  translator->Init();

  // 2) supply your own geode (if it has GSets already, they will be
  //    replaced.
  pfGeode *geode = new pfGeode();
  vtkActorToPFTranslator *translator2 = vtkActorToPFTranslator::New();
  translator2->SetActor(spikeActor);
  translator2->SetGeode(geode);
  translator2->Init();
  printf("done.\n");
  // ****** end setup

  // add geodes to scenegraph
  scsCenter->addChild(translator->GetGeode());
  scsCenter->addChild(geode); // could have also done translator2->getGeode()
  chan->setScene(scene);
}

