#include "SM_CommonFXPCH.h"
#include "SM_Engine3DPCH.h"
#include "SM_DemoEffect.h"
#include "SM_Voxelizer.h"

struct Edge3D
{
  float x, y, z;


  void AddDelta()
  {
  }
    
  Vector3D v3dPos;
  Vector3D v3dDelta;
  Vector3D v3dError;
};

class VoxelFX : public SM_DemoEffect
{
public:              
  VoxelFX(char const* pcName) : SM_DemoEffect(pcName)
  {
  }

  virtual          ~VoxelFX()
  {
  }

  int      Init(const char* pcCommand)
  {
    char* pcCopy =0;
    char* pcToken;
    int   iReturn=-1;
    
    pcCopy=new char[strlen(pcCommand)+1];
    strcpy(pcCopy, pcCommand);

    pcToken=strtok(pcCopy, " \t");

    if (strcmp(pcToken, "FILL THIS UP")==0)
    {
    }

    iReturn  = 0;
    
//FAILED:
    if (pcCopy) { delete[] pcCopy; pcCopy=0; }

    return iReturn;        
  }

  int      Shutdown()
  {
    return (0);
  }

  int      Start(float fTime)
  {
    return (0);
  }

  int      Stop()
  {
    return (0);
  }

  int      Reset()
  {
    return 0;
  }   
    
  int      Run(float fTime)
  {
    // Paint a poly example!!! 
    RenderContext RC;   

    RC.Set(
    Vector3D(0.0f, 0.0f, 0.0f),
    Quaternion(1.0f, 0.0f, 0.0f, 0.0f),
    90,
    0.75f,
    1.0f,
    200.0f);

    RC.SetViewport(0, 0, 640.0f, 480.0f);  
  
    RC.SyncRasterizer();
    RC.UpdateFrustum();
    
    MeshElement me;

    FVF_PosNormalDiffuseTex1 pVertices[3];
    unsigned short pusIndices[3]={0,2,1};

    pVertices[0].x=0.0f; pVertices[0].y=0.0f; pVertices[0].z=10.0f; 
    pVertices[0].nx=0.0f; pVertices[0].ny=0.0f; pVertices[0].nz=1.0f;
    pVertices[0].u=0.0f; pVertices[0].v=0.0f; pVertices[0].diffuse=0xFFFFFFFF;

    pVertices[1].x=10.0f*cosf(fTime+1.705f); pVertices[1].y=10.0f*sinf(fTime+1.705f); pVertices[1].z=10.0f; 
    pVertices[1].nx=0.0f; pVertices[1].ny=0.0f; pVertices[1].nz=1.0f;
    pVertices[1].u=0.0f; pVertices[1].v=0.0f; pVertices[1].diffuse=0xFFFFFFFF;

    pVertices[2].x=10.0f*cosf(fTime); pVertices[2].y=10.0f*sinf(fTime); pVertices[2].z=10.0f; 
    pVertices[2].nx=1.0f; pVertices[2].ny=0.0f; pVertices[2].nz=0.0f;
    pVertices[2].u=0.0f; pVertices[2].v=0.0f; pVertices[2].diffuse=0xFFFFFFFF;
    
    me.m_iShader            =ShaderManager::FindShader("NULL");
    me.m_iVB                =-1;
    me.m_iIB                =-1;
    me.m_pVertices          =pVertices; 
    me.m_pIndices           =pusIndices;
    me.m_uStartVertex       =0;
    me.m_uVertices          =3;
    me.m_uStartIndex        =0;
    me.m_uPrimitives        =1;
    me.m_WorldTransform     =Matrix4X4::Identity;
    me.m_iActiveLightMask   =-1;  
    me.m_fDepth             =0.0f;
    
    //RenderPipeline::Render(&me);
    SM_Voxelizer::DrawMeshElement(&me, &RC);
    RenderPipeline::Flush();

    return 1;
  }

  int      Command           (float fTime, const char* pcCommand)
  {
    SM_DemoEffect::Command(fTime, pcCommand);
    char* pcCopy =0;
    char* pcToken;
    int   iReturn=-1;
    
    pcCopy=new char[strlen(pcCommand)+1];
    strcpy(pcCopy, pcCommand);

    pcToken=strtok(pcCopy, " \t");

    if (strcmp(pcToken, "FILL THIS UP")==0)
    {
    }

    iReturn  = 0;
    
    if (pcCopy) { delete[] pcCopy; pcCopy=0; }
    return iReturn;
  }  
};

DEFINE_EFFECT(VoxelFX)
VoxelFX Voxel00("VOXEL");
