---------------------------------------------------------------
-- Gnosis export
--
-- Export selected model as Gnosis format.
--
-- 1.0
--
-- FileFormat v1
--	* Vertex Data
--	* Face Data
--
-- FileFormat v2
--	* Vertex Data
--	* Face Data
--	* Normal Data
--	* UV Data
--
-- http://wwww.verajankorva.com
---------------------------------------------------------------

-- Functions
GExport = -1
GExport_V1 = -1
GExport_V2 = -1

---------------------------------------------------------------
-- Export model
---------------------------------------------------------------
fn GExport _o _v =
(
	case _v of
	(
		1: GExport_V1 _o
		2: GExport_V2 _o		
		default:messageBox ("Error!\nCan not write version "+(_v as string))
	)
)
---------------------------------------------------------------

---------------------------------------------------------------
-- Version 2 fileformat
---------------------------------------------------------------
fn GExport_V2 _o =
(
	o = _o
	f = GetSaveFileName "Save Gnosis Model" types:"*.gm|*.gm"
	
	if f != undefined do
	(
		file = createFile f
		format "\n\n"
		s_Name = o.name		
		format "Exporting model [%]\n" s_Name
		format "GnosisModel\n" to:file
		format "2\n" to:file
		format "%\n" s_Name to:file
		i_VertCount = o.vertices.count
		i_FaceCount = o.faces.count
		i_TVertCount = getNumTVerts o
		format "Vertex count : %\n" i_VertCount
		format "Face count : %\n" i_FaceCount
		format "Texture vertex count : %\n" i_TVertCount
		format "%\n" i_VertCount to:file
		format "%\n" i_FaceCount to:file
		format "%\n" i_TVertCount to:file
		format "Vertex data\n{\n"
		for i=1 to i_VertCount do
		(
			v = Point3 	(o.vertices[i].pos.x) \
						(o.vertices[i].pos.y) \
						(o.vertices[i].pos.z)
			format "\tVertex% : %\n" i v
			format "%|%|%\n" (v.x) (v.y) (v.z) to:file
		)
		format "}\n"
		format "Face data\n{\n"
		for i=1 to i_FaceCount do
		(
			a_Face = getFace o i
			format "\tFace% : %\n" i a_Face
			format "%|%|%\n" 	(a_Face[1] as integer) \
								(a_Face[2] as integer) \
								(a_Face[3] as integer) to:file
		)
		format "}\n"
		format "Texture vertex data\n{\n"
		for i=1 to i_TVertCount do
		(
			tv = getTVert o i
			format "\tTexture vertex% : %\n" i tv
			format "%|%|%\n" 	(tv[1]) \
								(tv[2]) \
								(tv[3]) to:file
		)
		format "}\n"
		format "Texture vertex face data\n{\n"
		for i=1 to i_FaceCount do
		(
			tvf = getTVFace o i
			format "\tTexture vertex face% : %\n" i tvf
			format "%|%|%\n"	(tvf[1] as integer) \
								(tvf[2] as integer) \
								(tvf[3] as integer) to:file
		)
		format "}\n"
		format "Vertex normal data\n{\n"
		for i=1 to i_VertCount do
		(
			n = getNormal o i
			format "\tVertex% normal : %\n" i (n)
			format "%|%|%\n" (n[1]) (n[2]) (n[3]) to:file
		)
		format "}\n"
		format "Face normal data\n{\n"
		for i=1 to i_FaceCount do
		(
			n = getFaceNormal o i
			format "\tFace% normal : %\n" i (n)
			format "%|%|%\n" (n[1]) (n[2]) (n[3]) to:file
		)
		format "}\n"		
		close file
	)
)
---------------------------------------------------------------

---------------------------------------------------------------
-- Version 1 fileformat
---------------------------------------------------------------
fn GExport_V1 _o =
(
	o = _o
	f = GetSaveFileName "Save Gnosis Model" types:"*.gm|*.gm"
	
	if f != undefined do
	(
		file = createFile f
		format "\n\n"
		s_Name = o.name		
		format "Exporting model [%]\n" s_Name
		format "GnosisModel\n" to:file
		format "1\n" to:file
		format "%\n" s_Name to:file
		i_VertCount = o.vertices.count
		i_FaceCount = o.faces.count
		format "Vertex count : %\n" i_VertCount
		format "Face count : %\n" i_FaceCount
		format "%\n" i_VertCount to:file
		format "%\n" i_FaceCount to:file
		format "Vertex data\n{\n"
		for i=1 to i_VertCount do
		(
			v = Point3 	(o.vertices[i].pos.x) \
						(o.vertices[i].pos.y) \
						(o.vertices[i].pos.z)
			format "\tVertex% : %\n" i v
			format "%|%|%|" (v.x) (v.y) (v.z) to:file
		)
		format "\n" to:file
		format "}\n"
		format "Face data\n{\n"
		for i=1 to i_FaceCount do
		(
			a_Face = getFace o i
			format "\tFace% : %\n" i a_Face
			format "%|%|%|" 	(a_Face[1] as integer) \
								(a_Face[2] as integer) \
								(a_Face[3] as integer) to:file
		)
		format "\n" to:file		
		format "}\n"
		close file
	)
)
---------------------------------------------------------------

if selection.count != 0 do
(
	s = selection[1]
	o = undefined
	o = snapshot s
	GExport o 2
	delete o
)