//====================================================================
//#
//#  vrJugglerExampleOOD.cc                            #####     
//#                                                   ##   ##
//#  Author(s): Paul Rajlich                          ##
//#  Email: prajlich@ncsa.uiuc.ed                     ##   ##
//#  Date created:                                     #####
//#
//#  Description: vrJuggler application that uses vtkActorToPFTranslator
//#    class
//#
//====================================================================

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

// vrJuggler stuff
#include <Kernel/vjKernel.h>
#include <Kernel/Pf/vjPfApp.h>
#include <Kernel/vjDebug.h>
#include <Kernel/vjProjection.h>
#include <Input/InputManager/vjPosInterface.h>
#include <Input/InputManager/vjDigitalInterface.h>

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

#include "vtkActorToPFTranslator.h"

class myApp : public vjPfApp {
public:
  vtkSphereSource *sphere;
  pfGroup *rootNode;
  vjPosInterface mWand;
  vjDigitalInterface mButton0;
  vjDigitalInterface mButton1;
  vjDigitalInterface mButton2;

  myApp(vjKernel *kern) : vjPfApp(kern) {
    mWand.init("VJWand");
    mButton0.init("VJButton0");
    mButton1.init("VJButton1");
    mButton2.init("VJButton2");
  }
  virtual void init() { vjProjection::setNearFar(0.5, 1000); }
  virtual pfGroup* getScene() { return rootNode; } 

  // for future reference
  virtual void apiInit() { }
  virtual void preForkInit() { }
  virtual void preDrawChan(pfChannel* chan, void* chandata) { }
  virtual void preSync() { }
  virtual void preFrame() { }
  virtual void intraFrame() {
    if (mButton0->getData() == 1)
      sphere->SetRadius(sphere->GetRadius()+0.1);
    if (mButton1->getData() == 1)
      sphere->SetRadius(sphere->GetRadius()-0.1);
  }

  // create scene
  virtual void initScene() {
    pfLightSource *light;
    rootNode = new pfGroup;
  
    light = new pfLightSource;
    light->setColor(PFLT_DIFFUSE, 1.0, 1.0, 1.0);
    light->setAmbient(0.0, 0.0, 0.0);
    rootNode->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);
    sphereActor->GetProperty()->SetOpacity(1.0);

    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);
    rootNode->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()
  }
};

int main(int argc, char **argv) {
  vjKernel *kernel = vjKernel::instance();
  myApp *app = new myApp(kernel);

  for (int i=1; i<argc; i++)
    kernel->loadConfigFile(argv[i]);
 
  kernel->start();
  kernel->setApplication(app);

  while (1);
}

