示例#1
0
        private void DrawQuad(TerrainNode node, TerrainQuad quad, List <TileSampler> samplers)
        {
            if (!quad.IsVisible)
            {
                return;
            }
            if (!quad.Drawable)
            {
                return;
            }

            if (quad.IsLeaf)
            {
                MPB.Clear();

                for (int i = 0; i < samplers.Count; ++i)
                {
                    // Set the unifroms needed to draw the texture for this sampler
                    samplers[i].SetTile(MPB, quad.Level, quad.Tx, quad.Ty);
                }

                // Set the uniforms unique to each quad
                node.SetPerQuadUniforms(quad, MPB);

                DrawNode(node);
            }
            else
            {
                // Draw quads in a order based on distance to camera
                var order = new byte[4];

                var cameraX = node.LocalCameraPosition.x;
                var cameraY = node.LocalCameraPosition.y;
                var quadX   = quad.Oy + quad.Length / 2.0;
                var quadY   = quad.Oy + quad.Length / 2.0;

                if (cameraY < quadY)
                {
                    if (cameraX < quadX)
                    {
                        order[0] = 0;
                        order[1] = 1;
                        order[2] = 2;
                        order[3] = 3;
                    }
                    else
                    {
                        order[0] = 1;
                        order[1] = 0;
                        order[2] = 3;
                        order[3] = 2;
                    }
                }
                else
                {
                    if (cameraX < quadX)
                    {
                        order[0] = 2;
                        order[1] = 0;
                        order[2] = 3;
                        order[3] = 1;
                    }
                    else
                    {
                        order[0] = 3;
                        order[1] = 1;
                        order[2] = 2;
                        order[3] = 0;
                    }
                }

                var done = 0;

                for (byte i = 0; i < 4; ++i)
                {
                    if (quad.GetChild(order[i]).Visibility == Frustum.VISIBILITY.INVISIBLE)
                    {
                        done |= (1 << order[i]);
                    }
                    else if (quad.GetChild(order[i]).Drawable)
                    {
                        DrawQuad(node, quad.GetChild(order[i]), samplers);

                        done |= (1 << order[i]);
                    }
                }

                if (done < 15)
                {
                    // If the a leaf quad needs to be drawn but its tiles are not ready then this will draw the next parent tile instead that is ready.
                    // Because of the current set up all tiles always have there tasks run on the frame they are generated so this section of code is never reached.

                    MPB.Clear();

                    for (int i = 0; i < samplers.Count; ++i)
                    {
                        // Set the unifroms needed to draw the texture for this sampler
                        samplers[i].SetTile(MPB, quad.Level, quad.Tx, quad.Ty);
                    }

                    // Set the uniforms unique to each quad
                    node.SetPerQuadUniforms(quad, MPB);

                    DrawNode(node);
                }
            }
        }