AppTitle "Infinite Asteroid Belt"Graphics3D 800,600,32,2Global SCALEX# = 2.0 ; starfield scale XGlobal SCALEY# = 0.2 ; starfield scale YGlobal SCALEZ# = 2.0 ; starfield scale ZGlobal TurnSpeed# = 4.0 ; cam turn speedGlobal RollSpeed# = 0.5 ; cam roll speedGlobal CameraSpeed# = 0.01 ; cam move speedGlobal WIDTH% = GraphicsWidth() ; grab screen widthGlobal HEIGHT% = GraphicsHeight() ; grab screen heightGlobal TIMER% = CreateTimer(60) ; timerType star Field col% Field x#,y#,z# End Type; cameraGlobal cam=CreateCamera()CameraRange cam,0.01,10PositionEntity cam,0,0,0; starfield meshGlobal starfield=CreateMesh()Global surf=CreateSurface(starfield)Global star=CreateQuad()EntityTexture starfield,CreateStarTexture()EntityFX starfield,1+2+32EntityBlend starfield,3; add stars to starfieldAddStars(20000,0.001,0.005)MoveMouse WIDTH/2,HEIGHT/2; main loopWhile Not KeyHit(1) ; camera movement Movement(cam) ; update stars UpdateStarfield(cam,2,1) RenderWorld() WaitTimer TIMER Flip 0 WendEnd; create a simple star textureFunction CreateStarTexture(size%=256,flags%=3) Local tex%=CreateTexture(size,size,flags) Local tb%=TextureBuffer(tex) Local i#,j%,col%,rgb% SetBuffer tb LockBuffer tb For j=0 To 255 col=255-j If col>255 Then col=255 rgb=col*$1000000+col*$10000+col*$100+col For i=0 To 360 Step 0.1 WritePixelFast (size/2)+(Sin(i)*(j*size/512)),(size/2)+(Cos(i)*(j*size/512)),rgb,tb Next Next UnlockBuffer tb SetBuffer BackBuffer() Return tex End Function; rebuild starfield meshFunction UpdateStarfield(parent%,maxdist#=2.0,fade%=False) Local s.star,px#,py#,pz#,d#,a# ClearSurface(surf) For s.star = Each star ; calc star position px=EntityX(parent)-sx py=EntityY(parent)-sy pz=EntityZ(parent)-sz ; check if star must be moved If px<-SCALEX Then sx=sx-(SCALEX*2) If px>+SCALEX Then sx=sx+(SCALEX*2) ;If py<-SCALEY Then sy=sy-(SCALEY*2) ;If py>+SCALEY Then sy=sy+(SCALEY*2) If pz<-SCALEZ Then sz=sz-(SCALEZ*2) If pz>+SCALEZ Then sz=sz+(SCALEZ*2) ; reposition star PositionEntity star,sx,sy,sz ; star is visible? If EntityInView(star,cam) Then ; get distance d=EntityDistance(star,cam) ; check if not to far away If d<maxdist Then ; align star to cam PointEntity star,cam ; add alpha a=1.0 : If fade Then a=Normalize(d,maxdist*0.5,maxdist,1,0) ; add star to starfield again AddToSurface(star,surf,starfield,scol,scol,scol,a) EndIf EndIf Next End Function; add stars to starfield meshFunction AddStars(amount%=1,min#=0.01,max#=0.02) Local i%,s.star,size# For i=1 To amount s.star = New star size#=Rnd(min,max) scol=Rand(64,255) sx=Rnd(-SCALEX,SCALEX) sy=Rnd(Rnd(Rnd(-SCALEY)),Rnd(Rnd(SCALEY))) sz=Rnd(-SCALEZ,SCALEZ) PositionEntity(star,sx,sy,sz,1) ScaleEntity star,size,size,size AddToSurface(star,surf,starfield,255,255,255,1) Next End Function; simple spaceship freeflightFunction Movement(cam%,sensitivity#=1.0) Local roll#,cx#,cz#,tx#,ty# cx=(KeyDown(205)-KeyDown(203))*CameraSpeed cz=(KeyDown(200)-KeyDown(208))*CameraSpeed roll=(KeyDown(203)-KeyDown(205))*RollSpeed tx=Normalize(MouseX(),0,WIDTH , 1,-1) ty=Normalize(MouseY(),0,HEIGHT,-1, 1) If ty<0 Then ty=(Abs(ty)^sensitivity)*-1 Else ty=ty^sensitivity If tx<0 Then tx=(Abs(tx)^sensitivity)*-1 Else tx=tx^sensitivity TurnEntity cam,ty*TurnSpeed,tx*TurnSpeed,roll*TurnSpeed MoveEntity cam,0,0,cz End Function; normalize a valueFunction Normalize#(value#=128.0,value_min#=0.0,value_max#=255.0,norm_min#=0.0,norm_max#=1.0) Return ((value-value_min)/(value_max-value_min))*(norm_max-norm_min)+norm_min End Function; add a mesh to another mesh's surfaceFunction AddToSurface(mesh,surf,singlesurfaceentity,r%,g%,b%,a#) Local vert%[2],vr%[2],vg%[2],vb%[2],va#[2] Local surface%,oldvert%,i%,i2% surface = GetSurface(mesh,1) For i = 0 To CountTriangles(surface)-1 For i2 = 0 To 2 oldvert = TriangleVertex(surface,i,i2) vr[i2]=r vg[i2]=g vb[i2]=b va[i2]=a TFormPoint VertexX(surface,oldvert),VertexY(surface,oldvert),VertexZ(surface,oldvert), mesh,singlesurfaceentity vert[i2] = AddVertex(surf,TFormedX(),TFormedY(),TFormedZ(),VertexU(surface,oldvert),VertexV(surface,oldvert)) VertexColor surf,vert[i2],r,g,b,a Next AddTriangle(surf,vert[0],vert[1],vert[2]) Next End Function; create a quadFunction CreateQuad(r%=255,g%=255,b%=255,a#=1.0) Local mesh%,surf%,v1%,v2%,v3%,v4% mesh=CreateMesh() surf=CreateSurface(mesh) v1=AddVertex(surf,-1,1,0,1,0) v2=AddVertex(surf,1,1,0,0,0) v3=AddVertex(surf,-1,-1,0,1,1) v4=AddVertex(surf,1,-1,0,0,1) VertexColor surf,v1,r,g,b,a VertexColor surf,v3,r,g,b,a VertexColor surf,v2,r,g,b,a VertexColor surf,v4,r,g,b,a AddTriangle(surf,0,1,2) AddTriangle(surf,3,2,1) FlipMesh mesh Return mesh End Function
AppTitle "Infinite Starfield"Graphics3D 800,600,32,2Global SCALEX# = 1.0 ; starfield scale XGlobal SCALEY# = 1.0 ; starfield scale YGlobal SCALEZ# = 1.0 ; starfield scale ZGlobal FARDIST# = 1.0 ; fade out distanceGlobal DENSITY# = 20.0 ; 20% densityGlobal TurnSpeed# = 4.0 ; cam turn speedGlobal CameraSpeed# = 0.01 ; cam move speedGlobal WIDTH% = GraphicsWidth() ; grab screen widthGlobal HEIGHT% = GraphicsHeight() ; grab screen heightGlobal TIMER% = CreateTimer(60) ; timerGlobal MAXSTARS% = 25000Global visible%,stars%Global imagesize%=128Global image=CreateImage(imagesize,imagesize)Global buff=ImageBuffer(image)Type star Field col% Field scale# Field x#,y#,z# Field visible% End Type; cameraGlobal cam=CreateCamera()CameraRange cam,0.01,10PositionEntity cam,0,0,0; star quadGlobal star=CreateQuad()HideEntity star; starfield meshGlobal starfield=CreateMesh()Global surf=CreateSurface(starfield)EntityTexture starfield,CreateStarTexture()EntityFX starfield,1+2+32EntityBlend starfield,3; add stars to starfieldAddStars(MAXSTARS,0.0025,0.005)MoveMouse WIDTH/2,HEIGHT/2; main loopWhile Not KeyHit(1) ; camera movement Movement(cam) DENSITY=DENSITY+((MouseDown(1)-MouseDown(2))/2.0) If DENSITY>100 Then DENSITY=100 Else If DENSITY<0 Then DENSITY=0 ; update stars UpdateStarfield(cam,True) RenderWorld() WaitTimer TIMER DrawImage image,0,0 Color 255,255,255 Text 0,imagesize+ 0,"Density: "+DENSITY+"%" Text 0,imagesize+14,"Visible: "+visible Text 0,imagesize+28,"Stars: "+stars Flip 0 WendEnd; rebuild starfield meshFunction UpdateStarfield(parent%,fader%=False) Local s.star,px#,py#,pz#,d#,a#,rgb%,x%,y% Local cx#=EntityX(parent) Local cy#=EntityY(parent) Local cz#=EntityZ(parent) ClearSurface(surf) visible=0 stars=0 SetBuffer buff Color 0,0,0 Rect 0,0,imagesize,imagesize,1 SetBuffer BackBuffer() LockBuffer buff For s.star = Each star stars=stars+1 ; calc star position px=cx-sx py=cy-sy pz=cz-sz ; check if star must be moved If px<-SCALEX Then sx=sx-(SCALEX*2) : svisible=False : If Rnd(100)<DENSITY Then svisible=True If px>+SCALEX Then sx=sx+(SCALEX*2) : svisible=False : If Rnd(100)<DENSITY Then svisible=True If py<-SCALEY Then sy=sy-(SCALEY*2) : svisible=False : If Rnd(100)<DENSITY Then svisible=True If py>+SCALEY Then sy=sy+(SCALEY*2) : svisible=False : If Rnd(100)<DENSITY Then svisible=True If pz<-SCALEZ Then sz=sz-(SCALEZ*2) : svisible=False : If Rnd(100)<DENSITY Then svisible=True If pz>+SCALEZ Then sz=sz+(SCALEZ*2) : svisible=False : If Rnd(100)<DENSITY Then svisible=True If svisible Then ; reposition star PositionEntity star,sx,sy,sz x=Int(Normalize(px,-SCALEX,SCALEX,imagesize/2,-imagesize/2)) y=Int(Normalize(pz,-SCALEZ,SCALEZ,-imagesize/2,imagesize/2)) rgb=64*$10000+64*$100+64 ; get distance d=EntityDistance(star,cam) ; check if not to far away If d<FARDIST Then ; star is visible? If EntityInView(star,cam) Then rgb=255*$100 ; align star to cam PointEntity star,cam ScaleEntity star,sscale,sscale,sscale ; add alpha a=1.0 : If fader Then a=Normalize(d,FARDIST*0.5,FARDIST,1,0) ; add star To starfield again AddToSurface(star,surf,starfield,scol,scol,scol,a) visible=visible+1 EndIf EndIf WritePixelFast ((imagesize/2)+x) Mod imagesize,((imagesize/2)+y) Mod imagesize Mod imagesize,rgb,buff EndIf Next UnlockBuffer buff Return DENSITY End Function; add stars to starfield meshFunction AddStars(amount%=1,min#=0.01,max#=0.02) Local i%,s.star For i=1 To amount s.star = New star scol=Rand(64,255) sx=Rnd(-SCALEX,SCALEX) sy=Rnd(-SCALEY,SCALEY) sz=Rnd(-SCALEZ,SCALEZ) sscale=Rnd(min,max) If Distance3D(0,0,0,sx,sy,sz)<=FARDIST Then If Rand(100)<DENSITY Then svisible=True Else If Rand(100)<DENSITY Then svisible=True EndIf Next End Function; simple 3D distance calculationFunction Distance3D#(x1#,y1#,z1#,x2#,y2#,z2#) Local x#=x1-x2 Local y#=y1-y2 Local z#=z1-z2 Return Sqr(x*x+y*y+z*z) End Function; simple spaceship freeflightFunction Movement(cam%,sensitivity#=1.0) Local cx#,cz#,tx#,ty#,multi%=1 cx=(KeyDown(205)-KeyDown(203))*CameraSpeed cz=(KeyDown(200)-KeyDown(208))*CameraSpeed If KeyDown(42) Or KeyDown(54) Then multi=3 tx=Normalize(MouseX(),0,WIDTH , 1,-1) ty=Normalize(MouseY(),0,HEIGHT,-1, 1) If ty<0 Then ty=(Abs(ty)^sensitivity)*-1 Else ty=ty^sensitivity If tx<0 Then tx=(Abs(tx)^sensitivity)*-1 Else tx=tx^sensitivity TurnEntity cam,ty*TurnSpeed,tx*TurnSpeed,0 MoveEntity cam,cx*multi,0,cz*multi End Function; create a simple star textureFunction CreateStarTexture(size%=256,flags%=3) Local tex%=CreateTexture(size,size,flags) Local tb%=TextureBuffer(tex) Local i#,j%,col%,rgb% SetBuffer tb LockBuffer tb For j=0 To 255 col=255-j If col>255 Then col=255 rgb=col*$1000000+col*$10000+col*$100+col For i=0 To 360 Step 0.1 WritePixelFast (size/2)+(Sin(i)*(j*size/512)),(size/2)+(Cos(i)*(j*size/512)),rgb,tb Next Next UnlockBuffer tb SetBuffer BackBuffer() Return tex End Function; normalize a valueFunction Normalize#(value#=128.0,value_min#=0.0,value_max#=255.0,norm_min#=0.0,norm_max#=1.0) Return ((value-value_min)/(value_max-value_min))*(norm_max-norm_min)+norm_min End Function; add a mesh to another mesh's surfaceFunction AddToSurface(mesh,surf,singlesurfaceentity,r%,g%,b%,a#) Local vert%[2],vr%[2],vg%[2],vb%[2],va#[2] Local surface%,oldvert%,i%,i2% surface = GetSurface(mesh,1) For i = 0 To CountTriangles(surface)-1 For i2 = 0 To 2 oldvert = TriangleVertex(surface,i,i2) vr[i2]=r vg[i2]=g vb[i2]=b va[i2]=a TFormPoint VertexX(surface,oldvert),VertexY(surface,oldvert),VertexZ(surface,oldvert), mesh,singlesurfaceentity vert[i2] = AddVertex(surf,TFormedX(),TFormedY(),TFormedZ(),VertexU(surface,oldvert),VertexV(surface,oldvert)) VertexColor surf,vert[i2],r,g,b,a Next AddTriangle(surf,vert[0],vert[1],vert[2]) Next End Function; create a quadFunction CreateQuad(r%=255,g%=255,b%=255,a#=1.0) Local mesh%,surf%,v1%,v2%,v3%,v4% mesh=CreateMesh() surf=CreateSurface(mesh) v1=AddVertex(surf,-1,1,0,1,0) v2=AddVertex(surf,1,1,0,0,0) v3=AddVertex(surf,-1,-1,0,1,1) v4=AddVertex(surf,1,-1,0,0,1) VertexColor surf,v1,r,g,b,a VertexColor surf,v3,r,g,b,a VertexColor surf,v2,r,g,b,a VertexColor surf,v4,r,g,b,a AddTriangle(surf,0,1,2) AddTriangle(surf,3,2,1) FlipMesh mesh Return mesh End Function