Exemplo n.º 1
0
        private void btnGpuCalculate_Click(object sender, EventArgs e)
        {
            if (!(Selection1.HasValue && (Selection2.Count > 0 || cbGpuSelfShadow.Checked)))
            {
                return;
            }

            var processor = new GPUProcessor {
                Terrain = Terrain
            };

            var useCache = cbUseCache.Checked;

            var target = GetPatch(Selection1.Value);

            ShadowFromTo selfShadow = null;

            if (cbGpuSelfShadow.Checked)
            {
                var to_key = target.Id;
                var path   = ShadowFromTo.GenerateSelfShadowPath(to_key);
                if (useCache && File.Exists(path))
                {
                    selfShadow = ShadowFromTo.ReadFrom(path);
                }
                else
                {
                    target.InitializeHorizons();
                    if (false)
                    {
                        target.FillPointsAndMatrices(Terrain);
                        target.ParallelAddNearHorizon(Terrain);
                    }
                    else
                    {
                        target.FillPoints(Terrain);
                        target.FillMatricesRelativeToPoint(Terrain, target.Points[0][0]);

                        // GPU
                        processor.AddNearHorizon(target);
                    }

                    var self_shadow = new ShadowFromTo(to_key, to_key);
                    self_shadow.CopyFrom(target.Horizons);
                    _selfShadowDictionary[to_key] = self_shadow;
                }
            }
            else
            {
                target.InitializeHorizons();
            }

            if (selfShadow != null)
            {
                for (var line = 0; line < TerrainPatch.DefaultSize; line++)
                {
                    for (var sample = 0; sample < TerrainPatch.DefaultSize; sample++)
                    {
                        target.Horizons[line][sample].Merge(selfShadow.Horizons[line][sample]);
                    }
                }
            }

            // Now, for the main show
            var casters = Selection2.Select(ToGlobalKey).Select(TerrainPatch.FromId).ToList();

            processor.UpdateHorizons(target, casters);

            for (var line = 0; line < TerrainPatch.DefaultSize; line++)
            {
                for (var sample = 0; sample < TerrainPatch.DefaultSize; sample++)
                {
                    target.Horizons[line][sample].ConvertSlopeToDegrees();
                }
            }

            RenderMode = PatchRenderMode.AzEl;
            Invalidate();
        }
Exemplo n.º 2
0
        private void btnShadow2To1_Click(object sender, EventArgs e)
        {
            if (!(Selection1.HasValue && (Selection2.Count > 0 || cbSelfShadow.Checked)))
            {
                return;
            }

            var useCache = cbUseCache.Checked;

            var to_patch = GetPatch(Selection1.Value);
            var to_key   = to_patch.Id;

            foreach (var local_from_key in Selection2)
            {
                var from_key = ToGlobalKey(local_from_key);
                var label    = new FromToLabel(from_key.X, from_key.Y, to_key.X, to_key.Y);
                if (!useCache || !_fromToDictionary.ContainsKey(label))
                {
                    ShadowFromTo fromto = null;
                    if (useCache)
                    {
                        string path = ShadowFromTo.GenerateDefaultPath(from_key.X, from_key.Y, to_key.X, to_key.Y);
                        if (File.Exists(path))
                        {
                            fromto = ShadowFromTo.ReadFrom(path);
                            _fromToDictionary[label] = fromto;
                        }
                    }
                    if (fromto != null)
                    {
                        continue;
                    }
                    var s2 = GetPatch(local_from_key);
                    to_patch.FillPointsAndMatrices(Terrain);
                    s2.FillPoints(Terrain);
                    to_patch.InitializeHorizons();
                    to_patch.UpdateHorizon(s2);
                    fromto                   = new ShadowFromTo(from_key, to_key);
                    fromto.Horizons          = to_patch.Horizons;
                    _fromToDictionary[label] = fromto;
                    to_patch.InitializeHorizons();
                }
            }

            ShadowFromTo selfShadow = null;

            if (cbSelfShadow.Checked)
            {
                var path = ShadowFromTo.GenerateSelfShadowPath(to_key);
                if (File.Exists(path))
                {
                    selfShadow = ShadowFromTo.ReadFrom(path);
                }
                else
                {
                    to_patch.FillPointsAndMatrices(Terrain);
                    to_patch.InitializeHorizons();
                    to_patch.ParallelAddNearHorizon(Terrain);
                    var self_shadow = new ShadowFromTo(to_key, to_key);
                    self_shadow.CopyFrom(to_patch.Horizons);
                    _selfShadowDictionary[to_key] = self_shadow;
                }
            }

            to_patch.InitializeHorizons();
            if (selfShadow != null)
            {
                for (var line = 0; line < TerrainPatch.DefaultSize; line++)
                {
                    for (var sample = 0; sample < TerrainPatch.DefaultSize; sample++)
                    {
                        to_patch.Horizons[line][sample].Merge(selfShadow.Horizons[line][sample]);
                    }
                }
            }

            foreach (var local_from_key in Selection2)
            {
                var from_key = ToGlobalKey(local_from_key);
                var label    = new FromToLabel(from_key.X, from_key.Y, to_key.X, to_key.Y);
                var other    = _fromToDictionary[label];
                for (var line = 0; line < TerrainPatch.DefaultSize; line++)
                {
                    for (var sample = 0; sample < TerrainPatch.DefaultSize; sample++)
                    {
                        to_patch.Horizons[line][sample].Merge(other.Horizons[line][sample]);
                    }
                }
            }

            for (var line = 0; line < TerrainPatch.DefaultSize; line++)
            {
                for (var sample = 0; sample < TerrainPatch.DefaultSize; sample++)
                {
                    to_patch.Horizons[line][sample].ConvertSlopeToDegrees();
                }
            }

            RenderMode = PatchRenderMode.AzEl;
            Invalidate();
        }