浏览代码

replaced delegate by a struct, to support seamless extensibility

vpenades 2 年之前
父节点
当前提交
8c941017fe

+ 5 - 3
demos/Demo4.VR/VRGameDemo.cs

@@ -134,15 +134,17 @@ namespace Primitives3D
 
                 var aspect = GraphicsDevice.Viewport.AspectRatio;
 
-                DrawScene(gameTime, leftView, (near, far) => Matrix.CreatePerspectiveFieldOfView(1, aspect, near, far));
+                var context = new XRSceneContext(leftView, (near, far) => Matrix.CreatePerspectiveFieldOfView(1, aspect, near, far));
+
+                DrawScene(gameTime, context);
             }
         }
 
-        protected override void DrawScene(GameTime gameTime, Matrix view, ProjectionDelegate projection)
+        protected override void DrawScene(GameTime gameTime, XRSceneContext sceneContext)
         {
             GraphicsDevice.Clear(Color.CornflowerBlue);
 
-            base.DrawScene(gameTime, view, projection);
+            base.DrawScene(gameTime, sceneContext);
         }
 
         #endregion

+ 4 - 4
demos/Demo4.VR/VRSceneDemo.cs

@@ -80,16 +80,16 @@ namespace Primitives3D
 
         #region draw
 
-        public override void Draw(GameTime gameTime, Matrix view, ProjectionDelegate projFunc)
+        public override void Draw(GameTime gameTime, XRSceneContext sceneContext)
         {
             var dc = new ModelDrawingContext(this.GraphicsDevice);
             
-            dc.SetCamera(Matrix.Invert(view));            
-            dc.SetProjectionMatrix(projFunc(0.1f, 100));
+            dc.SetCamera(Matrix.Invert(sceneContext.ViewMatrix));            
+            dc.SetProjectionMatrix(sceneContext.GetProjectionMatrix(0.1f, 100));
 
             dc.DrawSceneInstances(_LightsAndFog, _HouseView1, _HouseView2, _HouseView3, _HouseView4, _CharView1);
 
-            base.Draw(gameTime, view, projFunc);
+            base.Draw(gameTime, sceneContext);
         }
 
         #endregion

+ 5 - 3
demos/Demo4.VR/XRGame.cs

@@ -118,7 +118,9 @@ namespace Microsoft.Xna.Framework
                 if (eye == 0) leftView = view;
                 if (eye == 1) rightView = view;
 
-                DrawScene(gameTime, view, (near, far) => ovrDevice.CreateProjection(eye, near, far));
+                var context = new XRSceneContext(view, (near, far) => ovrDevice.CreateProjection(eye, near, far));
+
+                DrawScene(gameTime, context);
 
                 // Resolve eye rendertarget
                 GraphicsDevice.SetRenderTarget(null);
@@ -132,12 +134,12 @@ namespace Microsoft.Xna.Framework
             return;
         }        
 
-        protected virtual void DrawScene(GameTime gameTime, Matrix view, ProjectionDelegate projection)
+        protected virtual void DrawScene(GameTime gameTime, XRSceneContext sceneContext)
         {
             // prepare XR drawable components
             foreach(var component in this.Components.OfType<XRGameComponent>())
             {
-                component.SetEyeTransforms(view, projection);
+                component.SetSceneContext(sceneContext);
             }
 
             // draw any drawable components

+ 7 - 9
demos/Demo4.VR/XRGameComponent.cs

@@ -17,8 +17,8 @@ namespace Microsoft.Xna.Framework
         }
          
         public XRGame XRGame { get; }
-        private Matrix _EyeView;
-        private ProjectionDelegate _EyeProjection;        
+        
+        public XRSceneContext SceneContext { get; private set; }
 
         protected HandsState GetHandsState()
         {
@@ -28,23 +28,21 @@ namespace Microsoft.Xna.Framework
         /// <summary>
         /// This method must be called before calling <see cref="Draw(GameTime)"/>
         /// </summary>
-        /// <param name="view">the view matrix</param>
-        /// <param name="projFunction">the projection function</param>
-        internal void SetEyeTransforms(Matrix view, ProjectionDelegate projFunction)
+        /// <param name="context">the context containing scene transforms</param>        
+        internal void SetSceneContext(XRSceneContext context)
         {
-            _EyeView = view;
-            _EyeProjection = projFunction;
+            SceneContext = context;
         }
 
         [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
         public override void Draw(GameTime gameTime)
         {
-            Draw(gameTime, _EyeView, _EyeProjection);
+            Draw(gameTime, SceneContext);
 
             base.Draw(gameTime);
         }
 
-        public virtual void Draw(GameTime gameTime, Matrix view, ProjectionDelegate proj)
+        public virtual void Draw(GameTime gameTime, XRSceneContext sceneContext)
         {
 
         }

+ 26 - 0
demos/Demo4.VR/XRSceneContext.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Microsoft.Xna.Framework
+{
+    public readonly struct XRSceneContext
+    {
+        internal XRSceneContext(in Matrix vm, Func<float,float,Matrix> projFunc)
+        {
+            ViewMatrix = vm;
+            _ProjectionFunction= projFunc;
+        }
+
+        private readonly Func<float, float, Matrix> _ProjectionFunction;
+
+        public Matrix ViewMatrix { get; }
+
+        public Matrix GetProjectionMatrix(float near, float far)
+        {
+            return _ProjectionFunction(near, far);
+        }
+    }
+}