May 30, 2020, 03:39:14 AM

Author Topic: Strange Starfield Problem (for Kristian)  (Read 536 times)

Offline JBR

  • Jr. Member
  • **
  • Posts: 93
Strange Starfield Problem (for Kristian)
« on: November 18, 2019, 01:47:33 AM »
Hi,

Simplified your Elite code for my uses.

My method works perfectly with the copymesh system but when I change it to calculating each of the 5x5x5 manually from a 'central array' I get weirdness.

Try flying towards the spheres and after a while the stars stop showing, but if you move the mouse it shows the starfield. It does not matter how far into the spheres, moving the mouse shows the stars and then no shows the stars, etc.

Here is the code.

Code: [Select]
SuperStrict
 
Import "minib3d.bmx"

Global G_Width% = DesktopWidth() / 1.5
Global G_Depth% = DesktopHeight() / 1.5
 
Graphics3D G_Width%, G_Depth%, 32, 2
 
ClearTextureFilters()
 
Global STARS:Int = 256/4        ' number of stars per dimension
Global DIMENSION:Int = 1024

Global SPEED:Float = 1.0        ' overall move speed multiplicator
Global BRAKE:Float = 0.95       ' brake multiplicator

' ----------------------------------------------------------------------------
' Global variables
' ----------------------------------------------------------------------------
Global xspeed:Float, yspeed:Float, zspeed:Float, rspeed:Float
Global xadd:Float, yadd:Float, zadd:Float, radd:Float, tadd:Float
 
' ----------------------------------------------------------------------------
' Init scene
' ----------------------------------------------------------------------------

Local spheres:TEntity[100]

For Local i% = 0 To 100-1
spheres[i] = Createsphere(13)

entityfx spheres[i], 1+4

positionentity spheres[i], Rnd(-512,512), Rnd(-512,512), Rnd(500,20000)
scaleentity spheres[i],10,10,10

Local red# =  Rnd(128,255)
Local gre# =  Rnd(128,255)
Local blu# =  Rnd(128,255)

entitycolor spheres[i], red,gre,blu
Next


Global cam:TCamera = CreateCamera()

CameraRange cam, 1, 10000

PositionEntity cam, 0, 0, 0
 
' ----------------------------------------------------------------------------
' ----------------------------------------------------------------------------
Global G_Star_Mesh:TMesh = createMesh()
Global G_Star_Surf:TSurface = CreateSurface(G_Star_Mesh:TMesh)
Global G_Star_Vertex:Int
Global G_Star_Centre_Array_X#[ STARS ]
Global G_Star_Centre_Array_Y#[ STARS ]
Global G_Star_Centre_Array_Z#[ STARS ]

EntityFX G_Star_Mesh:TMesh, 1 + 2 + 4
EntityBlend G_Star_Mesh:TMesh, 3

For Local i%=0 To STARS-1
G_Star_Centre_Array_X#[ i% ] = Rnd(-DIMENSION/2,DIMENSION/2)
G_Star_Centre_Array_Y#[ i% ] = Rnd(-DIMENSION/2,DIMENSION/2)
G_Star_Centre_Array_Z#[ i% ] = Rnd(-DIMENSION/2,DIMENSION/2)
Next

For Local i% = 0 To STARS*125-1 ' 5*5*5 Create all the vertices &  triangles which are never deleted (change color/alpha)
Local V0:Int = AddVertex(G_Star_Surf:TSurface, 0, 0, 0)
Local V1:Int = AddVertex(G_Star_Surf:TSurface, 0, 0, 0)
Local V2:Int = AddVertex(G_Star_Surf:TSurface, 0, 0, 0)
Local V3:Int = AddVertex(G_Star_Surf:TSurface, 0, 0, 0)

    AddTriangle G_Star_Surf:TSurface, V0, V1, V2
    AddTriangle G_Star_Surf:TSurface, V0, V2, V3
Next












' fix for OpenB3D to enable Texture Blending and Alpha?
SetBlend ALPHABLEND
 
 
' Fix to really center the mouse (otherwise it "jumps" a little bit initially)
MoveMouse(GraphicsWidth() / 2, GraphicsHeight() / 2) ; Flip
MouseX()
MouseY()
MouseXSpeed()
MouseYSpeed()
Flip
 
Global time%

' ----------------------------------------------------------------------------
' Main Loop
' ----------------------------------------------------------------------------
While Not AppTerminate()

GCCollect()
time% = MilliSecs()

        If KeyHit(KEY_ESCAPE) Then End
 
        ' axis rotation
        Local pitch:Float = Normalize(MouseY(), 0, GraphicsHeight() - 1, -2, 2)
        Local yaw:Float = Normalize(MouseX(), 0, GraphicsWidth() - 1, 2, -2)
        Local roll:Int = KeyDown(KEY_Q) - KeyDown(KEY_E)
 
        ' XYZ movement
        Local xmove:Int = KeyDown(KEY_D) - KeyDown(KEY_A)
        Local ymove:Int = KeyDown(KEY_R) - KeyDown(KEY_F)
        Local zmove:Int = KeyDown(KEY_W) - KeyDown(KEY_S)
        Local turbo:Int = KeyDown(KEY_TAB)
 
        If xmove = 0 And xadd <> 0 Then xadd = 0 Else If xmove And xadd = 0 Then xadd = 0.001
        If ymove = 0 And yadd <> 0 Then yadd = 0 Else If ymove And yadd = 0 Then yadd = 0.001
        If zmove = 0 And zadd <> 0 Then zadd = 0 Else If zmove And zadd = 0 Then zadd = 0.001
        If roll = 0 And radd <> 0 Then radd = 0 Else If roll And radd = 0 Then radd = 0.01
        If turbo = 0 And tadd <> 0 Then tadd:*0.9 Else If turbo And tadd = 0 Then tadd = 0.1
 
        If tadd < 0.01 Then tadd = 0.0
 
        If Abs(xmove) Then xadd:+0.01
        If Abs(ymove) Then yadd:+0.01
        If Abs(zmove) Then zadd:+0.01
        If Abs(roll) Then radd:+0.025
        If Abs(turbo) Then tadd:+0.05
 
        xadd:*1.025
        yadd:*1.025
        zadd:*1.025
        radd:*1.025
        tadd:*1.025
 
        If xadd > 2.0 Then xadd = 2.0
        If yadd > 2.0 Then yadd = 2.0
        If zadd > 2.0 Then zadd = 2.0
        If radd > 1.5 Then radd = 1.5
        If tadd > 25.0 Then tadd = 25.0
 
        ' calculate movespeed/rollspeed
        If Abs(xmove) Then xspeed = xadd * SPEED * Sgn(xmove) * (1.0 + tadd)
        If Abs(ymove) Then yspeed = yadd * SPEED * Sgn(ymove) * (1.0 + tadd)
        If Abs(zmove) Then zspeed = zadd * SPEED * Sgn(zmove) * (1.0 + tadd)
        If Abs(roll) Then rspeed = radd * Sgn(roll)
       
        ' perform rotation and motion
        Turn(cam, pitch, yaw, rspeed)
        MoveEntity cam, xspeed, yspeed, zspeed




Local cam_xx# = entityx#(cam)
Local cam_yy# = entityy#(cam)
Local cam_zz# = entityz#(cam)

Local c_x% = Int(cam_xx#)
Local c_y% = Int(cam_yy#)
Local c_z% = Int(cam_zz#)

Local G_X_Shift% = (c_x% + DIMENSION/2) / DIMENSION
Local G_Y_Shift% = (c_y% + DIMENSION/2) / DIMENSION
Local G_Z_Shift% = (c_z% + DIMension/2) / DIMENSION

Local scale_x# = 1.0
Local scale_y# = 1.0
 

G_Star_Vertex% = 0

For Local x:Int = 0 To 4 Step 1
        For Local y:Int = 0 To 4 Step 1
For Local z:Int = 0 To 4 Step 1

Local add_x# = (G_X_Shift% + x - 2) * DIMENSION
Local add_y# = (G_Y_Shift% + y - 2) * DIMENSION
Local add_z# = (G_Z_Shift% + z - 2) * DIMENSION

For Local s% = 0 To STARS-1

Local sx# = G_Star_Centre_Array_X#[s%] + add_x#
Local sy# = G_Star_Centre_Array_Y#[s%] + add_y#
Local sz# = G_Star_Centre_Array_Z#[s%] + add_z#

Local distance# = Sqr( (cam_xx#-sx#)^2 + (cam_yy#-sy#)^2 + (cam_zz#-sz#)^2 )

If distance# < DIMENSION*1.5 Then ' we have a star

TFormVector scale_x#, 0, 0, cam, Null
        Local x1:Float = TFormedX()
        Local y1:Float = TFormedY()
              Local z1:Float = TFormedZ()
 
                TFormVector 0, scale_y#, 0, cam, Null
                Local x2:Float = TFormedX()
                Local y2:Float = TFormedY()
                Local z2:Float = TFormedZ()

VertexCoords G_Star_Surf:TSurface, G_Star_Vertex% + 0, sx - x1 - x2, sy - y1 - y2, sz - z1 - z2
VertexCoords G_Star_Surf:TSurface, G_Star_Vertex% + 1, sx - x1 + x2, sy - y1 + y2, sz - z1 + z2
VertexCoords G_Star_Surf:TSurface, G_Star_Vertex% + 2, sx + x1 + x2, sy + y1 + y2, sz + z1 + z2
VertexCoords G_Star_Surf:TSurface, G_Star_Vertex% + 3, sx + x1 - x2, sy + y1 - y2, sz + z1 - z2

VertexColor G_Star_Surf:TSurface, G_Star_Vertex% + 0, 255,255,255, 1.0

G_Star_Vertex% :+ 4

EndIf
Next
Next
Next
Next

For Local j% = G_Star_Vertex% To 8000-1 Step 4
VertexColor G_Star_Surf:TSurface, G_Star_Vertex% + 0, 255,255,255, 0.0
Next

renderworld


time% = MilliSecs()-time%
 
        ' fix for OpenB3D to have vertex colors enabled (otherwise its greyscale)
        Begin2D()
 
              DrawText("Star Cluster: " + STARS, 0, 15)

DrawText("Distances X: " + cam_xx#, 0, 30)
DrawText("Distances Y: " + cam_yy#, 0, 50)
DrawText("Distances Z: " + cam_zz#, 0, 70)

DrawText("TIME : " + time%, 0, 100)

DrawText("Stars : " + G_Star_Vertex%/4, 0,120)

DrawLine( 0,G_Depth%/2,G_Width,G_Depth/2)
DrawLine( G_Width%/2,0,G_Width%/2,G_Depth%)
 
        End2D()
 
        Flip True
 
        ' decrease speed
        xspeed:*BRAKE
        yspeed:*BRAKE
        zspeed:*BRAKE
        rspeed:*BRAKE
 
Wend
 
End
 
 
' ----------------------------------------------------------------------------
' Normalizes a value to given range
' ----------------------------------------------------------------------------
Function Normalize:Float(value:Float, vmin:Float, vmax:Float, nmin:Float, nmax:Float)
 
        Return((value - vmin) / (vmax - vmin)) * (nmax - nmin) + nmin
 
End Function
 
' ----------------------------------------------------------------------------
' Turn Entity using Quaternions
' ----------------------------------------------------------------------------
Function Turn(Ent:TEntity, X:Float = 0.0, Y:Float = 0.0, Z:Float = 0.0, Glob:Int = False)
 
        Local Pitch:Float = 0.0
        Local Yaw:Float = 0.0
        Local Roll:Float = 0.0
 
        Local Quat:TQuaternion = EulerToQuat(0.0, 0.0, 0.0)
        Local Turn_Quat:TQuaternion = EulerToQuat(0.0, 0.0, 0.0)
 
        If Glob = False
 
                Quat = EulerToQuat(EntityPitch(ent, True), EntityYaw(ent, True), EntityRoll(ent, True))
                Turn_Quat = EulerToQuat(X, Y, Z)
                Quat = MultiplyQuats(Quat, Turn_Quat)
                Quat = NormalizeQuat(Quat)
                QuatToEuler2(Quat.X, Quat.Y, Quat.Z, Quat.w, Pitch, Yaw, Roll)
                RotateEntity Ent, Pitch, Yaw, Roll
 
        Else
 
                RotateEntity Ent, EntityPitch(Ent) + X, EntityYaw(Ent) + Y, EntityRoll(Ent) + Z
 
        EndIf
 
End Function
 
' ----------------------------------------------------------------------------
' Euler to Quaternion
' ----------------------------------------------------------------------------
Function EulerToQuat:TQuaternion(pitch:Float, yaw:Float, roll:Float)
 
        Local cr:Float = Cos(-roll / 2.0)
        Local cp:Float = Cos(pitch / 2.0)
        Local cy:Float = Cos(yaw / 2.0)
        Local sr:Float = Sin(-roll / 2.0)
        Local sp:Float = Sin(pitch / 2.0)
        Local sy:Float = Sin(yaw / 2.0)
        Local cpcy:Float = cp * cy
        Local spsy:Float = sp * sy
        Local spcy:Float = sp * cy
        Local cpsy:Float = cp * sy
 
        Local q:TQuaternion = New TQuaternion
 
        q.w = cr * cpcy + sr * spsy
        q.x = sr * cpcy - cr * spsy
        q.y = cr * spcy + sr * cpsy
        q.z = cr * cpsy - sr * spcy
 
        Return q
 
End Function
 
' ----------------------------------------------------------------------------
' Quaternion to Euler
' ----------------------------------------------------------------------------
Function QuatToEuler2(x:Float, y:Float, z:Float, w:Float, pitch:Float Var, yaw:Float Var, roll:Float Var)
 
        Local QuatToEulerAccuracy:Double = 1.0 / 2 ^ 31
 
        Local sint:Float = (2.0 * w * y) - (2.0 * x * z)
        Local cost_temp:Float = 1.0 - (sint * sint)
        Local cost:Float
 
        If Abs(cost_temp) > QuatToEulerAccuracy
 
                cost = Sqr(cost_temp)
 
        Else
 
                cost = 0.0
 
        EndIf
 
        Local sinv:Float, cosv:Float, sinf:Float, cosf:Float
 
        If Abs(cost) > QuatToEulerAccuracy
 
                sinv = ((2.0 * y * z) + (2.0 * w * x)) / cost
                cosv = (1.0 - (2.0 * x * x) - (2.0 * y * y)) / cost
                sinf = ((2.0 * x * y) + (2.0 * w * z)) / cost
                cosf = (1.0 - (2.0 * y * y) - (2.0 * z * z)) / cost
 
        Else
 
                sinv = (2.0 * w * x) - (2.0 * y * z)
                cosv = 1.0 - (2.0 * x * x) - (2.0 * z * z)
                sinf = 0.0
                cosf = 1.0
 
        EndIf
 
        pitch = ATan2(sint, cost)
        yaw = ATan2(sinf, cosf)
        roll = -ATan2(sinv, cosv)
 
End Function
 
' ----------------------------------------------------------------------------
' Multiply Quaternion
' ----------------------------------------------------------------------------
Function MultiplyQuats:TQuaternion(q1:TQuaternion, q2:TQuaternion)
 
        Local q:TQuaternion = New TQuaternion
       
        q.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z
        q.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y
        q.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z
        q.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x
 
        Return q
 
End Function
 
' ----------------------------------------------------------------------------
' Normalize Quaternion
' ----------------------------------------------------------------------------
Function NormalizeQuat:TQuaternion(q:TQuaternion)
 
        Local uv:Float = Sqr(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z)
 
        q.w = q.w / uv
        q.x = q.x / uv
        q.y = q.y / uv
        q.z = q.z / uv
 
        Return q
 
End Function
 
' ------------------------------------------------------------------------------------------------
 
' ------------------------------------------------------------------------------------------------
 
 
' creates a texture from a pixmap
Function PixmapToTexture(pixmap:TPixmap, tex:TTexture)
 
        If PixmapFormat(pixmap) <> PF_RGBA8888 Then pixmap = ConvertPixmap(pixmap, PF_RGBA8888)
 
        ' OpenB3D only function to copy pixmap to texture
        'BufferToTex tex, PixmapPixelPtr(pixmap, 0, 0)
 
        ' MiniB3D workaround to copy pixmap to texture
        glBindTexture (GL_TEXTURE_2D, tex.gltex[0])
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, pixmap.width, pixmap.Height, GL_RGBA, GL_UNSIGNED_BYTE, PixmapPixelPtr(pixmap, 0, 0))
 
End Function
 
' --------------------------------------------------------------------------------
' Fixed BeginMax2D()
' --------------------------------------------------------------------------------
Function Begin2D()
 
        Local x:Int, y:Int, w:Int, h:Int
        GetViewport(x, y, w, h)
 
        'glPopClientAttrib()
        'glPopAttrib()
        glMatrixMode(GL_MODELVIEW)
        glPopMatrix()
        glMatrixMode(GL_PROJECTION)
        glPopMatrix()
        glMatrixMode(GL_TEXTURE)
        glPopMatrix()
        glMatrixMode(GL_COLOR)
        glPopMatrix()
       
        glDisable(GL_LIGHTING)
        glDisable(GL_DEPTH_TEST)
        glDisable(GL_SCISSOR_TEST)
        glDisable(GL_FOG)
        glDisable(GL_CULL_FACE)
 
        glMatrixMode GL_TEXTURE
        glLoadIdentity
 
        glMatrixMode GL_PROJECTION
        glLoadIdentity
        glOrtho 0, GraphicsWidth(), GraphicsHeight(), 0, -1, 1
 
        glMatrixMode GL_MODELVIEW
        glLoadIdentity
 
        SetViewport x, y, w, h
 
        Local MaxTex:Int
        glGetIntegerv(GL_MAX_TEXTURE_UNITS, Varptr(MaxTex))
 
        For Local Layer:Int = 0 Until MaxTex
 
                glActiveTexture(GL_TEXTURE0+Layer)
 
                glDisable(GL_TEXTURE_CUBE_MAP)
                glDisable(GL_TEXTURE_GEN_S)
                glDisable(GL_TEXTURE_GEN_T)
                glDisable(GL_TEXTURE_GEN_R)
 
                glDisable(GL_TEXTURE_2D)
 
        Next
 
        glActiveTexture(GL_TEXTURE0)
 
        glViewport(0, 0, GraphicsWidth(), GraphicsHeight())
        glScissor(0, 0, GraphicsWidth(), GraphicsHeight())
 
        glEnable GL_BLEND
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glEnable(GL_TEXTURE_2D)
 
End Function
 
' --------------------------------------------------------------------------------
' Fixed EndMax2D()
' --------------------------------------------------------------------------------
Function End2D()
 
        ' save the Max2D settings for later
        'glPushAttrib(GL_ALL_ATTRIB_BITS)
        'glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS)
        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glMatrixMode(GL_TEXTURE)
        glPushMatrix()
        glMatrixMode(GL_COLOR)
        glPushMatrix()
               
        glDisable(GL_TEXTURE_CUBE_MAP)
        glDisable(GL_TEXTURE_GEN_S)
        glDisable(GL_TEXTURE_GEN_T)
        glDisable(GL_TEXTURE_GEN_R)
 
        glDisable(GL_TEXTURE_2D)
        glDisable(GL_BLEND)
 
   ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' TGlobal.EnableStates()

'Function EnableStates()

glEnable(GL_LIGHTING)
    glEnable(GL_DEPTH_TEST)
glEnable(GL_FOG)
glEnable(GL_CULL_FACE)
glEnable(GL_SCISSOR_TEST)

glEnable(GL_NORMALIZE)

glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_COLOR_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)

'End Function




        'glDisable(GL_TEXTURE_2D)
 
        ' ' only needed for OpenB3D
        'TGlobal.alpha_enable[0] = 0    ' alpha blending was disabled by Max2d (GL_BLEND)
        'TGlobal.blend_mode[0] = 1      ' force alpha blending
        'TGlobal.fx1[0] = 0                     ' full bright/surface normals was enabled by EnableStates (GL_NORMAL_ARRAY)
        'TGlobal.fx2[0] = 1                     ' vertex colors was enabled by EnableStates (GL_COLOR_ARRAY)
 
        glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR)
        glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE)
 
        glClearDepth(1.0)
        glDepthFunc(GL_LEQUAL)
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
 
        glAlphaFunc(GL_GEQUAL, 0.5)
 
        ' ' only needed for OpenB3D
        'For Local cam:TCamera=EachIn TCamera.cam_list
               
                 ' active camera - was if cam.hide[0]=0
                 'If cam = TGlobal.camera_in_use
               
                        ' fog with Max2d fix
                '       cam.UpdateFog()
                '       Exit
                       
                'EndIf
               
        'Next
 
End Function



Offline Krischan

  • Full Member
  • ***
  • Posts: 211
    • Krischan's Homepage
Re: Strange Starfield Problem (for Kristian)
« Reply #1 on: January 02, 2020, 12:37:19 AM »
Hi JBR,

first sorry for the late answer, if I don't reply within a week you should write me a PN as I read mails more often than threads here.

Well I can't see ANY problem with your code. I've only changed the minib3d to module import (Import sidesign.minib3d) and updated the Z speed limits as it takes a *lot* of time to get from one end to the other. But nothing vanishes here.

Hint: do you use the latest miniB3D version with the small fixes? My module date is December 7, 2011, which is already quite old compared to OpenB3D, but seems to work here. So try this first as there could lie your problem.

Oh, and you really should switch over to OpenB3D as it's faster and still maintained.
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
My Blitzbasic Archive | Extrasolar Project | My Github projects

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal