Rotation around axis for parts of points mesh

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Rotation around axis for parts of points mesh

Post by applePi »

usually we use RotateEntity or RotateNode, but if we want to rotate parts of points mesh regions we should use a formula such as:
nx.f = Cos(-angle)*MeshData(i)\x - Sin(angle)*MeshData(i)\z
nz.f = Cos(-angle)*MeshData(i)\z + Sin(angle)*MeshData(i)\x


suitable for astronomy stars, galaxies, the universe, and saturn rings simulations
here is a 100000 points star with a radius 100 , have a blue core with a radius 70 . Press Up then Z to look from above the star.
the good thing is that it runs okay on my humble desktop computer with a low end Geforce 210
Image

Code: Select all

Enumeration
   #mainwin = 200
   #plane
   #ball
   #camera
 EndEnumeration

Global flag=1, stop
Global.f x,y,z,angle,angleBall, ang, rotVal = 0.6
Global zoom.f = 600
Global ro.f = -35
Global Dim MeshData.PB_MeshVertex(0)

ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "Up/Down to move Cam, Z/A for camera rotation, space stop red points rotation", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


Define.f KeyX, KeyY

Declare CreateMatrix()
Declare DrawMatrix()
Declare isolate()

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
  Add3DArchive("/", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-60, 0, 0, 0)
    WorldShadows(#PB_Shadow_Modulative, -1, RGB(100, 250, 100))
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    ;MaterialShadingMode(0, #PB_Material_Wireframe     )
    MaterialCullingMode(0, #PB_Material_NoCulling)
    CreatePlane(#plane, 20, 15, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(0))
MoveEntity(#plane,0,-400,0) 
ScaleEntity(#plane,50,100,100)
CreateSphere(#ball,11)
CreateEntity(#ball,MeshID(#ball), #PB_Material_None)
MoveEntity(#ball, 0,30.5,-4)
;;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
    CreateMatrix()
            
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, 0, 500, 600, #PB_Absolute)
    ;CameraFOV(0, 70)
    CameraBackColor(#camera, $000000)
    ;CameraLookAt(0,0,0,0)
    RotateCamera(#camera, -35, 0, 0)
        
    CreateLight(0, RGB(255,255,255), 10, 60, -10)
    AmbientColor(RGB(90, 90, 90))
    
    MaterialBlendingMode(0, #PB_Material_AlphaBlend)   ; allow alphablending
;MaterialBlendingMode(0, #PB_Material_Add )
;MaterialBlendingMode(0, #PB_Material_Color )
DisableMaterialLighting(0, 1)
       
    Repeat
      Event = WindowEvent()
                  
      ExamineKeyboard()
      
    If KeyboardPushed(#PB_Key_Up)
    zoom-10
    MoveCamera(#camera, 0,500, zoom, #PB_Absolute)
  ElseIf KeyboardPushed(#PB_Key_Down)
    zoom+10
    MoveCamera(#camera, 0,500, zoom,#PB_Absolute)
  ElseIf KeyboardPushed(#PB_Key_Z) 
    ro-1
    RotateCamera(#camera,ro,0,0)
  ElseIf KeyboardPushed(#PB_Key_A)  
    ro+1
    RotateCamera(#camera,ro,0,0)
  ElseIf KeyboardReleased(#PB_Key_Space) 
    If stop
      stop ! 1
      rotVal = 0.6
    Else
      rotVal = 0
      stop ! 1
      EndIf
    
  EndIf  
        
      rot.f+rotVal
      RotateEntity(0,0,rot,0)
      
      isolate() ; rotate blue points at the planet core
    
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End


Procedure DrawMatrix()
  For i=0 To 100000 
    x = Random(200)-100:y = Random(200)-100:z = Random(200)-100
  ; draw only the points inside a sphere with radius 100  
  If (Pow(x,2) + Pow(y,2) + Pow(z,2)) <= 10000
  If y< -80 And z< -80
    r=255:g=0:b=0
    ElseIf x < -80 And z< -80
      r=0:g=255:b=0
       ElseIf x< -80 And y< -80
         r=255:g=255:b=0
       Else
         r=255:g=0:b=0
         
   EndIf 
;(x - x0)^2 + (y - y0)^2 + (z - z0)^2 <= r^2  
;check if point inside a sphere:radius 70 plot as blue       
If (Pow(x,2) + Pow(y,2) + Pow(z,2)) <= 4900
  r=0:g=0:b=255
  
EndIf
MeshVertexPosition(x, y, z)
MeshVertexColor(RGBA(r,g,b,100)) 

EndIf

Next
    
EndProcedure  
Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_PointList, #True)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(0))
    
  CreateEntity(0, MeshID(0), #PB_Material_None)
  
  ScaleEntity(0, 2, 2, 2)
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex ,0, MeshVertexCount(0)-1)
  GetMeshData(0,0, MeshData(), #PB_Mesh_Color  ,0, MeshVertexCount(0)-1)
  
EndProcedure
    
 Procedure isolate() ; rotate blue core
      angle.f = 0.05
      angleBall.f + 1
  
  xb.f + Cos(Radian(angleBall)) * 100
  zb.f + Sin(Radian(angleBall)) * 100
  MoveEntity(#ball, xb, 70 ,zb,#PB_Absolute) 
  
  size = ArraySize(MeshData())
  For i=0 To size
     
     If MeshData(i)\Color = RGBA(255,0,0,100)
       
       nx.f = Cos(-angle)*MeshData(i)\x - Sin(angle)*MeshData(i)\z
       nz.f = Cos(-angle)*MeshData(i)\z + Sin(angle)*MeshData(i)\x
       MeshData(i)\x = nx
       MeshData(i)\z = nz
       
       
     EndIf
   Next
   SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
   
 EndProcedure