public DemoPartitionContext(
            GraphicsDevice graphicsDevice, ContentManager content, CDLODSettings settings,
            ICDLODVisibleRanges visibleRanges)
        {
            GraphicsDevice = graphicsDevice;
            Content = content;
            this.settings = settings;

            TerrainRenderer = new DemoTerrainRenderer(GraphicsDevice, Content, settings);
            TerrainRenderer.InitializeMorphConsts(visibleRanges);

            var heightColors = new HeightColorCollection();
            // default settings.
            heightColors.AddColor(-1.0000f, new Color(  0,   0, 128, 255));
            heightColors.AddColor(-0.2500f, new Color(  0,   0, 255, 255));
            heightColors.AddColor( 0.0000f, new Color(  0, 128, 255, 255));
            heightColors.AddColor( 0.0625f, new Color(240, 240,  64, 255));
            heightColors.AddColor( 0.1250f, new Color( 32, 160,   0, 255));
            heightColors.AddColor( 0.3750f, new Color(224, 224,   0, 255));
            heightColors.AddColor( 0.7500f, new Color(128, 128, 128, 255));
            heightColors.AddColor( 1.0000f, new Color(255, 255, 255, 255));

            TerrainRenderer.InitializeHeightColors(heightColors);

            Selection = new CDLODSelection(settings, visibleRanges);
        }
        public DemoPartitionContext(
            GraphicsDevice graphicsDevice, ContentManager content, CDLODSettings settings,
            ICDLODVisibleRanges visibleRanges, SampleSourceDelegate noiseSource,
            float noiseMinX, float noiseMinY, float noiseWidth, float noiseHeight)
        {
            GraphicsDevice = graphicsDevice;
            Content = content;
            this.settings = settings;
            NoiseSource = noiseSource;
            NoiseMinX = noiseMinX;
            NoiseMinY = noiseMinY;
            NoiseWidth = noiseWidth;
            NoiseHeight = noiseHeight;

            TerrainRenderer = new DemoTerrainRenderer(GraphicsDevice, Content, settings);
            TerrainRenderer.InitializeMorphConsts(visibleRanges);

            var heightColors = new HeightColorCollection();
            // default settings.
            heightColors.AddColor(-1.0000f, Color.Navy);
            heightColors.AddColor(-0.2500f, Color.Blue);
            heightColors.AddColor( 0.0000f, Color.LightSeaGreen);
            heightColors.AddColor( 0.0625f, Color.Khaki);
            heightColors.AddColor( 0.1250f, Color.DarkGreen);
            heightColors.AddColor( 0.3750f, Color.DarkGoldenrod);
            heightColors.AddColor( 0.7500f, Color.Gray);
            heightColors.AddColor( 1.0000f, Color.White);

            TerrainRenderer.InitializeHeightColors(heightColors);

            Selection = new CDLODSelection(settings, visibleRanges);
        }
Exemple #3
0
        public void Select(CDLODSelection selection)
        {
            // Prepare selection's state per a terrain.
            selection.ClearSelectedNodes();

            // Select visible nodes.
            quadTree.Select(selection);
        }
Exemple #4
0
 public void Select(CDLODSelection selection)
 {
     for (int y = 0; y < topNodeCountY; y++)
     {
         for (int x = 0; x < topNodeCountX; x++)
         {
             topNodes[x, y].Select(selection, false, false);
         }
     }
 }
        public void Draw(GameTime gameTime, Effect effect, CDLODSelection selection)
        {
            if (selection.SelectedNodeCount == 0) return;

            // create instances
            for (int i = 0; i < selection.SelectedNodeCount; i++)
                selection.GetPatchInstanceVertex(i, out instances[i]);

            var offset = instanceVertexBuffer.SetData(instances, 0, selection.SelectedNodeCount);

            vertexBufferBindings[0] = new VertexBufferBinding(patchMesh.VertexBuffer, 0);
            vertexBufferBindings[1] = new VertexBufferBinding(instanceVertexBuffer.VertexBuffer, offset, 1);

            GraphicsDevice.SetVertexBuffers(vertexBufferBindings);
            GraphicsDevice.Indices = patchMesh.IndexBuffer;

            foreach (var pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawInstancedPrimitives(
                    PrimitiveType.TriangleList, 0, 0,
                    patchMesh.NumVertices, 0, patchMesh.PrimitiveCount, selection.SelectedNodeCount);
            }
        }
Exemple #6
0
        bool PreSelect(CDLODSelection selection, bool parentCompletelyInFrustum)
        {
            BoundingBox boundingBox;
            GetBoundingBox(ref selection.TerrainOffset, selection.Settings.MapScale, selection.Settings.HeightScale, out boundingBox);

            // do not check the intersection between AABB and the view frustum.

            BoundingSphere sphere;
            selection.GetVisibilitySphere(level, out sphere);
            if (!boundingBox.Intersects(sphere))
                return false;

            return true;
        }
Exemple #7
0
        public bool Select(CDLODSelection selection, bool parentCompletelyInFrustum, bool ignoreVisibilityCheck)
        {
            BoundingBox boundingBox;
            GetBoundingBox(ref selection.TerrainOffset, selection.Settings.MapScale, selection.Settings.HeightScale, out boundingBox);

            ContainmentType containmentType = ContainmentType.Contains;
            if (!parentCompletelyInFrustum)
            {
                selection.Frustum.Contains(ref boundingBox, out containmentType);
            }

            BoundingSphere sphere;
            bool intersected = true;

            if (!ignoreVisibilityCheck)
            {
                selection.GetVisibilitySphere(level, out sphere);
                boundingBox.Intersects(ref sphere, out intersected);
                if (!intersected)
                    return false;
            }

            if (level == 0)
            {
                // we reach a leaf node.
                if (containmentType != ContainmentType.Disjoint)
                    selection.AddSelectedNode(this);
                return true;
            }

            // If this node is out of the next visibility, we do not need to check children.
            selection.GetVisibilitySphere(level - 1, out sphere);
            boundingBox.Intersects(ref sphere, out intersected);
            if (!intersected)
            {
                if (containmentType != ContainmentType.Disjoint)
                    selection.AddSelectedNode(this);
                return true;
            }

            bool weAreCompletelyInFrustum = (containmentType == ContainmentType.Contains);

            // Check a child node's visibility on ahead.
            var someChildrenSelected = false;
            someChildrenSelected |= childTopLeft.PreSelect(selection, weAreCompletelyInFrustum);
            if (childTopRight != null)
                someChildrenSelected |= childTopRight.PreSelect(selection, weAreCompletelyInFrustum);
            if (childBottomLeft != null)
                someChildrenSelected |= childBottomLeft.PreSelect(selection, weAreCompletelyInFrustum);
            if (childBottomRight != null)
                someChildrenSelected |= childBottomRight.PreSelect(selection, weAreCompletelyInFrustum);

            if (someChildrenSelected)
            {
                // Select all children to avoid T-junctions by ignoring a visibiliy range check
                // if can select at least one.
                // The original code tries to select finer nodes as far as possible,
                // and hides parts of a coaser node overlapped by them at render time.
                // But using HW instancing, we must use a same mesh, so can not use such a overlap.
                childTopLeft.Select(selection, weAreCompletelyInFrustum, true);
                if (childTopRight != null)
                    childTopRight.Select(selection, weAreCompletelyInFrustum, true);
                if (childBottomLeft != null)
                    childBottomLeft.Select(selection, weAreCompletelyInFrustum, true);
                if (childBottomRight != null)
                    childBottomRight.Select(selection, weAreCompletelyInFrustum, true);
            }
            else
            {
                if (containmentType != ContainmentType.Disjoint)
                    selection.AddSelectedNode(this);
            }

            return true;
        }
        public void Draw(GameTime gameTime, CDLODSelection selection)
        {
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            // Prepare effect parameters.

            // Get the eye position in world space.
            Vector3 eyePosition;
            View.GetEyePosition(ref selection.View, out eyePosition);

            // Calcualte the eye position in terrain space.
            Vector3 terrainEyePosition = eyePosition - selection.TerrainOffset;

            // Calculate the view matrix in terrain space.
            Matrix inverseView;
            Matrix.Invert(ref selection.View, out inverseView);
            inverseView.Translation = terrainEyePosition;
            Matrix terrainView;
            Matrix.Invert(ref inverseView, out terrainView);

            // per a selection (a terrain).
            effect.HeightMap = selection.HeightMapTexture;
            effect.Projection = selection.Projection;
            effect.EyePosition = eyePosition;
            effect.TerrainEyePosition = terrainEyePosition;
            effect.TerrainView = terrainView;

            // render settings.
            effect.AmbientLightColor = ambientLightColor;
            effect.LightDirection = lightDirection;
            effect.DiffuseLightColor = diffuseLightColor;
            effect.LightEnabled = LightEnabled;
            effect.FogEnabled = FogEnabled;
            effect.FogStart = FogStart;
            effect.FogEnd = FogEnd;
            effect.FogColor = fogColor;

            // WhiteSolid tequnique
            if (WhiteSolidVisible)
            {
                sourceEffect.CurrentTechnique = effect.WhiteSolidTequnique;
                renderer.Draw(gameTime, sourceEffect, selection);
            }

            // HeightColor tequnique
            if (HeightColorVisible)
            {
                sourceEffect.CurrentTechnique = effect.HeightColorTequnique;
                renderer.Draw(gameTime, sourceEffect, selection);
            }

            // Wireframe tequnique
            if (WireframeVisible)
            {
                sourceEffect.CurrentTechnique = effect.WireframeTequnique;
                renderer.Draw(gameTime, sourceEffect, selection);
            }

            if (NodeBoundingBoxVisible)
            {
                debugEffect.View = selection.View;
                debugEffect.Projection = selection.Projection;

                CDLODSelectedNode selectedNode;
                BoundingBox box;
                for (int i = 0; i < selection.SelectedNodeCount; i++)
                {
                    selection.GetSelectedNode(i, out selectedNode);

                    selectedNode.GetBoundingBox(
                        ref selection.TerrainOffset, settings.MapScale, settings.HeightScale, out box);
                    var level = selectedNode.Level;
                    level %= 4;
                    boundingBoxDrawer.Draw(ref box, debugEffect, ref debugLevelColors[level]);
                }
            }

            // unbind the height map texture.
            effect.HeightMap = null;
        }