Code archives/3D Graphics - Misc/Add an image to a Texture

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

Download source code

Add an image to a Texture by Damien Sturdy2004
This code will load an image in and position it onto a texture, scaling the image down if necesary but keeping its ratio.


Note it uses UV coordinates to position the image.

to position an image half way across the texture and half way down, scaling the image to a maximum of 32 pixels across and 32 down, you would use:

MergeImageWithTexture(Texture,image,.5,.5,32,32)

Optional parameters alow masking of the image before applying. If these are not specified, no masking will occur:

MergeImageWithTexture(texture,image,.5,.5,32,32,Redmask,Greenmask,Bluemask)


The mergeimagefilewithtexture function is exactly the same as above except you give it an image filename to get the image from, instead of an image handle!


Hope Someone else find this useful!!!
Graphics3D 640,480
cube=CreateCube()
camera=CreateCamera()
PositionEntity camera,0,1,-5
PointEntity camera, cube
light=CreateLight()

;create the texture
Texture=CreateTexture(640,640)
SetBuffer TextureBuffer(Texture)
For n=1 To 100
Color Rnd(255),Rnd(255),Rnd(255)
Rect Rnd(640),Rnd(640),Rnd(320),Rnd(320),1
Next

;;;Create the sample image

image=CreateImage(640,128)
SetBuffer ImageBuffer(image)
Color 255,255,255
 font=LoadFont(arial,128)
SetFont font
Text 320,64,"HEYYYY!!!!!",1,1
SetBuffer BackBuffer()

Color 255,255,255



;;;;;:DO THE FUNCTION!

MergeImageWithTexture(Texture,image,.5,.5,640,32)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



EntityTexture cube,Texture
Repeat
RenderWorld
TurnEntity cube,-.2,0,0
TurnEntity cube,0,1,0,1
Flip
Until KeyDown(1)
End





;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function mergeimagefilewithtexture(Texture,filename$,u#,v#,sizex#,sizey#,maskr=-1,maskg=0,maskb=0)
Local img=LoadImage(filename$),th#,tw#,x#,y#,ratio1#,ratio2#,use#,cb
cb=GraphicsBuffer()
If maskr<>-1 Then MaskImage img,maskr,maskg,masgb
	x#=ImageWidth(img)
	targx#=sizex
	y#=ImageHeight(img)
	targy#=sizey
	ratio1#=targx#/x#
	ratio2#=targy#/y#
	
	x2#=x#/ratio1#
	y2#=y#/ratio2#
	If ratio1>ratio2 Then use#=ratio1 Else use#=ratio2
	ScaleImage img,use,use

	cb=GraphicsBuffer()
	SetBuffer TextureBuffer(Texture)
	tw#=TextureWidth(Texture)
	tw#=tw#/100.0
	th#=TextureHeight(Texture)
	th#=th#/100.0
	MidHandle img
	If maskr<>-1 Then DrawImage img,tw#*(u#*100),th#*(v*100) Else DrawBlock img,tw#*(u#*100),th#*(v*100)
	SetBuffer cb
FreeImage img
End Function

Function mergeimagewithtexture(Texture,Image,u#,v#,sizex#,sizey#,maskr=-1,maskg=0,maskb=0)
Local img=CopyImage(image),th#,tw#,x#,y#,ratio1#,ratio2#,use#,cb
cb=GraphicsBuffer()
	If maskr<>-1 Then MaskImage img,maskr,maskg,masgb

	x#=ImageWidth(img)
	targx#=sizex
	y#=ImageHeight(img)
	targy#=sizey
	ratio1#=targx#/x#
	ratio2#=targy#/y#
	
	x2#=x#/ratio1#
	y2#=y#/ratio2#
	If ratio1>ratio2 Then use#=ratio1 Else use#=ratio2
	ScaleImage img,use,use

	cb=GraphicsBuffer()
	SetBuffer TextureBuffer(Texture)
	tw#=TextureWidth(Texture)
	tw#=tw#/100.0
	th#=TextureHeight(Texture)
	th#=th#/100.0
	MidHandle img
	If maskr<>-1 Then DrawImage img,tw#*(u#*100),th#*(v*100) Else DrawBlock img,tw#*(u#*100),th#*(v*100)
	SetBuffer cb
FreeImage img
End Function

Comments

PsychicParrot2004
Useful little function, that! :)


WendellM2004
Nice demo. I've played around with that, but not in such an organized way. Thanks!


Clyde2004
Cheers Buddy, nice one :)


Code Archives Forum