### Author Topic: Fake 3D Starfield  (Read 826 times)

#### CrystalNoir

• Mc. Print
• Posts: 28
##### Fake 3D Starfield
« on: 2011-Sep-03 »
Hi all

Here is a code that I wrote to simulate a 3D Starfield, in an amiga style. This code is inspired from an Blitzbasic code, I found severals years ago.

I wrote this code on Purebasic several years ago, and then I wanted to translate it for GLBasic

I provide you a zip file with the project and the source.

Have fun

Happy GLB !

PS : Sorry for my aproximative english.
« Last Edit: 2011-Sep-05 by CrystalNoir »

#### mentalthink

• Prof. Inline
• Posts: 2739
• Integrated Brain
##### Re: Fake 3D Starfield
« Reply #1 on: 2011-Oct-07 »
Very nive Cristal Noir, in the Samples forlder have a same effect, well your effect itÂ´s a little different. Very Cool!!!.

Thanks for sharing!!!.

#### BdR

• Dr. Type
• Posts: 303
##### Re: Fake 3D Starfield
« Reply #2 on: 2012-Apr-16 »
I just remembered I also had an old 3D starfield demo in Blitzbasic which I had already moved to GLBasic. I just zipped it and put it online here:

starfield_glb.zip (507kb)

It's based on a very good tutorial by Hugo Elias and Matt Fairclough

• HelpEditors
• Prof. Inline
• Posts: 5677
• Its me!
##### Re: Fake 3D Starfield
« Reply #3 on: 2012-Apr-16 »
My version (which I believe was also converted from BlitzMax) :

Code: GLBasic [Select]
// --------------------------------- //
// Project: Test1
// Start: Tuesday, April 10, 2012
// IDE Version: 11.001

TYPE T3DStar
xPos
yPos
zPos
zVel

FUNCTION Initialise%:
self.xPos=RND(1000)-500.0
self.yPos=RND(1000)-500.0
self.zPos=RND(900)+100
self.zVel=0.5+RND(5)
ENDFUNCTION

ENDTYPE

TYPE T3DStarProcess
screenWidth%;screenHeight%

stars[] AS T3DStar

FUNCTION Initialise%:screenWidth%,screenHeight%
LOCAL s AS T3DStar

self.screenWidth%=screenWidth%
self.screenHeight%=screenHeight%

DIM self.stars[500]

self.Reset()

RETURN TRUE
ENDFUNCTION

FUNCTION Finish%:
DIM self.stars[0]
ENDFUNCTION

FUNCTION Display%:
LOCAL s AS T3DStar
LOCAL c

ALPHAMODE -1.0
FOREACH s IN self.stars[]
c=((255-((s.zPos*255.0)*0.001))*s.zVel)*0.2
SETPIXEL ((s.xPos/s.zPos)*100.0)+(self.screenWidth%*0.5), _
((s.yPos/s.zPos)*100.0)+(self.screenHeight%*0.5), _
RGB(c,c,c)

NEXT
ENDFUNCTION

FUNCTION Process%:speed
LOCAL s AS T3DStar

FOREACH s IN self.stars[]
DEC s.zPos,s.zVel*speed
IF s.zPos<1.0
s.Initialise()
ENDIF
NEXT
ENDFUNCTION

FUNCTION Reset%:
LOCAL loop%

FOR loop%=0 TO BOUNDS(self.stars[],0)-1
self.stars[loop%].Initialise()
NEXT
ENDFUNCTION
ENDTYPE

#### I love my Brick

• Mr. Drawsprite
• Posts: 61
##### Re: Fake 3D Starfield
« Reply #4 on: 2012-Jun-15 »
Hi all

Here is a code that I wrote to simulate a 3D Starfield, in an amiga style. asic

Hi CrystalNoir.  Ive tweaked your code & added a fake Nebula in the background that swirls and grows.  Hope you like it.

Code: GLBasic [Select]
GETSCREENSIZE w,h

GRABSPRITE 0,0,0,w,h

// Variables Setup

GLOBAL speed# = 1.9
GLOBAL cx = 640/2
GLOBAL cy = 480/2

GLOBAL quit% = 0

// Types

TYPE Stars
x#
y#
z#
sx#
sy#
ENDTYPE

GLOBAL Tlist[] AS Stars

FOR i = 1 TO 400
GOSUB NewStar
NEXT

//////////////// LOOP /////////////////

WHILE TRUE

ROTOSPRITE 0,0,0,0.1

FOR a=0 TO 100

randx=RND(w) ; randy=RND(h)

SETPIXEL randx,randy,RGB(255,200,rnd(255))

randx=RND(w) ; randy=RND(h)

SETPIXEL randx,randy,RGB(0,0,RND(255))
NEXT

GRABSPRITE 0,0,0,w,h

GOSUB DrawTheStars

SHOWSCREEN

WEND

// ------------------------------------------------------------- //
// ---  NEWSTAR  ---
// ------------------------------------------------------------- //
SUB NewStar:
GLOBAL Item AS Stars

Item.z# = RND(55)+200
Item.x# = RND(2000)-1000
Item.y# = RND(2000)-1000

DIMPUSH Tlist[],Item

ENDSUB // NEWSTAR

// ------------------------------------------------------------- //
// ---  DRAWTHESTARS  ---
// ------------------------------------------------------------- //
SUB DrawTheStars:

FOREACH Item IN Tlist[]

IF Item.z# < speed# OR Item.sx# < 0 OR Item.sx# > 640 OR Item.sy# < 0 OR Item.sy# > 480
Item.z# = RND(55)+200
Item.x# = RND(2000)-1000
Item.y# = RND(2000)-1000
ENDIF

Item.z# = Item.z# - speed#
Item.sx# = ( Item.x#*spread) / (Item.z#) + cx#
Item.sy# = ( Item.y#*spread) / (4+Item.z#) + cy#

SETPIXEL Item.sx#, Item.sy#, RGB(255,255,255)
NEXT

ENDSUB // DRAWTHESTARS

I like things and do stuff.

#### CrystalNoir

• Mc. Print
• Posts: 28
##### Re: Fake 3D Starfield
« Reply #5 on: 2013-Jul-08 »
Hi !

Fantastic ! I didn't see answers ! sorry for that, but these tweaks are very cool thx !