示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ARPanel">Augmented Reality Panel</see> control.
        /// </summary>
        public ARPanel()
        {
            SizeChanged += panel_SizeChanged;
            Loaded      += panel_Loaded;
            Unloaded    += panel_Unloaded;
            view         = Matrix.CreateLookAt(new Vector3(0, 0, 1), Vector3.Zero, Vector3.Up);
            world        = Matrix.CreateWorld(Vector3.Zero, new Vector3(0, 0, -1), new Vector3(0, 1, 0));

            if (IsDesignMode)
            {
                _attitude = Matrix.Identity; //This will make control render design time looking horizontal north
            }
        }
        private void CurrentValueChanged(MotionReading reading)
        {
            if (viewport.Width == 0)
            {
                InitializeViewport();
            }


            // Get the RotationMatrix from the MotionReading.
            // Rotate it 90 degrees around the X axis to put it in xna coordinate system.
            Matrix attitude = Matrix.CreateRotationX(MathHelper.PiOver2) * reading.Attitude.RotationMatrix;

            // Loop through the points in the list
            for (int i = 0; i < points.Count; i++)
            {
                // Create a World matrix for the point.
                Matrix world = Matrix.CreateWorld(points[i], new Vector3(0, 0, 1), new Vector3(0, 1, 0));

                // Use Viewport.Project to project the point from 3D space into screen coordinates.
                Vector3 projected = viewport.Project(Vector3.Zero, projection, view, world * attitude);


                if (projected.Z > 1 || projected.Z < 0)
                {
                    // If the point is outside of this range, it is behind the camera.
                    // So hide the TextBlock for this point.
                    textBlocks[i].Visibility = Visibility.Collapsed;
                }
                else
                {
                    // Otherwise, show the TextBlock
                    textBlocks[i].Visibility = Visibility.Visible;

                    // Create a TranslateTransform to position the TextBlock.
                    // Offset by half of the TextBlock's RenderSize to center it on the point.
                    TranslateTransform tt = new TranslateTransform();
                    tt.X = projected.X - (textBlocks[i].RenderSize.Width / 2);
                    tt.Y = projected.Y - (textBlocks[i].RenderSize.Height / 2);
                    textBlocks[i].RenderTransform = tt;
                }
            }
        }
示例#3
0
 public Sun()
 {
     Transform = Matrix.CreateWorld(new Vector3(), Vector3.Forward, Vector3.Up);
 }
        MathboxRenderer(Machine machine, ScreenManager screenManager)
        {
            Machine       = machine;
            Memory        = Machine.Mathbox.Memory16;
            ScreenManager = screenManager;

            if (!(screenManager.Game is I_Robot.Game game))
            {
                throw new Exception("VideoInterpreter can only be used with I_Robot.Game");
            }
            Game = game;

            DisplayListManager = new DisplayList.Manager(this);
            Object             = new ObjectRenderer(this);
            Terrain            = new TerrainRenderer(this);

            // create our scene buffer
            // this buffer has a z-buffer
            SceneBuffer = new RenderTarget2D(
                Game.GraphicsDevice,
                Game.GraphicsDevice.Viewport.Width,
                Game.GraphicsDevice.Viewport.Height,
                false,
                Game.GraphicsDevice.PresentationParameters.BackBufferFormat,
                DepthFormat.None,
                8,
                RenderTargetUsage.DiscardContents);

            // create our two screen buffers
            // these buffers do not require depth sorting, they are simply raw bitmaps
            // however the contents need to be preserved when rendering context is reset
            for (int n = 0; n < ScreenBuffers.Length; n++)
            {
                ScreenBuffers[n] = new RenderTarget2D(
                    Game.GraphicsDevice,
                    Game.GraphicsDevice.Viewport.Width,
                    Game.GraphicsDevice.Viewport.Height,
                    false,
                    Game.GraphicsDevice.PresentationParameters.BackBufferFormat,
                    DepthFormat.None,
                    0,
                    RenderTargetUsage.PreserveContents);
            }

            camTarget   = new Vector3(0f, 0f, 0f);
            camPosition = new Vector3(0f, 0f, -1f);

            double scaleToMonitor = Emulation.Machine.MonitorAspectRatio / Emulation.Machine.NativeAspectRatio;

            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45f),
                (float)(Game.GraphicsDevice.Viewport.AspectRatio / scaleToMonitor),
                0.1f,
                65536f);

            // it's important to move the projection matrix down a bit, this matches what I, Robot seems to do
            projectionMatrix = projectionMatrix * Matrix.CreateTranslation(new Vector3(0, -0.1f, 0));
            viewMatrix       = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up);
            worldMatrix      = Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Down);

            basicEffect       = new BasicEffect(Game.GraphicsDevice);
            basicEffect.Alpha = 1f;
            basicEffect.VertexColorEnabled = true; // Want to see the colors of the vertices, this needs to be on
            // Lighting requires normal information which VertexPositionColor does not have
            // If you want to use lighting and VPC you need to create a custom def
            basicEffect.LightingEnabled = false;
        }