And an example of how to use the functions please?
Code archives/3D Graphics - Effects/Automatic Starfield generator
This code has been declared by its author to be Public Domain code.
Download source code
| It's a simple function set that I made a while back to help me get an easy to maintain starfield. Every frame, you call AnimateStarfield(). You can put some parms to adjust exactly where you want it to be. Usually your x and z are the same as where your view is. The starfield always go in the same direction. I didn't make it too advanced. And there's a function to delete the starfield, of course. |
; FILENAME: func_stars.bb ;---------------------------------------------------------------------------------------- ; starfield management ;---------------------------------------------------------------------------------------- ; Define the type for each stars in a starfield Type star_info Field ptr Field xpos# Field ypos# Field zpos# Field velocity# End Type Global star_count% = 0 Function AnimateStarfield(x#=0,y#=0,z#=0,occ%=6) Local overlap_x# = Rnd(0, 2500.0) Local overlap_z# = Rnd(0, 5000.0) If Rand(1,occ%) = occ% Then AddStar(x#, y#, z#, overlap_x#, overlap_z#) For star.star_info = Each star_info star\xpos# = star\xpos# + star\velocity# If (star\xpos#) > (x# + overlap_x# + 2500.0) Then FreeEntity star\ptr Delete star.star_info star_count% = star_count% - 1 Else ;PositionMesh star\ptr, star\velocity#, 0, 0 PositionEntity star\ptr, star\xpos#, star\ypos#, star\zpos# EndIf Next End Function Function DeleteAllStars() For star.star_info = Each star_info FreeEntity star\ptr Delete star.star_info Next star_count% = 0 End Function Function AddStar(xref#, yref#, zref#, overlap_x#, overlap_z#) Local starsize# = Rnd(0.5,2.01) Local speed# = Rnd(2.0,8.0) If starsize# > 2 Then starsize# = 18.0 - speed# EndIf star.star_info = New star_info ; star\ptr = make_star (2 + Int starsize#) star\ptr = CreateSphere(2 + Int starsize#) star\xpos# = xref# - overlap_x# - 2500.0 star\zpos# = zref# - overlap_z# + 2500.0 star\ypos# = 650.0 + Rnd(0, 350.0) star\velocity# = speed# ; EntityTexture star\ptr, ptr_texture(127),0,1 EntityBlend star\ptr, 3 EntityFX star\ptr, 1 ScaleEntity star\ptr, starsize#, starsize#, starsize# EntityColor star\ptr, 255, 255, 255 star_count% = star_count% + 1 ; PositionEntity star\ptr, star\xpos#, star\ypos#, star\zpos# End Function |
Comments
Every frame, you call AnimateStarfield(). You can put some parms to adjust exactly where you want it to be.
Then call DeleteAllStars() to delete the starfield (useful when you leave the "in game" scene).
CODE EXAMPLE:
Include "func_stars.bb" Graphics3D 1024,768,32,1 Light=CreateLight() cam=CreateCamera() CameraRange cam, .01, 10000 land = CreatePlane() tex = create_checker_tex(64,96,64,128,192,128,1,1) EntityTexture land, tex EntityAlpha land, 0.5 mirror=CreateMirror() sens# = 0.25 centerofworld = CreatePivot () : PositionEntity centerofworld, 0,1,0 PositionEntity cam,0,1,0 For i = 1 To 1000 : AnimateStarfield() : Next ; let it create and move stars for 1000 cycles While Not KeyDown(1) x# = x + sens : z# = z + sens PositionEntity centerofworld,Sin(x),1,Cos(z) PointEntity cam, centerofworld AnimateStarfield() ; cycling through the starfield algorythm RenderWorld () Color 0,255,0 Text 0,0, "PRESS [ESC] TO EXIT" Flip Wend DeleteAllStars() ; deletes all star entities End Function create_checker_tex(red1,green1,blue1,red2,green2,blue2,scale_u#,scale_v#) texture_handle = CreateTexture(32,32,256) SetBuffer TextureBuffer(texture_handle) Color red1,green1,blue1 : Rect 0,0,32,32 : Color red2,green2,blue2 : Rect 0,0,16,16,1 : Rect 16,16,16,16,1 ScaleTexture texture_handle,scale_u#,scale_v# SetBuffer BackBuffer() Return texture_handle End Function
Do not forget to have the include file in the same directory as this test.