//====================================================================
//#
//#  pfTexturedCube.cxx                                #####     
//#                                                   ##   ##
//#  Author(s): Paul Rajlich                          ##
//#  Email: prajlich@ncsa.uiuc.ed                     ##   ##
//#  Date created:                                     #####
//#
//#  Description: testing vtkActorToPF texturing
//#    Based on texturedCube.tcl from VTK distribtion.
//#
//====================================================================


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

#include "vtkTexture.h"
#include "vtkPNMReader.h"
#include "vtkTIFFReader.h"
#include "vtkCubeSource.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 Textured Cube");
  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
  //vtkPNMReader *pnm = vtkPNMReader::New();
  //pnm->SetFileName("test.pnm");

  vtkTIFFReader *tiff = vtkTIFFReader::New();
  tiff->SetFileName("../vtkdata/vtk.tif");

  vtkTexture *atex = vtkTexture::New();
  atex->SetInput(tiff->GetOutput());
  //atex->SetInput(pnm->GetOutput());
  atex->InterpolateOn();

  vtkCubeSource *cube = vtkCubeSource::New();
  cube->SetXLength(3);
  cube->SetYLength(2);
  cube->SetZLength(1);
 
  vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
  cubeMapper->SetInput(cube->GetOutput());
  
  vtkActor *cubeActor = vtkActor::New();
  cubeActor->SetMapper(cubeMapper);
  cubeActor->SetTexture(atex);

  // ---- end vtk pipline ----

  pfDCS *center = new pfDCS;
  center->setRot(0.0f, 90.0f, 0.0f);
  center->setTrans(0.0f, 6.0f, 0.0f);

  vtkActorToPFTranslator *cubeT = vtkActorToPFTranslator::New();
  cubeT->SetActor(cubeActor);
  cubeT->VerboseOn();
  cubeT->Init();
  center->addChild(cubeT->GetGeode());

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

  float t = 0.0f;
  while (t < 10.0f) {
    pfSync();
    pfFrame();
    t = pfGetTime();
    center->setRot(t*18.0f, 90.0f, 0.0f);
  }
  pfExit();
}
