Code archives/3D Graphics - Misc/Pixies

This code has been declared by its author to be Public Domain code.

Download source code

Pixies by skidracer2003
The LoadPixie function returns a sprite that is scaled to give 1:1 texel:pixel scaling and screen coordinate based positioning.
; pixies.bb
; by skidracer

; pixies are pixel perfect sprite overlays

; LoadPixie(camera,imagefile$)

; returns a sprite parented to a camera 
; with following features 
;  1:1 pixel-texel scale for zero filtered sharp overlays
;  position pixies in screen coordinates

; 20.8.2003 untested with odd sized sprites
; 21.8.2003 modified to handle odd textures

displaywidth=1024
displayheight=768

Graphics3D displaywidth,displayheight  

cam=CreateCamera() 
CameraRange cam,.1,1000 

pixie=LoadPixie(cam,"simon.bmp")

While Not KeyHit(1) 
	PositionEntity pixie,MouseX(),MouseY(),0
	RenderWorld 
	UpdateWorld 
	TurnEntity cam,1,2,0	;test texel drift
	Flip 
	Wend 
End 

Function LoadPixie(camera,file$)
; load squared texture
	texture=LoadTexture(file)
	width=TextureWidth(texture)
	height=TextureHeight(texture)
	image=LoadImage(file)
	iwidth=ImageWidth(image)
	iheight=ImageHeight(image)
	If iwidth<>width Or iheight<>height
		buffer=TextureBuffer(texture)
		ibuffer=ImageBuffer(image)
		For y=0 To height-1
			For x=0 To width-1
				WritePixel x,y,ReadPixel(x,y,ibuffer),buffer
			Next
		Next
		ScaleTexture texture,Float(width)/iwidth,Float(height)/iheight ; will blitzmax need float()?
		width=iwidth
		height=iheight
	EndIf
	FreeImage image
; change these for viewports
	viewwidth=GraphicsWidth()
	viewheight=GraphicsHeight()
; find existing pixiespace parented to camera
	magic=0
	n=CountChildren(camera)
	For i=1 To n
		If EntityName(GetChild(camera,i))="pixiespace" 
			magic=GetChild(GetChild(camera,i),1)
		EndIf
	Next
	If magic=0
		magic=CreatePivot(camera) 
		NameEntity(magic,"pixiespace")
		aspect#=Float(viewheight)/viewwidth
		PositionEntity magic,-1,aspect,1 
		scale#=2.0/viewwidth 
		ScaleEntity magic,scale,-scale,-scale 
		magic=CreatePivot(magic)
		PositionEntity magic,-.5,-.5,0
	EndIf
; create sprite from texture as child of magic overlay	
	sprite=CreateSprite()
	EntityParent sprite,magic		;cludge for blitz bug in createsprite(parent)
	brush=CreateBrush()
	BrushFX brush,1
	BrushTexture brush,texture
	PaintEntity sprite,brush
	FreeBrush brush
	SpriteViewMode sprite,2 
	scale#=1.0/viewwidth 
	ScaleSprite sprite,width*scale,height*scale
	Return sprite
End Function

Comments

kalimon2004
cool.

slow but cool.

I was about to begin codign a version of this although now that i see it made I'm intrigued by the "magic" thingie. ( when I designed the alogritm I stopped In overwritting the buffers)


skidracer2004
slow???


puki2004
"Boydy" likes killing pixies or something - I wonder if he will delete this thread and the code? Best rename it elvesies or goblinsies or something else.


skidracer2004
Boids?


puki2004
That will throw "Boydy-Boy" off the scent - smart thinking "skid-person" - he'll arrive in this thread to delete it and be instantly attracted to the 'Boids' post - sheer genius "Skid-person", sheer genius.


bytecode772006
it is only pixelperfect on square textures... but how can we make it pixel perfect on non square textures?


Moore2008
Yeah its slow.
But replace
	If iwidth<>width Or iheight<>height
		buffer=TextureBuffer(texture)
		ibuffer=ImageBuffer(image)
		For y=0 To height-1
			For x=0 To width-1
				WritePixel x,y,ReadPixel(x,y,ibuffer),buffer
			Next
		Next
		ScaleTexture texture,Float(width)/iwidth,Float(height)/iheight ; will blitzmax need float()?


with....

[code]
CopyRect 0,0,iwidth,iheight,0,0,ImageBuffer(image), TextureBuffer(texture)
</code>

On my rig the first bit of code runs in 198 millisecs and the copyrect runs in 8 millisecs.


Code Archives Forum