Positioning mesh in a single surface system

Blitz3D Forums/Blitz3D Programming/Positioning mesh in a single surface system

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I use this which seems to be pretty accurate.
Displays quads at 2D screen coordinates using a pivot (pivot2d) which the mesh is attached to.
Credit to skidracer (pixies routine):

; Quads at 2D pixel locations

Const sw=640,sh=480

Const qwidth=180,qheight=120 ; quads width/height

Graphics3D sw,sh,0,2
SetBuffer BackBuffer()
HidePointer

Type quad
	Field container  ; container holding quad (surface)
	Field vert       ; quads first vertex in container
	Field x,y        ; quads x/y screen pos (in 2d pixels)
	Field w,h        ; quads width and height (in 2d pixels)
	Field rotdir#
End Type

; init 3d display (camera + quads pivot)
spritecamera=CreateCamera()
pivot2d=CreatePivot(spritecamera)
aspect#=Float(GraphicsHeight())/Float(GraphicsWidth())
scale#=2.0/GraphicsWidth()
PositionEntity pivot2d,-1,aspect,1.0001
ScaleEntity pivot2d,scale,scale,scale

; create a quad container (surface to hold quads)
quadsurf=CreateQuadContainer(pivot2d)

; add quad to container pos(x,y) size(w,h) color(r,g,b)
quad1.quad=NewQuad(quadsurf , 0,0 , qwidth,qheight , 60,200,70)

MoveMouse 320,240


; MAIN LOOP
Repeat

	mx=MouseX() :	my=MouseY()
	PositionQuad quad1,mx,my,0

	RenderWorld
	
	; just to confirm quad is displayed correctly
	; let's draw a standard 2D rect (same pos/width)
	Color 170,10,60
	Rect mx,my,qwidth,qheight,0 
	
	; cursor
	Color 200,200,200
	Line mx,my+8,mx,my-8
	Line mx+8,my,mx-8,my

	Flip

Until KeyHit(1)

End


; create a container (surface) for holding quads
Function CreateQuadContainer(pivot)
	Local quadmesh=CreateMesh(pivot)
	Local surf=CreateSurface(quadmesh)
	EntityFX quadmesh,1+2
	ScaleEntity quadmesh,1,-1,1
	PositionEntity quadmesh,-0.5,0.5,0
	Return surf
End Function

; adds a new quad to an existing container
Function NewQuad.quad(surf,x,y,w,h,r=255,g=255,b=255)
	q.quad=New quad
	q\container=surf
	q\x=x : q\y=y :	q\w=w : q\h=h
	q\vert=AddVertex(surf,x,y,0 ,0,0)
	AddVertex surf,x+w,y,0 ,1,0
	AddVertex surf,x,y+h,0 ,0,1
	AddVertex surf,x+w,y+h,0 ,1,1
	AddTriangle surf,q\vert+0,q\vert+1,q\vert+2
	AddTriangle surf,q\vert+3,q\vert+2,q\vert+1
	For o=0 To 3
		VertexColor surf,q\vert+o,r,g,b
	Next
	Repeat
		q\rotdir=Rnd(-2.5,2.5)+0.1
	Until q\rotdir<>0
	Return q
End Function

; position quad at 2d pixel coordinates
Function PositionQuad(q.quad,x,y,z#=1.0001)
	Local s=q\container , v=q\vert
	dx#=x-VertexX(s,v) : dy#=y-VertexY(s,v) :	dz#=z-VertexZ(s,v)
	For o=0 To 3
		VertexCoords s,v+o,VertexX(s,v+o)+dx,VertexY(s,v+o)+dy,VertexZ(s,v+o)+dz
	Next
	q\x=VertexX(s,v) : q\y=VertexY(s,v)
End Function