January 15, 2021, 06:31:19 PM

Author Topic: [bb] Infinite Starfield / Infinite Asteroid Belt by Krischan [ 1+ years ago ]  (Read 762 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Infinite Starfield / Infinite Asteroid Belt
Author : Krischan
Posted : 1+ years ago

Description : This creates an really infinite starfield or asteroid belt using a single surface entity. The stars are recycled when they are too far away from the camera. This can be used for 3D stars, space dust or even asteroid belts.

The main code shows the starfield which is cube-shaped in X/Y/Z dimensions while the asteroid belt is a starfield with very small Y dimensions, no recycling in Y direction and different Y random positioning. Movement with mouse/arrow keys, which is different in both demos.

Oh, and I am sure this can be improved!

Starfield


Asteroid Belt (see codebox below)


Code: [Select]
AppTitle "Infinite Asteroid Belt"

Graphics3D 800,600,32,2

Global SCALEX# = 2.0 ; starfield scale X
Global SCALEY# = 0.2 ; starfield scale Y
Global SCALEZ# = 2.0 ; starfield scale Z

Global TurnSpeed# = 4.0 ; cam turn speed
Global RollSpeed# = 0.5 ; cam roll speed
Global CameraSpeed# = 0.01 ; cam move speed

Global WIDTH% = GraphicsWidth() ; grab screen width
Global HEIGHT% = GraphicsHeight() ; grab screen height
Global TIMER% = CreateTimer(60) ; timer

Type star

Field col%
Field x#,y#,z#

End Type

; camera
Global cam=CreateCamera()
CameraRange cam,0.01,10
PositionEntity cam,0,0,0

; starfield mesh
Global starfield=CreateMesh()
Global surf=CreateSurface(starfield)
Global star=CreateQuad()
EntityTexture starfield,CreateStarTexture()
EntityFX starfield,1+2+32
EntityBlend starfield,3

; add stars to starfield
AddStars(20000,0.001,0.005)

MoveMouse WIDTH/2,HEIGHT/2

; main loop
While Not KeyHit(1)

; camera movement
Movement(cam)

; update stars
UpdateStarfield(cam,2,1)

RenderWorld()

WaitTimer TIMER

Flip 0

Wend

End

; create a simple star texture
Function 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 mesh
Function 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 mesh
Function 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 freeflight
Function 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 value
Function 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 surface
Function 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 quad
Function 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


Code :
Code: BlitzBasic
  1. AppTitle "Infinite Starfield"
  2.  
  3. Graphics3D 800,600,32,2
  4.  
  5. Global SCALEX#          = 2.0                           ; starfield scale X
  6. Global SCALEY#          = 2.0                           ; starfield scale Y
  7. Global SCALEZ#          = 2.0                           ; starfield scale Z
  8.  
  9. Global TurnSpeed#       = 4.0                           ; cam turn speed
  10. Global RollSpeed#       = 0.5                           ; cam roll speed
  11. Global CameraSpeed#     = 0.01                          ; cam move speed
  12.  
  13. Global WIDTH%           = GraphicsWidth()       ; grab screen width
  14. Global HEIGHT%          = GraphicsHeight()      ; grab screen height
  15. Global TIMER%           = CreateTimer(60)       ; timer
  16.  
  17. Type star
  18.        
  19.         Field col%
  20.         Field x#,y#,z#
  21.        
  22. End Type
  23.  
  24. ; camera
  25. Global cam=CreateCamera()
  26. CameraRange cam,0.01,10
  27. PositionEntity cam,0,0,0
  28.  
  29. ; starfield mesh
  30. Global starfield=CreateMesh()
  31. Global surf=CreateSurface(starfield)
  32. Global star=CreateQuad()
  33. EntityTexture starfield,CreateStarTexture()
  34. EntityFX starfield,1+2+32
  35. EntityBlend starfield,3
  36.  
  37. ; add stars to starfield
  38. AddStars(20000,0.001,0.005)
  39.  
  40. MoveMouse WIDTH/2,HEIGHT/2
  41.  
  42. ; main loop
  43. While Not KeyHit(1)
  44.        
  45.         ; camera movement
  46.         Movement(cam)
  47.        
  48.         ; update stars
  49.         UpdateStarfield(cam,2,1)
  50.  
  51.         RenderWorld()
  52.        
  53.         WaitTimer TIMER
  54.        
  55.         Flip 0
  56.        
  57. Wend
  58.  
  59. End
  60.  
  61. ; create a simple star texture
  62. Function CreateStarTexture(size%=256,flags%=3)
  63.        
  64.         Local tex%=CreateTexture(size,size,flags)
  65.         Local tb%=TextureBuffer(tex)
  66.        
  67.         Local i#,j%,col%,rgb%
  68.        
  69.         SetBuffer tb
  70.         LockBuffer tb
  71.        
  72.         For j=0 To 255
  73.                
  74.                 col=255-j
  75.                 If col>255 Then col=255
  76.                 rgb=col*$1000000+col*$10000+col*$100+col
  77.                
  78.                 For i=0 To 360 Step 0.1
  79.                        
  80.                         WritePixelFast (size/2)+(Sin(i)*(j*size/512)),(size/2)+(Cos(i)*(j*size/512)),rgb,tb
  81.                        
  82.                 Next
  83.                
  84.         Next
  85.        
  86.         UnlockBuffer tb
  87.         SetBuffer BackBuffer()
  88.        
  89.         Return tex
  90.        
  91. End Function
  92.  
  93. ; rebuild starfield mesh
  94. Function UpdateStarfield(parent%,maxdist#=2.0,fade%=False)
  95.        
  96.         Local s.star,px#,py#,pz#,d#,a#
  97.        
  98.         ClearSurface(surf)
  99.        
  100.         For s.star = Each star
  101.                
  102.                 ; calc star position
  103.                 px=EntityX(parent)-sx
  104.                 py=EntityY(parent)-sy
  105.                 pz=EntityZ(parent)-sz
  106.                
  107.                 ; check if star must be moved
  108.                 If px<-SCALEX Then sx=sx-(SCALEX*2)
  109.                 If px>+SCALEX Then sx=sx+(SCALEX*2)
  110.                 If py<-SCALEY Then sy=sy-(SCALEY*2)
  111.                 If py>+SCALEY Then sy=sy+(SCALEY*2)
  112.                 If pz<-SCALEZ Then sz=sz-(SCALEZ*2)
  113.                 If pz>+SCALEZ Then sz=sz+(SCALEZ*2)
  114.                
  115.                 ; reposition star
  116.                 PositionEntity star,sx,sy,sz
  117.                
  118.                 ; star is visible?
  119.                 If EntityInView(star,cam) Then
  120.                        
  121.                         ; get distance
  122.                         d=EntityDistance(star,cam)
  123.                        
  124.                         ; check if not to far away
  125.                         If d<maxdist Then
  126.                                
  127.                                 ; align star to cam
  128.                                 PointEntity star,cam
  129.                                
  130.                                 ; add alpha
  131.                                 a=1.0 : If fade Then a=Normalize(d,maxdist*0.5,maxdist,1,0)
  132.                                
  133.                                 ; add star to starfield again
  134.                                 AddToSurface(star,surf,starfield,scol,scol,scol,a)
  135.                                
  136.                         EndIf
  137.                        
  138.                 EndIf
  139.                
  140.         Next
  141.        
  142. End Function
  143.  
  144. ; add stars to starfield mesh
  145. Function AddStars(amount%=1,min#=0.01,max#=0.02)
  146.        
  147.         Local i%,s.star,size#
  148.        
  149.         For i=1 To amount
  150.                
  151.                 s.star = New star
  152.                
  153.                 size#=Rnd(min,max)
  154.                
  155.                 scol=Rand(64,255)
  156.                 sx=Rnd(-SCALEX,SCALEX)
  157.                 sy=Rnd(-SCALEY,SCALEY)
  158.                 sz=Rnd(-SCALEZ,SCALEZ)
  159.                
  160.                 PositionEntity(star,sx,sy,sz,1)
  161.                 ScaleEntity star,size,size,size
  162.                 AddToSurface(star,surf,starfield,255,255,255,1)
  163.                
  164.         Next
  165.        
  166. End Function
  167.  
  168. ; simple spaceship freeflight
  169. Function Movement(cam%,sensitivity#=1.0)
  170.        
  171.         Local roll#,cx#,cz#,tx#,ty#
  172.        
  173.         cx=(KeyDown(205)-KeyDown(203))*CameraSpeed
  174.         cz=(KeyDown(200)-KeyDown(208))*CameraSpeed
  175.         roll=(MouseDown(2)-MouseDown(1))*RollSpeed
  176.        
  177.         tx=Normalize(MouseX(),0,WIDTH , 1,-1)
  178.         ty=Normalize(MouseY(),0,HEIGHT,-1, 1)
  179.        
  180.         If ty<0 Then ty=(Abs(ty)^sensitivity)*-1 Else ty=ty^sensitivity
  181.         If tx<0 Then tx=(Abs(tx)^sensitivity)*-1 Else tx=tx^sensitivity
  182.        
  183.         TurnEntity cam,ty*TurnSpeed,tx*TurnSpeed,roll*TurnSpeed
  184.         MoveEntity cam,cx,0,cz
  185.        
  186. End Function
  187.  
  188. ; normalize a value
  189. Function Normalize#(value#=128.0,value_min#=0.0,value_max#=255.0,norm_min#=0.0,norm_max#=1.0)
  190.        
  191.         Return ((value-value_min)/(value_max-value_min))*(norm_max-norm_min)+norm_min
  192.        
  193. End Function
  194.  
  195. ; add a mesh to another mesh's surface
  196. Function AddToSurface(mesh,surf,singlesurfaceentity,r%,g%,b%,a#)
  197.        
  198.         Local vert%[2],vr%[2],vg%[2],vb%[2],va#[2]
  199.         Local surface%,oldvert%,i%,i2%
  200.        
  201.         surface = GetSurface(mesh,1)
  202.        
  203.         For i = 0 To CountTriangles(surface)-1
  204.                
  205.                 For i2 = 0 To 2
  206.                        
  207.                         oldvert = TriangleVertex(surface,i,i2)
  208.                        
  209.                         vr[i2]=r
  210.                         vg[i2]=g
  211.                         vb[i2]=b
  212.                         va[i2]=a
  213.                        
  214.                         TFormPoint VertexX(surface,oldvert),VertexY(surface,oldvert),VertexZ(surface,oldvert), mesh,singlesurfaceentity
  215.                         vert[i2] = AddVertex(surf,TFormedX(),TFormedY(),TFormedZ(),VertexU(surface,oldvert),VertexV(surface,oldvert))
  216.                         VertexColor surf,vert[i2],r,g,b,a
  217.                        
  218.                 Next
  219.                
  220.                 AddTriangle(surf,vert[0],vert[1],vert[2])
  221.                
  222.         Next
  223.        
  224. End Function
  225.  
  226. ; create a quad
  227. Function CreateQuad(r%=255,g%=255,b%=255,a#=1.0)
  228.        
  229.         Local mesh%,surf%,v1%,v2%,v3%,v4%
  230.        
  231.         mesh=CreateMesh()
  232.         surf=CreateSurface(mesh)
  233.        
  234.         v1=AddVertex(surf,-1,1,0,1,0)
  235.         v2=AddVertex(surf,1,1,0,0,0)
  236.         v3=AddVertex(surf,-1,-1,0,1,1)
  237.         v4=AddVertex(surf,1,-1,0,0,1)
  238.        
  239.         VertexColor surf,v1,r,g,b,a
  240.         VertexColor surf,v3,r,g,b,a
  241.         VertexColor surf,v2,r,g,b,a
  242.         VertexColor surf,v4,r,g,b,a
  243.        
  244.         AddTriangle(surf,0,1,2)
  245.         AddTriangle(surf,3,2,1)
  246.        
  247.         FlipMesh mesh
  248.        
  249.         Return mesh
  250.        
  251. End Function


Comments :


Krischan(Posted 1+ years ago)

 Here is an update, now you can set a density to simulate different amounts of objects, enter/leave a starfield/asteroid field or whatever. Use arrows and mouse to move, SHIFT=faster and the Mouse buttons increase/decrease the density in percent. Watch the radar to see how the density changes (only noticable while moving).EDIT: I am not very happy with the Createquad / AddToSurface solution here, any suggestions how to improve it are welcome...
Code: [Select]
AppTitle "Infinite Starfield"

Graphics3D 800,600,32,2

Global SCALEX# = 1.0 ; starfield scale X
Global SCALEY# = 1.0 ; starfield scale Y
Global SCALEZ# = 1.0 ; starfield scale Z
Global FARDIST# = 1.0 ; fade out distance
Global DENSITY# = 20.0 ; 20% density

Global TurnSpeed# = 4.0 ; cam turn speed
Global CameraSpeed# = 0.01 ; cam move speed

Global WIDTH% = GraphicsWidth() ; grab screen width
Global HEIGHT% = GraphicsHeight() ; grab screen height
Global TIMER% = CreateTimer(60) ; timer

Global MAXSTARS% = 25000

Global visible%,stars%

Global imagesize%=128
Global image=CreateImage(imagesize,imagesize)
Global buff=ImageBuffer(image)

Type star

Field col%
Field scale#
Field x#,y#,z#
Field visible%

End Type

; camera
Global cam=CreateCamera()
CameraRange cam,0.01,10
PositionEntity cam,0,0,0

; star quad
Global star=CreateQuad()
HideEntity star

; starfield mesh
Global starfield=CreateMesh()
Global surf=CreateSurface(starfield)
EntityTexture starfield,CreateStarTexture()
EntityFX starfield,1+2+32
EntityBlend starfield,3

; add stars to starfield
AddStars(MAXSTARS,0.0025,0.005)

MoveMouse WIDTH/2,HEIGHT/2

; main loop
While 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

Wend

End

; rebuild starfield mesh
Function 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 mesh
Function 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 calculation
Function 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 freeflight
Function 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 texture
Function 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 value
Function 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 surface
Function 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 quad
Function 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



ClayPigeon(Posted 1+ years ago)

 It MAV's when you move. I'm assuming this is probably because when you move, it tries to draw pixels outside the buffer.


Krischan(Posted 1+ years ago)

 Interesting, never happened here. I changed the writepixel line, should work now so please test it again.


puki(Posted 1+ years ago)

 It all works for me.  All very exciting.I found it a bit grey so I just applied 'super-lighting' which is where you nonce the lighting to 512 rather than the "Andy Pandy" 255 that most people use.


MErren(Posted 1+ years ago)

 very cool !! [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal