Ejemplo n.º 1
0
 private void FindSprites(DrawingContext context)
 {
     if (spriteQuery == null)
     {
         spriteQuery      = context.CreateSpatialQuery <ISprite>(sprite => sprite.Visible);
         findSpriteAction = new Action <ISprite>(AddSprite);
     }
     spriteQuery.FindAll(context.ViewFrustum, findSpriteAction);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Draws this pass using the specified drawing context.
        /// </summary>
        public override void Draw(DrawingContext context, IList <IDrawableObject> drawables)
        {
            var context3D = context as DrawingContext3D;

            Begin(context3D);
            {
                if (drawingPass == null)
                {
                    drawingPass = new DrawingPass();
                    drawingPass.MaterialUsage = MaterialUsage.DepthAndNormal;
                }
                drawingPass.Draw(context, drawables);
            }
            End(context3D);

            context.textures[TextureUsage.DepthBuffer]  = depthBuffer;
            context.textures[TextureUsage.NormalBuffer] = normalBuffer;

            if (lightQuery == null)
            {
                lightQuery     = context.CreateSpatialQuery <IDeferredLight>(null);
                deferredLights = new FastList <IDeferredLight>();
            }

            BeginLights(context3D);
            {
                lightQuery.FindAll(context.ViewFrustum, deferredLights);
                for (int i = 0; i < deferredLights.Count; ++i)
                {
                    DrawLight(context3D, deferredLights[i]);
                }
                deferredLights.Clear();
            }
            EndLights(context3D);

            context.textures[TextureUsage.LightBuffer] = lightBuffer;
        }
Ejemplo n.º 3
0
        public void Draw(DrawingContext context, Material material)
        {
            Vector3[] positions;
            ushort[]  indices;

            if (geometryQuery == null)
            {
                geometryQuery = context.CreateSpatialQuery <IGeometry>(null);
            }

            if (primitive == null)
            {
                primitive = new DynamicPrimitive(context.GraphicsDevice);
            }

            // Ray picking
            var mouseState = Mouse.GetState();
            var pickRay    = context.GraphicsDevice.Viewport.CreatePickRay(mouseState.X, mouseState.Y, context.View, context.Projection);

            geometryQuery.FindAll(ref pickRay, geometry =>
            {
                if (geometry.TryGetTriangles(out positions, out indices))
                {
                    var rayInGeometrySpace = pickRay.Transform(Matrix.Invert(geometry.Transform));
                    for (int i = 0; i < indices.Length; i += 3)
                    {
                        var triangle = new Triangle(positions[indices[i]], positions[indices[i + 1]], positions[indices[i + 2]]);
                        if (triangle.Intersects(rayInGeometrySpace).HasValue)
                        {
                            primitive.AddTriangle(triangle, geometry.Transform, new Color(255, 255, 0), 2);
                        }
                    }
                }
            });

            // Box picking
            var speed    = 0.01f;
            var keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Left))
            {
                pickBox.Min += Vector3.Left * speed;
                pickBox.Max += Vector3.Left * speed;
            }
            if (keyboard.IsKeyDown(Keys.Right))
            {
                pickBox.Min += Vector3.Right * speed;
                pickBox.Max += Vector3.Right * speed;
            }
            if (keyboard.IsKeyDown(Keys.Up))
            {
                pickBox.Min += Vector3.Forward * speed;
                pickBox.Max += Vector3.Forward * speed;
            }
            if (keyboard.IsKeyDown(Keys.Down))
            {
                pickBox.Min += Vector3.Backward * speed;
                pickBox.Max += Vector3.Backward * speed;
            }
            if (keyboard.IsKeyDown(Keys.PageUp))
            {
                pickBox.Min += Vector3.Up * speed;
                pickBox.Max += Vector3.Up * speed;
            }
            if (keyboard.IsKeyDown(Keys.PageDown))
            {
                pickBox.Min += Vector3.Down * speed;
                pickBox.Max += Vector3.Down * speed;
            }

            primitive.AddBox(pickBox, null, Color.Black, 2);

            geometryQuery.FindAll(ref pickBox, geometry =>
            {
                Matrix transform = geometry.Transform;
                if (geometry.TryGetTriangles(out positions, out indices))
                {
                    for (int i = 0; i < indices.Length; i += 3)
                    {
                        var triangle = new Triangle(positions[indices[i]], positions[indices[i + 1]], positions[indices[i + 2]]);

                        Vector3.Transform(ref triangle.V1, ref transform, out triangle.V1);
                        Vector3.Transform(ref triangle.V2, ref transform, out triangle.V2);
                        Vector3.Transform(ref triangle.V3, ref transform, out triangle.V3);

                        var count = triangle.Intersects(ref pickBox, intersections, 0);
                        if (count > 2)
                        {
                            intersections[count++] = intersections[0];
                            primitive.AddLine(intersections.Take(count), null, new Color(255, 0, 0), 1);
                        }
                    }
                }
            });


            // Box Frustum test
            primitive.AddFrustum(testFrustum, null, new Color(0, 128, 0) * 0.2f, 4);
            var intersectionCount = pickBox.Intersects(testFrustum, intersections, 0);

            if (intersectionCount > 2)
            {
                intersections[intersectionCount++] = intersections[0];
                primitive.AddLine(intersections.Take(intersectionCount), null, new Color(0, 0, 255), 4);
            }

            primitive.Draw(context, null);
            primitive.Clear();
        }