3d stars

Just starting out? Need help? Post your questions and find answers here.
neumanix
User
User
Posts: 11
Joined: Thu Aug 13, 2009 3:13 pm
Location: Finland

3d stars

Post by neumanix »

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?

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
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: 3d stars

Post by Demivec »

neumanix wrote: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?
Welcome. :)

Use this for the calculation:

Code: Select all

Co l = 255 - s(n)\z3d * 12.75
You are using structures correctly in your code.


Note: I had some problems while running the code. It wouldn't respond when I pressed Escape. I corrected this by moving ExamineKeyboard() to the beginning of the Repeat/Until loop instead of having it at the end.
neumanix
User
User
Posts: 11
Joined: Thu Aug 13, 2009 3:13 pm
Location: Finland

Re: 3d stars

Post by neumanix »

Thanks!
I accually figured it out myself.
Still there is sort of a twinkle in the ones that come almost straight at you, but hey... they are stars aren't they :)

I also changed the col variable to float type. For some reason the stars turned yellow when it was of byte type.

I find it strange that PB doesn't have a built in floating point random generator. I found the macros on the web somewhere.

Here's the updated code:

Code: Select all

InitSprite()

#gfxw=640
#gfxh=480
#numstars=10000
col.f

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()*10
  s(n)\y3d=rnd2()*10
  s(n)\z3d=rnd1()*20+1
  s(n)\speed=rnd1()/100+0.02
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<=0
    s(n)\z3d=20
    s(n)\x3d=rnd2()*10
    s(n)\y3d=rnd2()*10
  EndIf
  s(n)\x = s(n)\x3d / s(n)\z3d * 100 + #gfxw/2
  s(n)\y = s(n)\y3d / s(n)\z3d * 100 + #gfxh/2
  col=255/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(#PB_Screen_WaitSynchronization)
Until GetAsyncKeyState_(27)
End
Post Reply