How to Calculate CameraZoom for 1:1 pixels

Blitz3D Forums/Blitz3D Programming/How to Calculate CameraZoom for 1:1 pixels

Here is a basic 'bare-bones' version of my SpriteControl which will get your quads on screen with mimimum of fuss:
; "Sprite Control BASIC.bb" - 'Bare Bones' include

Global spritepivot,spritecamera

; Create a complete sprite display with camera/pivot
Function SpriteGraphics3D(gw,gh,d=0,m=0,dist#=1)
	Graphics3D gw,gh,d,m
	SetBuffer BackBuffer()
	spritecamera=CreateCamera()
	spritepivot=CreatePivot(spritecamera)
	Local aspect#=Float(gh)/Float(gw)
	Local scale#=2.0/gw
	PositionEntity spritepivot,-1,aspect,dist
	ScaleEntity spritepivot,scale,scale,scale
	Return spritepivot
End Function

; Loads an image into a texture and attaches it to sprite quad
Function LoadImage3D(file$,texflags=4,par=-1)
	; Create quad mesh
	If par=-1 par=spritepivot
	Local sprite=CreateMesh(par)
	Local s=CreateSurface(sprite)
	AddVertex s,0,0,0 ,0,0 : AddVertex s,2,0,0 , 1,0
	AddVertex s,0,-2,0 ,0,1 : AddVertex s,2,-2,0 , 1,1
	AddTriangle s,0,1,2 : AddTriangle s,3,2,1
	; transfer image into texture buffer (with alpha)
	Local tmpimage=LoadImage(file$)
	Local spritetexture=LoadTexture(file$,texflags)
	Local iw=ImageWidth(tmpimage) , ih=ImageHeight(tmpimage)
	Local tw=TextureWidth(spritetexture) , th=TextureHeight(spritetexture)
	If iw<>tw Or ih<>th
		ib=ImageBuffer(tmpimage)
		tb=TextureBuffer(spritetexture)
		For x=0 To iw-1
			For y=0 To ih-1
				rgbc=ReadPixel(x,y,ib) And $00ffffff
				If rgbc=((r Shl 16)+(g Shl 8)+b)
					WritePixel x,y,($00000000),tb
				Else
					WritePixel x,y,(rgbc Or $ff000000),tb
				EndIf
			Next
		Next
	EndIf
	FreeImage tmpimage
	; Texture and resize sprite. Move handle to top/left
	ScaleTexture spritetexture,Float(tw)/Float(iw),Float(th)/Float(ih)
	EntityTexture sprite,spritetexture
	EntityFX sprite,1+16 : EntityOrder sprite,-100
	ScaleEntity sprite,Float(iw)/2,Float(ih)/2,1
	PositionEntity sprite,-10000,-10000,0
	Return sprite
End Function

; Position 3d sprite at 2D screen coordinates
Function DrawImage3D(sprite,x,y,z=0)
	PositionEntity sprite,x+0.5,-y+0.5,0
End Function

; Flush out an existing quad sprite and it's texture
Function FreeImage3D(sprite)
	Local brush=GetEntityBrush(sprite)
	Local tex=GetBrushTexture(brush)
	If brush<>0 FreeBrush brush
	If tex<>0 FreeTexture tex
	If sprite<>0 FreeEntity sprite
End Function


An example piece of test code:
Include "Sprite Control BASIC.bb"

Const sw=640,sh=480

SpriteGraphics3D sw,sh
CameraClsColor spritecamera,50,10,70

; load and create quad sprite
quad=LoadImage3D("myimage.bmp")

; position the quad
DrawImage3D quad,sw/2,sh/2
RenderWorld
Flip
WaitKey

; cleanup
FreeImage3D quad

End

The above works with the main camera set at the default zoom ratio of 1.
If you zoom the main camera (1 onwards) then use this function which zooms the camera as well as the sprite pivot:
; Use this command to zoom the main camera and sprite pivot
; NOTE: Only positive values if 1 and greater can be used
Function CameraZoom3D(cam,z#)
	If z<1 z=1
	If EntityClass$(cam)="Camera" CameraZoom cam,z
	Local numchilds=CountChildren(cam)
	If numchilds>0
	For c=1 To numchilds
		Local child=GetChild(cam,c)
		If EntityName$(child)="SpriteControl Pivot"
			PositionEntity child,EntityX(child),EntityY(child),z+0.001
		EndIf
	Next
	EndIf
End Function


With the above example you would then use:
CameraZoom3D spritecamera,zoomamount#

Hope that helps.
Note: The above is mainly thanks to skidracer and his 'pixies' method for displaying pixel-perfect sprites.