//====================================================================
//#
//#  pfTcutSph.cxx                                     #####     
//#                                                   ##   ##
//#  Author(s): Paul Rajlich                          ##
//#  Email: prajlich@ncsa.uiuc.ed                     ##   ##
//#  Date created:                                     #####
//#
//#  Description: testing vtkActorToPF texturing
//#    Based on tcutSph.tcl from VTK distribtion.
//#    Cut an outer sphere to reveal an inner sphere.
//#
//====================================================================

#include <stdlib.h>
#include <Performer/pf/pfChannel.h>
#include <Performer/pf/pfLightSource.h>
#include <Performer/pf/pfDCS.h>

#include "vtkSphereSource.h"
#include "vtkPlanes.h"
#include "vtkPoints.h"
#include "vtkNormals.h"
#include "vtkImplicitTextureCoords.h"
#include "vtkDataSetMapper.h"
#include "vtkStructuredPointsReader.h"
#include "vtkTexture.h"

#include "vtkActorToPFTranslator.h"

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

  pfInit();
  pfMultiprocess(PFMP_DEFAULT);  pfConfig();

  pfPipe *p = pfGetPipe(0);
  pfPipeWindow *pw = new pfPipeWindow(p);
  pw->setWinType(PFPWIN_TYPE_X);
  pw->setName("vtkActorToPF Texture Cut Sphere");
  pw->setOriginSize(100, 100, 500, 500);
  pw->open();

  pfChannel *chan = new pfChannel(p);

  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.3, 0.3, 0.3);
  scene->addChild(light);
 
  // ---- vtk pipeline ---- 
  // hidden sphere
  vtkSphereSource *sphere1 = vtkSphereSource::New();
    sphere1->SetThetaResolution(12); sphere1->SetPhiResolution(12);
    sphere1->SetRadius(0.5);

  vtkPolyDataMapper *innerMapper = vtkPolyDataMapper::New();
    innerMapper->SetInput(sphere1->GetOutput());

  vtkActor *innerSphere = vtkActor::New();
    innerSphere->SetMapper(innerMapper);
    innerSphere->GetProperty()->SetColor (1, .9216, .8039);

  // sphere to texture
  vtkSphereSource *sphere2 = vtkSphereSource::New();
    sphere2->SetThetaResolution(24); sphere2->SetPhiResolution(24);
    sphere2->SetRadius(1.0);

  vtkPlanes *planes = vtkPlanes::New();
    float pts[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
    vtkPoints *points = vtkPoints::New();
        points->InsertPoint (0, pts);
        points->InsertPoint (1, pts + 3);
    float nrms[6] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0};
    vtkNormals *normals = vtkNormals::New();
        normals->InsertNormal (0, nrms);
        normals->InsertNormal (1, nrms + 3);
    planes->SetPoints (points);
    planes->SetNormals (normals);

  vtkImplicitTextureCoords *tcoords = vtkImplicitTextureCoords::New();
    tcoords->SetInput(sphere2->GetOutput());
    tcoords->SetRFunction(planes);

  vtkDataSetMapper *outerMapper = vtkDataSetMapper::New();
    outerMapper->SetInput(tcoords->GetOutput());

  vtkStructuredPointsReader *tmap = vtkStructuredPointsReader::New();
    tmap->SetFileName("../vtkdata/texThres.vtk");

  vtkTexture *texture = vtkTexture::New();
    texture->SetInput(tmap->GetOutput());
    texture->InterpolateOff();
    texture->RepeatOff ();

  vtkActor *outerSphere = vtkActor::New();
    outerSphere->SetMapper(outerMapper);
    outerSphere->SetTexture(texture);
    outerSphere->GetProperty()->SetColor (1, .6275, .4784);

  // ---- end vtk pipeline ----

  pfDCS *center = new pfDCS;
  center->setTrans(0.0f, 4.0f, 0.0f);

  vtkActorToPFTranslator *innerT = vtkActorToPFTranslator::New(innerSphere); 
  vtkActorToPFTranslator *outerT = vtkActorToPFTranslator::New();
  outerT->SetActor(outerSphere);
  outerT->VerboseOn();
  outerT->Init();
  center->addChild(innerT->GetGeode());
  center->addChild(outerT->GetGeode());

  scene->addChild(center);
  chan->setScene(scene);

  float t = 0.0f;
  while (t < 30.0f) {
    pfSync();
    pfFrame();
    t = pfGetTime();
    center->setRot(t*40.0f, 45.0f, 0.0f);
  }
  pfExit();
}
