Esempio n. 1
0
        /// <summary>
        /// Draws the hull.
        /// </summary>
        /// <param name="drawBuffer">The Lightmap Draw Buffer</param>
        public void Draw(ILightmapDrawBuffer drawBuffer)
        {
            // Create the matrices (3X speed boost versus prior version)
            this.cos = (float)Math.Cos(this.angle);
            this.sin = (float)Math.Sin(this.angle);

            // vertexMatrix = scale * rotation * translation;
            this.vertexMatrix.M11 = this.scale.X * this.cos;
            this.vertexMatrix.M12 = this.scale.X * this.sin;
            this.vertexMatrix.M21 = this.scale.Y * -this.sin;
            this.vertexMatrix.M22 = this.scale.Y * this.cos;
            this.vertexMatrix.M41 = this.position.X;
            this.vertexMatrix.M42 = this.position.Y;

            // normalMatrix = scaleInv * rotation;
            this.normalMatrix.M11 = (1f / this.scale.X) * this.cos;
            this.normalMatrix.M12 = (1f / this.scale.X) * this.sin;
            this.normalMatrix.M21 = (1f / this.scale.Y) * -this.sin;
            this.normalMatrix.M22 = (1f / this.scale.Y) * this.cos;

            drawBuffer.SetStartVertex();

            // Add the vertices to the buffer

            var hullVerticesLength = this.Vertices.Length;
            for (var i = 0; i < hullVerticesLength; i++)
            {
                // Transform the vertices to world coordinates
                this.point = this.Vertices[i];

                Vector2.Transform(ref this.point.Position, ref this.vertexMatrix, out this.hullVertex.Position);
                Vector2.TransformNormal(ref this.point.Normal, ref this.normalMatrix, out this.hullVertex.Normal);

                this.hullVertex.Color = ShadowBlack;

                drawBuffer.AddVertex(this.hullVertex);
            }

            var hullIndicesLength = this.Indices.Length;
            for (var i = 0; i < hullIndicesLength; i++)
            {
                drawBuffer.AddIndex(this.Indices[i]);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LightmapGenerator"/> class.
 /// </summary>
 /// <param name="device">The device.</param>
 /// <param name="lightmapEffect">The lightmap effect.</param>
 /// <param name="drawBuffer">The draw buffer.</param>
 public LightmapGenerator(GraphicsDevice device, LightmapEffect lightmapEffect, ILightmapDrawBuffer drawBuffer)
 {
     this.device = device;
     this.lightmapEffect = lightmapEffect;
     this.drawBuffer = drawBuffer;
 }
Esempio n. 3
0
        /// <summary>
        /// Draws the light with shadows from the hulls
        /// </summary>
        /// <param name="lightmapEffect">The lightmap effect.</param>
        /// <param name="helper">The helper.</param>
        /// <param name="hulls">The hulls.</param>
        public void Draw(LightmapEffect lightmapEffect, ILightmapPass lightmapPass, ILightmapDrawBuffer helper, IEnumerable<IHull> hulls)
        {
            lightmapEffect.Effect.GraphicsDevice.ScissorRectangle = lightmapPass.GetScissor(this);

            // lightmapEffect.Effect.GraphicsDevice.ScissorRectangle = new Rectangle(0, 0, 200, 200);

            // 1) Clear hull buffers
            helper.Clear();

            // 2) Prepare to draw hulls
            foreach (var hull in hulls.Where(x => Vector2.DistanceSquared(this.Position, x.Position) < this.RadiusSquared + x.RadiusSquared))
            {
                hull.Draw(helper);
            }

            // 3) Set lightmapEffect stuff
            lightmapEffect.LightPosition = this.Position;
            lightmapEffect.LightTexture = this.Texture;
            lightmapEffect.LightInesityFactor = this.IntensityFactor;

            switch (this.ShadowType)
            {
                case ShadowType.Illuminated:
                    lightmapEffect.CurrentTechnique = lightmapEffect.IlluminatedShadowTechnique;
                    break;
                case ShadowType.Occluded:
                    lightmapEffect.CurrentTechnique = lightmapEffect.OccludedShadowTechnique;
                    break;
                default:
                    lightmapEffect.CurrentTechnique = lightmapEffect.SolidShadowTechnique;
                    break;
            }

            // 4) Draw hulls for each shadow pass
            foreach (var pass in lightmapEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                helper.Draw();
            }

            // 5) Draw light for each light pass
            lightmapEffect.CurrentTechnique = lightmapEffect.LightTechnique;

            foreach (var pass in lightmapEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                helper.DrawClippedFov(this.Position, this.Rotation, this.Radius * 2, this.Color, this.Fov);
            }

            // 6) Clear the target's alpha chanel
            lightmapEffect.CurrentTechnique = lightmapEffect.AlphaClearTechnique;

            //lightmapEffect.Effect.GraphicsDevice.ScissorRectangle = lightmapEffect.Effect.GraphicsDevice.Viewport.Bounds;

            foreach (var pass in lightmapEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                helper.DrawUnitQuad();
            }
        }