type texture
	u as float
	v as float
endtype

type face
	v1 as tPoint
	v2 as tPoint
	v3 as tPoint
	
	vp1 as integer
	vp2 as integer
	vp3 as integer				
	
	u1 as texture
	u2 as texture
	u3 as texture
	
	up1	as integer
	up2	as integer
	up3	as integer	
	
	np1	as integer
	np2	as integer
	np3	as integer		
	
	n1	as tPoint
	n2	as tPoint
	n3 	as tPoint	
endtype

type center
	min		as float
	max		as float
	shift	as float
endtype

type bounds
	name	as string
	x		as center
	y		as center
	z		as center
endtype

//
// 	This will split apart a model based on it's groupd
//	creating an individual model for each group
//	Remember that the individual models will be in the
//	c:/Users/yourid/AppData/Local/AGKApps/appname/folder
//
function	MakeCarFromModel(folder as string, name as string)

	ProcessModel(folder, name)

endfunction

function	ProcessModel(folder as string, name as string)
	model 		as integer
	line		as string
	n			as string
	path		as string
	peek		as string
	i			as integer
	r			as integer
	texture		as integer
	vertex		as tPoint[]
	textures	as texture[]
	normals		as tPoint[]
	faces		as face[]	
	group		as string
	names		as string[]

	model = OpenToRead(folder+"/"+name)
	
	line = ReadLine(model)
	
	repeat
		if mid(line, 1, 3) = "vn "					// Normals
			normals.insert(AddNormal(line))
		endif
				
		if mid(line, 1, 2) = "v "					// Vertex
			vertex.insert(AddVertex(line))
		endif
		
		if mid(line, 1, 3) = "vt "					// Texture/UV
			textures.insert(AddTexture(line))
		endif
		
		if mid(line, 1, 2) = "f "					// Faces
			faces.insert(AddFace(line, vertex, textures, normals))
		endif
				
		if mid(line, 1, 2) = "g "					// Group
			if group <> ""
				names.insert(BuildModel(folder, group, faces))
			endif
			faces.length = -1			
			group = GetStringToken2( line, " ", 2 )
		endif				
				
		line = ReadLine(model)
	until fileEof(model) = 1

	names.insert(BuildModel(folder, group, faces))
	
	CloseFile(model)
	
endfunction names


function	AddFace(line as string, vertex ref as tPoint[], textures ref as texture[], normals ref as tPoint[])
	f as face
	v1 as string
	v2 as string
	v3 as string
	
	v1 = GetStringToken2( line, " ", 2 )
	v2 = GetStringToken2( line, " ", 3 )
	v3 = GetStringToken2( line, " ", 4 )	
	
	f.vp1 = val(GetStringToken2( v1, "/", 1 )) - 1
	f.vp2 = val(GetStringToken2( v2, "/", 1 )) - 1
	f.vp3 = val(GetStringToken2( v3, "/", 1 )) - 1
	
	f.v1 = vertex[f.vp1]
	f.v2 = vertex[f.vp2]
	f.v3 = vertex[f.vp3]

	f.up1 = val(GetStringToken2( v1, "/", 2 )) - 1
	f.up2 = val(GetStringToken2( v2, "/", 2 )) - 1
	f.up3 = val(GetStringToken2( v3, "/", 2 )) - 1

	f.u1 = textures[f.up1]
	f.u2 = textures[f.up2]
	f.u3 = textures[f.up3]		
	
	f.np1 = val(GetStringToken2( v1, "/", 3 )) - 1
	f.np2 = val(GetStringToken2( v2, "/", 3 )) - 1
	f.np3 = val(GetStringToken2( v3, "/", 3 )) - 1	

	if f.np1 > 0
		f.n1 = normals[f.np1]
		f.n2 = normals[f.np2]
		f.n3 = normals[f.np3]	
	endif
	
endfunction f

function	AddNormal(line as string)
	v as tPoint
	v.x = ValFloat(GetStringToken2( line, " ", 2 ))
	v.y = ValFloat(GetStringToken2( line, " ", 3 ))
	v.z = ValFloat(GetStringToken2( line, " ", 4 )) 
endfunction v

function	AddVertex(line as string)
	v as tPoint
	v.x = ValFloat(GetStringToken2( line, " ", 2 ))
	v.y = ValFloat(GetStringToken2( line, " ", 3 ))
	v.z = ValFloat(GetStringToken2( line, " ", 4 )) 
endfunction v

function	AddTexture(line as string)
	t as texture
	t.u = ValFloat(GetStringToken2( line, " ", 2 ))
	t.v = ValFloat(GetStringToken2( line, " ", 3 ))
endfunction t


function	BuildModel(folder as string, name as string, faces as face[])
	file 	as integer
	vert	as tPoint[]
	tex		as texture[]
	norm	as tPoint[]
	i		as integer
	f		as face
	v		as tPoint
	t		as texture 
	b		as bounds
	n		as string
	s		as string
	u		as string
	c		as String
	m		as string
	mm		as string
	p1		as string
	
	if mid(name, 1, 5) = "Group"
		exitfunction ""
	endif
	
	b.x.min = 9999999
	b.y.min = 9999999
	b.z.min = 9999999

	b.x.max = -9999999
	b.y.max = -9999999
	b.z.max = -9999999

	for i=0 to faces.length
		f = faces[i]		
		b = GetBounds(f.v1, b)
		b = GetBounds(f.v2, b)
		b = GetBounds(f.v3, b)		
	next
	
	b.x.shift = b.x.min + ( b.x.max - b.x.min ) / 2
	b.y.shift = b.y.min + ( b.y.max - b.y.min ) / 2
	b.z.shift = b.z.min + ( b.z.max - b.z.min ) / 2 

	n = n + ".obj"
	
	file = OpenToWrite(folder+"/"+name+".obj")

	for i=0 to faces.length
		faces[i].vp1 = ProcessVert(faces[i].v1, vert, FALSE)
		faces[i].vp2 = ProcessVert(faces[i].v2, vert, FALSE)
		faces[i].vp3 = ProcessVert(faces[i].v3, vert, FALSE)	
		faces[i].up1 = ProcessUV(faces[i].u1, tex)
		faces[i].up2 = ProcessUV(faces[i].u2, tex)
		faces[i].up3 = ProcessUV(faces[i].u3, tex)	
		faces[i].np1 = ProcessVert(faces[i].n1, norm, FALSE)
		faces[i].np2 = ProcessVert(faces[i].n2, norm, FALSE)
		faces[i].np3 = ProcessVert(faces[i].n3, norm, FALSE)			
	next

	for i=0 to vert.length
//		WriteLine(file, "v "+str((vert[i].x - b.x.shift))+" "+str(vert[i].y - b.y.shift)+" "+str((vert[i].z - b.z.shift)))
		WriteLine(file, "v "+str(vert[i].x)+" "+str(vert[i].y)+" "+str(vert[i].z))
	next
	
	for i=0 to norm.length
		WriteLine(file, "vn "+str(norm[i].x)+" "+str(norm[i].y)+" "+str(norm[i].z))
	next	

	for i=0 to tex.length
		WriteLine(file, "vt "+str(tex[i].u)+" "+str(tex[i].v))
	next

	for i=0 to faces.length
		f = faces[i]
		WriteLine(file, "f "+str(f.vp1 + 1)+"/"+str(f.up1 + 1)+"/"+str(f.np1 + 1)+" "+str(f.vp2 + 1)+"/"+str(f.up2 + 1)+"/"+str(f.np2 + 1)+" "+str(f.vp3 + 1)+"/"+str(f.up3 + 1)+"/"+str(f.np3 + 1))
	next

	CloseFile(file)
	
endfunction folder+"/"+name+".obj"

function	ProcessVert(v as tPoint, vert ref as tPoint[], duplicates as integer)
	i	as integer
	vt	as tPoint
	
	vert.insert(v)
	i = vert.length
	
endfunction i

function	ProcessUV(u as texture, tex ref as texture[])
	i	as integer
	uv	as texture

	tex.insert(u)
	i = tex.length
endfunction i

function	getBounds(v as tPoint, b as bounds)
	if v.x > b.x.max
		b.x.max = v.x
	endif
	
	if v.x < b.x.min
		b.x.min = v.x
	endif
	
	if v.y > b.y.max
		b.y.max = v.y
	endif
	
	if v.y < b.y.min
		b.y.min = v.y
	endif	

	if v.z > b.z.max
		b.z.max = v.z
	endif
	
	if v.z < b.z.min
		b.z.min = v.z
	endif
	
endfunction b
