public override void Draw(GameTime gameTime)
        {
            var count = BoxHandles.Count;

            GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            cubePrimitive.Effect.DiffuseColor = new Vector3(1f, 0f, 0f);
            ActiveBoxesWorld.ForEach(boxWorld => cubePrimitive.Draw(boxWorld, Camera.View, Camera.Projection));
            cubePrimitive.Effect.DiffuseColor = new Vector3(0.1f, 0.1f, 0.3f);
            InactiveBoxesWorld.ForEach(boxWorld => cubePrimitive.Draw(boxWorld, Camera.View, Camera.Projection));

            SpheresWorld.ForEach(sphereWorld => spherePrimitive.Draw(sphereWorld, Camera.View, Camera.Projection));

            base.Draw(gameTime);
        }
Пример #2
0
        /// <inheritdoc />
        public override void Draw(GameTime gameTime)
        {
            // Set the background color to black, and use the default depth configuration
            Game.Background = Color.Black;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;



            // We get the base transform for each mesh
            var modelMeshesBaseTransforms = new Matrix[Model.Bones.Count];

            Model.CopyAbsoluteBoneTransformsTo(modelMeshesBaseTransforms);
            foreach (var modelMesh in Model.Meshes)
            {
                // We set the main matrices for each mesh to draw
                var worldMatrix = modelMeshesBaseTransforms[modelMesh.ParentBone.Index];
                // World is used to transform from model space to world space
                Effect.Parameters["World"].SetValue(worldMatrix);
                // InverseTransposeWorld is used to rotate normals
                Effect.Parameters["InverseTransposeWorld"].SetValue(Matrix.Transpose(Matrix.Invert(worldMatrix)));
                // WorldViewProjection is used to transform from model space to clip space
                Effect.Parameters["WorldViewProjection"].SetValue(worldMatrix * Camera.View * Camera.Projection);

                // Once we set these matrices we draw
                modelMesh.Draw();
            }

            lightBox.Draw(LightBoxWorld, Camera.View, Camera.Projection);

            base.Draw(gameTime);
        }
        /// <inheritdoc />
        public override void Draw(GameTime gameTime)
        {
            Game.Background = Color.Black;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;



            var worldView = SphereWorld * Camera.View;

            SphereEffect.Parameters["matWorld"].SetValue(SphereWorld);
            SphereEffect.Parameters["matWorldViewProj"].SetValue(worldView * Camera.Projection);
            SphereEffect.Parameters["matInverseTransposeWorld"].SetValue(Matrix.Transpose(Matrix.Invert(SphereWorld)));

            Sphere.Meshes.FirstOrDefault().Draw();

            for (int index = 0; index < Lights.Count; index++)
            {
                LightBox.Effect.DiffuseColor = Lights[index].ShowColor;
                LightBox.Draw(Matrix.CreateTranslation(Lights[index].Position), Camera.View, Camera.Projection);
            }


            Game.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.Opaque, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise);
            Game.SpriteBatch.DrawString(SpriteFont, "Con la tecla 'J' se cambia el material", new Vector2(100, GraphicsDevice.Viewport.Height - 100), Color.White);
            Game.SpriteBatch.End();


            base.Draw(gameTime);
        }
Пример #4
0
        private void Red_Render(object sender, MerjTek.WpfIntegration.GraphicsDeviceEventArgs e)
        {
            e.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);

            // Compute some values for the cube rotation
            float time  = (float)watch.Elapsed.TotalSeconds;
            float yaw   = time * 0.4f;
            float pitch = time * 0.7f;
            float roll  = time * 1.1f;

            // Create the world-view-projection matrices for the cube and camera
            Microsoft.Xna.Framework.Matrix world      = Microsoft.Xna.Framework.Matrix.CreateFromYawPitchRoll(yaw, pitch, roll);
            Microsoft.Xna.Framework.Matrix view       = Microsoft.Xna.Framework.Matrix.CreateLookAt(new Vector3(0, 0, 2.5f), Vector3.Zero, Vector3.Up);
            Microsoft.Xna.Framework.Matrix projection = Microsoft.Xna.Framework.Matrix.CreatePerspectiveFieldOfView(1, e.GraphicsDevice.Viewport.AspectRatio, 1, 10);

            // Draw a cube
            Microsoft.Xna.Framework.Color color = Microsoft.Xna.Framework.Color.Green;
            cube.Draw(world, view, projection, color);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(XamarinBlue);
            GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;

            var cameraPosition   = new Vector3(3, 5, 20f);
            var aspect           = GraphicsDevice.Viewport.AspectRatio;
            var viewMatrix       = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
            var projectionMatrix = Matrix.CreatePerspectiveFieldOfView(1, aspect, 1, 100);

            // go through all bodies in the world.
            foreach (RigidBody body in world.RigidBodies)
            {
                // convert the current body.Shape to a BoxShape
                var shape = body.Shape as BoxShape;

                // the cube we want to draw is a unit cube. So by setting
                // the world matrix to a scale matrix with the shape.Size
                // we can draw different sized boxes.
                // => WorldMatrix = Scale * Orientation * Translation
                var scale       = Matrix.CreateScale(shape.Size.X, shape.Size.Y, shape.Size.Z);
                var orientation = new Matrix(
                    body.Orientation.M11, body.Orientation.M12, body.Orientation.M13, 0.0f,
                    body.Orientation.M21, body.Orientation.M22, body.Orientation.M23, 0.0f,
                    body.Orientation.M31, body.Orientation.M32, body.Orientation.M33, 0.0f,
                    0, 0, 0, 1);
                var translation = Matrix.CreateTranslation(body.Position.X, body.Position.Y, body.Position.Z);
                var worldMatrix = scale * orientation * translation;

                var color = (shape.Mass != 1.0f) ? XamarinPurple : XamarinGreen;

                // draw every cube
                renderCube.Draw(worldMatrix, viewMatrix, projectionMatrix, color);
            }

            // calculate the text position
            var titleSafeArea = GraphicsDevice.Viewport.TitleSafeArea;
            var width         = titleSafeArea.Width * 0.75f;
            var left          = (titleSafeArea.Width - width) / 2f + titleSafeArea.X;
            var top           = Math.Max(titleSafeArea.Y, left);
            var hudLocation   = new Vector2(left, top);
            // calculate the text size
            string text      = "tap to create more cubes";
            var    textWidth = font.MeasureString(text).X;
            var    textScale = width / textWidth;

            // draw the HUD
            spriteBatch.Begin(SpriteSortMode.Immediate);
            spriteBatch.DrawString(font, text, hudLocation, XamarinBlack, 0f, Vector2.Zero, new Vector2(textScale), SpriteEffects.None, 0f);
            spriteBatch.End();

            base.Draw(gameTime);
        }
Пример #6
0
        /// <summary>
        /// Invoked when our second control is ready to render.
        /// </summary>
        private void OnGraphicsControlDraw(object sender, DrawEventArgs e)
        {
            e.GraphicsDevice.Clear(Color.CornflowerBlue);

            // Create the world-view-projection matrices for the cube and camera
            var    position   = ((SceneViewModel)DataContext).Position;
            Matrix world      = Matrix.CreateFromYawPitchRoll(_yaw, _pitch, 0f) * Matrix.CreateTranslation(position);
            Matrix view       = Matrix.CreateLookAt(new Vector3(0, 0, 2.5f), Vector3.Zero, Vector3.Up);
            Matrix projection = Matrix.CreatePerspectiveFieldOfView(1, e.GraphicsDevice.Viewport.AspectRatio, 1, 10);

            // Draw a cube
            _cube.Draw(world, view, projection, Color.Red);
        }
        public override void Draw(GameTime gameTime)
        {
            Game.Background = Color.Black;

            var count = BoxHandles.Count;

            GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            cubePrimitive.Effect.DiffuseColor = new Vector3(1f, 0f, 0f);
            ActiveBoxesWorld.ForEach(boxWorld => cubePrimitive.Draw(boxWorld, Camera.View, Camera.Projection));
            cubePrimitive.Effect.DiffuseColor = new Vector3(0.1f, 0.1f, 0.3f);
            InactiveBoxesWorld.ForEach(boxWorld => cubePrimitive.Draw(boxWorld, Camera.View, Camera.Projection));

            SpheresWorld.ForEach(sphereWorld => spherePrimitive.Draw(sphereWorld, Camera.View, Camera.Projection));

            Game.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.Opaque, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise);
            Game.SpriteBatch.DrawString(SpriteFont, "Box handled: " + count + ".", new Vector2(GraphicsDevice.Viewport.Width - 400, 0), Color.White);
            Game.SpriteBatch.DrawString(SpriteFont, "Launch spheres with the 'Z' key.", new Vector2(GraphicsDevice.Viewport.Width - 400, 25), Color.White);
            Game.SpriteBatch.End();

            base.Draw(gameTime);
        }
Пример #8
0
        public override void Draw(GameTime gameTime, IDrawContext drawContext)
        {
            // todo: 先に LaF を描画。
            base.Draw(gameTime, drawContext);

            if (!CubeVisible)
            {
                return;
            }

            drawContext.Flush();

            // わざと Control の領域を越えるように調整。
            var renderSize = RenderSize;

            renderSize.Width  += 64;
            renderSize.Height += 64;

            using (var setViewport = drawContext.BeginViewport(new Rect(-32, -32, renderSize.Width, renderSize.Height)))
            {
                using (var setClip = drawContext.BeginNewClip(new Rect(-32, -32, renderSize.Width, renderSize.Height)))
                {
                    var effect = Screen.BasicEffect;

                    var cameraPosition = new Vector3(0, 0, 2.5f);
                    var aspect         = ((float)renderSize.Width / (float)renderSize.Height);

                    effect.World        = Orientation * Matrix.CreateScale(Scale);
                    effect.View         = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
                    effect.Projection   = Matrix.CreatePerspectiveFieldOfView(1, aspect, 0.1f, 10);
                    effect.DiffuseColor = ForegroundColor.ToVector3();
                    // どうもデフォルトで Specular が設定されているようだ。
                    effect.SpecularColor = Color.Black.ToVector3();
                    effect.Alpha         = 1;
                    effect.EnableDefaultLighting();
                    effect.VertexColorEnabled = true;

                    CubePrimitive.Draw(effect);
                }
            }
        }
Пример #9
0
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;

            Vector3 cameraPosition = new Vector3(3, 5, 20f);

            float aspect = GraphicsDevice.Viewport.AspectRatio;


            Matrix worldMatrix;
            Matrix viewMatrix       = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
            Matrix projectionMatrix = Matrix.CreatePerspectiveFieldOfView(1, aspect, 1, 100);

            // go through all bodies in the world.
            foreach (RigidBody body in world.RigidBodies)
            {
                // convert the current body.Shape to a BoxShape
                BoxShape shape = body.Shape as BoxShape;

                // the cube we want to draw is a unit cube. So by setting
                // the world matrix to a scale matrix with the shape.Size
                // we can draw different sized boxes.
                // => WorldMatrix = Scale * Orientation * Translation
                worldMatrix = Matrix.CreateScale(shape.Size.X, shape.Size.Y, shape.Size.Z) *

                              new Matrix(body.Orientation.M11, body.Orientation.M12, body.Orientation.M13, 0.0f,
                                         body.Orientation.M21, body.Orientation.M22, body.Orientation.M23, 0.0f,
                                         body.Orientation.M31, body.Orientation.M32, body.Orientation.M33, 0.0f,
                                         0, 0, 0, 1) *

                              Matrix.CreateTranslation(body.Position.X, body.Position.Y, body.Position.Z);

                // draw every cube
                Color color = (shape.Mass != 1.0f) ? Color.Gray : Color.Red;
                renderCube.Draw(worldMatrix, viewMatrix, projectionMatrix, color);
            }

            base.Draw(gameTime);
        }
Пример #10
0
 public override void Draw(GameTime gameTime, Matrix world, Camera camera, Vector3 light1Position, Vector3 light2Position)
 {
     world = GetWorldMatrix(world);
     base.Draw(gameTime, world, camera, light1Position, light2Position);
     _cuboid.Draw(world, Color, camera, light1Position, light2Position, Texture, Options);
 }
Пример #11
0
        /// <summary>
        ///     Draws the scene with shadows.
        /// </summary>
        private void DrawShadows()
        {
            #region Pass 1

            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            // Set the render target as our shadow map, we are drawing the depth into this texture
            GraphicsDevice.SetRenderTarget(ShadowMapRenderTarget);
            GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0);

            Effect.CurrentTechnique = Effect.Techniques["DepthPass"];

            // We get the base transform for each mesh
            var modelMeshesBaseTransforms = new Matrix[Model.Bones.Count];
            Model.CopyAbsoluteBoneTransformsTo(modelMeshesBaseTransforms);
            foreach (var modelMesh in Model.Meshes)
            {
                foreach (var part in modelMesh.MeshParts)
                {
                    part.Effect = Effect;
                }

                // We set the main matrices for each mesh to draw
                var worldMatrix = modelMeshesBaseTransforms[modelMesh.ParentBone.Index];

                // WorldViewProjection is used to transform from model space to clip space
                Effect.Parameters["WorldViewProjection"]
                .SetValue(worldMatrix * TargetLightCamera.View * TargetLightCamera.Projection);

                // Once we set these matrices we draw
                modelMesh.Draw();
            }

            #endregion

            #region Pass 2

            // Set the render target as null, we are drawing on the screen!
            GraphicsDevice.SetRenderTarget(null);
            GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.CornflowerBlue, 1f, 0);

            Effect.CurrentTechnique = Effect.Techniques["DrawShadowedPCF"];
            Effect.Parameters["baseTexture"].SetValue(BasicEffect.Texture);
            Effect.Parameters["shadowMap"].SetValue(ShadowMapRenderTarget);
            Effect.Parameters["lightPosition"].SetValue(LightPosition);
            Effect.Parameters["shadowMapSize"].SetValue(Vector2.One * ShadowmapSize);
            Effect.Parameters["LightViewProjection"].SetValue(TargetLightCamera.View * TargetLightCamera.Projection);
            foreach (var modelMesh in Model.Meshes)
            {
                foreach (var part in modelMesh.MeshParts)
                {
                    part.Effect = Effect;
                }

                // We set the main matrices for each mesh to draw
                var worldMatrix = modelMeshesBaseTransforms[modelMesh.ParentBone.Index];

                // WorldViewProjection is used to transform from model space to clip space
                Effect.Parameters["WorldViewProjection"].SetValue(worldMatrix * Camera.View * Camera.Projection);
                Effect.Parameters["World"].SetValue(worldMatrix);
                Effect.Parameters["InverseTransposeWorld"].SetValue(Matrix.Transpose(Matrix.Invert(worldMatrix)));

                // Once we set these matrices we draw
                modelMesh.Draw();
            }

            LightBox.Draw(Matrix.CreateTranslation(LightPosition), Camera.View, Camera.Projection);

            #endregion

            // Debug our shadowmap!
            // Show a simple quad with the texture
            DebugTextureEffect.Parameters["World"].SetValue(QuadShadowsWorld);
            DebugTextureEffect.Parameters["baseTexture"].SetValue(ShadowMapRenderTarget);
            FullScreenQuad.Draw(DebugTextureEffect);
        }
Пример #12
0
        protected override void LoadContent()
        {
            Simulation = Simulation.Create(BufferPool, new NarrowPhaseCallbacks(),
                                           new PoseIntegratorCallbacks(new NumericVector3(0, -10, 0)), new PositionFirstTimestepper());

            SphereHandles      = new List <BodyHandle>();
            ActiveBoxesWorld   = new List <Matrix>();
            InactiveBoxesWorld = new List <Matrix>();
            SpheresWorld       = new List <Matrix>();
            Random             = new Random();
            BoxHandles         = new List <BodyHandle>(800);
            Radii = new List <float>();

            var boxShape = new Box(1, 1, 1);

            boxShape.ComputeInertia(1, out var boxInertia);
            var       boxIndex     = Simulation.Shapes.Add(boxShape);
            const int pyramidCount = 40;

            for (var pyramidIndex = 0; pyramidIndex < pyramidCount; ++pyramidIndex)
            {
                const int rowCount = 20;
                for (var rowIndex = 0; rowIndex < rowCount; ++rowIndex)
                {
                    var columnCount = rowCount - rowIndex;
                    for (var columnIndex = 0; columnIndex < columnCount; ++columnIndex)
                    {
                        var bh = Simulation.Bodies.Add(BodyDescription.CreateDynamic(
                                                           new NumericVector3((-columnCount * 0.5f + columnIndex) * boxShape.Width,
                                                                              (rowIndex + 0.5f) * boxShape.Height,
                                                                              (pyramidIndex - pyramidCount * 0.5f) * (boxShape.Length + 4)),
                                                           boxInertia,
                                                           new CollidableDescription(boxIndex, 0.1f),
                                                           new BodyActivityDescription(0.01f)));
                        BoxHandles.Add(bh);
                    }
                }
            }

            //Prevent the boxes from falling into the void.
            Simulation.Statics.Add(new StaticDescription(new NumericVector3(0, -0.5f, 0),
                                                         new CollidableDescription(Simulation.Shapes.Add(new Box(2500, 1, 2500)), 0.1f)));

            cubePrimitive = new CubePrimitive(GraphicsDevice, 1f, Color.White);

            spherePrimitive = new SpherePrimitive(GraphicsDevice);

            var count = BoxHandles.Count;

            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            for (var index = 0; index < count; index++)
            {
                var bodyHandle    = BoxHandles[index];
                var bodyReference = Simulation.Bodies.GetBodyReference(bodyHandle);
                var position      = bodyReference.Pose.Position;
                var quaternion    = bodyReference.Pose.Orientation;
                var world         =
                    Matrix.CreateFromQuaternion(new Quaternion(quaternion.X, quaternion.Y, quaternion.Z,
                                                               quaternion.W)) * Matrix.CreateTranslation(new Vector3(position.X, position.Y, position.Z));

                cubePrimitive.Draw(world, Camera.View, Camera.Projection);
            }

            base.LoadContent();
        }