' ------------------------------------------------------------------------------------------------
' Creates a single Quad
' ------------------------------------------------------------------------------------------------
Function CreateQuad:TMesh(parent:TEntity = Null, r:Int = 255, g:Int = 255, b:Int = 255, a:Float = 1.0, surfs:Int = 1)
	
	Local v1:Int, v2:Int, v3:Int, v4:Int
	
	Local mesh:TMesh = CreateMesh(parent)
	
	For Local i:Int = 1 To surfs
	
		Local surf:TSurface = CreateSurface(mesh)
	
		v1 = AddVertex(surf, -1, 1, 0, 0, 0)
		v2 = AddVertex(surf, 1, 1, 0, 1, 0)
		v3 = AddVertex(surf, -1, -1, 0, 0, 1)
		v4 = AddVertex(surf, 1, -1, 0, 1, 1)
	
		VertexColor surf, v1, r, g, b, a
		VertexColor surf, v3, r, g, b, a
		VertexColor surf, v2, r, g, b, a
		VertexColor surf, v4, r, g, b, a
	
		AddTriangle(surf, 0, 1, 2)
		AddTriangle(surf, 3, 2, 1)
	
	Next
		
	Return mesh
	
End Function



' ------------------------------------------------------------------------------------------------
' Update Mesh Vertex Colors
' ------------------------------------------------------------------------------------------------
Function UpdateMeshColor(mesh:TMesh, r:Int = 255, g:Int = 255, b:Int = 255, a:Float = 1.0)
	
	For Local s:Int = 1 To CountSurfaces(mesh)

		Local surf:TSurface = GetSurface(mesh, s)
	
		For Local v:Int = 0 To CountVertices(surf) - 1

			VertexColor surf, v, r, g, b, a
		
		Next

	Next
	
End Function



' ------------------------------------------------------------------------------------------------
' Update Mesh Vertex Colors
' ------------------------------------------------------------------------------------------------
Function UpdateMeshAlpha(mesh:TMesh, a:Float = 1.0)
	
	For Local s:Int = 1 To CountSurfaces(mesh)

		Local surf:TSurface = GetSurface(mesh, s)
	
		For Local v:Int = 0 To CountVertices(surf) - 1

			VertexColor surf, v, VertexRed(surf, v), VertexGreen(surf, v), VertexBlue(surf, v), a
		
		Next

	Next
	
End Function



' ------------------------------------------------------------------------------------------------
' Create Glow
' ------------------------------------------------------------------------------------------------
Function CreateGlow:TMesh(parent:TEntity = Null, radius1:Float = 1.0, radius2:Float = 2.0, segments:Int = 360, fx:Int = 0, blend:Int = 0, r1:Int = 255, g1:Int = 255, b1:Int = 255, al1:Float = 0.0, r2:Int = 0, g2:Int = 0, b2:Int = 0, al2:Float = 1.0, inverse:Int = False)
	
	Local a1:Float,a2:Float,a3:Float,a4:Float,angle:Int
	Local v:Int,v0:Int,v1:Int,v2:Int,v3:Int
	Local mesh:TMesh=CreateMesh(parent)
	Local surf:TSurface=CreateSurface(mesh)
	
	If segments>360  Then segments=360
	
	For angle=1 To segments
		
		a1=angle*360.0/segments
		a2=angle*360.0/segments +360.0/segments
		a3=angle*360.0/segments +180.0/segments
		a4=angle*360.0/segments -180.0/segments
		
		If inverse Then

			v0=AddVertex(surf,radius1*Cos(a1),radius1*Sin(a1),0,1,0)
			v1=AddVertex(surf,radius1*Cos(a2),radius1*Sin(a2),0,1,1)
			v2=AddVertex(surf,radius2*Cos(a3),radius2*Sin(a3),0,0,0)
			v3=AddVertex(surf,radius2*Cos(a4),radius2*Sin(a4),0,0,1)

		Else

			v0=AddVertex(surf,radius1*Cos(a1),radius1*Sin(a1),0,0,1)
			v1=AddVertex(surf,radius1*Cos(a2),radius1*Sin(a2),0,0,0)
			v2=AddVertex(surf,radius2*Cos(a3),radius2*Sin(a3),0,1,1)
			v3=AddVertex(surf,radius2*Cos(a4),radius2*Sin(a4),0,1,0)
		
		EndIf

		VertexColor surf,v0,r1,g1,b1,al1
		VertexColor surf,v1,r1,g1,b1,al1
		VertexColor surf,v2,r2,g2,b2,al2
		VertexColor surf,v3,r2,g2,b2,al2
		
		AddTriangle surf,v2,v1,v0
		AddTriangle surf,v0,v3,v2
		
	Next
	
	For v=0 To CountVertices(surf)-1
		
		VertexTexCoords surf,v,-VertexX(surf,v),-VertexY(surf,v),0,1
		
	Next
	
	If fx>0 Then EntityFX mesh,fx
	If blend>0 Then EntityBlend mesh,blend
	
	Return mesh
	
End Function



Function UpdateGlowColor(mesh:TMesh, r1:Int = 255, g1:Int = 255, b1:Int = 255, al1:Float = 0.0, r2:Int = 0, g2:Int = 0, b2:Int = 0, al2:Float = 0.0)
	
	Local v:Int
	Local surf:TSurface=GetSurface(mesh,1)
	
	For v=1 To CountVertices(surf)/4
		
		VertexColor surf,((v-1)*4)+0,r1,g1,b1,al1
		VertexColor surf,((v-1)*4)+1,r1,g1,b1,al1
		VertexColor surf,((v-1)*4)+2,r2,g2,b2,al2
		VertexColor surf,((v-1)*4)+3,r2,g2,b2,al2
		
	Next
	
End Function