Moving Background Starfield

Blitz3D Forums/Blitz3D Beginners Area/Moving Background Starfield

blackbag(Posted 2008) [#1]
Third time of writing, having some difficulty in explaining what I am trying to do.

Imagine the old game defender,
Put some stars in the background,
Add some paralax,

Easy peasy in 2d.

I want to get that effect behind a 3d scene. But do it before renderworld() and it gets cleared, do it after and the stars are "in front" of the 3d scene.

Been messing around with readpixelfast and writepixelfast but don't seen to get the right results with this,
	For back.stars = Each stars
	
		back\x = back\x + back\z
		If back\x > 800 Then back\x = back\x - 800
		If ReadPixelFast (back\x,back\z,BackBuffer())=-16777216 Then
			WritePixelFast (back\x,back\y,-1,BackBuffer())
		EndIf
		
	Next


It works, but does not seem to detect "not black" pixels to not write over. What argb value should I be looking for. Or is this wrong wrong wrong....


Rob Farley(Posted 2008) [#2]
Is there any reason why you're not just using 3D sprites with an entity order set so it draws first?


Rob Farley(Posted 2008) [#3]
Quite bit of coding for you...

You might want to play with the random position of the star, to give more depth you need to increase the z start position, rand(10,500) looks pretty good, although because you are further away you also need to increase the x and y so it fills vertically and horizontally... basically just play with it...

And obviously have the start Z position ie the 10 part of rand(10,100) to be behind your action.

Graphics3D 800,600,32,2


Global StarTex = CreateStar(32)
Global camera = CreateCamera()

Type Star
	Field Mesh
End Type

Function NewStar()
	S.Star = New Star
	S\Mesh = CreateSprite()
	EntityTexture s\mesh,startex
	PositionEntity s\mesh,Rand(-100,100),Rand(-100,100),Rand(10,100) ; Random position of the star.
	EntityFX s\mesh,1
	EntityBlend s\mesh,3
	
	; if you can't see the star throw it away
	If EntityInView(s\mesh,camera) = False Then FreeEntity s\mesh:Delete s
End Function

Function UpdateStars()
	For S.Star = Each Star
		; if it goes off the left hand edge jump it to the right hand edge
		If EntityInView(s\mesh,camera) = False 
			TranslateEntity s\mesh,(EntityX(camera)-EntityX(s\mesh))*2,0,0
		EndIf
	Next
End Function

Function CreateStar(Size)
	; Create a star texture
	I = CreateImage(Size,Size)
	T = CreateTexture(Size,Size)
	LockBuffer ImageBuffer(i)
	
	S = Size / 2
	
	Xpos = S
	YPos = S
	
	For x=-S To S
	For y=-S To S
		r# = ((x*x)+(y*y))
		If r<(s) Then
			c# = 255.0 - ((255.0 / (s)) * r)
			argb=(c Or (c Shl 8) Or (c Shl 16) Or ($ff000000))
			WritePixelFast x+xpos,y+ypos,argb,ImageBuffer(i)
		EndIf
	Next
	Next
	
	UnlockBuffer ImageBuffer(i)
	CopyRect 0,0,size,size,0,0,ImageBuffer(i),TextureBuffer(t)
	FreeImage i
	Return t
End Function

For n=1 To 5000
	NewStar()
Next


; move the camera to the right
Repeat
	MoveEntity camera,.5,0,0
	UpdateStars()
	RenderWorld
	Flip
Until KeyHit(1)



blackbag(Posted 2008) [#4]
Thanks,

Just for my interest....

Can you explain the argb line?


Ross C(Posted 2008) [#5]
It is used to change the colour of a pixel. Write pixel uses a 24 bit colour value with alpha information.

			RGB1=ReadPixelFast(loop,loop1,TextureBuffer(texture))
			r=(RGB1 And $FF0000)shr 16;separate out the red
			g=(RGB1 And $FF00) shr 8;green
			b=RGB1 And $FF;and blue parts of the color
			a=(RGB1 And $FF000000)Shr 24
			
			a=255; remove any alpha information currently in the texture.

			newrgb= (a shl 24) or (r shl 16) or (g shl 8) or b; combine the ARGB back into a number

			WritePixelFast(loop,loop1,newrgb,TextureBuffer(texture)); write the info back to the texture


You need to extract the individual 8 bit Red, Green, Blue and Alpha values from it, using the method above. Tweak the values, then you can combine the value back into a 24 bit value.