3d stars
Posted: Thu Aug 13, 2009 3:19 pm
Hi! I've just started with the demo version of PB and trying to learn something. Here's some 3d stars code i made and I need some help with it.
How do I calculate the colors correctly, like from 0-255 for each RGB?
Also is this a correct way of using structures?
How do I calculate the colors correctly, like from 0-255 for each RGB?
Also is this a correct way of using structures?
Code: Select all
InitSprite()
InitKeyboard()
#gfxw=640
#gfxh=480
#numstars=10000
col.b
Macro rnd1() ; returns a random float from 0.0 to 1.0
( Random(64325) * 0.00001555 )
EndMacro
Macro rnd2() ; returns a random float from -1.0 to 1.0
( 2.0 * ( rnd1() - 0.5 ) )
EndMacro
Structure star
x3d.f
y3d.f
z3d.f
x.f
y.f
speed.f
EndStructure
Dim s.star(#numstars)
For n=0 To #numstars
s(n)\x3d=rnd2()*20
s(n)\y3d=rnd2()*20
s(n)\z3d=rnd1()*20
s(n)\speed=rnd1()/20+0.05
Next
OpenScreen(#gfxw,#gfxh,32,"")
Repeat
ClearScreen(0)
StartDrawing(ScreenOutput())
For n=0 To #numstars
s(n)\z3d = s(n)\z3d - s(n)\speed
If s(n)\z3d<1
s(n)\z3d=20
EndIf
s(n)\x = s(n)\x3d / s(n)\z3d * 200 + #gfxw/2
s(n)\y = s(n)\y3d / s(n)\z3d * 200 + #gfxh/2
col=-(s(n)\z3d)*12
If s(n)\x >=0 And s(n)\x <=#gfxw-1 And s(n)\y>=0 And s(n)\y<=#gfxh-1 ; only plot if within limits
Plot(s(n)\x , s(n)\y , RGB(col,col,col))
EndIf
Next
StopDrawing()
FlipBuffers()
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
End