Positioning mesh in a single surface system

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

Fry Crayola(Posted 2004) [#1]
I've created a single surface system to display my graphics in 2D using 3D commands. I understand that if the camera is a distance of 2 units away, it shows a 4x4 area of the mesh, at a zoom of 1.

So, I've set up my camera to be 400 units away, giving me an 800x800 area on my screen (fitting it nicely in my 800*600 resolution).

This means that if I want to place something right in the middle of the screen, I draw it with u,v coords of 400,300.

Can I expect any problems with this? I could easily use 40,30 and position the camera only 40 units away, using 0.1 to represent a one pixel movement. Would this be less memory intensive or is that not a problem at all?


Stevie G(Posted 2004) [#2]
It should make no difference, although if you have alot of textured / alpha quads close to the camera you may get some slowdown.


aCiD2(Posted 2004) [#3]
if you have an 800x800 mesh, then .5, .5 is the center of it. Infact .5, .5 is the centre regardless of size. UV of 400, 300 is the same as 0, 0. This is because u and v only extend up to 1. Anything more and you'll get texture wrapping


EOF(Posted 2004) [#4]
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



Fry Crayola(Posted 2004) [#5]
aCiD2 My bad, I explained wrong. Replace my u,v with x,y. Then it'll make more sense.

Stevie G If I move the mesh further away from the camera, but zoom in to a greater degree, will that achieve faster results or is it the same?