alphaballs

Community Forums/Showcase/alphaballs

JBR(Posted 2006) [#1]
Just messing around with metaballs(i think).


Graphics3D 1024,768,32,0
SetBuffer BackBuffer()
ClearTextureFilters
;-----------------------------------------------------------------------------------------
camera = CreateCamera()					; camera
CameraRange camera, 512-128, 512+128			; set nicely, as 16 bit color is not as accurate
AmbientLight 255,255,255						; full bright
RotateEntity camera, 90+90, 0, 0				; looking down z
PositionEntity camera, 0, 0, 512				; raise above
CameraClsMode camera, True, True				; cls & z-cls


Global Back_Texture% = CreateTexture( 1024, 1024, 1+16+32+256)
SetBuffer TextureBuffer(Back_Texture%)
	For a% = 1 To 1000
		Color Rnd(64,255),Rnd(64,255),Rnd(64,255)
		Oval Rnd(0,1023), Rnd(0,1023), 128,128, True
	Next


Global Back_Mesh% = CreateMesh()
EntityTexture Back_Mesh%, Back_Texture%
Global Back_Surf% = CreateSurface( Back_Mesh% )
v0% = AddVertex( Back_Surf%, -512, -384, 0,     0.0, 0.0 )
v1% = AddVertex( Back_Surf%, +512, -384, 0,     1.0, 0.0 )
v2% = AddVertex( Back_Surf%, +512, +384, 0,     1.0, .75 )
v3% = AddVertex( Back_Surf%, -512, +384, 0,     0.0, .75 )
AddTriangle Back_Surf%, v0,v1,v2
AddTriangle Back_Surf%, v0,v2,v3

Const Overlay_Width% = 1024/8
Const Overlay_Depth% =  768/8

Global Overlay_Texture% = CreateTexture( Overlay_Width%, Overlay_Width%, 2+16+32+256 )

Global Overlay_Mesh% = CreateMesh()
EntityTexture Overlay_Mesh%, Overlay_Texture%
Global Overlay_Surf% = CreateSurface( Overlay_Mesh% )
v0% = AddVertex( Overlay_Surf%, -512, -384, 0.1,     0.0, 0.0 )
v1% = AddVertex( Overlay_Surf%, +512, -384, 0.1,     1.0, 0.0 )
v2% = AddVertex( Overlay_Surf%, +512, +384, 0.1,     1.0, .75 )
v3% = AddVertex( Overlay_Surf%, -512, +384, 0.1,     0.0, .75 )
AddTriangle Overlay_Surf%, v0,v1,v2
AddTriangle Overlay_Surf%, v0,v2,v3




Overlay_Colour% = $ff0000			; $000000 for black

Dim Overlay_Array%( Overlay_Width%*Overlay_Depth%-1 )



Global blob_a#=0

While Not KeyHit(1) 
	For b% = 0 To Overlay_Width%*Overlay_Depth%-1		; set overlay to full colour (or a little getting through)
		Overlay_Array%( b% ) = 255 Shl 8
	Next

	For i%= 0 To 15
		xxx# = 200.0 * Cos(i%*(360/16.0)+blob_a#)
		yyy# = 200.0 * Sin(i%*(360/16.0)+blob_a#)
		overlay_blob( xxx#  , yyy#, 20/2, 15.0 )
	Next
	For i%= 0 To 15
		xxx# = 275.0 * Cos(i%*(360/16.0)+blob_a#)
		yyy# = 275.0 * Sin(i%*(360/16.0)+blob_a#)
		overlay_blob( xxx#  , yyy#, 20/2, 15.0 )
	Next
	For i%= 0 To 15
		xxx# = 375.0 * Cos(i%*(360/16.0)+blob_a#)
		yyy# = 375.0 * Sin(i%*(360/16.0)+blob_a#)
		overlay_blob( xxx#  , yyy#, 16, 15.0 )
	Next
	For i%= 0 To 15
		xxx# = 475.0 * Cos(i%*(360/16.0)+blob_a#)
		yyy# = 475.0 * Sin(i%*(360/16.0)+blob_a#)
		overlay_blob( xxx#  , yyy#, 16, 15.0 )
	Next
	overlay_blob( 0  , 0, 40/2, 15.0 )

	blob_a# = blob_a# + 1

	SetBuffer TextureBuffer(Overlay_Texture%)
	LockBuffer TextureBuffer(Overlay_Texture%)
	For y%=0 To Overlay_Depth%-1
		row% = y% * Overlay_Width%
		For x%=0 To Overlay_Width%-1
			WritePixelFast x%, y%, ((Overlay_Array%(row%+x%) Shr 8) Shl 24) + Overlay_Colour%
		Next
	Next
	UnlockBuffer TextureBuffer(Overlay_Texture%)
	SetBuffer BackBuffer()


	UpdateWorld()
	RenderWorld()
	Flip
Wend
End

Function overlay_blob( base_x#, base_y#, spread%, amp# )
	base_x# = (base_x# + 512.0)/8.0
	base_y# = (base_y# + 384.0)/8.0
	
	fx# = base_x# - Floor(base_x#)
	fy# = base_y# - Floor(base_y#)
	
	centre_x% = base_x#-fx#
	centre_y% = base_y#-fy#

	For y% = -spread% To spread%
		ycomp# = (y-fy)*(y-fy)			; done here for each row
		yy% = centre_y% + y%			; ditto

		If (yy%>=0) And (yy%<Overlay_Depth%) Then			; weed out the off the top 'n' bottom

			For x% = -spread% To spread%
				xx% = centre_x% + x%
				
				If (xx%>=0) And (xx%<Overlay_Width%) Then		; weed out the off left 'n' right
				
					d# = Sqr( (x-fx)*(x-fx) + (ycomp#) ) / spread%
			
					If (d# < 0.707) Then

						hh% = Overlay_Array%( xx% + yy%*Overlay_Width% )
					
						now% = hh% - (d*d*d*d - d*d + 0.25)*amp#*255*256		; minus the height scaled
					
						If now% < 0 Then now% = 0		; alpha of 0.0 is smallest
						
						Overlay_Array%( xx% + yy%*Overlay_Width% ) = now%

					EndIf
				EndIf
			Next
		EndIf
	Next
End Function


Jim


puki(Posted 2006) [#2]
Interesting. However, what are you going to do with this?


JBR(Posted 2006) [#3]
Was going to use it in a game but it didn't look as good as i'd hoped; so I thought i'd post it here.....

Jim