--this function extracts the vertices myobj and stores it into an
-- array and returns it
fn GetVerts myobj =
(  vpos = #()
   for i = 1 to myobj.numverts do
   (
     vpos[i] = myobj.verts[i].pos
   )
   return vpos
)
-- this function extracts the faces (ie the vertices that define the
-- faces) from my obj and returns it
fn GetFaces myobj =
(  faces = #()
   for i = 1 to myobj.numfaces do
   (
  	faces[i] = getFace myobj i
   )
   return faces
)
-- this function calculates the normals on each face.  The normals
-- are taken by calculating the cross product between two vectors on
-- the face.
fn GetFaceNormals myobj numface=
(
  facenormals = #()
  for i = 1 to numface do
  (
    facenormals[i]= - (getFaceNormal myobj i)
  )
  return facenormals
)

-- this function returns an array that holds the average
-- normal for all vertices in vpos by averaging the normals of all
-- the faces that uses that vertex
fn GetAvgNormals faces vpos facenormals =
(
  facetable = #()
  avgnormals = #()
  for i = 1 to vpos.count do
  (
    facetable[i] = #()
  )
  for i = 1 to faces.count do
  (
  	append facetable[faces[i].x] i
	append facetable[faces[i].y] i
	append facetable[faces[i].z] i
  )
  for i = 1 to facetable.count do
  (
  	avgnormals[i] = facenormals[facetable[i][1]]
        for j = 2 to facetable[i].count do
	(
	  avgnormals[i] = avgnormals[i] + facenormals[facetable[i][j]];
	)
	avgnormals[i]/facetable[i].count;
  )
  return avgnormals
)
fn PrintVertex fs vpos whichvertex =
(
  	format "  [%," vpos[whichvertex].x to: fs
  	format " %," vpos[whichvertex].y to: fs
  	format " %]" vpos[whichvertex].z to: fs

)
fn PrintIntVertex fs vpos whichvertex =
(
    a =vpos[whichvertex].x as Integer
	b =vpos[whichvertex].y as Integer
	c = vpos[whichvertex].z as Integer
  	format "  [%," a to: fs
  	format " %," b to: fs
  	format " %]" c to: fs

)
fn PrintVertexExchange fs vpos whichvertex =
(
  z=vpos[whichvertex].y * -1

  	format "  [%," vpos[whichvertex].x to: fs
  	format " %," vpos[whichvertex].z to: fs
  	format " %]" z to: fs

)
fn PrintUVVert fs myobj whichfacevert =
(
  uv = meshop.getMapVert myobj 1 whichfacevert
  d3dv = 1 - uv.y
  format "[ %, %]"  uv.x  d3dv to: fs
)

fn PrintArray arr count name namepostfix fs =
(
  format "var %" name to:fs
  format "%=[\n" namepostfix to:fs
  for i = 1 to count-1 do
  (
    PrintVertex fs arr i
    format ",\n" to: fs
  )
  PrintVertex fs arr count
  format "\n];\n" to: fs
)
fn PrintIntArray arr count name namepostfix fs =
(
  format "var %" name to:fs
  format "%=[\n" namepostfix to:fs
  for i = 1 to count-1 do
  (
    PrintIntVertex fs arr i
    format ",\n" to: fs
  )
  PrintIntVertex fs arr count
  format "\n];\n" to: fs
)
fn PrintArrayExchange arr count name namepostfix fs =
(
  format "var %" name to:fs
  format "%=[\n" namepostfix to:fs
  for i = 1 to count-1 do
  (
    PrintvertexExchange fs arr i
    format ",\n" to: fs
  )
  PrintvertexExchange fs arr count
  format "\n];\n" to: fs
)
fn Writeout mname smooth myobj vpos faces facenormals =
(
  fname = mname+".js"
  fs = OpenFile fname mode:"w"

  facetable = #()
  avgnormals = #()
  index = #()
  if smooth == true then
  (
    avgnormals = GetAvgNormals faces vpos facenormals
    PrintArray avgnormals avgnormals.count mname "Normals" fs
  )
  else(
    --Print the array of normals
    PrintArray facenormals facenormals.count mname "Normals" fs
  )
  --Print the array of unique vertices
  PrintArrayExchange vpos vpos.count mname "Vertices" fs

  --Print the UV's of each vertex of each face
  format "var %" mname to:fs
  format "UVs=[\n" to:fs
  for i = 1 to faces.count-1 do
  (
    mapface = meshop.getMapFace myobj 1 i
    format "  " to:fs
    PrintUVVert fs myobj mapface.x
    format "," to:fs
    PrintUVVert fs myobj mapface.y
    format "," to:fs
    PrintUVVert fs myobj mapface.z
    format ",\n" to:fs
  )
  mapface = meshop.getMapFace myobj 1 faces.count
  format "  " to:fs
  PrintUVVert fs myobj mapface.x
  format "," to:fs
  PrintUVVert fs myobj mapface.y
  format "," to:fs
  PrintUVVert fs myobj mapface.z
  format "\n" to:fs
  format "\n];\n" to: fs

  j=1
  format "var %" mname to:fs
  format "Faces=[\n" to:fs
  if smooth == false then
  (
    for i = 1 to faces.count do
    (
      index[j]= [faces[i].x-1,j-1,i-1]
      j = j + 1
      index[j]= [faces[i].y-1,j-1,i-1]
      j = j + 1
      index[j]= [faces[i].z-1,j-1,i-1]
      j = j + 1
    )
  )
  else
  (
    for i = 1 to faces.count do
    (
      index[j]= [faces[i].x-1,j-1,faces[i].x-1]
      j = j + 1
      index[j]= [faces[i].y-1,j-1,faces[i].y-1]
      j = j + 1
      index[j]= [faces[i].z-1,j-1,faces[i].z-1]
      j = j + 1
    )
  )
  for i = 1 to index.count-1 do
  (
    PrintIntVertex fs index i
    format ",\n" to: fs
  )
  PrintIntVertex fs index index.count
  format "\n];\n" to: fs
  close fs
)


fn dumpdata mname =
( 
  faces = #()
  vpos = #()
  facenormals = #()
  uvs = #()
  smooth = falses
  myobj = snapshotAsMesh selection[1]
  vpos = GetVerts myobj
  faces = GetFaces myobj
  facenormals = GetFaceNormals myobj faces.count
  Writeout mname smooth myobj vpos faces facenormals
)
Utility dumptool "C3DL Model Dump Tool"
(
	rollout nofileerror "Error!" width:160 height:67
	(
	  label errmsg "Output filename must not be empty" pos:[25,20] width:102 height:39
	)
 	editText mnamefield "" pos:[75,20] width:78 height:16
	label filelabel "Model Name:" pos:[5,20] width:65 height:16
--	checkbox smoothcheck "Smooth" pos:[5,50] width:107 height:16
	button done "Done" pos:[23,120] width:116 height:25 toolTip:""
	on done pressed  do(
	 	if mnamefield.text == "" then
		(
		   createDialog nofileerror
		)
		else if (selection[1]==undefined) then
		(
		   messageBox "ERROR!\nYou must select an object to export."
		)
		else
		(
	  	   dumpdata mnamefield.text
		)
	)
	
)

