flyplane.monkey2 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. Namespace myapp3d
  2. #Import "<std>"
  3. #Import "<mojo>"
  4. #Import "<mojo3d>"
  5. #Import "<mojo3d-vr>"
  6. #Import "assets/plane/"
  7. #Import "assets/water.jpg"
  8. #Import "assets/miramar-skybox.jpg"
  9. Using std..
  10. Using mojo..
  11. Using mojo3d..
  12. Using openvr..
  13. Class MyWindow Extends Window
  14. Field _renderer:VRRenderer
  15. Field _scene:Scene
  16. Field _camera:Camera
  17. Field _light:Light
  18. Field _water:Model
  19. Field _plane:Model
  20. Field _headpos:=AffineMat4f.Translation( 0,1.26,-.26 )
  21. Method New( title:String="Simple mojo3d app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )
  22. Super.New( title,width,height,flags )
  23. _renderer=New VRRenderer
  24. _renderer.OpenvrEventFilter+=OnOpenvrEvent
  25. Local state:=_renderer.GetControllerState( 0 )
  26. state.Changed+=Lambda()
  27. Print "ControllerState Changed"+
  28. ", ButtonPressedMask="+Hex(state.ButtonPressedMask)+
  29. ", ButtonTouchedMask="+Hex(state.ButtonTouchedMask)+
  30. ", Trackpad="+state.Trackpad+
  31. ", Trigger="+state.Trigger
  32. End
  33. _scene=New Scene
  34. 'Use more detailed CSM shadow split distances to compensate for the much nearer near clip plane.
  35. '
  36. _scene.CSMSplits=New Float[]( 2,4,16,256 )
  37. _scene.SkyTexture=Texture.Load( "asset::miramar-skybox.jpg",TextureFlags.FilterMipmap|TextureFlags.Cubemap )
  38. 'create light
  39. '
  40. _light=New Light
  41. _light.Rotate( 45,15,0 )
  42. ' _light.CastsShadow=true
  43. Local material:=New PbrMaterial( Color.Black )
  44. material.EmissiveFactor=New Color( 0,2,0 )
  45. 'create water material
  46. '
  47. Local waterMaterial:=New PbrMaterial
  48. waterMaterial.ScaleTextureMatrix( 100,100 )
  49. waterMaterial.ColorTexture=Texture.Load( "asset::water.jpg",TextureFlags.WrapST|TextureFlags.FilterMipmap )
  50. waterMaterial.ColorFactor=New Color( 0.05, 0.25, 0.3 )
  51. 'create water
  52. '
  53. _water=Model.CreateBox( New Boxf( -1000,-1,-1000,1000,0,1000 ),1,1,1,waterMaterial )
  54. 'create plane
  55. '
  56. _plane=Model.Load( "asset::plane.gltf" )
  57. _plane.Mesh.FitVertices( New Boxf( -10,-1.0,-10,10,1.85,9 ) )
  58. _plane.Move( 0,10,0 )
  59. New FlyBehaviour( _plane )
  60. 'create camera
  61. '
  62. _camera=New Camera( Self,_plane )
  63. _camera.Near=.1
  64. _camera.Far=100
  65. _camera.Move( 0,.5,.5 )
  66. SwapInterval=0
  67. End
  68. 'for hooking into low level openvr events
  69. '
  70. Method OnOpenvrEvent( event:VREvent_t Ptr )
  71. #rem
  72. 'Print "OpenvrEvent! type="+VRSystem().GetEventTypeNameFromEnum( Cast<EVREventType>( event->eventType ) )
  73. Select event->eventType
  74. Case VREvent_ButtonPress
  75. Print "Press:"+VRSystem().GetButtonIdNameFromEnum( Cast<EVRButtonId>( event->data.controller.button ) )
  76. Case VREvent_ButtonUnpress
  77. Print "Unpress:"+VRSystem().GetButtonIdNameFromEnum( Cast<EVRButtonId>( event->data.controller.button ) )
  78. Case VREvent_ButtonTouch
  79. Print "Touch:"+VRSystem().GetButtonIdNameFromEnum( Cast<EVRButtonId>( event->data.controller.button ) )
  80. Case VREvent_ButtonUntouch
  81. Print "Untouch:"+VRSystem().GetButtonIdNameFromEnum( Cast<EVRButtonId>( event->data.controller.button ) )
  82. End
  83. #end
  84. End
  85. Method OnRender( canvas:Canvas ) Override
  86. 'Esc to quit
  87. '
  88. If Keyboard.KeyHit( Key.Escape ) App.Terminate()
  89. RequestRender()
  90. 'need to call this for VRRenderer before renderer or you'll get an error.
  91. '
  92. _renderer.Update()
  93. If Keyboard.KeyHit( Key.Space ) _renderer.ResetSeatedPose()
  94. _camera.LocalMatrix=_headpos * _renderer.HeadMatrix
  95. _scene.Update()
  96. _water.Position=New Vec3f( Round(_camera.Position.x/1000)*1000,0,Round(_camera.Position.z/1000)*1000 )
  97. _scene.Render( canvas )
  98. 'upside down? I thought we were over that...
  99. canvas.DrawRect( 0,Height,Width,-Height,_renderer.LeftEyeImage )
  100. canvas.DrawText( "Width="+Width+", Height="+Height+", FPS="+App.FPS,0,0 )
  101. End
  102. End
  103. Function Main()
  104. New AppInstance
  105. New MyWindow
  106. App.Run()
  107. End