You intrigued me. And so, never having done something like this, I decided I had to come up with a solution.
Here:
set display mode 640,480,32,1
sync on
autocam off
hide mouse
backdrop on
color backdrop 0
gosub SetupStarfield
do
xrotate camera 0,wrapvalue(camera angle x(0)+mousemovey()/8.0)
yrotate camera 0,wrapvalue(camera angle y(0)+mousemovex()/8.0)
gosub UpdateStarfield
sync
loop
end
function Sign(a#)
if a#<0 then exitfunction -1
endfunction 1
SetupStarfield:
`Setup star table
StarCount=400
#constant MAX_DEPTH 434 `Approximately sqrt(250^2+250^2+250^2): The maximum distance a star can be from the origin
#constant X_COORD 1
#constant Y_COORD 2
#constant Z_COORD 3
#constant S_COLOR 4
dim Stars#(StarCount,4)
for a=1 to StarCount
`Give each star a random position in world space
for b=1 to 3
Stars#(a,b)=rnd(500)-250
next b
`Calculate the star's color based on its distance to the origin
b#=sqrt(Stars#(a,X_COORD)^2+Stars#(a,Y_COORD)+Stars#(a,Z_COORD))
b#=((MAX_DEPTH-b#)/MAX_DEPTH)*255
Stars#(a,S_COLOR)=b#
next a
`Prepare vectors for later use
a=make vector4(1)
a=make matrix4(2)
return
UpdateStarfield:
for a=1 to StarCount
`Set vector 1 to the star's position in world space
set vector4 1,Stars#(a,X_COORD)+camera position x(0),Stars#(a,Y_COORD)+camera position y(0),Stars#(a,Z_COORD)+camera position z(0),1
`Get the view matrix
view matrix4 2
`Transform the star's position in world space to view space
transform vector4 1,1,2
`Get the projection matrix
projection matrix4 2
`Project the star's position in view space to screen space
transform vector4 1,1,2
divide vector4 1,w vector4(1)
`Set the star's color and draw it
ink rgb(Stars#(a,S_COLOR),Stars#(a,S_COLOR),Stars#(a,S_COLOR)),0
dot (screen width()/2)+(screen width()/2)*x vector4(1),(screen height()/2)-(screen height()/2)*y vector4(1)
next a
`Reset the ink color
ink rgb(255,255,255),0
return
This should create a starry background that doesn't rotate with the camera. Use the mouse to look around.
I had to get into some interesting math to calculate the star positions in the sky. I left comments in the code, but if you have questions, feel free to ask.
Edit: Oops, I hadn't taken into account camera movement, so when I decided to try moving the camera, I got stars flying a little crazily around the screen. I've fixed the code above now.