empty.monkey2 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. Namespace myapp
  2. #Import "<std>"
  3. #Import "<mojo>"
  4. #Import "<mojo3d>"
  5. #Import "<mojo3d-vr>"
  6. #Import "assets/vivecontroller.gltf"
  7. Using std..
  8. Using mojo..
  9. Using mojo3d..
  10. Class MyWindow Extends Window
  11. Field _renderer:VRRenderer
  12. Field _scene:Scene
  13. Field _camera:Camera
  14. Field _light:Light
  15. Field _ground:Model
  16. Field _baseStations:=New Model[2]
  17. Field _controllers:=New Model[2]
  18. Method New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )
  19. Super.New( title,width,height,flags )
  20. SwapInterval=0
  21. Print "GL_VERSION="+opengl.glGetString( opengl.GL_VERSION )
  22. 'Enables VR: must happen before Scene.GetCurrent()
  23. '
  24. _renderer=New VRRenderer
  25. 'Use more detailed CSM shadow split distances to compensate for the much nearer near clip plane.
  26. '
  27. _renderer.CSMSplits=New Float[]( 2,4,16,256 )
  28. _renderer.TrackingSpace=VRTrackingSpace.Standing
  29. 'Get current scene
  30. '
  31. _scene=Scene.GetCurrent()
  32. 'create camera
  33. '
  34. _camera=New Camera
  35. _camera.Viewport=Rect 'Inital viewport same as view.
  36. _camera.Near=.01 'Note: near clip plane 1 centimeter from camera so we can look at things nice and close up.
  37. _camera.Far=256 'only 10m visiblity.
  38. 'create light
  39. '
  40. _light=New Light
  41. _light.Rotate( 75,15,0 )
  42. _light.CastsShadow=True
  43. 'create ground
  44. '
  45. Local groundBox:=New Boxf( -50,-.1,-50,50,0,50 )
  46. Local groundMaterial:=New PbrMaterial( Color.Green,0,1 )
  47. _ground=Model.CreateBox( groundBox,1,1,1,groundMaterial )
  48. Local groundCollider:=New BoxCollider( _ground )
  49. Local groundBody:=New RigidBody( _ground )
  50. groundCollider.Box=groundBox
  51. groundBody.Mass=0
  52. _ground.CastsShadow=False
  53. 'create base stations
  54. '
  55. Local baseStationBox:=New Boxf( -.042,-.042,-.042,.042,.042,.042 )
  56. Local baseStationMaterial:=New PbrMaterial( Color.Aluminum,1.0,1.0 )
  57. Local baseStation:=Model.CreateBox( baseStationBox,1,1,1,baseStationMaterial )
  58. _baseStations[0]=baseStation
  59. _baseStations[1]=baseStation.Copy()
  60. 'create controllers
  61. Local controller:=Model.Load( "asset::vivecontroller.gltf" )
  62. controller.Mesh.TransformVertices( AffineMat4f.Rotation( -Pi/2,0,0 ) )
  63. _controllers[0]=controller
  64. _controllers[1]=controller.Copy()
  65. End
  66. Method OnRender( canvas:Canvas ) Override
  67. RequestRender()
  68. 'Update camera viewport, ie: handle window/view resizing
  69. '
  70. _camera.Viewport=Rect
  71. 'Update VR - update all VR matrices, controller states etc.
  72. '
  73. _renderer.Update()
  74. 'Update camera
  75. '
  76. _camera.Matrix=_renderer.HeadMatrix
  77. 'Update controllers
  78. '
  79. Local cmatrix:=_renderer.ControllerMatrices[0]
  80. Local adj:=AffineMat4f.Rotation( 0,0,-cmatrix.m.GetRoll() )
  81. cmatrix=cmatrix * adj
  82. 'Local yaw:=_controllers[0].Basis.GetYaw()
  83. 'Local pitch:=_controllers[0].Basis.GetPitch()
  84. 'Local adj:=AffineMat4f.Rotation( pitch,yaw,0 )
  85. _controllers[0].Matrix=cmatrix
  86. _controllers[1].Matrix=_renderer.ControllerMatrices[1]
  87. 'Hide controllers when vr 'paused' (eg: when system menu up)
  88. '
  89. _controllers[0].Visible=_renderer.Active
  90. _controllers[1].Visible=_renderer.Active
  91. 'Update base stations
  92. '
  93. _baseStations[0].Matrix=_renderer.BaseStationMatrices[0]
  94. _baseStations[1].Matrix=_renderer.BaseStationMatrices[1]
  95. 'Update scene unless VR paused
  96. '
  97. If _renderer.Active _scene.Update()
  98. 'Render scene!
  99. '
  100. _scene.Render( canvas )
  101. 'Render left eye view to canvas
  102. '
  103. canvas.DrawRect( 0,Height,Width,-Height,_renderer.LeftEyeImage )
  104. canvas.DrawText( "Camera.Position="+_camera.Position+", FPS="+App.FPS,0,0 )
  105. End
  106. End
  107. Function Main()
  108. New AppInstance
  109. New MyWindow
  110. App.Run()
  111. End