hyperspace

Everything related to 3D programming
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

hyperspace

Post by DK_PETER »

Just a little quickie...Place everything inside the same folder.
Runs on everything from 5.4x and up...

Download these:
http://s000.tinyupload.com/?file_id=976 ... 2267849149
http://s000.tinyupload.com/?file_id=055 ... 9669258386
http://s000.tinyupload.com/?file_id=091 ... 4379384337

Save script: Filename: points.material

Code: Select all

material points
{
   receive_shadows off
   technique
   {
      pass
      {
	 point_sprites on
         point_size 80
         lighting off
         diffuse vertexcolour

       texture_unit
       {
         texture points.png
       }
      }
   }
}
Save the code:

Code: Select all

;Simple starfield hyperjump using a mesh mixed with particles---swoosh :-)
;By DK_PETER
UseJPEGImageDecoder()
UsePNGImageDecoder()
InitEngine3D()
InitSprite()
InitKeyboard()

Structure _Vector3D
  x.f
  y.f
  z.f
EndStructure

Structure _part
  id.i
  ma.i
  tx.i
  tx2.i
  ms.i
EndStructure
Global p._part
Global tunnel._part

Declare.i MoveVertices()
Declare.i CreateField()
Declare   Normalize(*V._Vector3d)
Declare.f RandomF(Min.f, Max.f, Res.i = 100000)
Declare.i CreateTubee(meshID.i, outerRadius.f, innerRadius.f, height.f, numSegBase.i=16, numSegHeight.i=1)

Global field.i, ret.i

ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "Starfield using a mesh", #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0))
Add3DArchive(".", #PB_3DArchive_FileSystem)
Parse3DScripts()
Fog($0, 1200, 600, 1000) ;Easy way to do a 'fade' - only useful in this example.
CreateCamera(0, 0, 0, 100, 100)

ret = CreateField()

Repeat
  
  Repeat
    ev = WindowEvent()
  Until  ev = 0
  
  ret = MoveVertices()
  
  RenderWorld()
  
  FlipBuffers()
  
  ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)

Procedure.i CreateField()
  field = CreateMesh(#PB_Any, #PB_Mesh_PointList, #PB_Mesh_Dynamic)
  For x = 0 To 2000
    MeshVertexPosition(RandomF(-500, 500), RandomF(-500, 500), RandomF(-1000, 0))
    MeshVertexColor(RGB(Random(255, 50), Random(255, 50), Random(255, 50)))
  Next x
  FinishMesh(#True)
  GetScriptMaterial(0, "points")
  MaterialBlendingMode(0, #PB_Material_Add)
  CreateEntity(0, MeshID(field), MaterialID(0), 0, 0, 0)
  p\id = CreateParticleEmitter(#PB_Any, 1000, 1000, 1, #PB_Particle_Point, 0, 0, -1000)
  p\tx = CreateTexture(#PB_Any, 20, 20)
  StartDrawing(TextureOutput(p\tx))
  DrawingMode(#PB_2DDrawing_Gradient)
  BackColor($FFFFFF) : FrontColor($0)
  CircularGradient(10, 10, 9)
  Circle(10, 10, 10)
  StopDrawing()
  p\ma = CreateMaterial(#PB_Any, TextureID(p\tx))
  MaterialBlendingMode(p\ma, #PB_Material_Add)
  ParticleMaterial(p\id, MaterialID(p\ma))
  ParticleEmitterDirection(p\id, 0, 0, 1)
  ParticleEmissionRate(p\id, 10000)
  ParticleTimeToLive(p\id, 1, 5)
  ParticleSize(p\id, 2, 2)
  CompilerIf #PB_Compiler_Version > 546
    ParticleEmitterAngle(p\id, 100)
    ParticleVelocity(p\id, #PB_Particle_MinimumVelocity, 50)
    ParticleVelocity(p\id, #PB_Particle_Velocity, 100)
    ParticleVelocity(p\id, #PB_Particle_MaximumVelocity, 200)
  CompilerElse
    ParticleVelocity(p\id, 50, 200)
  CompilerEndIf
  With tunnel
  CompilerIf #PB_Compiler_Version > 546
    \ms = CreateTube(#PB_Any, 100, 99.9, 1000) 
  CompilerElse
    \ms = CreateTubee(#PB_Any, 100, 99.9, 1000) 
  CompilerEndIf
    \tx = LoadTexture(#PB_Any, "bg.png")
    \tx2 = LoadTexture(#PB_Any, "bg2.png")
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    MaterialBlendingMode(\ma, #PB_Material_Add)
    AddMaterialLayer(\ma, TextureID(\tx2), #PB_Material_Add)
    MaterialCullingMode(\ma, #PB_Material_NoCulling)
    ScrollMaterial(\ma, 0.01, 0.1, #PB_Material_Animated,0)
    ScrollMaterial(\ma, -0.01, 0.1, #PB_Material_Animated,1)
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, -1800)
    ScaleEntity(\id, 2, 8, 1)
    RotateEntity(\id, 90, 0, 0)
  EndWith
EndProcedure

Procedure.i MoveVertices()
  Protected Dim md.PB_MeshVertex(0)
  GetMeshData(field,0, md(), #PB_Mesh_Vertex|#PB_Mesh_Color, 0, MeshVertexCount(field)-1)
  For x = 0 To 2000
    If Mod(x,2) = 0
      If md(x)\z + 0.4 > 0
        md(x)\x = RandomF(-500, 500)
        md(x)\z = -1000
        md(x)\Color = RGB(Random(255, 50), Random(255, 50), Random(255, 50))
      Else
        md(x)\z + 0.4
      EndIf
    Else
      If md(x)\z + 2 > 0
        md(x)\x = RandomF(-500,500)
        md(x)\z = -1000
        md(x)\Color = RGB(Random(255, 50), Random(255, 50), Random(255, 50))
      Else
        md(x)\z + 2
      EndIf
    EndIf
  Next x
  SetMeshData(field, 0, md(), #PB_Mesh_Vertex|#PB_Mesh_Color, 0, MeshVertexCount(field)-1)
  ProcedureReturn #True
  
EndProcedure

Procedure Normalize(*V._Vector3d)
  Define.f magSq, oneOverMag
  
  magSq = *V\x * *V\x + *V\y * *V\y + *V\z * *V\z
  If magsq > 0
    oneOverMag = 1.0 / Sqr(magSq)
    *V\x * oneOverMag
    *V\y * oneOverMag
    *V\z * oneOverMag
  EndIf
EndProcedure

Procedure.i CreateTubee(meshID.i, outerRadius.f, innerRadius.f, height.f, numSegBase.i=16, numSegHeight.i=1)
  
  Protected  Normal._Vector3d, returnMesh.i
  
  If meshID = -1
    returnMesh = CreateMesh(#PB_Any)
  Else
    returnMesh = meshID
    CreateMesh(returnMesh)
  EndIf
  
  If numSegBase < 1
    numSegBase = 1
  EndIf
  
  If numSegHeight < 1
    numSegHeight = 1
  EndIf
  
  deltaAngle.f = #PI*2 / numSegBase
  deltaHeight.f = height / numSegHeight
  height2.f = height / 2.0
  offset = 0
  
  For i = 0 To numSegHeight
    For j = 0 To numSegBase
      
      x0.f = outerRadius * Cos(j*deltaAngle)
      z0.f = outerRadius * Sin(j*deltaAngle)
      
      Normal\x = x0
      Normal\y = 0
      Normal\z = z0
      Normalize(@Normal)
      
      MeshVertexPosition(x0, i*deltaHeight-height2, z0)
      MeshVertexNormal(Normal\x, Normal\y, Normal\z)
      MeshVertexTextureCoordinate(j / numSegBase, i / numSegHeight)
      
      If  i <> numSegHeight
        
        MeshIndex(offset + numSegBase + 1)
        MeshIndex(offset)
        MeshIndex(offset + numSegBase)
        MeshIndex(offset + numSegBase + 1)
        MeshIndex(offset + 1)
        MeshIndex(offset)
      EndIf
      offset + 1
    Next
  Next   
  
  For i = 0 To numSegHeight
    For j = 0 To numSegBase
      
      x0.f = innerRadius * Cos(j*deltaAngle)
      z0.f = innerRadius * Sin(j*deltaAngle)
      
      Normal\x = x0
      Normal\y = 0
      Normal\z = z0
      Normalize(@Normal)
      
      MeshVertexPosition(x0, i*deltaHeight-height2, z0)
      MeshVertexNormal(Normal\x, Normal\y, Normal\z)
      MeshVertexTextureCoordinate(j / numSegBase, i / numSegHeight)
      
      If  i <> numSegHeight
        
        MeshIndex(offset + numSegBase + 1)
        MeshIndex(offset + numSegBase)
        MeshIndex(offset)
        MeshIndex(offset + numSegBase + 1)
        MeshIndex(offset)
        MeshIndex(offset + 1)
      EndIf
      offset + 1
    Next
  Next   
  
  ;low cap
  For j = 0 To numSegBase
    
    x0.f = innerRadius * Cos(j*deltaAngle)
    z0.f = innerRadius * Sin(j*deltaAngle)
    MeshVertexPosition(x0, -height2, z0)
    MeshVertexNormal(0, -1, 0)
    MeshVertexTextureCoordinate(j / numSegBase, 1)
    
    x0 = outerRadius * Cos(j*deltaAngle)
    z0 = outerRadius * Sin(j*deltaAngle)
    MeshVertexPosition(x0, -height2, z0)
    MeshVertexNormal(0, -1, 0)
    MeshVertexTextureCoordinate(j / numSegBase, 0)
    
    If j <> numSegBase
      
      MeshIndex(offset)
      MeshIndex(offset + 1)
      MeshIndex(offset + 3)
      MeshIndex(offset + 2)
      MeshIndex(offset)
      MeshIndex(offset + 3)
    EndIf
    offset + 2
  Next
  
  ;high cap
  For j = 0 To numSegBase
    
    x0.f = innerRadius * Cos(j*deltaAngle)
    z0.f = innerRadius * Sin(j*deltaAngle)
    MeshVertexPosition(x0, height2, z0)
    MeshVertexNormal(0, 1, 0)
    MeshVertexTextureCoordinate(j / numSegBase, 0)
    
    x0 = outerRadius * Cos(j*deltaAngle)
    z0 = outerRadius * Sin(j*deltaAngle)
    MeshVertexPosition(x0, height2, z0)
    MeshVertexNormal(0, 1, 0)
    MeshVertexTextureCoordinate(j / numSegBase, 1)
    
    If j <> numSegBase
      MeshIndex(offset + 1)
      MeshIndex(offset)
      MeshIndex(offset + 3)
      MeshIndex(offset)
      MeshIndex(offset + 2)
      MeshIndex(offset + 3)
    EndIf
    offset + 2
  Next
  FinishMesh(#True)
  ProcedureReturn returnMesh
EndProcedure   

Procedure.f RandomF(Min.f, Max.f, Res.i = 100000)
  ProcedureReturn (Min + (Max - Min) * Random(Res) / Res)
EndProcedure
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
BinoX
User
User
Posts: 46
Joined: Mon Jan 31, 2005 11:57 am

Re: hyperspace

Post by BinoX »

Very impressive :D

Never thought about doing it like that
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: hyperspace

Post by DK_PETER »

@Binox
Yes, It works very well...Unless you're using 'OpenGL' as subsystem, that is.
I don't know why, but when using opengl the stars (vertices) don't move towards you -
they are stationary...The rest works fine under OpenGL. It doesn't look too bad with
stationary stars, though. :-)
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Post Reply