Author |
Topic: Yet another starfield (Read 1557 times) |
|
David Williams
Developer
member is offline

meh

Gender: 
Posts: 452
|
 |
Re: Yet another starfield
« Reply #8 on: Apr 28th, 2015, 9:30pm » |
|
Code: REM 3D Starfield II
*FLOAT 64
MODE 8
OFF
N% = 1000
DIM X%(N%-1), Y%(N%-1), Z%(N%-1)
boxSz% = 40000
boxSz_2% = boxSz% DIV 2
maxZ_1 = 1 / boxSz%
FOR I% = 0 TO N%-1
X%(I%) = RND( boxSz% ) - boxSz_2% - 1
Y%(I%) = RND( boxSz% ) - boxSz_2% - 1
Z%(I%) = boxSz_2% * (1 - I%/(N%-1))
NEXT I%
REM Division table needed for ARM BBC BASIC version
REM (using this is probably slower for BB4W)
DIM divTbl( boxSz% )
divTbl(0) = 0
FOR I% = 1 TO boxSz%
divTbl(I%) = 1/I%
NEXT I%
ORIGIN 640, 512
GCOL 1
TIME = 0
*REFRESH OFF
REPEAT
CLS
FOR J% = 1 TO 5
T% = TIME
X% = boxSz_2% * SIN(2*PI*T%/1700)*SIN(4*PI*T%/3000 + 1.5)*SIN(9*PI*T%/9022 - 2.3)
Y% = boxSz_2% * COS(3*PI*T%/1830)*COS(3*PI*T%/3400 + 1.2)*SIN(5*PI*T%/9812 - 3.1)
Z% = boxSz_2% * SIN(2*PI*T%/1455)*SIN(6*PI*T%/3702 - 2.1)*SIN(3*PI*T%/10800 + 1.2)
FOR I% = 0 TO N%-1
dZ% = Z%(I%) - Z%
IF dZ% > 100 THEN
z = 1000 * divTbl(dZ%)
x% = (X%(I%) - X%) * z
IF ABS(x%) < 640 THEN
y% = (Y%(I%) - Y%) * z
IF ABS(y%) < 512 THEN
i = 1 - dZ%*maxZ_1
col% = 256*i*i
COLOUR 1, col%, col%, col%
CIRCLE FILL 2*x%, 2*y%, 12
ENDIF
ENDIF
ENDIF
NEXT I%
NEXT J%
*REFRESH
SYS "Sleep", 5
UNTIL FALSE
|
|
Logged
|
|
|
|
rtr2
Guest
|
 |
Re: Yet another starfield
« Reply #9 on: Apr 30th, 2015, 09:08am » |
|
on Apr 28th, 2015, 9:30pm, David Williams wrote: REM Division table needed for ARM BBC BASIC version REM (using this is probably slower for BB4W) |
|
I tried to benchmark that:
Code: DIM divTbl(40000)
FOR I% = 1 TO 40000
divTbl(I%) = 1/I%
NEXT I%
TIME = 0
FOR J%=1 TO 100 : FOR I%=1 TO 40000 : A = divTbl(I%) : NEXT : NEXT
PRINT "Using lookup table: ";TIME
TIME = 0
FOR J%=1 TO 100 : FOR I%=1 TO 40000 : A = 1/I% : NEXT : NEXT
PRINT "Using division : ";TIME Which gave (using BB4W v6.00a):
Code:Using lookup table: 311
Using division : 270 So although the table is a little slower, there's not much in it.
Richard.
|
|
Logged
|
|
|
|
David Williams
Developer
member is offline

meh

Gender: 
Posts: 452
|
 |
Re: Yet another starfield
« Reply #10 on: Apr 30th, 2015, 4:34pm » |
|
on Apr 30th, 2015, 09:08am, g4bau wrote:Which gave (using BB4W v6.00a):
Code:Using lookup table: 311
Using division : 270 So although the table is a little slower, there's not much in it. |
|
Thanks.
Going by ARM BBC BASIC on a Raspberry Pi 2, using a division table is, at best, about 34% faster (which surprised me - I thought it would be faster than that - perhaps upwards of 50%). At worst, if reading essentially random entries (5-byte floats) from a large division table (too big to fit in the CPU's L1 (or L2?) cache), it's only 1 or 2% faster. So, in real-world use I'm guessing around 20 to 25% faster on average.
For really speed-critical stuff, I've found that a division table of fixed-point integers to be worthwhile (with ARM BBC BASIC), when accuracy isn't too important.
David. --
|
|
|
|
|